新聞中心

EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > MSP430 定時(shí)器A和B 中斷定時(shí) 1秒操作

MSP430 定時(shí)器A和B 中斷定時(shí) 1秒操作

作者: 時(shí)間:2016-11-13 來(lái)源:網(wǎng)絡(luò) 收藏
/********************

TimerA0 1秒定時(shí) TIMER B 1 秒定時(shí)溢出中斷
:MCLK = 2MHz DCOCLK
LFXT1 = 32768Hz ACLK
***********************/

本文引用地址:http://m.butianyuan.cn/article/201611/316347.htm

#include <msp430x14x.h>
unsigned int i;
unsigned int times,times_b;
unsigned int sec;

/***** 定時(shí)器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ù)
}
/**** 定時(shí)器B 初使化 *****/
void TimerB_Init (void)
{
TBCTL = TBSSEL_1 + ID0 + TBCLR ; //ID_2 + ID1 +
TBCCTL0 = CCIE; // INTERRUPT ENABLE
TBCCR0 = 819; //
TBCTL |= MC0;
}
/***** 秒測(cè)試 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(); //定時(shí)器初使化
TimerB_Init();

_EINT(); // interrupt enable
for(;;)
{
_BIS_SR(LPM3_bits);
_NOP();
}

}

/****** 定時(shí)器 A 中斷 處理 ***********/
#pragma vector = TIMERA0_VECTOR
__interrupt void Timer_A (void)
{

times--;
if(times == 0)
{
times = 20;
sec=1;
Sec_Beep();
}
}
/****** 定時(shí)器B 中斷處理 ***********/
#pragma vector = TIMERB0_VECTOR
__interrupt void Timer_B0 (void)
{
times_b--;
if(times_b == 0)
{
times_b = 20;
P1OUT ^= 0x02;
}
}



關(guān)鍵詞: MSP430定時(shí)器中斷定

評(píng)論


技術(shù)專(zhuān)區(qū)

關(guān)閉