数字式开关电源的PID控制_第1页
数字式开关电源的PID控制_第2页
数字式开关电源的PID控制_第3页
数字式开关电源的PID控制_第4页
数字式开关电源的PID控制_第5页
已阅读5页,还剩13页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、/*开关电源的数字化控制 */*所用拓扑为Buck,单片机msp430*/*PID算法实现开关电源的恒流、恒压、恒功率功能*/*糖coffeeuestc*/*Part 1 Main.C*/#include #include LCD12864.h#include table.h#include GUI.h#include deal.h#include IndependentKey.h#include init.hvoid main( void ) WDTCTL = WDTPW + WDTHOLD; init_clk(); init_lcd(); GUI_CU(); init_ADC12(); i

2、nit_TA(); init_TB(); initKey(); _EINT(); while(1) KeyScan(); if(Mode=0) /恒压模式 DisplaySetU(); Delay(100); ADC(); Delay(100); else if(Mode=1) /恒流模式 DisplaySetI(); Delay(100); ADC(); Delay(100); else /恒功率模式 DisplaySetP(); Delay(100); ADC(); Delay(100); /*Part 2 Headfile*/*Part 2.1 LCD12864 Headfile*/#i

3、fndef _LCD12864#define _LCD12864#define LCD_CS BIT0#define LCD_SID BIT2#define LCD_CLK BIT4#define LCD_EN P3DIR#define LCD_CTRL P3OUT#define LCD_CS_Set_high LCD_CTRL |= LCD_CS#define LCD_CS_Set_low LCD_CTRL &= LCD_CS#define LCD_SID_Set_high LCD_CTRL |= LCD_SID#define LCD_SID_Set_low LCD_CTRL &= LCD_

4、SID#define LCD_CLK_Set_high LCD_CTRL |= LCD_CLK#define LCD_CLK_Set_low LCD_CTRL &= LCD_CLK#define LCD_delay 1void Write_Data(unsigned char byte ,unsigned char reffer);void Send_Byte(unsigned char byte);void ClearLCD(void);void Put_Array(unsigned char *data , unsigned char length ,unsigned char addre

5、ss);/*延时*/void Delay(unsigned char time) /主时钟1微秒延时 unsigned int i; i=0x08*time; for(; i; i-);/*/函数名称 :Initial_LCD(void)/函数功能 :启动初始化*/void Initial_LCD(void) LCD_EN |= LCD_CS + LCD_SID + LCD_CLK; Write_Data (0x30,0); /基本指令集 Delay(LCD_delay); Write_Data(0x30,0); Write_Data(0x0C,0); Delay(LCD_delay); Wr

6、ite_Data(0x01,0); /清屏 Delay(LCD_delay); Write_Data(0x06,0); Delay(LCD_delay);/*函数名称 :Write_Data(unsigned char byte,unsigned char reffer)*函数功能 :发送指令或数据 reffer 数据 1 指令 0*输入变量 : unsigned char byte unsigned char byte */void Write_Data(unsigned char byte ,unsigned char reffer) unsigned char temp,stemp; t

7、emp = byte; LCD_CS_Set_high; LCD_CLK_Set_low; if(reffer=1) Send_Byte(0xFA); else Send_Byte(0xF8); /发送第一个字节 stemp = (temp & 0xF0); Send_Byte(stemp); stemp = temp 4; Send_Byte(stemp); LCD_CS_Set_low;/*函数名称 :Send_Byte(unsigned char byte)*函数功能 :发送一字节数据*输入变量 :unsigned char byte */void Send_Byte(unsigned

8、char byte) unsigned char temp,i; temp = byte; for(i=0;i8;i+) /send 8 times if (temp&0x80) /send the highest LCD_SID_Set_high; else LCD_SID_Set_low; temp = (temp1); LCD_CLK_Set_low; LCD_CLK_Set_high; Delay(LCD_delay); /*函数名称: Put_Array ( unsigned char data, unsigned char length,unsigned char address

9、)*函数功能: 在LCD上显示一个数组*输入变量: unsigned char data unsigned char length unsigned char address */void Put_Array(unsigned char *data , unsigned char length ,unsigned char address) if(address!=0) Write_Data(address,0); for (unsigned char i=0;ilength;i+) Write_Data(datai,1); /*函数名称 :ClearLCD(void)*函数功能 :清屏*/v

