A题-报告1-程序清单 | ||||
| ||||
A题-报告1-程序清单 源程序//*************************************************************************** // ADC[1] I1 // ADC[2] U1 // ADC[3] U2 //*************************************************************************** #include"msp430.h" #include<stdint.h> #include"LCD12864.h" #include<stdlib.h> #include"mystdlib.h" #include"variables.h" #include"Init.h" #include"MyMath.h"
#define IR2110_ON P8OUT &= ~BIT1 #define IR2110_OFF P8OUT |= BIT1 #define BUCK_CCR TBCCR1 #define BOOST_CCR TBCCR2
voidLCD_display(); voidKey_response(int key_val);
double ADC_remainder = 0,*p_remainder=0; int key_read_mark=0; int LCD_display_mark=0; int Is_running = 0; int AD_display = 0;//0:not play; 1:display; int LCD_init_mark = 0;
int mode_open_loop = 0, mode_current_closed_loop = 1, mode_voltage_closed_loop = 2, mode_debug = 20, mode_overcurrent_protection = 30; int mode = 0; int X_set_mode = 1;//mode_current_closed_loop int I_set = 100;//200/100=2.0 int U_set = 300; int current_ring_width = 0; int error_count = 0;
intmain(void) { WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer while (REFCTL0 & REFGENBUSY); REFCTL0 |= REFVSEL_2 + REFON; UCS_init(); Port_init(); Data_init(); key_init(); ADC_init(); LCD12864_initial(); LCD12864_clear(); dma_init(); Timer_A0_init(); Timer_A1_init(); Timer_A2_init(); Timer_B0_init(); __enable_interrupt(); tb0.pwm1_ccr = TBCCR0 * 0.65; tb0.pwm2_ccr = tb0.pwm1_ccr + tb0.dead_time; tb1_count = 0; set_tb_buf(tb0.pwm1_ccr, tb1_count);
IR2110_ON;
while (1){ if (key_read_mark){ key_read(); key_read_mark = 0; Key_response(key_val); //LCD_display_mark = 1; } if (LCD_display_mark){ if(LCD_init_mark){ } LCD_display_mark = 0; LCD_display(); } if (ADC_buf_full_mark){ ADC_buf_full_mark = 0;
ADC[1].buf_2nd[ADC_buf_2nd_index] = Median_filter(ADC[1].buf[1-ADC_buf_1st_turn], ADC_BUF_1st_LEN, &ADC_remainder); ADC[1].remainder_1st += ADC_remainder;
ADC[2].buf_2nd[ADC_buf_2nd_index] = Median_filter(ADC[2].buf[1-ADC_buf_1st_turn], ADC_BUF_1st_LEN, &ADC_remainder); ADC[2].remainder_1st += ADC_remainder;
ADC[3].buf_2nd[ADC_buf_2nd_index] = Median_filter(ADC[3].buf[1-ADC_buf_1st_turn], ADC_BUF_1st_LEN, &ADC_remainder); ADC_buf_2nd_index++;
if (ADC_buf_2nd_index == ADC_BUF_2nd_LEN) { ADC_buf_2nd_index = 0; ADC[1].read_double = 0.3 * ADC[1].read_double + 0.7 * (ADC[1].remainder_1st / ADC_BUF_2nd_LEN + Mean(ADC[1].buf_2nd, ADC_BUF_2nd_LEN, &ADC_remainder)); ADC[1].read_double += ADC_remainder * 0.7; ADC[1].remainder_1st = 0; ADC[1].read = ADC[1].read_double;
ADC[2].read_double = (ADC[2].read_double + ADC[2].remainder_1st / ADC_BUF_2nd_LEN + Mean(ADC[2].buf_2nd, ADC_BUF_2nd_LEN, &ADC_remainder)) / 2; ADC[2].read_double += ADC_remainder / 2; ADC[2].remainder_1st = 0; ADC[2].read = ADC[2].read_double; ADC[3].read_double = (ADC[3].read_double + ADC[3].remainder_1st / ADC_BUF_2nd_LEN + Mean(ADC[3].buf_2nd, ADC_BUF_2nd_LEN, &ADC_remainder)) / 2; ADC[3].read_double += ADC_remainder / 2; ADC[3].remainder_1st = 0; ADC[3].read = ADC[3].read_double; if (ADC[2].read> ADC[2].target){ mode = mode_overcurrent_protection; //关闭dma DMA0CTL &= ~DMAEN; DMA1CTL &= ~DMAEN; TBCCR1 = 0; TBCCR2 = TBCCR0; TBCTL &= ~(MC1 + MC0); //stop mode TBCCTL1 &= ~(OUTMOD_7+OUT); TBCCTL2 &= ~(OUTMOD_7+OUT); TBCCTL2 |= OUT; _delay_us(2000); TBCCTL2 &= ~OUT; IR2110_OFF; }
if (mode == mode_current_closed_loop){ //过充保护 if (ADC[2].read> ADC[2].target){ mode = mode_overcurrent_protection; //关闭dma DMA0CTL &= ~DMAEN; DMA1CTL &= ~DMAEN;
TBCCR1 = 0; TBCCR2 = TBCCR0; TBCTL &= ~(MC1 + MC0); //stop mode TBCCTL1 &= ~(OUTMOD_7+OUT); TBCCTL2 &= ~(OUTMOD_7+OUT);
TBCCTL2 |= OUT; _delay_us(2000); TBCCTL2 &= ~OUT;
IR2110_OFF; } if (ADC[1].read< ADC[1].target- 200){ tb0.pwm1_ccr += 8; } elseif (ADC[1].read> ADC[1].target+ 200){ tb0.pwm1_ccr -= 8; } elseif (ADC[1].read< ADC[1].target- 40){ tb0.pwm1_ccr += 1; } elseif (ADC[1].read> ADC[1].target+ 40){ tb0.pwm1_ccr -= 1; } elseif (ADC[1].read< ADC[1].target- 8){ tb1_count++; if(tb1_count >= TB_DMA_BUF_LEN){ tb1_count = 0; tb0.pwm1_ccr += 1; } } elseif (ADC[1].read> ADC[1].target+ 8){ tb1_count--; if(tb1_count < 0){ tb1_count = TB_DMA_BUF_LEN-1; tb0.pwm1_ccr -= 1; } } elseif (ADC[1].read< ADC[1].target- current_ring_width){ error_count++; if(error_count>2){ error_count = 0; tb1_count++; if(tb1_count >= TB_DMA_BUF_LEN){ tb1_count = 0; tb0.pwm1_ccr += 1; } } } elseif (ADC[1].read> ADC[1].target+ current_ring_width){ error_count--; if(error_count>2){ error_count = 0; tb1_count--; if(tb1_count < 0){ tb1_count = TB_DMA_BUF_LEN-1; tb0.pwm1_ccr -= 1; } } } elseif(ADC[1].read>= ADC[1].target- current_ring_width && ADC[1].read<= ADC[1].target + current_ring_width) { error_count = 0; } if (tb0.pwm1_ccr> TBCCR0 * 0.92){ tb0.pwm1_ccr = TBCCR0 * 0.92; } elseif (tb0.pwm1_ccr< 0){ tb0.pwm1_ccr = 0; } set_tb_buf(tb0.pwm1_ccr, tb1_count); } if(mode == mode_voltage_closed_loop){ if(ADC[3].read< ADC[3].target- 100){ tb0.pwm1_ccr -= 5; } elseif(ADC[3].read> ADC[3].target+100){ tb0.pwm1_ccr += 5; } elseif(ADC[3].read< ADC[3].target- ADC_ring_width){ tb0.pwm1_ccr -= 1; } elseif(ADC[3].read> ADC[3].target+ADC_ring_width){ tb0.pwm1_ccr += 1; } if (tb0.pwm1_ccr> TBCCR0 * 0.95){ tb0.pwm1_ccr = TBCCR0 * 0.95; } elseif (tb0.pwm1_ccr< 0){ tb0.pwm1_ccr = 0; } set_tb_buf(tb0.pwm1_ccr, tb1_count); } } } }
return 0; } voidLCD_display(){
if(AD_display){ LCD12864_sendstring(1, 0, "I:"); MyItoa(ADC[1].read, LCD_DISP_BUF, 7); LCD12864_sendstring(1, 1, LCD_DISP_BUF);
LCD12864_sendstring(2, 0, "U:"); MyItoa(ADC[2].read, LCD_DISP_BUF, 7); LCD12864_sendstring(2, 1, LCD_DISP_BUF);
LCD12864_sendstring(2, 4, "U:"); MyItoa(ADC[3].read, LCD_DISP_BUF, 7); LCD12864_sendstring(2, 5, LCD_DISP_BUF); } else{ LCD12864_sendstring(1, 0, "I:"); MyFtoa(ADC[1].read * ADC[1].to_X+ ADC[1].off_X, LCD_DISP_BUF, 7, 3); LCD12864_sendstring(1, 1, LCD_DISP_BUF); LCD12864_sendstring(2, 0, "U:"); MyFtoa(ADC[2].read_double * ADC[2].to_X+ ADC[2].off_X, LCD_DISP_BUF, 7, 3); LCD12864_sendstring(2, 1, LCD_DISP_BUF); LCD12864_sendstring(2, 4, "U:"); MyFtoa(ADC[3].read_double * ADC[3].to_X+ ADC[3].off_X, LCD_DISP_BUF, 7, 3); LCD12864_sendstring(2, 5, LCD_DISP_BUF); } if(X_set_mode == mode_current_closed_loop){ MyFtoa(I_set/100.0, LCD_DISP_BUF, 5, 2); LCD_DISP_BUF[4]='A';LCD_DISP_BUF[5]=0; LCD12864_sendstring(1, 5, LCD_DISP_BUF); } else{//mode_voltage_closed_loop MyFtoa(U_set/10.0, LCD_DISP_BUF, 5, 1); LCD_DISP_BUF[4]='V';LCD_DISP_BUF[5]=0; LCD12864_sendstring(1, 5, LCD_DISP_BUF); } LCD_DISP_BUF[0]='b'; LCD_DISP_BUF[1]='u'; LCD_DISP_BUF[2]='c'; LCD_DISP_BUF[3]='k'; LCD_DISP_BUF[4]=' '; LCD_DISP_BUF[5]=':'; MyFtoa((tb0.pwm1_ccr+(double)(tb1_count)/TB_DMA_BUF_LEN)*100.0/TBCCR0, &LCD_DISP_BUF[6], 7, 3); LCD_DISP_BUF[13]='%';LCD_DISP_BUF[14]=0; LCD12864_sendstring(3, 0, LCD_DISP_BUF);
MyItoa(tb1_count, LCD_DISP_BUF, 6); LCD12864_sendstring(4, 0, LCD_DISP_BUF);
if(mode == mode_open_loop){ LCD12864_sendstring(3, 6, " "); LCD12864_sendstring(4, 6, "开环"); } elseif(mode == mode_current_closed_loop){ LCD12864_sendstring(3, 6, "电流"); LCD12864_sendstring(4, 6, "闭环"); } elseif(mode == mode_voltage_closed_loop){ LCD12864_sendstring(3, 6, "电压"); LCD12864_sendstring(4, 6, "闭环"); } elseif(mode == mode_overcurrent_protection){ LCD12864_sendstring(3, 6, " "); LCD12864_sendstring(4, 6, "过充"); } } voidKey_response(int key_val) { switch(key_val){ case 0: //开环 mode = mode_open_loop; TA0CCR0 = 400;//采样频率 break; case 4: //AD显示 AD_display = 1 - AD_display; break; case 8: //电流闭环 mode = mode_current_closed_loop; TA0CCR0 = 800;//采样频率 X_set_mode = mode_current_closed_loop; break; case 9: mode = mode_voltage_closed_loop; TA0CCR0 = 400;//采样频率 X_set_mode = mode_voltage_closed_loop; break; case 12: //过冲恢复 if(mode == mode_overcurrent_protection){ DMA0CTL |= DMAEN; DMA1CTL |= DMAEN; mode = mode_open_loop; TA0CCR0 = 400;//采样频率 I_set = 100; tb0.pwm1_ccr = TBCCR0 * 0.65; tb0.pwm2_ccr = tb0.pwm1_ccr + tb0.dead_time; set_tb_buf(tb0.pwm1_ccr, tb1_count); TBCCTL1 = OUTMOD_2; TBCCR1 = tb0.pwm1_ccr; TBCCTL2 = OUTMOD_6; TBCCR2 = tb0.pwm2_ccr; TBCTL = TBSSEL_2 + MC_3 + TBCLGRP_3 + TBCLR; IR2110_ON; } break; case 13: X_set_mode=X_set_mode==mode_current_closed_loop?mode_voltage_closed_loop:mode_current_closed_loop; break; case 1: tb1_count++; if(tb1_count >= TB_DMA_BUF_LEN){ tb1_count = 0; tb0.pwm1_ccr += 1; } break; case 5: tb1_count++; if(tb1_count >= TB_DMA_BUF_LEN){ tb1_count = 0; tb0.pwm1_ccr -= 1; } break; case 2: tb0.pwm1_ccr= tb0.pwm1_ccr+ 10; break; case 6: tb0.pwm1_ccr= tb0.pwm1_ccr- 10; break; case 3: tb0.pwm1_ccr= tb0.pwm1_ccr+ 100; break; case 7: tb0.pwm1_ccr= tb0.pwm1_ccr- 100; break; case 10: if(X_set_mode == mode_current_closed_loop && I_set<250) I_set++; if(X_set_mode == mode_voltage_closed_loop && U_set<360) U_set++; break; case 14: if(X_set_mode == mode_current_closed_loop && I_set>0) I_set--; if(X_set_mode == mode_voltage_closed_loop && U_set>0) U_set--; break; case 11: if(X_set_mode == mode_current_closed_loop && I_set<250) I_set+=10; if(X_set_mode == mode_voltage_closed_loop && U_set<360) U_set+=10; break; case 15: if(X_set_mode == mode_current_closed_loop && I_set>0) I_set-=10; if(X_set_mode == mode_voltage_closed_loop && U_set>0) U_set-=10; break; default: break; } if(tb0.pwm1_ccr> TBCCR0 * 0.92){ tb0.pwm1_ccr= TBCCR0 * 0.92; tb0.pwm2_ccr= tb0.pwm1_ccr+ tb0.dead_time; } elseif(tb0.pwm1_ccr< 0){ tb0.pwm1_ccr= 0; tb0.pwm2_ccr= tb0.pwm1_ccr+ tb0.dead_time; } if(mode == mode_current_closed_loop) { ADC[1].target= (I_set/100.0 - ADC[1].off_X) / ADC[1].to_X; } elseif(mode == mode_voltage_closed_loop) { ADC[3].target= (U_set/10.0 - ADC[3].off_X) / ADC[3].to_X; } elseif(mode == mode_open_loop) { set_tb_buf(tb0.pwm1_ccr, tb1_count); } if(key_val>=0&&key_val<16){ LCD_display_mark=1;//refresh LCD } } /********************************************** * 定时器A1之CCR0中断 **********************************************/ #pragma vector=TIMER1_A0_VECTOR __interruptvoidTIMER1_A0_ISR(void) { switch(__even_in_range(TA1IV,14)) { case 14:
time.count_us += 1000; if(time.count_us>= 1000){ time.count_ms++; time.count_us = 0;
if(time.count_ms%167 == 0){ key_read_mark = 1; } if(time.count_ms%500 == 0){//2Hz LCD_display_mark = 1; } if(time.count_ms>= 1000){ time.count_ms = 0; time.count_s++; LCD_init_mark = 1; } } break; default: break; } }
#pragma vector = ADC12_VECTOR __interruptvoidADC12_ISR(void) { switch(__even_in_range(ADC12IV,34)) { case 12: // Vector 12: ADC12IFG3 ADC[0].buf[ADC_buf_1st_turn][ADC_buf_1st_index]=ADC12MEM0; ADC[1].buf[ADC_buf_1st_turn][ADC_buf_1st_index]=ADC12MEM1; ADC[2].buf[ADC_buf_1st_turn][ADC_buf_1st_index]=ADC12MEM2; ADC[3].buf[ADC_buf_1st_turn][ADC_buf_1st_index]=ADC12MEM3; ADC_buf_1st_index++; if(ADC_buf_1st_index == ADC_BUF_1st_LEN) { ADC_buf_1st_index = 0; ADC_buf_full_mark = 1; ADC_buf_1st_turn = 1 - ADC_buf_1st_turn; } break; default: break; } }
|