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 <msp430x14x.h>
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; // 調(diào)整占空比
if(CCR2 < 10)
CCR2 = 448;
else CCR2 -= 32;
}
//此程序不能用定時器A中斷定時
評論