MSP430 定时器 A 和 B 中断定时 1 秒操作
/********************TimerA0 1 秒定时 TIMER B 1 秒定时溢出中断: MCLK = 2MHz DCOCLKLFXT1 = 32768Hz ACLK***********************/ #include 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 ENABLETBCCR0 = 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 clockDCOCTL = 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 0BCSCTL2 = 0x00; //MCLK = DCOCLK, DIVM=0, SMCLK=CCOCLK ,DIVS =0P1DIR |= 0x02; //p1.2 OUTP5DIR |= 0x10; //P5.4 OUTTimerA_Init(); //定时器初使化 TimerB_Init();_EINT(); // interrupt enablefor(;;) { _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; }}
tips:感谢大家的阅读,本文由我司收集整编。仅供参阅!