AVR單片機(jī)的SPWM程序
#pragma interrupt_handler T0:17 //定時(shí)器0的溢出中斷向量
unsigned int SPWM_OCR1A[101]={493,492,491,490,489,488,487,486,485,484,
483,482,481,479,478,477,476,475,474,473,
472,471,470,469,468,467,466,465,464,463,
462,462,461,460,459,458,457,456,455,454,
453,452,451,450,449,448,448,447,446,445,
444,443,442,441,440,440,439,438,437,436,
435,434,434,433,432,431,430,429,429,428,
427,426,425,424,424,423,422,421,420,420,
419,418,417,416,416,415,414,413,413,412,
411,410,410,409,408,407,407,406,405,404,404,};
//數(shù)組里面的值分別對(duì)應(yīng)于從45.0~55.0HZ的OCR1A的值,(將每個(gè)周期分成360份,即每一度為一份)步進(jìn)為0.1HZ 在寫程序的時(shí)候,
//只要將OCR1A的值替換成以上相應(yīng)的值就可以得到,對(duì)應(yīng)于正弦波的相應(yīng)的頻率
double SPWM_sin_value[360]={1.0174,1.0348,1.0523,1.0697,1.0871,1.1045,1.1218,1.1391,1.1564,1.1736,1.1908,1.2079,1.2249,1.2419,
1.2588,1.2756,1.2923,1.309,1.3255,1.342,1.3583,1.3746,1.3907,1.4067,1.4226,1.4383,1.4539,1.4694,1.4848,1.4999,
1.515,1.5299,1.5446,1.5591,1.5735,1.5877,1.6018,1.6156,1.6293,1.6427,1.656,1.6691,1.6819,1.6946,1.7071,1.7193,
1.7313,1.7431,1.7547,1.766,1.7771,1.788,1.7986,1.809,1.8191,1.829,1.8386,1.848,1.8571,1.866,1.8746,1.8829,1.891,
1.8987,1.9063,1.9135,1.9205,1.9271,1.9335,1.9396,1.9455,1.951,1.9563,1.9612,1.9659,1.9702,1.9743,1.9781,1.9816,
1.9848,1.9876,1.9902,1.9925,1.9945,1.9961,1.9975,1.9986,1.9993,1.9998,2,1.9998,1.9993,1.9986,1.9975,1.9961,1.9945,
1.9925,1.9902,1.9876,1.9848,1.9816,1.9781,1.9743,1.9702,1.9659,1.9612,1.9563,1.951,1.9455,1.9396,1.9335,1.9271,
1.9205,1.9135,1.9063,1.8987,1.891,1.8829,1.8746,1.866,1.8571,1.848,1.8386,1.829,1.8191,1.809,1.7986,1.788,1.7771,
1.766,1.7547,1.7431,1.7313,1.7193,1.7071,1.6946,1.6819,1.6691,1.656,1.6427,1.6293,1.6156,1.6018,1.5877,1.5735,
1.5591,1.5446,1.5299,1.515,1.5,1.4848,1.4694,1.4539,1.4383,1.4226,1.4067,1.3907,1.3746,1.3583,1.342,1.3255,1.309,
1.2923,1.2756,1.2588,1.2419,1.2249,1.2079,1.1908,1.1736,1.1564,1.1391,1.1218,1.1045,1.0871,1.0697,1.0523,1.0348,
1.0174,1,0.9825,0.9651,0.9476,0.9302,0.9128,0.8954,0.8781,0.8608,0.8435,0.8263,0.8091,0.792,0.775,0.758,0.7411,
0.7243,0.7076,0.6909,0.6744,0.6579,0.6416,0.6253,0.6092,0.5932,0.5773,0.5616,0.546,0.5305,0.5151,0.5,0.4849,0.47,
0.4553,0.4408,0.4264,0.4122,0.3981,0.3843,0.3706,0.3572,0.3439,0.3308,0.318,0.3053,0.2928,0.2806,0.2686,0.2568,0.2452,
0.2339,0.2228,0.2119,0.2013,0.1909,0.1808,0.1709,0.1613,0.1519,0.1428,0.1339,0.1253,0.117,0.1089,0.1012,0.0936,
0.0864,0.0794,0.0728,0.0664,0.0603,0.0544,0.0489,0.0436,0.0387,0.034,0.0297,0.0256,0.0218,0.0183,0.0151,0.0123,
0.0097,0.0074,0.0054,0.0038,0.0024,0.0013,0.0006,0.0001,0,0.0001,0.0006,0.0013,0.0024,0.0038,0.0054,0.0074,0.0097,
0.0123,0.0151,0.0183,0.0218,0.0256,0.0297,0.034,0.0387,0.0436,0.0489,0.0544,0.0603,0.0664,0.0728,0.0794,0.0864,0.0936,
0.1012,0.1089,0.117,0.1253,0.1339,0.1428,0.1519,0.1613,0.1709,0.1808,0.1909,0.2013,0.2119,0.2228,0.2339,0.2452,
0.2568,0.2686,0.2806,0.2928,0.3053,0.318,0.3308,0.3439,0.3572,0.3706,0.3843,0.3981,0.4122,0.4264,0.4408,0.4553,
0.47,0.4849,0.4999,0.5151,0.5305,0.546,0.5616,0.5773,0.5932,0.6092,0.6253,0.6416,0.6579,0.6744,0.6909,0.7076,0.7243,
0.7411,0.758,0.775,0.792,0.8091,0.8263,0.8435,0.8608,0.8781,0.8954,0.9128,0.9302,0.9476,0.9651,0.9825,0.9999,};
unsigned int M=100;
void PORT_inti() // 端口初始化
{
DDRA=0xff;
PORTA=0x00;
DDRB=0xff; // B,D,E端口為輸出
PORTB=0x00; // 端口剛開始的值都是0,或者說是都不帶上拉電阻的
DDRD=0xff;
DDRE=0x00;
PORTD=0x00;
PORTE=0x00;
DDRF=0X00; // AD轉(zhuǎn)換端口
PORTF=0X00;
DDRG=0X07;
DDRC=0X00;
}
void TO_inti() //定時(shí)器0的初始化
{
TCCR0=0x02; // 8分頻
TCNT0=0x9C; // 確定中斷的時(shí)間,0.05ms中斷一次
TIMSK|=BIT(0); // T/C0 溢出中斷使能
}
void T1_inti()
{
TCNT1H=0xfc; //計(jì)數(shù)器清零
TCNT1L=0xe0;
TCCR1A=0x2d; // 0010 1101 頻率相位修正PWM,發(fā)生匹配的時(shí)候,電平為0
TCCR1B=0x11; // 0001 0001 不分頻,要是分頻的話開關(guān)頻率就達(dá)不到那么高了
TCCR1C=0x00; //沒什么作用
OCR1A=493; // 在這種模式下 最大值寄存器OCR1A中的值,這個(gè)值與方波的頻率有關(guān)系,還和方波的占空比有關(guān)系
}
void T0() //在定時(shí)器0的中斷程序中向OCR1A和OCR1B送正弦函數(shù)表中的數(shù)值,調(diào)節(jié)M和OCR1A的值就可以調(diào)節(jié)頻率和幅度
{
if(count<360)
{
OCR1B=SPWM_sin_value[count]*M+OCR1A/2-M;//公式應(yīng)該是OCR1A=OCR1A/2+M(SPWM_sin_value[count]-1)其中OCR1A是三角波的最大值,M為振幅調(diào)節(jié)系數(shù)范圍是0~OCR1A/2,(SPWM_sin_value[count]-1)中減的一是應(yīng)為做的正弦表是一個(gè)全正的表,現(xiàn)在要變成(-1~+1)之間的表,調(diào)節(jié)OCR1A的值可以調(diào)節(jié)開關(guān)頻率,調(diào)節(jié)M的值克一調(diào)節(jié)相關(guān)方波的占空比
OCR1C=SPWM_sin_value[count]*M+OCR1A/2-M
count++;
}
else
count=0;
TCNT0=0x9C;//可以進(jìn)一步縮短所用的時(shí)間,這樣可以達(dá)到更好的傳送效果,但是也更加占用單片機(jī)的時(shí)鐘資源。
}
void main()
{
PORT_inti(); //端口初始化
TO_inti(); //產(chǎn)生 kHZ的開關(guān)頻率
T1_inti(); //相位修正PWM用于產(chǎn)生兩路的SPWM波
while(1) //死循環(huán) ,等待中斷
{
}
}
評(píng)論