#include <msp430g2553.h>
#define pwm_sf 200 //在这里可以任意调节频率,S是舵机的意思,
#define pwm_vf 1000 //v是指电机的意思 (可以在这里随意设定舵机跟电机的频率)
void pwm_sinit(unsigned char pwmvalue1) //pwmvlue是占空比,50是代表百分之50,从1到100变化
{
TACTL = TASSEL_2 + MC_1;
CCR0 = 1002000/pwm_sf; //通过示波器观察此时SMLK=1002000
CCTL1 = OUTMOD_7;
CCR1 = (1002000*pwmvalue1)/(100*pwm_sf);
P1DIR |= 0x15;
P1SEL |= 0x15;//通过特殊功能口输出
}
/*
void pwm_vinit(unsigned char pwmvalue2)
{
// TACTL = TASSEL_2 + MC_1;
CCR0 = 100200/pwm_vf;
CCTL2 = OUTMOD_7;
CCR2 = 1002000*pwmvalue2/100;
P2DIR |= 0x10;
P2SEL |= 0x10;
}
*/
void main(void)
{
WDTCTL = WDTPW +WDTHOLD; // Stop Watchdog Timer
DCOCTL=CALDCO_1MHZ;
BCSCTL1=CALBC1_1MHZ;
pwm_sinit(50); //50是指百分之50的占空比,可以任意改变这个参数()
//pwm_vinit(50);
BCSCTL3 = XCAP_2;
while(1)
{;}
} |