| 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"
- enum {
- VLOTAGE =0,
- CURREENT,
- };
- #define CAIYANG (680)
- #define M_PI (3.1415926)
- #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;
- }
- extern uint8_t read_complete;
- uint8_t flash_led = 0;
- //float data_f[500];
- //float data_c[500];
- extern uint16_t data_s[500];
- extern uint16_t data_i[500];
- float value = 0.0;
- extern uint16_t count_v ;
- extern uint16_t count_i ;
- static float voltage_show = 0.0;
- static float current_show = 0.0;
- float v_data[500] = {0};
- static float calculate_voltage(uint16_t *data)
- {
- float val_v = 0.0;
- memset(v_data,0,sizeof(float)*500);
- for(int j = 0; j < 500;j++)
- {
- if(count_v >=244)
- {
- v_data[j] = read_voltage_v(data[j]);
- }else
- {
- v_data[j] = 0;
- }
-
- val_v += (v_data[j] *v_data[j]);
- }
- val_v /= 500;
- val_v = sqrt(val_v);
- val_v /= 0.707;
- val_v /= 0.9904;
- return val_v;
- }
- static float calculate_current(uint16_t *data)
- {
- float val_v = 0.0;
- memset(v_data,0,sizeof(float)*500);
- for(int j = 0; j < 500;j++)
- {
- if(count_i >= 244)
- v_data[j] = read_voltage_I(data[j]);
- else
- v_data[j] = 0;
- val_v += (v_data[j] *v_data[j]);
- }
- val_v /= 500;
- val_v = sqrt(val_v);
- val_v /= 0.707;
- val_v /= 0.9904;
-
- return val_v;
- }
- 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();;
- while(1)
- {
-
- if(read_complete == 1)
- {
- read_complete = 0;
-
- voltage_show += calculate_voltage(data_s);
-
- current_show += calculate_current(data_i);
- count_i = 0;
- count_v = 0;
- index++;
- ADC_TimerEnable();
- if(index >= 20)
- {
- index = 0;
- show_val_2 = (voltage_show /20.0);
- voltage_show = 0;
- show_val_1 = (current_show /20.0);
- current_show = 0;
-
- }
-
- }
-
-
-
- display_voltage(show_val_1,show_val_2);
- if(flash_led)
- {
- led_flash();
- flash_led = 0;
-
- //display_voltage(show_val_1,show_val_2);
- //show_val_2 = read_voltage();
- }
-
- }
- }
|