www.pudn.com > msp430pwmgenerator.rar > pwm_timer_b.c
/******************************************************************************* 文件名:pwm_timer_b.c 编写者:czhang 描述:使用定时器B进行PWM输出。输出的频率和占空比由串行口发送来的指令确定。 版本: 1.0 2005-2-20 *******************************************************************************/ #include#include "pwm_timer_b.h" #define PWM_DIR P4DIR #define PWM_SEL P4SEL #define PWM_OUT P4OUT #define PWM_IN P4IN #define PWM_IO BIT1 unsigned int iFre; //频率 unsigned int iScale; //占空比 /***************************************************************************** 初始化定时器B *****************************************************************************/ void TimerBInit() { TBCTL = TBSSEL_1 + TBCLR ; //时钟源为ACLK TBCCTL1 = OUTMOD_7; //工作在复位/置位 模式 PWM_SEL |= PWM_IO; //选择输出端口的第二功能 PWM_DIR |= PWM_IO; } /***************************************************************************** 设置占空比 sc:要修改的占空比值 *****************************************************************************/ void SetScale(unsigned int sc) { iScale=sc; } /***************************************************************************** 设置频率 fre:要修改的输出频率值 *****************************************************************************/ void SetFre(unsigned int fre) { iFre=fre; } /***************************************************************************** 控制PWM运行或者停止 doit:0:停止 100:运行 其它:什么都不做,只返回运行状态 返回值:运行状态。 同doit的值 *****************************************************************************/ unsigned char GoPwm(unsigned char doit) { TBCTL &= ~(MC0+MC1); //关闭定时器 if(doit==100) { TBCCR1=iScale; TBCCR0=iFre; TBCTL |= MC_2; //打开定时器,计数模式2 } if((TBCTL&MC_0)==0) //判断PWM是否运行 return 0; else return 100; }