采用DCO 1MHZ的时钟,
问题:采用counter定时的时间明显比采用compare定时的时间要快,附上代码如下:
#include "msp430g2231.h"
#define _1ms 1000
#define _1kHz 1000
#define Timer 0
unsigned int flag=0;
unsigned int flag1=0;
void Timer_Init()
{
P1SEL &= ~BIT0 + ~BIT6;
P1DIR |= BIT0 + BIT6;
P1OUT |= BIT0 + BIT6;
/***********************************************/
//TODO:
//1-->> Capture/compare interrupt enable
//2-->> set the time(1ms) jump into the interrupt
//3-->> use SMCLK, upmode
/***********************************************/
#if Timer
TACCTL0 |= CCIE;//Capture/compare interrupt enable
TACCR0 = _1kHz;
TACTL |= TASSEL_2 | MC_1;//SEL SYSTEM CLK SMCLK,upmode
#else
TACCR0 = _1kHz;
TACTL |= MC_1 | TAIE | TASSEL_2;
#endif
_EINT();
}
int main( void )
{
// Stop watchdog timer to prevent time out reset
WDTCTL = WDTPW + WDTHOLD;
//鍐呴儴鏃堕挓鏍″噯
if (CALBC1_1MHZ ==0xFF || CALDCO_1MHZ == 0xFF) //濡傛灉鏍″噯浜嗘椂閽燂紝鎴栬€?segm A 娌℃湁琚摝闄わ紝閭d箞杩欎袱涓€间笉鍙兘鏄痜f
{
while(1); // If calibration constants erased // do not load, trap CPU!!
}
BCSCTL1 = CALBC1_1MHZ; // Set range /this test value is 0x86
DCOCTL = CALDCO_1MHZ; // Set DCO step + modulation /This test value is 0xca
Timer_Init();
while(1);
}
#pragma vector=TIMERA0_VECTOR
__interrupt void Blinky(void)
{
flag++;
/***********************************************/
//TODO: Set time is 0.5 second
/***********************************************/
if(flag%500==0)
{
P1OUT ^=BIT6;
}
if(flag%1000==0) //set time is 1 second
{
P1OUT ^=BIT0;
flag=0;
}
}
#pragma vector=TIMERA1_VECTOR
__interrupt void Blinky1(void)
{
flag1++;
/***********************************************/
//TODO: Set time is 0.5 second
/***********************************************/
if(flag1%500==0)
{
P1OUT ^=BIT0;
}
if(flag1%1000==0) //set time is 1 second
{
P1OUT ^=BIT6;
flag1=0;
}
}