|
MSP430占空比可变的PWM波程序
//*******************************************************************************
// Description: 设定一个变量来选择初始占空比,然后通过Timer_B定时1s,每隔一秒
// 改变一次占空比,生成占空比可变的PWM波。
// ACLK= TACLK = 32768Hz, MCLK= SMCLK= default DCO ~ 800kHz
// Daquan. Liu
// China University of Geosciences(Wuhan)
// 2010.12.27
// Built with IAR Embedded Workbench Version: V3.42A
//******************************************************************************
#include
unsigned char v=5;
void Init_TIMER_B();
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
P1DIR |= 0x0C; // P1.2 and P1.3 output
P1SEL |= 0x0C; // P1.2 and P1.3 TA1/2 options
Init_TIMER_B();
TACTL = TASSEL_2 + TACLR; // SMCLK, up mode
CCR0 = 512-1; // PWM Period
CCTL1 = OUTMOD_7 ; // CCR1 reset/set
CCTL2 = OUTMOD_7 ; //CCR2 reset/set
if(v==5)
{
CCR1 = 128; // CCR1 PWM duty cycle
CCR2 = 384; // CCR2 PWM duty cycle
}
else if (v==12)
{
CCR1 = 384; // CCR1 PWM duty cycle
CCR2 = 128; // CCR2 PWM duty cycle
}
else
{
CCR1 = 128; // CCR1 PWM duty cycle
CCR2 = 384; // CCR2 PWM duty cycle
}
TACTL |= MC_1; // Start Timer_A in up mode
TBCTL |= MC_1; // Start Timer_B in up mode
_EINT(); //开全局中断
_BIS_SR(LPM0_bits); // Enter LPM0
}
void Init_TIMER_B()
{
TBCTL |= TBSSEL0 +TBCLR; //ACLK
TBCCTL0 = CCIE;
TBCCR0 =32768; //定时1s
}
// Timer B0 interrupt service routine
#pragma vector=TIMERB0_VECTOR
__interrupt void Timer_B (void)
{
if(CCR1 >511)
CCR1 = 64;
else CCR1 += 32; // 调整占空比
if(CCR2 < 10)
CCR2 = 448;
else CCR2 -= 32;
}
//此程序不能用定时器A中断定时
|
|