逆变电源程序_第1页
逆变电源程序_第2页
逆变电源程序_第3页
逆变电源程序_第4页
逆变电源程序_第5页
已阅读5页,还剩11页未读 继续免费阅读

下载本文档

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

文档简介

// //main.c,晶振4MHz,20110722,系统测试// #include<pic.h>〃包含单片机内部资源预定义#include<math.h>#include"text.h"//资源定义#include"12864.h"〃显示#include"Initial.h"//初始化#include"keyscan.h"//按键扫描#include"pwm.h"// voidmain(void){Initial。;//调用初始化子函数LCD_lnitial();//LCD初始化子函数LCD_Prints(0,0,"双极性单相SPWM");LCD_Prints(1,0,"逆变器模拟装置");LCD_Prints(2,0,"频率: Hz");LCD_Prints(3,0,'调制度: =>>");while(1){while(!T0IF);〃判断40ms到否{T0IF=0;TMR0=98;asm("clrwdt");//清看门狗keyscan();〃按键扫描if(spwmint&fer_Hz<50)〃上电初始后,频率自动加到50Hz,调制度自动加到0.98{if(delay>15){fer_Hz+=1;delay=0;mark=1;if((fer_Hz%2)&m<15){m++;xian_m+=2;}}elsedelay++;}else{spwmint=0;LCD_Prints(3,6,"");}if(k2){m++;mark=1;xian_m+=2;}〃有k2键,调制度加0.02,艮卩m加1if(xian_m==102)xian_m=68;if(m>16){m=0;mark=1;}〃调制度范围为0.68~1,共17阶,每阶加0.02if(k1){fer_Hz=fer_Hz+10;mark=1;}〃有k1键,正弦波频率加10Hz

if(kO){fer_Hz=fer_Hz+1;mark=1;}〃有k0键,正弦波频率加1Hzif(fer_Hz>1OO)fer_Hz=10;〃正弦波频率范围为10~100HzR4=fer_Hz/1OO; //显示频率和调制度R5=(fer_Hz%100)/10;R6=fer_Hz%10;LCD_Printc(2,2,0x3A);LCD_Writedata(0x00);LCD_Writedata(0x30+R4);LCD_Writedata(0x30+R5);LCD_Writedata(0x30+R6);R4=xian_m/100;R5=xian_m%100/10;R6=xian_m%10;LCD_Printc(3,3,0x3A);LCD_Writedata(0x30+R4);LCD_Writedata(0x2E);LCD_Writedata(0x30+R5);LCD_Writedata(0x30+R6);if(mark){mark=0;//10~24Hz根据频率确定对应的//10~24Hz根据频率确定对应的抽样次数抽样次数PWMPWM周期if(25<=fer_Hz&fer_Hz<39)sta=1; //25~39Hzif(40<=fer_Hz&fer_Hz<59)sta=2; //40~59Hzif(25<=fer_Hz&fer_Hz<39)sta=1; //25~39Hzif(40<=fer_Hz&fer_Hz<59)sta=2; //40~59Hzif(60<=fer_Hz&fer_Hz<100)sta=3;//60~100Hzswitch(sta){case0: //10~24HzN=100;//预分频数为4PR2_time=((250000/fer_Hz)/N)-1;〃某一频率正弦波对应的一个}}choose_table=1;PR2=PR2_time;pwm_N=25;T2CKPS0=1;for(n=0;n<25;n++)〃计算1/4周期正弦波的PWM正脉宽时间{ 〃结果存于number1[]数组中R0=(tiaom[m]*sin100[n])/255;R1=(R0+1)/2;R2=(R1+128);R3=(R2*PR2_time)/255;number1[n]=R3;//将中断中的查表状态值改为1//修改PWM周期值//1/4抽样数给中断查pwm_high个数//欲分频系数为4

