微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > AVR单片机的SPWM程序

AVR单片机的SPWM程序

时间:11-24 来源:互联网 点击:
#include//程序中现在只有SPWM产生的相关的程序,其他的程序都已经删除

#include

#pragma interrupt_handler T0:17 //定时器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,};
//数组里面的值分别对应于从45.0~55.0HZ的OCR1A的值,(将每个周期分成360份,即每一度为一份)步进为0.1HZ 在写程序的时候,
//只要将OCR1A的值替换成以上相应的值就可以得到,对应于正弦波的相应的频率
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转换端口
PORTF=0X00;
DDRG=0X07;
DDRC=0X00;
}

void TO_inti() //定时器0的初始化
{
TCCR0=0x02; // 8分频
TCNT0=0x9C; // 确定中断的时间,0.05ms中断一次
TIMSK|=BIT(0); // T/C0 溢出中断使能
}

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top