AVR单片机的SPWM程序

来源:本站
导读:目前正在解读《AVR单片机的SPWM程序》的相关信息,《AVR单片机的SPWM程序》是由用户自行发布的知识型内容!下面请观看由(电工技术网 - www.9ddd.net)用户发布《AVR单片机的SPWM程序》的详细说明。
简介:本文给大家分享了一个AVR单片机的SPWM程序。

#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 溢出中断使能}void T1_inti(){TCNT1H=0xfc;      //计数器清零TCNT1L=0xe0;    TCCR1A=0x2d;      //  0010 1101 频率相位修正PWM,发生匹配的时候,电平为0TCCR1B=0x11;      //  0001 0001 不分频,要是分频的话开关频率就达不到那么高了TCCR1C=0x00;     //没什么作用OCR1A=493;       // 在这种模式下  最大值寄存器OCR1A中的值,这个值与方波的频率有关系,还和方波的占空比有关系} void T0()     //在定时器0的中断程序中向OCR1A和OCR1B送正弦函数表中的数值,调节M和OCR1A的值就可以调节频率和幅度{    if(count<360)   {      OCR1B=SPWM_sin_value[count]*M+OCR1A/2-M;//公式应该是OCR1A=OCR1A/2+M(SPWM_sin_value[count]-1)其中OCR1A是三角波的最大值,M为振幅调节系数范围是0~OCR1A/2,(SPWM_sin_value[count]-1)中减的一是应为做的正弦表是一个全正的表,现在要变成(-1~+1)之间的表,调节OCR1A的值可以调节开关频率,调节M的值克一调节相关方波的占空比   OCR1C=SPWM_sin_value[count]*M+OCR1A/2-M   count++;          } else  count=0;          TCNT0=0x9C;//可以进一步缩短所用的时间,这样可以达到更好的传送效果,但是也更加占用单片机的时钟资源。 } void main(){ PORT_inti();  //端口初始化 TO_inti();    //产生 kHZ的开关频率 T1_inti();    //相位修正PWM用于产生两路的SPWM波    while(1)   //死循环 ,等待中断    {     }}

提醒:《AVR单片机的SPWM程序》最后刷新时间 2024-03-14 00:57:46,本站为公益型个人网站,仅供个人学习和记录信息,不进行任何商业性质的盈利。如果内容、图片资源失效或内容涉及侵权,请反馈至,我们会及时处理。本站只保证内容的可读性,无法保证真实性,《AVR单片机的SPWM程序》该内容的真实性请自行鉴别。