2013年全国大学生电子设计竞赛电源组全国二等奖

7.附录2:系统主程序

#include \#include #define uchar unsigned char #define uint unsigned int int U1[40]; int U2[40]; int I[40];

float kp=0.0092,ki=0.01,k=0.5,h=0.9886; int ad=0,r=6,Ugd=36; float M; uchar flag=0;

long U1aver,U2aver,U1Q,U2Q,IQ,Iaver;

void clk_init(void) {

P5SEL |= 0x0C; P11DIR = BIT1+BIT2; P11SEL |= BIT1+BIT2; UCSCTL6 &= ~XT2OFF; UCSCTL3 |= SELREF_2; UCSCTL4 |= SELA_2; ACLK=REFO,SMCLK=DCO,MCLK=DCO

// Loop until XT1,XT2 & DCO stabilizes do {

// Port select XT2

// P11.1-2 to output direction // P11.1-2 to output SMCLK,MCLK // Enable XT2 // FLLref = REFO

// 17

UCSCTL7 &= ~(XT2OFFG + XT1LFOFFG + XT1HFOFFG + DCOFFG); // Clear XT2,XT1,DCO fault flags

SFRIFG1 &= ~OFIFG; // Clear fault flags }while (SFRIFG1&OFIFG); // Test oscillator fault flag UCSCTL6 &= ~XT2DRIVE0; // Decrease XT2 Drive according to

// expected frequency UCSCTL4 |= SELS_5 + SELM_5; // SMCLK=MCLK=XT2 }

void adc_init(void) {

P6SEL |=0XFF;

ADC12CTL0 =ADC12ON+ADC12SHT0_7+ADC12MSC; ADC12CTL1 =ADC12SHP+ADC12SSEL_2+ADC12CONSEQ_1; ADC12MCTL0=ADC12INCH_0; ADC12MCTL1=ADC12INCH_1;

ADC12MCTL2=ADC12INCH_2+ADC12EOS; ADC12CTL0 |=ADC12ENC; }

void adc_jieguo(void) {

ADC12CTL0 |=ADC12ENC; ADC12CTL0 |=ADC12SC; while((ADC12IFG&BIT2)==0); U1[ad]=ADC12MEM0;

18

I[ad] =ADC12MEM1; U2[ad]=ADC12MEM2; ADC12CTL0 &=~ADC12ENC; }

/*********************PID调节******************/ uint PID(float m1q,float m2q,float kp1,float ki1) { int xx; ek1=(m1q-m2q);

xx=ki1*(ek1-ek0)+kp1*ek1; if(xx>6)xx=6; else

if(xx<-6)xx=-6; M=M+xx;

if(M>112) M=112; //输出限定70% if(M<2) M=2; //输出限定10% ek0=ek1; return (M); }

/****************主程序*****************/ int main( void ) {

WDTCTL = WDTPW + WDTHOLD; clk_init(); ta_init(); tb_init(); adc_init(); uart_init(); _EINT();

19

P1DIR=0XFF; P1OUT=0XFF; while(1) {

crc(); } }

#pragma vector=USCI_A0_VECTOR __interrupt void USCI_A0_ISR(void) {

switch(__even_in_range(UCA0IV,4)) {

case 0:break; case 2:M=UCA0RXBUF<<4; break;

case 4:break; default: break; } }

#pragma vector=TIMER1_A0_VECTOR __interrupt void TIMER1_A0_ISR(void) {

adc_jieguo(); ad++; if(ad==40) {

ad=0; }

// Vector 0 - no interrupt // Vector 2 - RXIFG // Vector 4 - TXIFG 20

8.参考文献

[1]王兆安,刘进军.电力电子技术[M] . 5版. 北京:机械工业出版社,2009 [2]王兆安,黄俊.电力电子变流技术[M] . 3版. 北京:机械工业出版社,1993

[3]沈建华,杨艳琴.MSP430系列16位超低功耗单片机原理与应用. 北京:清华大学出版社,2004

[4]余永权,李小青.单片机应用系统的功率接口技术[M]. 北京:北京航天航空大学出版社,1992

[5]康华光.电子技术基础模拟部分. 5版. 北京:高等教育出版社,2005 [6]康华光.电子技术基础数字部分. 5版. 北京:高等教育出版社,2005 [7]谭浩强.C语言程序设计. 2版. 北京:清华大学出版社,2002

21

联系客服:779662525#qq.com(#替换为@) 苏ICP备20003344号-4