10、oid ClearLCD(void) Write_Data(0x01,0);/*函数名称: Set_cursor*功能描述: 设置光标的位置为CurY行,CurX列*输 入 : CurX=position(0:7) CurY=line(0:3)*/void Set_cursor(unsigned char CurY, unsigned char CurX) switch(CurY) case 0: Write_Data(0x80+CurX,0); break; / 写入第一行CurX列的地址 case 1: Write_Data(0x90+CurX,0); break; / 写入第二行CurX

11、列的地址 case 2: Write_Data(0x88+CurX,0); break; / 写入第三行CurX列的地址 case 3: Write_Data(0x98+CurX,0); break; / 写入第四行CurX列的地址 default: break; /*函数名称 :PutInt(unsigned int num)*函数功能 :显示十进制数*/void PutInt(unsigned int num) unsigned int snum; unsigned char temp12=0, temp112 =0; unsigned char i = 0,n = 0 ; snum =

12、num; if(snum =0 ) temp0 = 0; i+ ; while(num!=0) tempi = num%10+0x30; num = num/10; i+ ; n = i; for(i=0;in; i+) temp1i = tempn-1-i; Put_Array(temp1,n,0);void PutDig(unsigned char num) unsigned char snum; snum = num + 0x30; Write_Data(snum,1);/* 函数名称: PutcLCD()* 功能描述: LCD写char* 输入 : LCD_Char*/void Put

13、cLCD( unsigned char LCD_Char ) Write_Data(LCD_Char,1);/* 函数名称: PutsLCD()* 功能描述: LCD写入RAM字符串* 输入: 字符指针Str*/void PutsLCD(const char *Str) int Str_Temp; Str_Temp=*Str; / Str_Temp 所赋的值是Str所指向的字符 while(Str_Temp!=0x00) / 字符串还没有结束 Write_Data(Str_Temp,1); Str+; Str_Temp=*Str; /*函数名:ClrRam(void)函数功能:清整个GDRAM

14、空间*/void ClrRam() unsigned char x,y ; for(y=0;y64;y+) for(x=0;x16;x+) Write_Data(0x34,0); Write_Data(y+0x80,0); /行地址 Write_Data(x+0x80,0); /列地址 Write_Data(0x30,0); Write_Data(0x00,1); Write_Data(0x00,1); /*函数名: ShowAll函数说明:显示一幅128*64的图画传入参数:图形指针*img*/void ShowAll(unsigned char *pPicture) unsigned ch

15、ar i,j,k; Write_Data(0x36,0); for(i=0;i2;i+) /分上下两屏写 for(j=0;j32;j+) /行 Write_Data(0x80+j,0); /写X坐标(第几行上写) if(i=0) /写Y坐标,判断哪一屏 Write_Data(0x80,0); else Write_Data(0x88,0); for(k=0;k16;k+) /写一整行数据(16个字节,合八个字符) Write_Data(*pPicture+,1); /Write_Data(0x36,0);#endif/*Part 2.2 Table Headfile*/extern unsig

16、ned char img=0x00,0x00,0x00,0x01,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x07,0xFF,0xFF,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1F,0xFF,0xFF,0xF8,0x00,0x00,0x00,0x

17、00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x7F,0xFF,0xFF,0xFE,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0xFF,0xFF,0xFF,0xFF,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x07,0xFE,0x7F,0xFF,0xFF,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0F,0xFF,0x7F,0xFE,0x

18、DF,0xF0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1F,0xFF,0x7F,0xFF,0xBF,0xF8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3F,0xFB,0xFF,0xF9,0x7F,0xFC,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x7F,0xFB,0xFF,0xFF,0x3F,0xFE,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x

19、FF,0xFF,0xFF,0xFF,0xBF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0xFF,0xFF,0xC0,0x03,0xFF,0xFF,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0xFF,0xFE,0x00,0x00,0x7F,0xFF,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0xFF,0xF8,0x00,0x00,0x1F,0xFF,0xC0,0x00,0x00,0x00,0x00,0x00,0x

20、00,0x00,0x00,0x07,0xFF,0xF0,0x00,0x00,0x07,0xFF,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0F,0x7F,0xC0,0x00,0x00,0x03,0xFE,0xF0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0F,0xF7,0x80,0x00,0x00,0x01,0xF5,0xF0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1F,0xFB,0x00,0x00,0x00,0x00,0xE3,0xF8,0x00,0x

