各位大佬我又来了,这次是遇到的问题是这样,使用比较器输出固定的频率和占空比驱动舵机,按照查到的资料设置频率和占空比后舵机只有在2.5%,10%的时候才会动,而且动的角度也有问题,按照舵机的资料对应的占空比应该是0.5MS->25%, 1MS->5%, 1.5ms->7.5%,2MS->10%,2.5MS->12.5%,由于才开始摸这个芯片不是很了解,请各位大佬赐教。频率设置的是20毫秒,单片机程序如下:
#include "STC8H.h"
void pwma_init(unsigned long f_pwm_set);
void main()
{
pwma_init(480000); // 24M,设置50HZ频率(24000000)*(1/50) = 480000
while(1)
{
}
}
void CH1_PWM_SET(unsigned long f_set_ch1,float duty_ch1) //设置占空比
{
float a;
a = duty_ch1 * f_set_ch1;
P_SW2 |= 0x80;
PWMA_CCR1H = ((unsigned int)(a))/256;
PWMA_CCR1L = ((unsigned int)(a))%256;
PWMA_CCMR1 = 0x60;
PWMA_CCER1 &= 0xFD;
PWMA_CCER1 |= 0x01;
PWMA_OISR |=0x01;
PWMA_ENO |= 0x01;
P_SW2 &= 0x7f;
}
void pwma_init(unsigned long f_pwm_set) //频率设置
{
P1M0 = 0xfe;
P1M1 = 0xfe;
P_SW2 |= 0x80;
PWMA_CR1 &=0xfe;
PWMA_ARRH = f_pwm_set/256;
PWMA_ARRL = f_pwm_set%256;
PWMA_CR1 &= 0x8f;
P_SW2 &= 0x7f;
CH1_PWM_SET(f_pwm_set,2.5); 占空比2.5%
P_SW2 |= 0x80;
PWMA_BKR = 0x80;
PWMA_CR1 |= 0x01;
P_SW2 &= 0x7F;
}
|