for(n=0;n<25;n++)//表1值移入表0中{number0[n]=number1[n];}choose_table=0; //表值移完,中断查表状态值改为0break;case1://25~39HzN=160;//抽样总数160PR2_time=(1000000/fer_Hz)/N-1;〃某一频率正弦波对应的一个PWM周期for(n=0;n<40;n++)//计算1/4周期正弦波的PWM正脉宽时间{ 〃结果存于number1[]数组中R0=(tiaom[m]*sin160[n])/255;R1=(R0+1)/2;R2=(R1+128);R3=(R2*PR2_time)/255;number1[n]=R3;}choose_table=1; //将中断中的查表状态值改为1PR2=PR2_time; //修改PWM周期值pwm_N=40; //1/4抽样数给中断查pwm_high个数T2CKPS0=0; //欲分频系数为1for(n=0;n<40;n++)//表1值移入表0中{number0[n]=number1[n];}choose_table=0; //表值移完,中断查表状态值改为0break;case2: //40~59HzN=100;//抽样总数100PR2_time=(1000000/fer_Hz)/N-1;〃某一频率正弦波对应的一个PWM周期for(n=0;n<25;n++)〃计算1/4周期正弦波的PWM正脉宽时间{ 〃结果存于number1[]数组中R0=(tiaom[m]*sin100[n])/255;R1=(R0+1)/2;R2=(R1+128);R3=(R2*PR2_time)/255;number1[n]=R3;}choose_table=1; //将中断中的查表状态值改为1PR2=PR2_time; //修改PWM周期值pwm_N=25; //1/4抽样数给中断查pwm_high个数T2CKPS0=0; //欲分频系数为1

PWM周期for(n=0;n<25;n++)PWM周期for(n=0;n<25;n++)//表1值移入表0中{number0[n]=number1[n];}choose_table=0; //表值移完,中断查表状态值改为0break;case3://60~100HzN=76; //抽样总数76PR2_time=(1000000/fer_Hz)/N-1;〃某一频率正弦波对应的一个for(n=0;n<19;n++)〃计算1/4周期正弦波的PWM正脉宽时间{ 〃结果存于number1[]数组中number1[n]=R3;}number1[n]=R3;}choose_table=1;PR2=PR2_time;pwm_N=19;T2CKPS0=0;//将中断中的查表状态值改为1//修改PWM周期值//1/4抽样数给中断查pwm_high个数//欲分频系数为1for(n=0;n<19;n++)//表1值移入表0中number0[n]=number1[n];}choose_table=0; //表值移完,中断查表状态值改为0break;default:break;}//switch(sta)}//if(mark)}//while(!T0IF)}//while(1)}//==================================//中断程序voidinterruptpic(void){TMR2IF=0;pwm();if(A==35){RC0=~RC0;A=0;}A++;/初始化voidInitial(void)//上电复位{if(nPOR==0){PORTA=0x00;PORTB=0x00;PORTC=0x00;PORTD=0X00;PORTE=0X00;}while(nPOR==0){nPOR=1;}OPTION_REG=Ox06;〃分频器给TMRO,128分频TRISA=0x01;TRISB=0xC0;TRISC=OxcO;〃第二个IO口CCP1TRISD=0x00;TRISE=OxOO;ADCON1=0x06;〃全部为数字口//OPTION_REG=0X03;〃定时器0分频系数16TMR0=98;〃定时器初值,定时40ms256*157// //PWM工作方式设置INTC0N=0xC0;〃开总中断和外部中断PIE1=0x02;〃定时器2允许中断PIR1=0x00;TRISC=0x00;TMR2=0X00;CCP1C0N=0X0C;//CCP模块工作在PWM方式T2CON=0X00;〃分频1:1,TMR2ON=1,未启动定时器2TMR2ON=1; //启动定时器2mark=1;spwmint=1;}/* 文件:12864.h*功能:并行LCD显示*平台:PIC16F877A4MHz晶振作者:S_gqing*时间:2011.10IO口定义11为数据TOC\o"1-5"\h\z#defineLCD_RS RC5#defineLCD_RW RC6#defineLCD_EN RC7#defineLCD_BUSY_1RD7#defineT_BUSY TRISD7#defineT_DATA TRISD#defineLCD_DATA_IOPORTD *///指令数据选择位。0为指令//读写选择位。0为写1为读//并口使能位//读忙位//忙状态方向设置位//数据口方向设置〃八位数据10口/*=====================//读忙状态//说明:读到不忙才跳出程序=====================*/〃RS=0,RW=1,E=H,D0~D7=状态字voidLCD_CheckBusy(void){busy=1;RD=0x00; //先置位繁忙标志位TRISD=0xFF; 〃RC口设为输入口LCD_RS=0; //设置该字节数据为命令代码LCD_RW=1; //设置此次操作为读while(busy){asm("N0P");asm("N0P");asm("N0P");LCD_EN=1; //使能asm("N0P");asm("N0P");asm("N0P");if(!RD7)busy=0; 〃检测LCD是否工作繁忙asm("N0P");asm("N0P");asm("N0P");LCD_EN=0; //禁止}LCD_EN=0; //禁止TRISD=0X00; //恢复通信为输出}/*voidLCD_CheckBusy(void){ucharflag=1;LCD_RS=0; //指令LCD_RW=1;//读