21、00,0x00,0x00,0x00,0x00,0x00,0x00,0x1F,0xFF,0x03,0xF8,0x07,0xF8,0x7F,0xF8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3F,0xFE,0x0F,0xFE,0x1F,0xFC,0x7F,0xFC,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3F,0xFC,0x1C,0x0F,0x7C,0x0C,0x3F,0xFC,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3F,0xFC,0x1C,0x03,0xF0,0x

22、06,0x3F,0xFC,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x7F,0xF8,0x08,0x01,0xE0,0x0E,0x1F,0xFE,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x7F,0xF8,0x08,0x07,0xF0,0x0E,0x1F,0xFE,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x7F,0xF0,0x08,0x0F,0x38,0x0C,0x0F,0xFE,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x7F,0x

23、F0,0x0C,0x1E,0x1E,0x0C,0x0F,0xFE,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xF0,0x04,0x7C,0x0E,0x1C,0x0F,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xF0,0x05,0xF8,0x00,0x00,0x07,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xE3,0xBF,0x9C,0xFF,0xEF,0x07,0xFF,0x00,0x00,0x00,0x00,0x00,0x

24、00,0x00,0x00,0xFF,0xE3,0xBB,0x6D,0xFF,0xFF,0x87,0xDF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xEF,0xE3,0xBE,0x6F,0xF3,0xBB,0x87,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xF0,0xE3,0xBE,0xFF,0xFB,0xB8,0x07,0xCF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFE,0xE3,0xFB,0xFC,0x7B,0x9F,0x87,0xFF,0x00,0x

25、00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xE1,0xF3,0x1D,0xF3,0x9F,0x07,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xE0,0x07,0xF8,0x07,0x0C,0x07,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xF0,0x06,0xF8,0x0F,0x18,0x0F,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x7F,0xF0,0x0C,0x1C,0x1E,0x

26、0C,0x0F,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x7F,0xF0,0x0C,0x0E,0x3C,0x0C,0x0F,0xFE,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x7F,0xF8,0x0C,0x07,0x78,0x0C,0x0F,0xFE,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x7F,0xF8,0x18,0x03,0xF0,0x04,0x1F,0xFE,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3F,0x

27、F8,0x18,0x03,0xE0,0x04,0x1F,0xFE,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3F,0xFC,0x08,0x0F,0xF8,0x04,0x3F,0xFC,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3F,0xFE,0x0C,0x3F,0x3E,0x08,0x3F,0xFC,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1F,0xFE,0x07,0xFC,0x07,0xF0,0x7F,0xF8,0x00,0x00,0x00,0x00,0x00,0x

28、00,0x00,0x00,0x1F,0xFF,0x03,0xE0,0x00,0xA0,0xFF,0xF8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0F,0xFF,0x80,0x00,0x00,0x01,0xFF,0xF8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0F,0xFF,0xC0,0x00,0x00,0x03,0xFF,0xF0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x07,0xFF,0xE0,0x00,0x00,0x07,0xFF,0xE0,0x00,0x

29、00,0x00,0x00,0x00,0x00,0x00,0x00,0x07,0xFF,0xF8,0x00,0x00,0x0F,0xFF,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0xFF,0xFC,0x00,0x00,0x3F,0xFF,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0xFF,0xFF,0x00,0x00,0xFF,0xFF,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF,0xE0,0x07,0x

30、FF,0xFF,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x7F,0xFF,0xFF,0xFF,0xFF,0xFE,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3F,0xFF,0xFF,0xFF,0x3F,0xFC,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x

31、0F,0xFF,0xDD,0xC6,0xFF,0xF0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x07,0xFF,0xD6,0xDC,0x7F,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0xFF,0xB9,0xBD,0xBF,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0x7B,0xCD,0xBF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x

32、00,0x00,0x00,0x00,0x00,0x3F,0xF7,0xEE,0x7C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0F,0xFF,0x1F,0xF0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0xFF,0xFF,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3F,0xFC,0x00,0x00,0x00,0x00,0x

33、00,0x00,0x00,0x00,0x00,0x00,0x00;/*Part 2.3 GUI Headfile*/#ifndef _GUI#define _GUI/*开机界面*/void init_lcd() Initial_LCD(); Set_cursor(0,4);PutsLCD(SwPower); Set_cursor(1,4);PutsLCD(糖coffee); Set_cursor(2,4);PutsLCD(糖coffee); Set_cursor(3,4);PutsLCD(糖coffee); ShowAll(img); while(P1IN&0x1f)=0x1f); /*Ini

