PDA

View Full Version : Tmr0



laughing-gravy
- 1st September 2005, 07:21
Hello, I'm new to the forum.
I've been playing with TMR0, trying to get it to work in counter mode as a stopwatch.
It seems simple enough, but I'm missing something because it only increments intermittently.
I've tried Pulling the TOCKI pin high, and trying to interrupt by grounding it.
I've tried pulling the TOCKI pin low, and trying to interrupt by switching it high.

But I just cant get it to work.

Does anyone have any suggestions for me?

Thanks.

I'm using the PIC16F877A.

Here is my basic program flow:

OPTION_REG = %00111000 'Transition on TOCKI (RA4),
'Increment on falling edge
'Prescalar assigned to WDT for a 1:1 TMR0 ratio.

INTCON = %10100000 ' Enable Global Interrupt and TMR0 Interrupt.

ON INTERRUPT GOTO LapCount
Main:
:
:
:
goto Main

disable
LapCount:
If INTCON.2 = 1 then
i = i + 1

INTCON = %10100000 'Clear TMR0 Interrupt Flag (Bit 2)

Resume
Enable

Bruce
- 1st September 2005, 09:02
Assuming you start with Timer0 clear, in counter mode, with a 1:1 prescaler, you will need 256 clock transitions on T0CKI before it will generate the over-flow interrupt.

Also, you don't want to set INTCON = %10100000 when using PBP's ON INTERRUPT. Just clear the Timer0 int flag before exiting the interrupt routine with INTCON.2 = 0. PBP will handle GIE for you.

laughing-gravy
- 1st September 2005, 14:46
Thanks for the help Bruce.

OK, now I've eliminated the INTCON set-up, and added a TMR0 Control:

But I still cant get it to work.


Program code:

TRISA = %00010000 ' Set PORTA.4 (TOCKI) to input for timer0

OPTION_REG = %00111000
'Transition on TOCKI (RA4),
'Increment on falling edge
'Prescalar assigned to WDT for a 1:1 TMR0 ratio.

TMR0 = $FF ' Set TMR0 to 255. So one more tick and TMROIF will be set.

ON INTERRUPT GOTO LapCount
Main:
:
:
:
goto Main

disable
LapCount:
If INTCON.2 = 1 then
i = i + 1
endif
TMR0 = $FF
INTCON.2 = 0 'Clear TMR0 Interrupt Flag (Bit 2)

Resume
Enable

Bruce
- 1st September 2005, 17:00
You need to enable the interrupt.


TRISA = %00010000 ' Set PORTA.4 (TOCKI) to input for timer0

OPTION_REG = %00111000
'Transition on TOCKI (RA4),
'Increment on falling edge
'Prescalar assigned to WDT for a 1:1 TMR0 ratio.
INTCON.2 = 0 ' Clear Timer0 int flag
INTCON.5 = 1 ' Enable Timer0 int
TMR0 = $FF ' Set TMR0 to 255. So one more tick and TMROIF will be set.

ON INTERRUPT GOTO LapCount
Main:
' Do something here
goto Main

disable
LapCount:
If INTCON.2 = 1 then
i = i + 1
endif
toggle PORTB.3
TMR0 = $FF ' Indicate we're in interrupt handler
INTCON.2 = 0 ' Clear TMR0 Interrupt Flag (Bit 2)

Resume
Enable
Place a 10K pull-up resistor on RA4. Have your input switch ground RA4 when pressed to toggle RB3 (or any available port pin).

laughing-gravy
- 2nd September 2005, 05:37
Thanks for the help Bruce.

It's working fine now.

savnik
- 19th February 2007, 20:18
You need to enable the interrupt.


TRISA = %00010000 ' Set PORTA.4 (TOCKI) to input for timer0

OPTION_REG = %00111000
'Transition on TOCKI (RA4),
'Increment on falling edge
'Prescalar assigned to WDT for a 1:1 TMR0 ratio.
INTCON.2 = 0 ' Clear Timer0 int flag
INTCON.5 = 1 ' Enable Timer0 int
TMR0 = $FF ' Set TMR0 to 255. So one more tick and TMROIF will be set.

ON INTERRUPT GOTO LapCount
Main:
' Do something here
goto Main

disable
LapCount:
If INTCON.2 = 1 then
i = i + 1
endif
toggle PORTB.3
TMR0 = $FF ' Indicate we're in interrupt handler
INTCON.2 = 0 ' Clear TMR0 Interrupt Flag (Bit 2)

Resume
Enable
Place a 10K pull-up resistor on RA4. Have your input switch ground RA4 when pressed to toggle RB3 (or any available port pin).
I test and work fine , but i see a problem(;)
I connect a button to RA4.When push the button one time the i increment +1 and after release the button the i increment one +1 again.Why;

Bruce
- 19th February 2007, 20:51
My guess would be switch bounce. Wait on entry into your interrupt handler for RA4 to return high, or insert a short delay in the beginning of your interrupt handler, and see if that clears up your problem.