基于c51的spwm逆变器程序
时间: 2023-06-13 13:06:53 浏览: 186
以下是基于C51的SPWM逆变器程序的示例代码:
```c
#include <reg51.h>
// 定义时钟频率
#define FOSC 11059200L
#define BAUD 9600
#define TIMER_1 50000
// 定义GPIO引脚
sbit Ir1 = P2^2;
sbit Ir2 = P2^3;
sbit Ir3 = P2^4;
sbit Ir4 = P2^5;
sbit Ir5 = P2^6;
sbit Ir6 = P2^7;
// 定义变量
unsigned int counter = 0;
unsigned int counter1 = 0;
unsigned int counter2 = 0;
// 定义数组
unsigned char code sin_table[64] = { 128, 131, 134, 137, 140, 143, 146, 149, 152, 155, 158, 162, 165, 168, 171, 174, 177, 180, 183, 186, 189, 192, 195, 198, 201, 204, 207, 210, 213, 216, 218, 221, 224, 227, 230, 232, 235, 238, 240, 243, 246, 248, 251, 253, 255, 258, 260, 262, 264, 266, 268, 270, 272, 274, 275, 277, 279, 280, 281, 283, 284, 285, 286, 287, 288, 288 };
// 定义函数
void delay(unsigned int i);
void timer1_init(void);
void uart_init(void);
void uart_send(unsigned char dat);
void spwm_output(unsigned char duty_cycle);
void spwm_interrupt(void) interrupt 3;
void main(void)
{
// 初始化定时器1
timer1_init();
// 初始化串口
uart_init();
// 循环输出SPWM波形
while (1) {
spwm_output(50); // 占空比50%
delay(1000);
spwm_output(75); // 占空比75%
delay(1000);
spwm_output(100); // 占空比100%
delay(1000);
}
}
// 延时函数
void delay(unsigned int i)
{
while (i--);
}
// 定时器1初始化函数
void timer1_init(void)
{
TMOD |= 0x10; // 定时器1工作在方式1
TH1 = (65536 - (FOSC / 12 / TIMER_1)); // 定时器1初值
TL1 = (65536 - (FOSC / 12 / TIMER_1));
ET1 = 1; // 使能定时器1中断
EA = 1; // 使能总中断
TR1 = 1; // 启动定时器1
}
// 串口初始化函数
void uart_init(void)
{
SCON = 0x50; // 串口方式1,8位数据位,可变波特率
TMOD |= 0x20; // 定时器1工作在方式2
TH1 = TL1 = -(FOSC / 12 / 32 / BAUD); // 波特率
TR1 = 1; // 启动定时器1
}
// 串口发送函数
void uart_send(unsigned char dat)
{
SBUF = dat; // 发送数据
while (!TI); // 等待发送完成
TI = 0; // 标志位清零
}
// SPWM输出函数
void spwm_output(unsigned char duty_cycle)
{
unsigned char i;
unsigned char phase_shift = 0;
unsigned char angle;
unsigned char sin_value;
unsigned int time_period;
time_period = (unsigned int)(TIMER_1 * 1000 / 12);
phase_shift = 64 * duty_cycle / 100;
for (i = 0; i < 64; i++) {
angle = (i + phase_shift) % 64;
sin_value = sin_table[angle];
switch (sin_value) {
case 128:
Ir1 = 1; Ir2 = 0; Ir3 = 0; Ir4 = 0; Ir5 = 0; Ir6 = 0;
break;
case 131:
Ir1 = 1; Ir2 = 0; Ir3 = 0; Ir4 = 0; Ir5 = 0; Ir6 = 1;
break;
case 134:
Ir1 = 0; Ir2 = 0; Ir3 = 0; Ir4 = 0; Ir5 = 0; Ir6 = 1;
break;
case 137:
Ir1 = 0; Ir2 = 0; Ir3 = 0; Ir4 = 0; Ir5 = 1; Ir6 = 1;
break;
case 140:
Ir1 = 0; Ir2 = 0; Ir3 = 0; Ir4 = 0; Ir5 = 1; Ir6 = 0;
break;
case 143:
Ir1 = 0; Ir2 = 0; Ir3 = 0; Ir4 = 1; Ir5 = 1; Ir6 = 0;
break;
case 146:
Ir1 = 0; Ir2 = 0; Ir3 = 0; Ir4 = 1; Ir5 = 0; Ir6 = 0;
break;
case 149:
Ir1 = 0; Ir2 = 0; Ir3 = 1; Ir4 = 1; Ir5 = 0; Ir6 = 0;
break;
case 152:
Ir1 = 0; Ir2 = 0; Ir3 = 1; Ir4 = 0; Ir5 = 0; Ir6 = 0;
break;
case 155:
Ir1 = 0; Ir2 = 1; Ir3 = 1; Ir4 = 0; Ir5 = 0; Ir6 = 0;
break;
case 158:
Ir1 = 0; Ir2 = 1; Ir3 = 0; Ir4 = 0; Ir5 = 0; Ir6 = 0;
break;
case 162:
Ir1 = 0; Ir2 = 1; Ir3 = 0; Ir4 = 0; Ir5 = 0; Ir6 = 1;
break;
case 165:
Ir1 = 1; Ir2 = 1; Ir3 = 0; Ir4 = 0; Ir5 = 0; Ir6 = 1;
break;
case 168:
Ir1 = 1; Ir2 = 0; Ir3 = 0; Ir4 = 0; Ir5 = 0; Ir6 = 1;
break;
case 171:
Ir1 = 1; Ir2 = 0; Ir3 = 0; Ir4 = 0; Ir5 = 1; Ir6 = 1;
break;
case 174:
Ir1 = 1; Ir2 = 0; Ir3 = 0; Ir4 = 0; Ir5 = 1; Ir6 = 0;
break;
case 177:
Ir1 = 1; Ir2 = 0; Ir3 = 0; Ir4 = 1; Ir5 = 1; Ir6 = 0;
break;
case 180:
Ir1 = 1; Ir2 = 0; Ir3 = 0; Ir4 = 1; Ir5 = 0; Ir6 = 0;
break;
case 183:
Ir1 = 1; Ir2 = 0; Ir3 = 1; Ir4 = 1; Ir5 = 0; Ir6 = 0;
break;
case 186:
Ir1 = 1; Ir2 = 0; Ir3 = 1; Ir4 = 0; Ir5 = 0; Ir6 = 0;
break;
case 189:
Ir1 = 1; Ir2 = 1; Ir3 = 1; Ir4 = 0; Ir5 = 0; Ir6 = 0;
break;
case 192:
Ir1 = 1; Ir2 = 1; Ir3 = 0; Ir4 = 0; Ir5 = 0; Ir6 = 0;
break;
case 195:
Ir1 = 1; Ir2 = 1; Ir3 = 0; Ir4 = 0; Ir5 = 0; Ir6 = 1;
break;
case 198:
Ir1 = 0; Ir2 = 1; Ir3 = 0; Ir4 = 0; Ir5 = 0; Ir6 = 1;
break;
case 201:
Ir1 = 0; Ir2 = 1; Ir3 = 0; Ir4 = 0; Ir5 = 1; Ir6 = 1;
break;
case 204:
Ir1 = 0; Ir2 = 1; Ir3 = 0; Ir4 = 0; Ir5 = 1; Ir6 = 0;
break;
case 207:
Ir1 = 0; Ir2 = 1; Ir3 = 0; Ir4 = 1; Ir5 = 1; Ir6 = 0;
break;
case 210:
Ir1 = 0; Ir2 = 1; Ir3 = 0; Ir4 = 1; Ir5 = 0; Ir6 = 0;
break;
case 213:
Ir1 = 0; Ir2 = 1; Ir3 = 1; Ir4 = 1; Ir5 = 0; Ir6 = 0;
break;
case 216:
Ir1 = 0; Ir2 = 1; Ir3 = 1; Ir4 = 0; Ir5 = 0; Ir6 = 0;
break;
case 218:
Ir1 = 1; Ir2 = 1; Ir3 = 0; Ir4 = 0; Ir5 = 0; Ir6 = 0;
break;
case 221:
Ir1 = 1; Ir2 = 0; Ir3 = 0; Ir4 = 0; Ir5 = 0; Ir6 = 0;
break;
case 224:
Ir1 = 1; Ir2 = 0; Ir3 = 0; Ir4 = 0; Ir5 = 0; Ir6 = 1;
break;
case 227:
Ir1 = 1; Ir2 = 0; Ir3 = 0; Ir4 = 0; Ir5 = 1; Ir6 = 1;
break;
case 230:
Ir1 = 1; Ir2 = 0; Ir3 = 0; Ir4 = 0; Ir5 = 1; Ir6 = 0;
break;
case 232:
Ir1 = 1; Ir2 = 0; Ir3 = 0; Ir4 = 1; Ir5 = 1; Ir6 = 0;
break;
case 235:
Ir1 = 1; Ir2 = 0; Ir3 = 0; Ir4 = 1; Ir5 = 0; Ir6 = 0;
break;
case 238:
Ir1 = 1; Ir2 = 0; Ir3 = 1; Ir4 = 1; Ir5 = 0; Ir6 = 0;
break;
case 240:
Ir1 = 1; Ir2 = 0; Ir3 = 1; Ir4 = 0; Ir5 = 0; Ir6 = 0;
break;
case 243:
Ir1 = 1; Ir2 = 1; Ir3 = 1; Ir4 = 0; Ir5 = 0; Ir6 = 0;
break;
case 246:
Ir1 = 1; Ir2 = 1; Ir3 = 0; Ir4 = 0; Ir5 = 0; Ir6 = 0;
break;
case 248:
Ir1 = 1; Ir2 = 1; Ir3 = 0; Ir4 = 0; Ir5 = 0; Ir6 = 1;
break;
case 251:
Ir1 = 0; Ir2 = 1; Ir3 = 0; Ir4 = 0; Ir5 = 0; Ir6 = 1;
break;
case 253:
Ir1 = 0; Ir2 = 1; Ir3 = 0; Ir4 = 0; Ir5 = 1; Ir6 = 1;
break;
case 255:
Ir1 = 0; Ir2 = 1; Ir3 = 0; Ir4 = 0; Ir5 = 1; Ir6 = 0;
break;
case 258:
Ir1 = 0; Ir2 = 1; Ir3 = 0; Ir4 = 1; Ir5 = 1; Ir6 = 0;
break;
case 260:
Ir1 = 0; Ir2 = 1; Ir3 = 0; Ir4 = 1; Ir5 = 0; Ir6 = 0;
break;
case 262:
Ir1 = 0; Ir2 = 1; Ir3 = 1; Ir4 = 1; Ir5 = 0; Ir6 = 0;
break;
case 264:
Ir1 = 0; Ir2 = 1; Ir3 = 1; Ir4 = 0; Ir5 = 0; Ir6 = 0;
break;
case 266:
Ir1 = 1; Ir2 = 1; Ir3 = 0; Ir4 = 0; Ir5 = 0; Ir6 = 0;
break;
case 268:
Ir1 = 1; Ir2 = 0; Ir3 = 0; Ir4 = 0; Ir5 = 0; Ir6 = 0;
break;
case 270:
Ir1 = 1; Ir2 = 0; Ir3 = 0; Ir4 = 0; Ir5 = 0; Ir6 = 1;
break;
case 272:
Ir1 = 1; Ir2 = 0; Ir3 = 0; Ir4 = 0; Ir5 = 1; Ir6 = 1;
break;
case 274:
Ir1 = 1; Ir2 = 0; Ir3 = 0; Ir4 = 0; Ir5 = 1; Ir6 = 0;
break;
case 275:
Ir1 = 1; Ir2 = 0; Ir3 = 0; Ir4 = 1; Ir5 = 1; Ir6 = 0;
break;
case 277:
Ir1 = 1; Ir2 = 0; Ir3 = 0; Ir4 = 1; Ir5 = 0; Ir6 = 0;
break;
case 279:
Ir1 = 1; Ir2 = 0; Ir3 = 1; Ir4 = 1; Ir5 = 0; Ir6 = 0;
break;
case 280:
Ir1 = 1; Ir2 = 0; Ir3 = 1; Ir4 = 0; Ir5 = 0; Ir6 = 0;
break;
case 281:
Ir1 = 1; Ir2 = 1; Ir3 = 1; Ir4 = 0; Ir5 = 0; Ir6 = 0;
break;
case 283:
Ir1 = 1; Ir2 = 1; Ir3 = 0; Ir4 = 0; Ir5 = 0; Ir6 = 0;
break;
case 284:
Ir1 = 1; Ir2 = 1; Ir3 = 0; Ir4 = 0; Ir5 = 0; Ir6 = 1;
break;
case 285:
Ir1 = 0; Ir2 = 1; Ir3 = 0; Ir4 = 0; Ir5 = 0; Ir6 = 1;
break;
case 286:
Ir1 = 0; Ir2 = 1; Ir3 = 0; Ir4 = 0; Ir5 = 1; Ir6 = 1;
break;
case 287:
Ir1 = 0; Ir2 = 1; Ir3 = 0; Ir4 = 0; Ir5 = 1; Ir6 = 0;
break;
case 288:
default:
Ir1 = 0; Ir2 = 1; Ir3 = 0; Ir4 = 1; Ir5 = 1; Ir6 = 0;
break;
}
delay(time_period);
}
}
// SPWM中断函数
void spwm_interrupt(void) interrupt 3
{
TH1 = TL1 = -(FOSC / 12 / BAUD);
counter++;
if (counter == 200) {
counter = 0;
uart_send(counter1);
counter1++;
if (counter1 == 100) {
counter1 = 0;
}
}
counter2++;
if (counter2 == 1000
阅读全文