用keil5在Protues软件中完成三相逆变桥SPWM调制的仿真过程的代码
时间: 2024-05-08 21:21:39 浏览: 301
以下是用Keil 5编写的三相逆变桥SPWM调制仿真代码,并在Proteus软件中进行仿真:
```c
#include <reg51.h>
#include <intrins.h>
#define FOSC 11059200L
#define BAUD 9600
#define TIMEROUT 20
#define PWMOUT 200
typedef unsigned char uchar;
typedef unsigned int uint;
sbit ENA=P2^0;
sbit ENB=P2^1;
sbit ENC=P2^2;
uchar code sin[180]=
{
128,131,134,137,140,143,146,149,152,155,158,161,165,168,171,174,177,180,183,186,189,192,195,198,201,204,207,210,213,215,218,221,224,227,230,233,236,239,242,244,247,250,252,255,258,260,263,266,268,271,273,276,278,281,283,285,288,290,292,294,296,298,300,302,304,306,308,309,311,313,314,316,317,319,320,321,323,324,325,326,327,328,329,330,331,332,332,333,334,334,335,335,335,336,336,336,336,336,336,335,335,335,334,334,333,332,332,331,330,329,328,327,326,325,324,323,321,320,319,317,316,314,313,311,309,308,306,304,302,300,298,296,294,292,290,288,285,283,281,278,276,273,271,268,266,263,260,258,255,252,250,247,244,242,239,236,233,230,227,224,221,218,215,213,210,207,204,201,198,195,192,189,186,183,180,177,174,171,168,165,161,158,155,152,149,146,143,140,137,134,131,128,124,121,118,115,112,109,106,103,100,97,94,90,87,84,81,78,75,72,69,66,63,60,57,54,51,48,45,42,39,37,34,31,28,25,22,19,16,13,10,8,5,2,0,253,250,247,244,241,238,235,232,229,226,223,220,217,214,211,208,205,202,199,196,193,190,187,184,181,178,175,172,169,167,164,161,158,155,152,149,146,143,140,137,134,131,128,125,122,119,116,113,110,107,104,101,98,95,92,89,86,83,80,77,74,71,68,65,62,59,56,53,50,47,44,41,38,35,33,30,27,24,21,18,15,12,9,6,3,1
};
uchar cnt=0; //计时器中断计数器
uchar idx=0; //正弦波表下标
uchar t_out=0; //定时器中断时间
//定时器0初始化函数
void Timer0_Init()
{
TMOD|=0x01; //16位定时器,工作方式1
TH0=(65536-FOSC/12/50)/256;
TL0=(65536-FOSC/12/50)%256;
TR0=1; //启动定时器
ET0=1; //允许定时器中断
EA=1; //开总中断
}
//定时器0中断服务函数
void Timer0_ISR() interrupt 1
{
TH0=(65536-FOSC/12/50)/256; //重新赋值
TL0=(65536-FOSC/12/50)%256;
cnt++;
if(cnt>=10) //10个定时器中断为1个SPWM周期
{
cnt=0;
idx++;
if(idx>=180) idx=0;
}
if(cnt<=sin[idx]) ENA=1; else ENA=0;
if(cnt<=sin[idx+60]) ENB=1; else ENB=0;
if(cnt<=sin[idx+120]) ENC=1; else ENC=0;
t_out++;
if(t_out>=TIMEROUT) //定时器计时到达
{
t_out=0;
P1=PWMOUT; //PWM输出
}
}
void main()
{
Timer0_Init();
while(1);
}
```
在Proteus软件中,将MCU型号选择为AT89C51,连接相应的IO口和晶振电路,并添加一个三相逆变桥电路。然后将编写好的单片机程序烧录到MCU芯片中,并进行仿真即可。
阅读全文