以下这个是我用来驱动舵机的,如Steering_Eng_Angle_PWM1(-70),就是让舵机转动-70度,可以改它的参数调整输出的频率,并把它改成你要的PWM方式,希望对你有用。
#include "msp430G2553.h"
#include "include/matrix_keyboard.h"
#include "include/common.h"
#include "include/uart.h"
/*----------------------------------------------------------------------------
* @描述:定时器初始化
* @输入:无
* @返回:无
* @历史:
版本 日期 作者 改动内容和原因
------ ----------- --------- ----------------------
1.0 2014.07.08 zxjun 创建模块
----------------------------------------------------------------------------*/
void init_timer(void)
{
TA0CCR0 = 59999;//(time<<1)*10; //60000*0.5=30ms
TA0CTL = TASSEL_2 + MC_1 + ID_3 + TACLR; //clock = smclk/8=2M,即0.5us
TA0CCTL0 |= CCIE; //打开定时器A0中断
}
void init_pwm(void)
{
P2SEL |= BIT1;
P2DIR |= BIT1;
P2SEL |= BIT4;
P2DIR |= BIT4;
//P2SEL |= BIT1;
//P2DIR |= BIT1;
TA1CCR0 = 19999; // PWM Period/2 pwm频率= 200 0000 /20000
//TA1CCR1 = 19999; // PWM Period/2 pwm频率= 200 0000 /20000
TA1CCTL1 = OUTMOD_7; // CCR1 toggle/set
//TA1CCTL2 = OUTMOD_7; // CCR0 toggle/set
//TA1CCR1 = pwm_cycle_array[pwm_cycle-1]; // CCR1 PWM duty cycle
TA1CTL = TASSEL_2 + ID_3 + MC_1; //预分频系统ID_3=SYS_FREQ/8;
}
void Steering_Eng_Angle_PWM1(float angle)
{
unsigned int angle_tmp=(unsigned int)(1520+10*angle);
//TA1CCR0=19999;//set_pwm_freq(100);//t=10ms
TA1CCR1=angle_tmp*2-1;//10000us/(19999+1)=angle_tmp/(TA1CCR1+1)
//USART_OUT("angle_tmp=%d,TA1CCR1=%d\r\n",angle_tmp,angle_tmp*2-1);
} |