maychang 发表于 2013-8-12 20:03
那么,软件有问题的可能性很大。
#include <avr/io.h>
#include<avr/interrupt.h>
#include<util/delay.h>
#define BIT(x) (1<<x)
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 sin_table[11] = {50,100,150,200,250,300,250,200,150,100,50};
unsigned int Count=0;
unsigned int M=100;
//输出PWM
int main()
{
DDRB = BIT(5)|BIT(6)|BIT(7);//port_init()
PORTB= BIT(5)|BIT(6)|BIT(7);
TO_inti();
T1_inti();
sei();
while(1);
}//是OCR1为周期,输出的两路PWM波形
/**/
void TO_inti() //定时器0的初始化
{
TCCR0=0x02; // 8分频
TCNT0=0xCE; // 确定中断的时间,0.05ms中断一次
TIMSK|=BIT(0); // T/C0 溢出中断使能
}
void T1_inti()
{
OCR1A = 493; // 在这种模式下 最大值寄存器OCR1A中的值,这个值与方波的频率有关系,还和方波的占空比有关系
TCNT1H=0xfc; //计数器清零
TCNT1L=0xe0;
TCCR1A=0x2d; // 0010 1101 频率相位修正PWM,发生匹配的时候,电平为0
TCCR1B=0x11; // 0001 0001 不分频,要是分频的话开关频率就达不到那么高了
TCCR1C=0x00; //没什么作用
/*
OCR1B = 200;
OCR1C = 200;
TCNT1H = 0 ; //计数器清零
TCNT1L = 0 ;
TCCR1A |= (1<<WGM10)|(1<<WGM11)|(1<<COM1C1)|(1<<COM1B1)|(1<<COM1B0);//等于top时置1,匹配时置0 ;;
TCCR1B |= (1<<WGM13)|(1<<WGM12)|(1<<CS10);//无分频 波形产生模式:1111
//TIMSK = 1<<TOIE1;*/
}
ISR(TIMER0_OVF_vect)
{
/*
PORTA &= ~(1<<PA0);
_delay_ms(100);
PORTA |= (1<<PA0);
*/
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;
// OCR1B=sin_table[Count];
// OCR1C=sin_table[Count];
Count++;
}
else
Count=0;
TCNT0=0x9C;//可以进一步缩短所用的时间,这样可以达到更好的传送效果,但是也更加占用单片机的时钟资源。
}
|