利用相位修正pwm模式,因為有反相器的原因用的模式11,求指正。
#include "iom128v.h"
#include "macros.h"
#define uchar unsigned char
#define uint unsigned int
//延時函數
void Delay_ms(uint i)
{uint j;
for(;i>0;i--)
{
for(j=8000;j>0;j--);
}
}
//相位修正PWM初始化函數
void PBPWM_Init()
{
DDRB|=BIT(5)|BIT(6);//PB5~PB6為輸出
PORTB=0x00;
}
void timer1()
{
TCCR1A=0x73;//相位修正PWM,11模式
TCCR1B=0x13;//00010010,64分頻
OCR1A=1280;//TOP值,20ms
TCNT1=0;
OCR1B=96;
}
void main()
{
PBPWM_Init();//調用相位修正PWM初始化函數
timer1();
while(1)
{
OCR1B=64;//調節占空比
Delay_ms(60);
OCR1B=128;
Delay_ms(60);
}
}
實現舵機左右擺動,不過感覺擺動幅值較小,貌似不到120度,是延時的原因還是占空比的設置問題?OCR1B賦值多少?
|