T_DATA=0xff;〃RD口设为输入口T_BUSY=1; //设为输入口while(flag){asm("NOP");asm("NOP");LCD_EN=1;//__delay_us(5);asm("NOP");asm("NOP");asm("NOP");asm("NOP");asm("NOP");if(!LCD_BUSY_1)flag=0;LCD_EN=0;}LCD_EN=0;//T_DATA=0x00;//RD设为输出口__delay_us(2);//asm("NOP");asm("NOP");}*//*=====================//写命令到LCD//输入:八位命令__cmd=====================*/voidLCD_Writecmd(ucharcom){LCD_CheckBusy();LCD_RS=0;LCD_RW=0;LCD_DATA_IO=com;LCD_EN=1;//__delay_us(3);asm("NOP");asm("NOP");asm("NOP");LCD_EN=0;}/*======================//写一字节数据到LCD//输入:八位数据__data======================*/voidLCD_Writedata(uchardata){LCD_CheckBusy();LCD_RS=1;LCD_RW=0;LCD_DATA_IO=data;LCD_EN=1;//__delay_us(3);asm("NOP");asm("NOP");asm("NOP");LCD_EN=0;}/*=========================读一个数据到单片机返回数值x=========================*/ucharLCD_Readdata(void){unsignedchardata;LCD_CheckBusy();//检测LCD是否工作繁忙TRISD=0xFF;//RD口设为输入口LCD_RS=1; //设置该字节数据是显示数据LCD_RW=1; //设置该次操作为读LCD_EN=1;asm("NOP");asm("NOP");asm("NOP");data=PORTD; //PORTD口数据送dataLCD_EN=0;//使能TRISD=0x00; //下次循环为输出口returndata;//返回数值data}/*========================//LCD光标定位(行和列定位)//输入:行—row列—line〃说明:row为0~3,line为0~7=========================*/voidLCD_SetPos(ucharrow,ucharline){if(row==0)LCD_Writecmd(0x80+line);elseif(row==1)LCD_Writecmd(0x90+line);elseif(row==2)LCD_Writecmd(0x88+line);elseLCD_Writecmd(0x98+line);}/*=========================//写一字节数据到LCD〃输入:row__行川ne__列,data__数据//说明:row为0~3,line为0~15,data为待显示一字节数据=========================*/voidLCD_Printc(ucharrow,ucharline,uchardata){LCD_SetPos(row,line);LCD_Writedata(data);}/*=========================//写字符串到LCD//输入:row__行川ne__列,*string__字符串的首地址指针〃说明:row为0~3,line为0~15,字符串用常量const修饰=========================*/voidLCD_Prints(ucharrow,ucharline,constuchar*string){LCD_SetPos(row,line);while(*string){LCD_Writedata(*string);string++;}}/*======================//清GDRAM内容======================*/voidLCD_CleanGDRAM(void){ucharx,y;for(y=0;y<64;y++){for(x=0;x<16;x++){LCD_Writecmd(0x34);LCD_Writecmd(0x80+y);〃行地址LCD_Writecmd(0x80+x);〃列地址LCD_Writecmd(0x30);LCD_Writedata(0x00);LCD_Writedata(0x00);}

