#include <MSP430G2553.h>
#define CPU_F ((double)12000000)//cpu frequency12000000
#define delay_us(x) __delay_cycles((long)(CPU_F*(double)x/1000000.0))
#define delay_ms(x) __delay_cycles((long)(CPU_F*(double)x/1000.0))
//功能:TA0.1输出PWM
//说明:TA0.1由P1.2 p1.6输出
void timerA0_1_init()
{
TACTL |= TASSEL_2 + MC_1; //选SMCLK(可以测出来),增计数
//若选ACLK,经示波器PWM时有时无
CCTL1 = OUTMOD_7; //输出模式为复位/置位
CCR0 = 1115000/1000; //SMCLK时钟频率为1.115MHZ,PWM(频率):1.115MHZ/CCR0
CCR1 = (1115000/1000) * 30/100; //占空比:CCR1/CCR0 ;
}
//功能:TA1.1输出PWM
//说明:TA1.1由P2.1 P2.2输出;
//TA1.1与TA1.2共用TA1CCR0,TA1CTL
void timerA1_1_init()
{
TA1CCTL1 = OUTMOD_7;
TA1CCR1 = (1115000/200) * 10/100; ; //占空比:TACCR1/TACCR0
}
//功能:TA1.2输出PWM
//说明:TA1.2由P2.4 P2.5输出
void timerA1_2_init()
{
TA1CTL |= TASSEL_2 + MC_1; //选SMCLK,增计数
//若选ACLK,经示波器PWM时有时无
TA1CCTL2 = OUTMOD_7; //输出模式为复位/置位
TA1CCR0 = 1115000/200; ///SMCLK时钟频率为1.115MHZ,PWM(频率):1.115MHZ/CCR0
TA1CCR2 = (1115000/200) * 60/100; //占空比: CCR2/CCR0
}
void port_init()
{
P1SEL |= BIT2 + BIT6;
P1DIR |= BIT2 + BIT6;
//P1.2/6 设为输出TA0.1
P2SEL |= BIT1 + BIT2;
P2DIR |= BIT1 + BIT2;
//P2.1/2设为输出TA1.1
P2SEL |= BIT4 + BIT5;
P2DIR |= BIT4 + BIT5;
//P2.4/5 设为输出TA1.2
}
void main()
{
WDTCTL = WDTPW + WDTHOLD;
port_init();
timerA0_1_init();
timerA1_2_init();
timerA1_1_init();
_BIS_SR(CPUOFF); // Enter LPM0 进入低功耗模式0 SMCLK ON,ACLK ON
//while(1);
}
举例2:
#include <MSP430G2553.h>
void set_timerA(void) {
P2DIR |= 0x12; // P2.1 and P2.4 output
P2SEL |= 0x12; // P2.1 and P2.4 TA1 options
TA1CCR0 = 1000000/1000-1; // PWM Period
TA1CCTL1 = OUTMOD_7; // CCR1 reset/set
TA1CCTL2 = OUTMOD_7;
TA1CCR1 =(1115000/1000)*80/100;
TA1CCR2 = (1115000/1000)*20/100;
TA1CTL = TASSEL_2 + MC_1; // SMCLK, up mode
// CCTL0 = CCIE; // CCR0 interrupt enabled
}
//参数说明:duty是占空比,50是代表百分之50,范围1-100
void pwm_sinit(unsigned char duty)
{
TACTL = TASSEL_2 + MC_1;
CCR0 = 1115000/500; //通过示波器观察此时SMLK=1115000
CCTL1 = OUTMOD_7;
CCR1 = (1115000*duty)/(100*500);
P1DIR |= 0x44; //P1.2/6 output
P1SEL |= 0x44; //通过特殊功能口输出
}
int main( void )
{
// Stop watchdog timer to prevent time out reset
WDTCTL = WDTPW + WDTHOLD;
Set_TimerA();
pwm_sinit(50);
while(1);
}
MSP430G2553之timerA产生PWM,布布扣,bubuko.com
原文地址:http://www.cnblogs.com/minglicnblogs/p/3863842.html