main.c 3.0 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. enum {
  10. VLOTAGE =0,
  11. CURREENT,
  12. };
  13. #define CAIYANG (680)
  14. #define M_PI (3.1415926)
  15. #define VLAUE1 (880*1000 + CAIYANG)
  16. float read_voltage_v(uint16_t val)
  17. {
  18. float value = 0.0;
  19. uint32_t temp = 0;
  20. temp = val;
  21. temp *= 33;
  22. temp *= 1000;
  23. temp = temp >> 12;
  24. temp *= VLAUE1;
  25. value = temp;
  26. value /= 10.0;
  27. value /= CAIYANG;
  28. value /= 1000.0;
  29. // value /= 1.414213;
  30. return value;
  31. }
  32. float read_voltage_I(uint16_t val)
  33. {
  34. float value = 0.0;
  35. uint32_t temp = 0;
  36. temp = val;
  37. temp *= 33;
  38. temp *= 1000;
  39. temp = temp >> 12;
  40. temp *= 2000;
  41. value = temp;
  42. value /= 10.0;
  43. value /= 100.0;
  44. value /= 1000.0;
  45. // value /= 1.414213;
  46. return value;
  47. }
  48. extern uint8_t read_complete;
  49. uint8_t flash_led = 0;
  50. //float data_f[500];
  51. //float data_c[500];
  52. extern uint16_t data_s[500];
  53. extern uint16_t data_i[500];
  54. float value = 0.0;
  55. extern uint16_t count_v ;
  56. extern uint16_t count_i ;
  57. static float voltage_show = 0.0;
  58. static float current_show = 0.0;
  59. float v_data[500] = {0};
  60. static float calculate_voltage(uint16_t *data)
  61. {
  62. float val_v = 0.0;
  63. memset(v_data,0,sizeof(float)*500);
  64. for(int j = 0; j < 500;j++)
  65. {
  66. if(count_v >=244)
  67. {
  68. v_data[j] = read_voltage_v(data[j]);
  69. }else
  70. {
  71. v_data[j] = 0;
  72. }
  73. val_v += (v_data[j] *v_data[j]);
  74. }
  75. val_v /= 500;
  76. val_v = sqrt(val_v);
  77. val_v /= 0.707;
  78. val_v /= 0.9904;
  79. return val_v;
  80. }
  81. static float calculate_current(uint16_t *data)
  82. {
  83. float val_v = 0.0;
  84. memset(v_data,0,sizeof(float)*500);
  85. for(int j = 0; j < 500;j++)
  86. {
  87. if(count_i >= 244)
  88. v_data[j] = read_voltage_I(data[j]);
  89. else
  90. v_data[j] = 0;
  91. val_v += (v_data[j] *v_data[j]);
  92. }
  93. val_v /= 500;
  94. val_v = sqrt(val_v);
  95. val_v /= 0.707;
  96. val_v /= 0.9904;
  97. return val_v;
  98. }
  99. int main()
  100. {
  101. InitSysTick();
  102. DisplayInit();
  103. BeepInit();
  104. AdcInit();
  105. LedInit();
  106. InitTimer();
  107. while(!dma_flag_get(DMA_CH0,DMA_FLAG_FTF));
  108. dma_flag_clear(DMA_CH0,DMA_FLAG_FTF);
  109. float voltage = 0.0;
  110. float cureent = 0.0;
  111. float show_val_1 =888.8;
  112. float show_val_2 =888.8;
  113. int index = 0;
  114. ADC_TimerEnable();;
  115. while(1)
  116. {
  117. if(read_complete == 1)
  118. {
  119. read_complete = 0;
  120. voltage_show += calculate_voltage(data_s);
  121. current_show += calculate_current(data_i);
  122. count_i = 0;
  123. count_v = 0;
  124. index++;
  125. ADC_TimerEnable();
  126. if(index >= 20)
  127. {
  128. index = 0;
  129. show_val_2 = (voltage_show /20.0);
  130. voltage_show = 0;
  131. show_val_1 = (current_show /20.0);
  132. current_show = 0;
  133. }
  134. }
  135. display_voltage(show_val_1,show_val_2);
  136. if(flash_led)
  137. {
  138. led_flash();
  139. flash_led = 0;
  140. //display_voltage(show_val_1,show_val_2);
  141. //show_val_2 = read_voltage();
  142. }
  143. }
  144. }