34、tial_LCD(); Set_cursor(0,0); PutsLCD(Set I:01.00 A); Set_cursor(1,0); PutsLCD(Out U: V); Set_cursor(2,0); PutsLCD(Out I: A); Set_cursor(3,0); PutsLCD(Out P: W);*/*恒流源界面*/void GUI_CC() Initial_LCD(); Set_cursor(0,0); PutsLCD(Set I:01.00 A); Set_cursor(1,0); PutsLCD(Out U: V); Set_cursor(2,0); PutsLCD

35、(Out I: A); Set_cursor(3,0); PutsLCD(Out P: W);/*恒压源界面*/void GUI_CU() Initial_LCD(); Set_cursor(0,0); PutsLCD(Set U:01.00 V); Set_cursor(1,0); PutsLCD(Out U: V); Set_cursor(2,0); PutsLCD(Out I: A); Set_cursor(3,0); PutsLCD(Out P: W);/*恒功率界面*/void GUI_CP() Initial_LCD(); Set_cursor(0,0); PutsLCD(Set

36、P:01.00 W); Set_cursor(1,0); PutsLCD(Out U: V); Set_cursor(2,0); PutsLCD(Out I: A); Set_cursor(3,0); PutsLCD(Out P: W);#endif/*Part 2.4 deal Headfile*/#ifndef _deal#define _dealunsigned int Us = 10; /设置时扩大10倍,默认1.0Vunsigned int Is = 10; /设置时扩大10倍,默认1.0Aunsigned int Ps = 10; /设置时扩大10倍,默认1.0Wfloat Kru

37、 = 0.25; /分压系数5/20float Kri = 1.3; /放大系数约0.05*25unsigned int Uo6;unsigned int Io6;long unsigned int Umem;unsigned int Uout;long unsigned int Imem;unsigned int Iout;unsigned int Pout;signed int iError, lastError, iIncpid;float Kp = 0.05;float Ki = 0.01;unsigned int PWM = 50;unsigned char Mode = 0;/*求

38、数组的平均数*/unsigned int mean(unsigned int a) unsigned int dat = 0; unsigned int max = a0; unsigned int min = a0; for(unsigned int j = 0; j 6; j+) if(max aj) min = aj; for (unsigned int i = 0; i 2; return dat;/*显示输出电压*/void displayU() unsigned char iU,fU; iU = Uout/100; fU = Uout%100; Set_cursor(1,3); P

39、utcLCD(0);PutDig(iU/10);PutcLCD(.);PutDig(iU%10);PutDig(fU/10);/PutDig(fU%10);/*显示输出电流*/void displayI() unsigned char iI,fI; iI = Iout/100; fI = Iout%100; Set_cursor(2,3); PutcLCD(0);PutDig(iI/10);PutcLCD(.);PutDig(iI%10);PutDig(fI/10);/PutDig(fI%10);/*显示输出功率*/void displayP() unsigned char iP,fP; iP

40、 = Pout/100; fP = Pout%100; Set_cursor(3,3); PutDig(iP/10);PutDig(iP%10);PutcLCD(.);PutDig(fP/10);PutDig(fP%10);/*处理电压PWM*/void deal_U() if(UoutUs*100+25)|(Uout+25100*Us) /精度 25mV iError = Us*100 - Uout; /当前误差 iIncpid = (signed int)(Kp*iError + Ki*lastError); PWM = PWM + iIncpid; if(PWM380) PWM = 38

41、0; TBCCR2 = PWM; /*处理电流PWM*/void deal_I() if(IoutIs*100+5)|(IoutIs*100+15) /精度 4.8mA iError = Is*100 - Iout; iIncpid = (signed int)(Kp*iError + Ki*lastError); PWM = PWM + iIncpid; if(PWM380) PWM = 380; TBCCR2 = PWM; /*处理功率PWM*/void deal_P() if(PoutPs*10+3)|(10*PsPout+2) /输出功率大于设定功率 iError = Ps*10 -

42、Pout; iIncpid = (signed int)(Kp*iError + Ki*lastError); PWM = PWM + iIncpid; if(PWM380) PWM = 380; TBCCR2 = PWM; /*计算输出电压电流功率*/void ADC() static unsigned int index = 0; Uoindex = ADC12MEM0; Ioindex = ADC12MEM1; index+; if(index = 6) Umem = mean(Uo); Imem = mean(Io); Uout = (unsigned int)(2500*4.1*Umem/4096); /放大1000倍显示 Iout = (unsigned int)(2500*Imem/(4096*1.3);

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论