PIC 16-bit timer problem

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.

Thanks

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

Reply to
PigPOg
Loading thread data ...

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 vector.

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.

Reply to
Howard Henry Schlunder

Sorry, this was a typing error in the post.

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 investigating.

Reply to
PigPOg

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

Reply to
PigPOg

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.