基于AVR单片机的三相spwm波(转)AVR, 单片机, spwm
相位差120,稍为整一下,也可以搞成变频
//包含所需头文件
#include <ioM16v.h>
#include <macros.h>
/*------宏定义------*/
#define uchar unsigned char
#define uint unsigned int
flash uchar SinParam[234]={
127, 130, 133, 136, 139, 142, 145, 148, 152, 155, 158, 160, 163, 166, 169, 172, 175, 178, 180, 183,
186, 188, 191, 194, 196, 199, 201, 203, 206, 208, 210, 212, 214, 216, 218, 220, 222, 223, 225, 227,
228, 230, 231, 232, 233, 235, 236, 237, 237, 238, 239, 240, 240, 241, 241, 241, 242, 242, 242, 242,
242, 242, 241, 241, 241, 240, 240, 239, 238, 237, 237, 236, 235, 233, 232, 231, 230, 228, 227, 225,
223, 222, 220, 218, 216, 214, 212, 210, 208, 206, 203, 201, 199, 196, 194, 191, 188, 186, 183, 180,
178, 175, 172, 169, 166, 163, 160, 158, 155, 152, 148, 145, 142, 139, 136, 133, 130, 127, 124, 121,
118, 115, 112, 109, 106, 102, 99, 96, 94, 91, 88, 85, 82, 79, 76, 74, 71, 68, 66, 63,
60, 58, 55, 53, 51, 48, 46, 44, 42, 40, 38, 36, 34, 32, 31, 29, 27, 26, 24, 23,
22, 21, 19, 18, 17, 17, 16, 15, 14, 14, 13, 13, 13, 12, 12, 12, 12, 12, 12, 13,
13, 13, 14, 14, 15, 16, 17, 17, 18, 19, 21, 22, 23, 24, 26, 27, 29, 31, 32, 34,
36, 38, 40, 42, 44, 46, 48, 51, 53, 55, 58, 60, 63, 66, 68, 71, 74, 76, 79, 82,
85, 88, 91, 94, 96, 99, 102, 106, 109, 112, 115, 118, 121, 124};
// 234点正弦波样本值
//包含所需头文件
#include <ioM16v.h>
#include <macros.h>
/*------宏定义------*/
#define uchar unsigned char
#define uint unsigned int
uint x_SW =3,X_A =0, X_B =74; X_C=156;
//端口初始化
void port_init(void)
{
PORTA = 0x00;
DDRA = 0x00;
PORTB = 0x00;
DDRB=0x08;
PORTC = 0x00;
DDRC = 0x00;
PORTD = 0x00;
DDRD = 0xF0;
}
//定时器T0初始化
void timer0_init(void)
{
TCCR0 = 0x00;//停止定时器
TCNT0 = 0x00;//初始值
OCR0 = 0x7F;//匹配值
TIMSK |= 0x01;//中断允许
TCCR0 = 0x6E;//启动定时器
}
//T0溢出中断服务程序
#pragma interrupt_handler timer0_ovf_isr:10
void timer0_ovf_isr(void)
{
X_C += x_SW; // 新样点指针
if (X_C> 234)
X_C -=234; // 样点指针调整
OCR0 = SinParam[X_C]; // 取样点指针到比较匹配寄存器
TCNT0 = 0x00;//初始值
}
//定时T1初始化
void timer1_init(void)
{
TCCR1B = 0x00;//停止定时器
TIMSK |= 0x04;//中断允许
TCNT1H = 0x00;
TCNT1L = 0x00;//初始值
OCR1AH = 0x00;
OCR1AL = 0x7F;//匹配A值
OCR1BH = 0x00;
OCR1BL = 0x7F;//匹配B值
ICR1H = 0x00;
ICR1L = 0x7F;//输入捕捉匹配值
TCCR1A = 0xA1;
TCCR1B = 0x0A;//启动定时器
}
//定时器T1溢出中断服务程序
#pragma interrupt_handler timer1_ovf_isr:9
void timer1_ovf_isr(void)
{
X_A += x_SW; // 新样点指针
if (X_A> 234) X_A -=234; // 样点指针调整
OCR1AH = 0x00;
OCR1AL = SinParam[X_A];//匹配A值
X_B += x_SW; // 新样点指针
if (X_B> 234) X_B -=234; // 样点指针调整
OCR1BH = 0x00;
OCR1BL =SinParam[X_B];//匹配B值
TCNT1H = 0x00; //重装值高位
TCNT1L = 0x00; //重装值低位
}
//定时器T2初始化
void timer2_init(void)
{
TCCR2 = 0x00;//停止定时器
ASSR = 0x00;//异步时钟模式
TCNT2 = 0x00;//初始值
OCR2 = 0x01;//匹配值
TIMSK |= 0x00;//中断允许
TCCR2 = 0x19;//启动定时器
}
void init_devices(void)
{
CLI(); //禁止所有中断
MCUCR = 0x00;
MCUCSR = 0x80;//禁止JTAG
GICR = 0x00;
port_init();
timer0_init();
timer1_init();
timer2_init();
SEI();//开全局中断
}
//主函数
void main(void)
{
init_devices();
//TCCR2 = 0x6A;//启动定时器
//OCR2=156;
OCR0=128;
TCNT0 = 0x00;//初始值
TCNT2 = 0x00;//初始值
TIMSK |= 0x40;//中断允许
SEI(); // 开放全局中断
//在这继续添加你的代码
while(1)
{
}
}
做过实物,只要你后级做的好,波形会很好看 |
|