A题-报告3-程序清单 | ||||
| ||||
A题-报告3-程序清单 #include "stm32f10x.h" #include "usart.h" #include "sys.h" #include "delay.h" #include "adc.h" #include "dac.h" #include "key.h" #include "OLED_IIC.h" #include "iic.h" #include "led.h" #include "dma.h" #define TIMES 64 #define CHANNEL 4 #define boost_on led0=0; #define boost_off led0=1; #define buck_on led1=0; #define buck_off led1=1; #define mode_current led2=1; #define mode_voltage led2=0; extern u32 ADC_VALUE[CHANNEL][TIMES]; u32 ADC_MEAN[CHANNEL]; u32 ADC_MEAN0[CHANNEL]={0}; u16 voltage=1650; u16 voltage0=1600; u16 vol_mode2=0; u16 vol_mode20=10; u16 cur_mode2=0; u16 cur_mode20=10; u16 vol_mode4=0; u16 vol_mode40=10; u16 cur_mode4=0; u16 cur_mode40=10; u8 MODE=1; u8 MODE0=1; void adc_mean( void ); void dis_play( void ); int main(void) { delay_init(); dac_init(); key_init(); led_init(); adc_init(); IIC_init(); OLED_Init(); dac_set_ch1(voltage&0x0fff); OLED_Fill(0x00); delay_ms(100); led0=1; led1=1; led2=1; OLED_write_string1(0,0,"init_success"); OLED_write_string1(0,1,"dac_mode2:"); OLED_write_string1(0,3,"vol_mode2:"); OLED_write_string1(0,4,"cur_mode2:"); OLED_write_string1(0,6,"vol_mode4:"); OLED_write_string1(0,7,"cur_mode4:"); while(1) { dac_set_ch1(voltage); adc_mean(); vol_mode2=(ADC_MEAN[0]*3300/4096); cur_mode2=(ADC_MEAN[1]*3300 *2/4096); vol_mode4=(ADC_MEAN[2]*3300/4096); cur_mode4=(ADC_MEAN[3]*3300/4096); dis_play(); switch(MODE) { case 1: OLED_write_string1(88,0,"mode1");break; case 2: OLED_write_string1(88,0,"mode2");break; case 3: OLED_write_string1(88,0,"mode3");break; case 4: OLED_write_string1(88,0,"mode4");break; } switch(key_adc()) { case 0: break; case 1: voltage++;break; case 2: voltage+=20;break; case 3: voltage--;break; case 4: voltage=3475;break; case 5: MODE=1; boost_off; delay_ms(5); buck_off; mode_current; break; case 6: MODE=2; boost_off; delay_ms(5); buck_on; mode_current; delay_ms(1000); delay_ms(1000); delay_ms(1000); break; case 7: MODE=3; buck_off; delay_ms(5); boost_on; mode_current; break; case 8: MODE=4; boost_off; delay_ms(5); buck_off; mode_voltage; break; } if(MODE==2) { if(vol_mode2>2181) { buck_off; OLED_Fill(0x00); OLED_write_string2(0,0,"Charge Over!"); while(1); } } if(MODE==4) { if(vol_mode4>2773) { boost_off; delay_ms(5); buck_on; } else if(vol_mode4<2350) { buck_off; delay_ms(5); boost_on; } } } }
void adc_mean( void ) { u32 sum=0; u8 i,j; for(i=0;i<CHANNEL;i++) { sum=0; for(j=0;j<TIMES;j++) { sum+=ADC_VALUE[i][j]; ADC_VALUE[i][j]=adc_get_voltage1(i); } ADC_MEAN[i]=sum/TIMES; } }
void dis_play( void ) { u16 temp;
if((voltage<voltage0-1)||(voltage>voltage0+1)) { voltage0=voltage; temp=(u16)(voltage*3.3*1000/4096); OLED_SetPos(88,1); OLED_write_char((temp%10000)/1000+48); OLED_write_char('.'); OLED_write_char((temp%1000)/100+48); OLED_write_char((temp%100)/10+48); OLED_write_char((temp%10)/1+48); OLED_write_char('V'); }
if((vol_mode2<vol_mode20-5)||(vol_mode2>vol_mode20+5)) { vol_mode20=vol_mode2; OLED_SetPos(88,3); OLED_write_char((vol_mode2%10000)/1000+48); OLED_write_char('.'); OLED_write_char((vol_mode2%1000)/100+48); OLED_write_char((vol_mode2%100)/10+48); OLED_write_char((vol_mode2%10)/1+48); OLED_write_char('V'); }
if((cur_mode2<cur_mode20-5)||(cur_mode2>cur_mode20+5)) { cur_mode20=cur_mode2; OLED_SetPos(88,4); OLED_write_char((cur_mode2%10000)/1000+48); OLED_write_char('.'); OLED_write_char((cur_mode2%1000)/100+48); OLED_write_char((cur_mode2%100)/10+48); OLED_write_char((cur_mode2%10)/1+48); OLED_write_char('A'); }
if((vol_mode4<vol_mode40-5)||(vol_mode4>vol_mode40+5)) { vol_mode40=vol_mode4; OLED_SetPos(88,6); OLED_write_char((vol_mode4%10000)/1000+48); OLED_write_char('.'); OLED_write_char((vol_mode4%1000)/100+48); OLED_write_char((vol_mode4%100)/10+48); OLED_write_char((vol_mode4%10)/1+48); OLED_write_char('V'); }
if((cur_mode4<cur_mode40-5)||(cur_mode4>cur_mode40+5)) { cur_mode40=cur_mode4; OLED_SetPos(88,7); OLED_write_char((cur_mode4%10000)/1000+48); OLED_write_char('.'); OLED_write_char((cur_mode4%1000)/100+48); OLED_write_char((cur_mode4%100)/10+48); OLED_write_char((cur_mode4%10)/1+48); OLED_write_char('V'); } }
|