How do you make timer 0 start and stop

Thread Starter

Neyolight

Joined Dec 13, 2011
54
Hi all

I am using PIC18F4620 and C18 complier. I want my timer 0 to increment at every instruction clock cycle BUT only when I want it to and stop when I want it to.

Currently, I have T0CON = 0x88

I have defined an interrupt , at the end of the interrupt I read Timer0( number of rising edge during interrupt). The reading Im getting has ~200 MORE points. I feed 0s into TMR0H and TMR0L at the beginning and end of the interrupt (after reading timer0) . Where am I getting these additional 200 clicks from?

Slightly lost right now...


I want to use Timer 0 as a COUNTER to increment on INTERNAL INSTRUCTION CLOCK! How do I do that?

Currently I am using Timer0 as a timer to increment on internal instruction clock. This is because there is no option to make it counter for internal clock.


Heres from PIC18F4620 data sheet :

T0CS: Timer0 Clock Source Select bit
1 = Transition on T0CKI pin
0 = Internal instruction cycle clock (CLKO)


Timer0 can operate as either a timer or a counter; the
mode is selected with the T0CS bit (T0CON<5>). In
Timer mode (T0CS = 0), the module increments on
every clock by default unless a different prescaler value
is selected (see Section11.3 “Prescaler”). If the
TMR0 register is written to, the increment is inhibited
for the following two instruction cycles. The user can
work around this by writing an adjusted value to the
TMR0 register.
The Counter mode is selected by setting the T0CS bit
(= 1). In this mode, Timer0 increments either on every
rising or falling edge of pin RA4/T0CKI. The incrementing
edge is determined by the Timer0 Source Edge
Select bit, T0SE (T0CON<4>); clearing this bit selects
the rising edge. Restrictions on the external clock input
are discussed below.
 

be80be

Joined Jul 5, 2008
2,395
TMR0ON = 1 ;starts the timer
TMR0ON = 0 ;turns it off or stop it

And if you read page 123 you'll get the rest of the story http://ww1.microchip.com/downloads/en/devicedoc/39626b.pdf

T08BIT = 1 ; if you want a 8 bit counter
; 0 for 16 bits

T0CS = 0 ; Internal instruction cycle clock what you want

T0SE = 1 or 0 ; one high to low and 0 low to high
transition

PSA = 0 ; Timer0 Prescaler Assignment bit you'll more then likely want this set to 0

You'll want to set this as well
T0PS2:T0PS0: Timer0 Prescaler Select bits
111 = 1:256 Prescale value
110 = 1:128 Prescale value
101 = 1:64 Prescale value
100 = 1:32 Prescale value
011 = 1:16 Prescale value
010 = 1:8 Prescale value
001 = 1:4 Prescale value
000 = 1:2 Prescale value
I would figure what I need and do something like this
Rich (BB code):
T0CON = 0b00000111;
	'TMR0ON 0 = Stops Timer0
	'T08BIT 0 = Timer0 is timer/counter 16-bit 
	'T0CS 0 = Internal instruction cycle clock 
	'T0SE 0 = Increment on low-to-high 
	'PSA 0 = Timer0 prescaler is assigned. 
	'T0PS2:T0PS0 111 = 1:256 Prescale value

TMR0ON = 1; ' to start it counting
 
Last edited:

Thread Starter

Neyolight

Joined Dec 13, 2011
54
Thanks for that, those are the bit of TOCON register which I have set it as 10001000 (0x88)

Still I get reading which are 200 points/300 sometimes more that what is expected.
 
Top