PIC 16-bit timer problem

Do you have a question? Post it now! No Registration Necessary

Translate This Thread From English to

Threaded View
Can anyone advise please?

I am running a PIC18F252 from a 4MHZ crystal and I want to use TMR0
(in 16 bit mode) to generate a time delay of 10 seconds.

Firstly, despite clearing TMR0IP, I can't get the program to branch to
the 18h interrupt vector. I have got this part set correctly as I can
service a port level change in another part of the program.

So, I decided to just poll the TMR0IF bit. This works but the
interrupt occurs every 16 seconds (approx). The only way I can change
(reduce) the time delay is by altering the prescaler. Changing the
values in TMR0H and TMR0L have no effect.

I'm convinced I am not initialising correctly, either that, or I have
completely misunderstood how the timers work. All I want to do is to
test a flag/register bit periodically to see if the 10 second period
has expired. My code is below, any advice would be most welcome.


START    BCF    INTCON,TMR01E        ;Interrupt disabled.
    BCF    INTCON,TMR01F        ;Flag cleared.
    BCF    INTCON2,TMR0IP        ;Low priority interrupt.
    MOVLW    H'07'
    MOVWF    T0CON            ;Prescale25%6.
    MOVLW    HIGH_TIME_VALUE        ;Set time to 10 seconds.
    MOVWF    TMR0H
    MOVWF    TMR0L
    BSF    INTCON,TMR0IE        ;Interrupt enabled.
    BSF    T0CON,TMR0ON        ;Start timer.
LOOP    BTFSS    INTCON,TMR01F        ;Loop until timeout.
    GOTO    LOOP

Re: PIC 16-bit timer problem
Quoted text here. Click to load it

What are the constants TMR01E, TMR01F, HIGH_TIME_VALUE, and LOW_TIME_VALUE
defined as?  Aren't TMR01E and TMR01F supposed to be TMR0IE and TMR0IF?  If
you are setting the wrong enable bit, that would be a possible explaination
as to why you can't get the program counter to branch to the interrupt

HIGH_TIME_VALUE and LOW_TIME_VALUE should be defined as H'67' and H'6A'
respectively for approximately a 10 second timeout.  After the timer
interrupt occurs, are you remembering to put 0x676A back into the
TMR0H:TMR0L registers in the proper high then low order?  If you don't write
to TMR0H:TMR0L except during initialization, the first interrupt will occur
after 10 seconds, but all future interrupts will be 16.77 seconds.

Re: PIC 16-bit timer problem
On Fri, 23 Sep 2005 03:10:55 -0700, "Howard Henry Schlunder"

Quoted text here. Click to load it

Sorry, this was a typing error in the post.

Quoted text here. Click to load it

I have defined the HIGH and LOW values correctly. You have partly
answered my original post in that TMR0 should indeed branch to the 18h
vector on interrupt but that's the bit I can't get to work. I'm still

Re: PIC 16-bit timer problem
Quoted text here. Click to load it

How embarrassing... I forgot to enable the GIE bit!

Site Timeline