C18, PIC18F252: Setting timer0 with an interrupt ?

Jan 24, 2004 5 Replies

Hi all...



I'm trying to set up timer0 to time about 10ms and inc a global variable that gets displayed on an LCD. Everything is pretty much working except that I can't change the time out value for timer0. I init the timer before a while loop that display the variable "time". The isr inc the value on every time out of timer0. It also toggles RB4 so I can check the freq (for testing). The freq is always about 4 Hz. TIME_TIMEOUT is defined as 5000. I've tried to change this value but nothing happens. What am I missing ?



Thanks



BTW it runs at 2MHz. There is only one isr.


//------------- code begins -------------------------- OpenTimer0(TIMER_INT_ON & T0_16BIT & T0_SOURCE_INT & T0_PS_1_1); WriteTimer0(TIME_TIMEOUT);



// INTCON=0; //make sure interrupts are disable // INTCONbits.GIE=1; //enable global interrupts // INTCONbits.PEIE=1; //enable peripheral interrupts // INTCONbits.TMR0IE=1; //enable TMR0 overflow interrupt enable bit INTCON=0xe0; //all 4 lines above combined



while(1) { itoa(time,timeStr); SetDDRamAddr(LCD_LINE2); // Display value of time while(BusyXLCD()); // Wait if LCD busy putsXLCD(timeStr);



}; //--------------- code ends ----------------------



The isr is here:



//------------- code begins ---------------------- #pragma code #pragma interrupt InterruptHandlerLow void InterruptHandlerLow() { if (INTCONbits.TMR0IF==1) { time++; PORTBbits.RB4=~PORTBbits.RB4; WriteTimer0(TIME_TIMEOUT); INTCONbits.TMR0IF=0; //clear interrupt flag }



} //--------------- code ends ----------------------


that

every

nothing

Hmmm something smells.......

2Mhz(Xtal) / 4(CLK0) / 4(Prescaler) / 16384(16bit count) / 2(Port bit toggle) = "about 4Hz"

I like Gary Kato's question.

Greg the Grog

Here is the source code:

/* $Id: t0write.c,v 1.2 2000/08/04 20:58:59 ConnerJ Exp $ */

#include

#include

/********************************************************************

  • Function Name: WriteTimer0 *

  • Return Value: void *

  • Parameters: int: value to write to Timer0 *

  • Description: This routine writes a 16-bit value to Timer0 *

  • Timer0. *

********************************************************************/

void WriteTimer0(unsigned int timer0)

{

union Timers timer;

timer.lt = timer0; // Copy timer value into union

TMR0H = timer.bt[1]; // Write low byte to Timer0

TMR0L = timer.bt[0]; // Write high byte to Timer0

}

Thanks

Shouldn't you be using | instead of & here?

Nope, that's Microchip's C18 way of expressing constant flags. They are defined accordingly so you can combine them with '&'. A bit confusing, I agree.

I found my error. This micro triggers an interrupt when the count goes from ffff to 0000. The delay of 10ms would need :

(2MHz/4(sclk))^-1=2us now

10ms/2us=5000 so ...

the timer should count 5000 times before it triggers the int. So :

16bit counter -1 = 65535. I need 5000, so the value should have been 60535 (and not 5000). It works :-)! I was used to the zilog z8 where the counters count down.

Thanks

that

every

nothing

Join the Discussion

Have something to add? Share your thoughts — no account required.

Didn't find your answer?

Ask the community — no account required