/********************
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; //增计数
}
/**** 守时器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;
}
}