adc.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * adc.c
  3. *
  4. * Created on: 2020. 8. 3.
  5. * Author: parkyj
  6. */
  7. #include "main.h"
  8. #include "adc.h"
  9. #include "NessLab.h"
  10. uint8_t ADC_Convert_Temperature(double val);
  11. extern ADC_HandleTypeDef hadc1;
  12. extern DMA_HandleTypeDef hdma_adc1;
  13. extern Nesslab_Prot Currstatus;
  14. //volatile uint16_t ADC1valuearray[ADC1_CNT][ADC_AVERAGECNT];
  15. volatile uint16_t ADC1value[ADC1_CNT];
  16. volatile uint16_t ADC1_Arrage[ADC_AVERAGECNT] = {0,};
  17. volatile uint32_t ADC1_Arrage_Ret[ADC_AVERAGECNT] = {0,};
  18. uint16_t adc1cnt = 0 ;
  19. uint32_t TotalCnt = 0;
  20. extern volatile uint32_t TDD_125ms_Cnt;
  21. /*
  22. *
  23. * ADC 0 :DL TX
  24. * ADC 1 :DL RX
  25. * ADC 2 :TEMP
  26. * */
  27. void ADC_Value_Get(){
  28. uint16_t CalcRet = 0 ;
  29. uint16_t Tx_Det_Volt = ((ADC1value[0] * (3.3 / 4095))* 1000);
  30. uint16_t Rx_Det_Volt = ((ADC1value[1] * (3.3 / 4095))* 1000);
  31. int8_t Real_Temperature = ADC_Convert_Temperature((ADC1value[2] * (3.3 / 4095)));
  32. /*DL TX Calc*/
  33. // Currstatus.DownLink_Forward_Det_H = ((Tx_Det_Volt & 0xFF00) >> 8);
  34. // Currstatus.DownLink_Forward_Det_L = (Tx_Det_Volt & 0x00FF);
  35. // printf("Tx_Det_Volt : %d \r\n",Tx_Det_Volt);
  36. /*DL RX Calc*/
  37. Currstatus.DownLink_Reverse_Det_H = ((Rx_Det_Volt & 0xFF00) >> 8);
  38. Currstatus.DownLink_Reverse_Det_L = (Rx_Det_Volt & 0x00FF);
  39. /*Temp Calc*/
  40. Currstatus.Temp_Monitor = Real_Temperature;
  41. }
  42. void ADC_Initialize(){
  43. while(!(HAL_ADCEx_Calibration_Start(&hadc1)==HAL_OK));
  44. HAL_ADC_Start_DMA(&hadc1, (uint16_t*)ADC1value,(uint32_t) 3);
  45. }
  46. uint8_t ADC_Convert_Temperature(double val){
  47. int16_t ref_0temp = 500;
  48. int16_t ret = val * 1000;
  49. int8_t cnt = 0;
  50. // printf("ret : %d \r\n", ret);
  51. if( ret - ref_0temp > 0){
  52. for(int i = 0; i < ret - ref_0temp; i += 10){
  53. cnt++;
  54. }
  55. }else{
  56. for(int i = 0; i > ret - ref_0temp; i -= 10){
  57. cnt--;
  58. }
  59. }
  60. // printf("Temp : %d\r\n",cnt);
  61. return cnt;
  62. }
  63. uint32_t SumFunc(uint32_t* data,uint16_t size){
  64. uint32_t ret = 0;
  65. for (uint16_t i = 0; i < size; i++) // 배열의 요소 개수만큼 반복
  66. {
  67. ret += data[i]; // sum과 배열의 요소를 더해서 다시 sum에 저장
  68. // printf("ret : %d data[%d] \r\n",ret,i,data[i]);
  69. }
  70. return ret;
  71. }
  72. void DascendigFunc(int32_t* src,uint32_t size ){
  73. int32_t temp;
  74. for(int i = 0 ; i < size - 1 ; i ++) {
  75. for(int j = i+1 ; j < size ; j ++) {
  76. if(src[i] < src[j]) {
  77. temp = src[j];
  78. src[j] = src[i];
  79. src[i] = temp;
  80. // printf("temp");
  81. }
  82. }
  83. }
  84. }
  85. #define Percent100 5
  86. void ADC_Check(){
  87. float tempval = 0;
  88. // for(int i = 0; i < 3 ; i++)
  89. // printf("ADC1value[%d] : %d \r\n",i,ADC1value[i]);
  90. #if 0 // PYJ.2020.09.09_BEGIN --
  91. Currstatus.DownLink_Forward_Det_H
  92. = (((uint16_t)tempval & 0xFF00) >> 8);
  93. Currstatus.DownLink_Forward_Det_L
  94. = (((uint16_t)tempval & 0x00FF) );
  95. #endif // PYJ.2020.09.09_END --
  96. ADC_Value_Get();
  97. // Currstatus.Temp_Monitor = ADC_Convert_Temperature((ADC1value[2]/1000));
  98. // printf("Currstatus.DownLink_Forward_Det : %d \r\n",Currstatus.DownLink_Forward_Det_H << 8 | Currstatus.DownLink_Forward_Det_L);
  99. }
  100. void ADC_TDD_Arrange(){
  101. uint32_t ADC1_Average_value = 0;
  102. if(TDD_125ms_Cnt > ADC_DATA_CYCLE)
  103. {
  104. // printf("================TotalCnt %d =================\r\n",TotalCnt);
  105. #if 1 // PYJ.2020.09.09_BEGIN --
  106. DascendigFunc(&ADC1_Arrage_Ret[0],ADC_AVERAGECNT);
  107. // for(int i = 0; i < 100; i++){/*ADC Data Dascending Complete*/
  108. // printf("ADC1_Arrage_Ret[%d] : %d \r\n",i,ADC1_Arrage_Ret[i]);
  109. // }
  110. ADC1_Average_value = SumFunc(&ADC1_Arrage_Ret[0],Percent100);
  111. // printf("ADC1_Average_value Sum : %d \r\n",ADC1_Average_value);
  112. ADC1_Average_value /=Percent100;
  113. // printf("ADC1_Average_value : %d \r\n",ADC1_Average_value);
  114. for(int i = 0; i < ADC_AVERAGECNT; i++){
  115. ADC1_Arrage_Ret[i] = 0;
  116. }
  117. Currstatus.DownLink_Forward_Det_H
  118. = (((uint16_t)ADC1_Average_value & 0xFF00) >> 8);
  119. Currstatus.DownLink_Forward_Det_L
  120. = (((uint16_t)ADC1_Average_value & 0x00FF) );
  121. #else
  122. for(int i = 0; i < ADC_AVERAGECNT; i++){
  123. ADC1_Arrage_Ret[i] = ADC1_Arrage[i];
  124. }
  125. #endif // PYJ.2020.09.09_END --
  126. TotalCnt = 0;
  127. TDD_125ms_Cnt = 0;
  128. }
  129. }
  130. void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
  131. {
  132. if(hadc->Instance == hadc1.Instance && TDD_125ms_Cnt < 125)
  133. {
  134. ADC1_Arrage[adc1cnt] = ADC1value[0];
  135. // for(int i = 0; i < 2; i++){
  136. // printf("ADC1value[%d] : %d , %f\r\n",i,ADC1value[i],(float)((ADC1value[i]) *3.3 /4095));
  137. // }
  138. adc1cnt++;
  139. if(adc1cnt == ADC_AVERAGECNT){
  140. // DascendigFunc(&ADC1_Arrage[0],ADC_AVERAGECNT);
  141. adc1cnt = 0;
  142. TotalCnt++;
  143. if(TotalCnt > 2)
  144. TotalCnt = 2;
  145. for(int i = 0; i < ADC_AVERAGECNT; i++){/*ADC Data Dascending Complete*/
  146. if(ADC1_Arrage_Ret[i] <= ADC1_Arrage[i])
  147. ADC1_Arrage_Ret[i] = ADC1_Arrage[i];
  148. }
  149. DascendigFunc(&ADC1_Arrage_Ret[0],ADC_AVERAGECNT);
  150. // for(int i = 0; i < 100; i++){/*ADC Data Dascending Complete*/
  151. // printf("ADC1_Arrage_Ret[%d] : %d \r\n",i,ADC1_Arrage_Ret[i]);
  152. // }
  153. // else
  154. // ADC1_Arrage_Ret[i] /= TotalCnt;
  155. }
  156. // if(adc1cnt < ADC_AVERAGECNT){
  157. // for(int i = 0; i < ADC1_CNT; i++){
  158. // ADC1valuearray[i][adc1cnt] = ADC1value[i];
  159. // }
  160. // adc1cnt++;
  161. // }
  162. }
  163. }