main.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. #include "display.h"
  2. #include "beep.h"
  3. #include "adc.h"
  4. #include "led.h"
  5. #include "timer.h"
  6. #include "stdint.h"
  7. #include "SysTick.h"
  8. #include "math.h"
  9. #define CAIYANG (680)
  10. #define VLAUE1 (880*1000 + CAIYANG)
  11. float read_voltage_v(uint16_t val)
  12. {
  13. float value = 0.0;
  14. uint32_t temp = 0;
  15. temp = val;
  16. temp *= 33;
  17. temp *= 1000;
  18. temp = temp >> 12;
  19. temp *= VLAUE1;
  20. value = temp;
  21. value /= 10.0;
  22. value /= CAIYANG;
  23. value /= 1000.0;
  24. value /= 1.414213;
  25. return value;
  26. }
  27. float read_voltage_I(uint16_t val)
  28. {
  29. float value = 0.0;
  30. uint32_t temp = 0;
  31. temp = val;
  32. temp *= 33;
  33. temp *= 1000;
  34. temp = temp >> 12;
  35. temp *= 2000;
  36. value = temp;
  37. value /= 10.0;
  38. value /= 100.0;
  39. value /= 1000.0;
  40. value /= 1.414213;
  41. return value;
  42. }
  43. uint8_t flash_led = 0;
  44. uint8_t read_flag = 0;
  45. extern uint16_t voltage_s[200];
  46. extern uint16_t current_s[200];
  47. void filter(uint16_t arr[],uint32_t lenth,float arr_o[])
  48. {
  49. float a[5] = {1,0,0.4860,0,0.0177};
  50. float b[5] = {0.0940,0.3759,0.5639,0.3759,0.0940};
  51. arr_o[0] = b[0] * arr[0];
  52. arr_o[1] = b[0] * arr[1] + b[1] * arr[0] - a[1] * arr_o[0];
  53. arr_o[2] = b[0] * arr[2] + b[1] * arr[1] + b[2] * arr[0] - a[1] * arr_o[1] - a[2] * arr_o[0];
  54. arr_o[3] = b[0] * arr[3] + b[1] * arr[2] + b[2] * arr[1] + b[3] * arr[0] - a[1] * arr_o[2] - a[2] * arr_o[1] - a[3] * arr_o[0];
  55. for (int i = 4; i < lenth; i++)
  56. {
  57. arr_o[i] = b[0] * arr[i] + b[1] * arr[i - 1] + b[2] * arr[i - 2] + b[3] * arr[i - 3] + b[4] * arr[i - 4] \
  58. - a[1] * arr_o[i - 1] - a[2] * arr_o[i - 2] - a[3] * arr_o[i - 3] - a[4] * arr_o[i - 4];
  59. }
  60. }
  61. #define VALUE_A (0xD0)
  62. void filter_kk(uint16_t arr[])
  63. {
  64. uint16_t value_1 = arr[0];
  65. for(int i = 1;i < 200;i++)
  66. {
  67. if((arr[i]-value_1) > VALUE_A)
  68. {
  69. arr[i] = value_1;
  70. }else if((value_1 - arr[i])> VALUE_A)
  71. {
  72. arr[i] = value_1;
  73. }
  74. value_1 = arr[i];
  75. }
  76. }
  77. float V_max[100] = {0};
  78. float I_max[100] = {0};
  79. int main()
  80. {
  81. InitSysTick();
  82. DisplayInit();
  83. BeepInit();
  84. AdcInit();
  85. LedInit();
  86. InitTimer();
  87. while(!dma_flag_get(DMA_CH0,DMA_FLAG_FTF));
  88. dma_flag_clear(DMA_CH0,DMA_FLAG_FTF);
  89. float voltage = 0.0;
  90. float cureent = 0.0;
  91. float show_val_1 =888.8;
  92. float show_val_2 =888.8;
  93. int index = 0;
  94. ADC_TimerEnable();
  95. float value_i[200] = {0.0};
  96. float value_v[200] = {0.0};
  97. while(1)
  98. {
  99. if(read_flag)
  100. {
  101. //calculate
  102. read_flag = 0;
  103. float v_max = 0;
  104. float i_max = 0;
  105. filter_kk(voltage_s);
  106. filter_kk(current_s);
  107. filter(voltage_s,200,value_v);
  108. filter(current_s,200,value_i);
  109. for(int i = 0; i < 200;i++)
  110. {
  111. v_max = v_max > value_v[i] ? v_max : value_v[i];
  112. i_max = i_max > value_i[i] ? i_max : value_i[i];
  113. }
  114. // voltage = read_voltage_v(v_1);
  115. // cureent = read_voltage_I(v_2);
  116. V_max[index] = read_voltage_v(v_max);
  117. I_max[index] = read_voltage_I(i_max);
  118. index ++;
  119. if(index == 100)
  120. {
  121. for(int i = 0; i < 100;i++)
  122. {
  123. voltage +=V_max[i];
  124. cureent +=I_max[i];
  125. // V_max[i] = Filter(V_max[i],V_max[i-1]);
  126. // I_max[i] = Filter(I_max[i],I_max[i-1]);
  127. // voltage += read_voltage_v(V_max[i]);
  128. // cureent += read_voltage_I(I_max[i]);
  129. }
  130. show_val_1 = (voltage/100)-3.0;
  131. show_val_2 = cureent/100;
  132. voltage = 0.0;
  133. cureent = 0.0;
  134. index = 0;
  135. }
  136. ADC_TimerEnable();
  137. }
  138. // if(read_flag == 0){
  139. // float val = read_voltage();
  140. // if(val > 1000)
  141. // val = 0.0;
  142. // voltage = (val > voltage ) ? val : voltage;
  143. // val = read_current();
  144. // current = (val > current ) ? val : current;
  145. // }else{
  146. // voltage_s[index] = voltage;
  147. // current_s[index++] = current;
  148. // //display_voltage(voltage);
  149. // if(index == 100)
  150. // {
  151. // index = 0;
  152. // Bubble_Sort(voltage_s,100);
  153. // Bubble_Sort(current_s,100);
  154. // show_val_1 = voltage_s[50];
  155. // show_val_2 = current_s[50];
  156. // }
  157. // voltage = 0.0;
  158. // current = 0.0;
  159. // read_flag = 0;
  160. // //
  161. // ADC_TimerEnable();
  162. // }
  163. display_voltage(show_val_1,show_val_2);
  164. if(flash_led)
  165. {
  166. led_flash();
  167. flash_led = 0;
  168. }
  169. }
  170. }