Search code examples
ctimermicrocontrollermicrochippic18

How to make timer0 use a prescaler of 1:64 in 16 bit mode?


I really do not have good understanding about timers, but I am trying to change timer0 to make it use a prescaler of 1:64 in 16 bit mode? I got the Timer code from Microchip and it is using 8 Bit mode with no prescaler. Download Files

This is some portion of the code that I suspect has to deal with Prescalers.

TMR_CON = 0b00000000 | CLOCK_DIVIDER_SETTING;
TMR_IP = 1;
TMR_IF = 0;
TMR_IE = 1;
TMR_ON = 1;

This is some info from the PIC18F87J11 DATASHEET about Prescalers

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 am assuming that to use 1:64 prescalar, the code must be changed to the following, is that correct?

TMR_CON = 0b00000101 | CLOCK_DIVIDER_SETTING;
TMR_IP = 1;
TMR_IF = 0;
TMR_IE = 1;
TMR_ON = 1;

Now, please tell me how do I change it from 8 bit mode to 16 bit mode ? Like I said I am beginner, so please explain things to my level of understanding.

I appreciate it in advance!


Solution

  • From Microchip datasheet:

    T0CON: TIMER0 CONTROL REGISTER   
    
    bit 7 TMR0ON: Timer0 On/Off Control bit
      1 = Enables Timer0
      0 = Stops Timer0
    bit 6 T08BIT: Timer0 8-Bit/16-Bit Control bit
      1 = Timer0 is configured as an 8-bit timer/counter
      0 = Timer0 is configured as a 16-bit timer/counter
    bit 5 T0CS: Timer0 Clock Source Select bit
      1 = Transition on T0CKI pin input edge
      0 = Internal clock (FOSC/4)
    bit 4 T0SE: Timer0 Source Edge Select bit
      1 = Increments on high-to-low transition on T0CKI pin
      0 = Increments on low-to-high transition on T0CKI pin
    bit 3 PSA: Timer0 Prescaler Assignment bit
      1 = TImer0 prescaler is not assigned; Timer0 clock input bypasses prescaler
      0 = Timer0 prescaler is assigned; Timer0 clock input comes from prescaler output
    bit 2-0 T0PS<2:0>: 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
    
    1. Clear T08BIT bit to select 16bit mode.
    2. Clear T0CS bit if you wont Internal clock (FOSC/4) as Timer0/Prescaler input.
    3. Clear PSA bit to select Prescaler.
    4. Set T0PS<2:0> to select Prescaler rate.
    5. Set TMR0ON to 1 ro enable timer0.

    This is equal:

    T0CON =b'10000nnn' //where nnn is Prescaler value
    

    If you need interrupts on Timer0 overflow than also enable Timer0 interrupt bits (GIE/GIEH, PEIE/GIEL, TMR0IE, TMR0IF).