MSP430 定時器A和B 中斷定時 1秒操作
TimerA0 1秒定時 TIMER B 1 秒定時溢出中斷
:MCLK = 2MHz DCOCLK
LFXT1 = 32768Hz ACLK
***********************/
#include <msp430x14x.h>
unsigned int i;
unsigned int times,times_b;
unsigned int sec;
/***** 定時器A 初使化 *******/
void TimerA_Init(void)
{
TACTL = TASSEL_1 + ID0 + TACLR ; //ACLK=32768Hz ,清除tar ,2分頻
CCTL0 = CCIE; //
CCR0 = 819; // (1/32768)*819 =20 ms ??
TACTL |= MC0; //增計(jì)數(shù)
}
/**** 定時器B 初使化 *****/
void TimerB_Init (void)
{
TBCTL = TBSSEL_1 + ID0 + TBCLR ; //ID_2 + ID1 +
TBCCTL0 = CCIE; // INTERRUPT ENABLE
TBCCR0 = 819; //
TBCTL |= MC0;
}
/***** 秒測試 1秒響一下 ********/
void Sec_Beep(void)//beep every second
{
if(sec==1)
{
sec = 0;
P5OUT ^= 0x10;
}
}
void main( void )
{
times = 20;
times_b = 20;
WDTCTL = WDTPW + WDTHOLD;
// SYstem clock
DCOCTL = 0x60;
BCSCTL1 = 0x86;//XT20FF ,XTS=0 LOW FREQ , RESEL=6 DCOCLK=2MHz
// XT2on, XTS DIVA.1 DIVA.0 XT5V RSEL.2 RSEL.1 RSEL.0
// 1 0 0 0 0 1 1 0
BCSCTL2 = 0x00; //MCLK = DCOCLK, DIVM=0, SMCLK=CCOCLK ,DIVS =0
P1DIR |= 0x02; //p1.2 OUT
P5DIR |= 0x10; //P5.4 OUT
TimerA_Init(); //定時器初使化
TimerB_Init();
_EINT(); // interrupt enable
for(;;)
{
_BIS_SR(LPM3_bits);
_NOP();
}
}
/****** 定時器 A 中斷 處理 ***********/
#pragma vector = TIMERA0_VECTOR
__interrupt void Timer_A (void)
{
times--;
if(times == 0)
{
times = 20;
sec=1;
Sec_Beep();
}
}
/****** 定時器B 中斷處理 ***********/
#pragma vector = TIMERB0_VECTOR
__interrupt void Timer_B0 (void)
{
times_b--;
if(times_b == 0)
{
times_b = 20;
P1OUT ^= 0x02;
}
}
評論