您好,欢迎访问三七文档
#includec8051f020.h#defineTcount13333sbitSPWM_OUT1=P0^4;sbitSPWM_OUT2=P0^5;//调制度为0.8//unsignedintcodesin_H[25]={26,78,129,178,224,267,305,338,367,389,405,415,418,415,405,389,367,338,305,267,224,178,129,78,26};unsignedintcodesin_L[25]={497,445,394,345,299,256,218,185,156,134,118,108,105,108,118,134,156,185,218,256,299,345,394,445,497};unsignedmax=24;unsignedchari_SPWM1,i_SPWM2;unsignedinttemp0,temp1;voidSysclkInit(void){OSCICN=0x87;//设置SYSCLK为16MHz内部振荡器}voidPortInit(void){XBR0=0x1d;//使CEX0和CEX1输出到P0.0、P0.1XBR1=0x14;//外部中断0端脚来测频率,外部中断1端脚来测相位差XBR2=0x40;//允许交叉开关和弱上拉P0MDOUT=0xff;//端口引脚输出方式为推挽}voidPCA0Init(void){PCA0MD=0x00;//设置空闲等待方式,PCA时基=SYSCLK/12//禁止CF中断PCA0CPM0=0x4d;//PCA输出方式选择,8位pwmPCA0CPM1=0x4d;}voidmain(){SysclkInit();PortInit();WDTCN=0xde;//禁止看门狗定时器WDTCN=0xad;PCA0Init();i_SPWM1=0;i_SPWM2=0;temp0=sin_L[0];temp1=sin_L[0]+Tcount;PCA0CPL0=(0xff&temp0);PCA0CPH0=(0xff&(temp08));//PCA0CP0赋值PCA0CPL1=(0xff&temp1);PCA0CPH1=(0xff&(temp18));//PCA0CP0赋值PCA0CPM0=0x4d;PCA0CPM1=0x4d;EIP1=0x08;//PCA高优先级EIE1|=0x08;//允许PCA中断EA=1;PCA0CN=0x40;//允许PCA计数while(1){}PCON|=0x01;}voidPCA_ISR(void)interrupt9{if(CCF0)//CCF0:PCA0模块0捕捉/比较标志在发生一次匹配或捕捉时该位由硬件置位。当CCF中断被允许时,该位置‘1’//将导致CPU转向CCF中断服务程序。该位不能由硬件自动清0,必须用软件清0{CCF0=0;if(SPWM_OUT1){temp0=(PCA0CPH08)|PCA0CPL0;temp0+=sin_H[i_SPWM1];}else{if(i_SPWM1==max)i_SPWM1=0;elsei_SPWM1++;temp0=(PCA0CPH08)|PCA0CPL0;if(i_SPWM1==0){temp0+=sin_L[max]+Tcount;}else{temp0+=sin_L[i_SPWM1];}}PCA0CPL0=(0xff&temp0);PCA0CPH0=(0xff&(temp08));}elseif(CCF1)//CCF1:PCA0模块1捕捉/比较标志在发生一次匹配或捕捉时该位由硬件置位。当CCF中断被允许时,该位置‘1’//将导致CPU转向CCF中断服务程序。该位不能由硬件自动清0,必须用软件清0{CCF1=0;if(SPWM_OUT2){temp1=(PCA0CPH18)|PCA0CPL1;temp1+=sin_H[i_SPWM2];}else{if(i_SPWM2==max)i_SPWM2=0;elsei_SPWM2++;temp1=(PCA0CPH18)|PCA0CPL1;if(i_SPWM2==0){temp1=temp0+Tcount;}else{temp1+=sin_L[i_SPWM2];}}PCA0CPL1=(0xff&temp1);PCA0CPH1=(0xff&(temp18));}elseif(CCF2){CCF2=0;}elseif(CCF3){CCF3=0;}elseif(CCF4){CCF4=0;}elseif(CF){CF=0;}}
本文标题:SPWM-C程序
链接地址:https://www.777doc.com/doc-6494207 .html