cancel
Showing results for 
Search instead for 
Did you mean: 

How can I use the timer counter without any interrupt

bearjoe
Associate II
Posted on February 28, 2005 at 06:33

How can I use the timer counter without any interrupt

2 REPLIES 2
bearjoe
Associate II
Posted on February 25, 2005 at 04:05

Hi, Please help me, in our application, interrupt is forbidden to use for timer. And I must change the PA7 output every 500ms.

And the below is my program, my questions is that why when we test the software, it appears that timer A spend only 250us completing roll from 0xFFFD to 0x3CAC.In theory, it should be completed in 50ms.

void main(void)

{

ResetTimer();

TimerCounter=0;

//--Read from SPI forever ---------------------

do

{

GetTimer();

}while (1);

return; // never go here, for lint

}

void ResetTimer(void)

{

U8 temp=0;

/*

** Disable all interrupt of timer A

** Disable output compare and input capture mode

** Disable PWM and one pulse mode.

*/

TACR1 = 0x00;

/* Set Timer frequency to fosc/8*/

TACR2 = 0x08;

/*Set the timer enable*/

TACSR = TACSR & 0xFB ;

/*See if TOF is set, this means that timer over flow then clear the TOF*/

if (TACSR && 0x20)

{

temp=TACSR;

temp=TACLR;

}

/*Reset the timer counter to FFFC*/

TACHR = 0xFF;

TACLR = 0xFC;

return;

}

void GetTimer(void)

{

U16 CurrentTimer;

U8 temp;

/*Get the timer A value*/

CurrentTimer= (TACHR * 0x100) + TACLR;

/*Check if timer A has run 50 ms*/

if (CurrentTimer<=0x3CAC)

{

TimerCounter = TimerCounter + 1;

/*If program has run over 500 ms, then switch the PA7 status*/

if (TimerCounter >= 10)

{

/*switch the PA7*/

temp = PADR;

temp = (((temp & 0x80) ^ 0x80) & 0x80) | (temp & 0x7F) ;

PADR = temp;

TimerCounter=0;

}

ResetTimer();

}

return;

} :-o

wolfgang2399
Associate II
Posted on February 28, 2005 at 06:33

Hi bearjoe,

which C-compiler do you use????

Your code may result an error:

/*Get the timer A value*/

CurrentTimer= (TACHR * 0x100) + TACLR;

Please check the compiled assembler code of your command.

Better you use the strict ANSI (valid with Cosmic compiler???)

CurrentTimer = ((unsigned short)TACHR * 0x100) + TACLR;

If you are using the HiWare (Metrowerks) Compiler you must (!) split your command in two commands to ensure that you read the MSByte first:

CurrentTimer = (unsigned short)TACHR * 0x100;

CurrentTimer += TACLR;

A better optimized assembler-code (with HiWare) you'll get with the following sequence

*(char*)&CurrentTimer = TACHR;

*((char*)&CurrentTimer +1) = TACLR;

Perhaps that's it (?)

WoRo