How do you make timer 0 start and stop

Discussion in 'Embedded Systems and Microcontrollers' started by Neyolight, Mar 13, 2012.

  1. Neyolight

    Thread Starter Member

    Dec 13, 2011
    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.
  2. be80be

    AAC Fanatic!

    Jul 5, 2008
    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

    I would figure what I need and do something like this
    Code ( (Unknown Language)):
    2. T0CON = 0b00000111;
    3.     'TMR0ON 0 = Stops Timer0
    4.     'T08BIT 0 = Timer0 is timer/counter 16-bit
    5.     'T0CS 0 = Internal instruction cycle clock
    6.     'T0SE 0 = Increment on low-to-high
    7.     'PSA 0 = Timer0 prescaler is assigned.
    8.     'T0PS2:T0PS0 111 = 1:256 Prescale value
    10. TMR0ON = 1; ' to start it counting
    Last edited: Mar 13, 2012
  3. Neyolight

    Thread Starter Member

    Dec 13, 2011
    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.