| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
- #include "display.h"
- #include "beep.h"
- #include "adc.h"
- #include "led.h"
- #include "timer.h"
- #include "stdint.h"
- #include "SysTick.h"
- #include "math.h"
- #define CAIYANG (680)
- #define VLAUE1 (880*1000 + CAIYANG)
- float read_voltage_v(uint16_t val)
- {
- float value = 0.0;
- uint32_t temp = 0;
- temp = val;
- temp *= 33;
- temp *= 1000;
- temp = temp >> 12;
- temp *= VLAUE1;
- value = temp;
- value /= 10.0;
- value /= CAIYANG;
- value /= 1000.0;
- value /= 1.414213;
- return value;
- }
- float read_voltage_I(uint16_t val)
- {
- float value = 0.0;
- uint32_t temp = 0;
- temp = val;
- temp *= 33;
- temp *= 1000;
- temp = temp >> 12;
- temp *= 2000;
- value = temp;
- value /= 10.0;
- value /= 100.0;
- value /= 1000.0;
- value /= 1.414213;
- return value;
- }
- uint8_t flash_led = 0;
- uint8_t read_flag = 0;
- extern uint16_t voltage_s[200];
- extern uint16_t current_s[200];
- void filter(uint16_t arr[],uint32_t lenth,float arr_o[])
- {
- float a[5] = {1,0,0.4860,0,0.0177};
- float b[5] = {0.0940,0.3759,0.5639,0.3759,0.0940};
- arr_o[0] = b[0] * arr[0];
- arr_o[1] = b[0] * arr[1] + b[1] * arr[0] - a[1] * arr_o[0];
- 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];
- 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];
- for (int i = 4; i < lenth; i++)
- {
- 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] \
- - a[1] * arr_o[i - 1] - a[2] * arr_o[i - 2] - a[3] * arr_o[i - 3] - a[4] * arr_o[i - 4];
- }
- }
- #define VALUE_A (0xD0)
- void filter_kk(uint16_t arr[])
- {
- uint16_t value_1 = arr[0];
- for(int i = 1;i < 200;i++)
- {
- if((arr[i]-value_1) > VALUE_A)
- {
- arr[i] = value_1;
- }else if((value_1 - arr[i])> VALUE_A)
- {
- arr[i] = value_1;
- }
- value_1 = arr[i];
- }
- }
- float V_max[100] = {0};
- float I_max[100] = {0};
- int main()
- {
- InitSysTick();
- DisplayInit();
- BeepInit();
- AdcInit();
- LedInit();
- InitTimer();
- while(!dma_flag_get(DMA_CH0,DMA_FLAG_FTF));
- dma_flag_clear(DMA_CH0,DMA_FLAG_FTF);
- float voltage = 0.0;
- float cureent = 0.0;
- float show_val_1 =888.8;
- float show_val_2 =888.8;
- int index = 0;
- ADC_TimerEnable();
- float value_i[200] = {0.0};
- float value_v[200] = {0.0};
- while(1)
- {
- if(read_flag)
- {
- //calculate
- read_flag = 0;
- float v_max = 0;
- float i_max = 0;
- filter_kk(voltage_s);
- filter_kk(current_s);
- filter(voltage_s,200,value_v);
- filter(current_s,200,value_i);
- for(int i = 0; i < 200;i++)
- {
- v_max = v_max > value_v[i] ? v_max : value_v[i];
- i_max = i_max > value_i[i] ? i_max : value_i[i];
- }
-
- // voltage = read_voltage_v(v_1);
- // cureent = read_voltage_I(v_2);
-
- V_max[index] = read_voltage_v(v_max);
- I_max[index] = read_voltage_I(i_max);
- index ++;
- if(index == 100)
- {
- for(int i = 0; i < 100;i++)
- {
- voltage +=V_max[i];
- cureent +=I_max[i];
- // V_max[i] = Filter(V_max[i],V_max[i-1]);
- // I_max[i] = Filter(I_max[i],I_max[i-1]);
- // voltage += read_voltage_v(V_max[i]);
- // cureent += read_voltage_I(I_max[i]);
- }
-
- show_val_1 = (voltage/100)-3.0;
- show_val_2 = cureent/100;
- voltage = 0.0;
- cureent = 0.0;
- index = 0;
- }
-
- ADC_TimerEnable();
- }
-
- // if(read_flag == 0){
- // float val = read_voltage();
- // if(val > 1000)
- // val = 0.0;
- // voltage = (val > voltage ) ? val : voltage;
- // val = read_current();
- // current = (val > current ) ? val : current;
- // }else{
- // voltage_s[index] = voltage;
- // current_s[index++] = current;
- // //display_voltage(voltage);
- // if(index == 100)
- // {
- // index = 0;
- // Bubble_Sort(voltage_s,100);
- // Bubble_Sort(current_s,100);
- // show_val_1 = voltage_s[50];
- // show_val_2 = current_s[50];
- // }
- // voltage = 0.0;
- // current = 0.0;
- // read_flag = 0;
- // //
- // ADC_TimerEnable();
- // }
- display_voltage(show_val_1,show_val_2);
- if(flash_led)
- {
- led_flash();
- flash_led = 0;
- }
-
- }
- }
|