/*======================//延时程序======================*/voidDelay_LCD(void){uchark;for(k=50;k>0;k++);}/*=========================//LCD初始化=========================*/voidLCD_lnitial(void){//Delay_LCD();__delay_ms(500);//延时50mSLCD_Writecmd(0x30);//选择8位数据,基本指令集LCD_Writecmd(0x30);//选择8位数据,基本指令集//LCD_Writecmd(0x01);//LCD_Writecmd(0x06);LCD_Writecmd(0x0c);//开显示(无游标,观标不闪)LCD_Writecmd(0x01);//清显示,设定地址指针为00HLCD_Writecmd(0x06);//指定在资料的读取和写入时,设定游标的移动方向为右及指定显示的移位LCD_CleanGDRAM();#defineucharunsignedchar#defineuintunsignedint#defineRAPORTA#defineRBPORTB#defineRCPORTC#defineRDPORTD#defineREPORTE//指令数据选择位。//指令数据选择位。0为指令1为数据//读写选择位。0为写1为读//并口使能位//读忙位//忙状态方向设置位//数据口方向设置TOC\o"1-5"\h\z#defineLCD_RS RC5#defineLCD_RW RC6#defineLCD_EN RC7#defineLCD_BUSY_1RD7#defineT_BUSY TRISD7#defineT_DATA TRISD

#defineLCD_DATA_IO// staticvolatilebitxs7staticvolatilebitxs6staticvolatilebitxs5staticvolatilebitxs4staticvolatilebitxs3staticvolatilebitxs2staticvolatilebitxs1staticvolatilebitxs0PORTD〃八位数据#defineLCD_DATA_IO// staticvolatilebitxs7staticvolatilebitxs6staticvolatilebitxs5staticvolatilebitxs4staticvolatilebitxs3staticvolatilebitxs2staticvolatilebitxs1staticvolatilebitxs0@(unsigned)&PORTE*8+2;@(unsigned)&P0RTE*8+1;@(unsigned)&P0RTE*8+0;@(unsigned)&P0RTB*8+5;@(unsigned)&P0RTB*8+4;@(unsigned)&P0RTB*8+3;@(unsigned)&P0RTB*8+2;@(unsigned)&P0RTB*8+1;//定义十二个按键//定义十二个按键staticvolatilebitk0staticvolatilebitk1staticvolatilebitk2staticvolatilebitk3staticvolatilebitk4staticvolatilebitk5staticvolatilebitk6staticvolatilebitk7@(unsigned)&key1*8+0;@(unsigned)&key1*8+1;@(unsigned)&key1*8+2;@(unsigned)&key1*8+3;//定义十二个按键//定义十二个按键staticvolatilebitk0staticvolatilebitk1staticvolatilebitk2staticvolatilebitk3staticvolatilebitk4staticvolatilebitk5staticvolatilebitk6staticvolatilebitk7@(unsigned)&key1*8+0;@(unsigned)&key1*8+1;@(unsigned)&key1*8+2;@(unsigned)&key1*8+3;@(unsigned)&key1*8+4;@(unsigned)&key1*8+5;@(unsigned)&key1*8+6;@(unsigned)&key1*8+7;staticvolatilebitstaticvolatilebitstaticvolatilebitstaticvolatilebitk8@(unsigned)&key2*8+0;k9@(unsigned)&key2*8+1;k10@(unsigned)&key2*8+2;k11@(unsigned)&key2*8+3;staticvolatilebitstaticvolatilebitstaticvolatilebitstaticvolatilebitstaticvolatilebitlk0@(unsigned)&lkey1*8+0;staticvolatilebitlk1@(unsigned)&lkey1*8+1;staticvolatilebitlk2@(unsigned)&lkey1*8+2;staticvolatilebitlk3@(unsigned)&lkey1*8+3;staticvolatilebitlk4@(unsigned)&lkey1*8+4;staticvolatilebitlk5@(unsigned)&lkey1*8+5;staticvolatilebitlk6@(unsigned)&lkey1*8+6;staticvolatilebitlk7@(unsigned)&lkey1*8+7;staticvolatilebitlk8@(unsigned)&lkey2*8+0;staticvolatilebitlk9@(unsigned)&lkey2*8+1;staticvolatilebitlk10@(unsigned)&lkey2*8+2;staticvolatilebitlk11@(unsigned)&lkey2*8+3;

// //定义变量ucharR0,R1,R2,R3,R4,R5,R6,R7;ucharkeycnt;//uchardsbit;//uchardsbuf[8];//uinttimecnt,adcnt,timepwm;//unsignedlongintadbuf;//uchardsfl;//==========================constuchartiaom[17]={173,179,184,189,194,199,204,209,214,219,224,230,235,240,245,250,255};const ucharsin76[19]={3,24,45,66,86,105,124,142,159,175,190,203,215,226,235,242,248,252,254};const ucharsin100[25]={3,19,34,50,66,81,96,111,125,139,152,164,176,188,198,208,217,225,232,238,243,248,251,253,255};constucharsin160[40]={2,12,22,32,41,51,61,71,80,90,99,108,117,126,135,143,151,159,167,174,181,188,195,201,207,213,218,223,228,232,236,240,243,246,248,250,252,253,254,255};ucharPR2_time,choose_way,fer_Hz=10;ucharnumber0[40];ucharnumber1[40];ucharsta,delay;ucharpwm_N,sam_times=O;〃抽样总数,某一次抽样序号中断中读数据状态值ucharn,N,m=0,choose_table=0;bitmark,busy;bitspwmint;ucharxian_m=68;ucharA;// //声明本模块中所调用的函数类型// voidInitial(void); //上电复位//定时、走时//定时、走时//按键扫描voidLCD_lnitial(void);voidkeyscan(void);//voidAD(ucharm,ucharn);//voidMCP41O1O(ucharm);//voidDA12(unsignedintm);//uintAD1O(ucharm,ucharn);#ifndef_XTAL_FREQ//声明4MHz//声明4MHz晶振#endifvoidkeyscan(void) //按键扫描{keycnt++;xs0=xs1=xs2=xs3=xs4=xs5=xs6=xs7=RD7=0;//关显示TRISB=0XF0; //低四位为输入//RD6=1;RD5=0;RD4=1; //(k4~k7)asm("nop");asm("nop");RO=(RBSXfO)&OxfO;R0=R0>>4;// RD4=O;RD5=1;RD6=1; //(kO~k3)//asm("nop");asm("nop");// RO+=((RDAOXOF)&OXOF);// RD6=O;RD5=1;RD4=1; //(k8~k11//asm("nop");asm("nop");//R1=(RDAOXOf)&OxOf;if(RO|R1) //有键?{if(keycnt>=12){keycnt=O;key1=RO;key2=R1;}else{key1=lkey1;key2=lkey2;}}else{key1=key2=O;keycnt=O;}R2=key1;R3=key2;key1=key1&(Ikey"key1);lkey1=R2;key2=key2&(Ikey2Akey2);Ikey2=R3;}// 〃单片机内部1O位AD,入口参数为(adconO,adcon1)//返回为采样到的AD值// uintAD10(ucharm,ucharn){uchari;uintvan;union //定义共用体以存每次AD结果{uinty1;uchary2[2];}ad_temp;van=0;PIE1=0x00;//禁止AD中断ADCON1=n;//设置通道类型参考电压及对其格式PEIE=0;//禁止外设中断PIR1=0x00;//AD中断标志位清零ADCON0=m;//设置AD通道及开ADONi=4;while(i--){;} //采样时间12周期i=8;while(i--)//连续采样8次,求平均值{ADIF=0;//

温馨提示

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

评论

0/150

提交评论