+ Reply to Thread
Results 1 to 11 of 11
  1. #1
    Join Date
    Feb 2012
    Location
    PERTH AUSTRALIA
    Posts
    765

    Default 18f27k40 - timer0 changes from K22, K80 series 8bit/16bit mode

    Hi guys ,

    found few changes on how timer0 on this chip implements the 8bit mode and the values set in TMR0L and TMR0H from the K80, K22 series

    apart from the changes in TMR0 registers and their use to setup on the 27k40 from the k22,k80 series , it appears that the 8bit mode implements different from K80,K22 series

    this appears to effect the values that need to be placed into TMR0L, TMR0H in 8 bit mode
    can you please check this

    currently in 8 bit mode on the k40 the values set in TMR0L, TMR0H result in a 500us interupt

    Where the same values 8bit mode on a k22 on TMR0 = 10ms interupt


    I simply got around it by setting it to 16 bit mode , but like to know what values i should set in 8bit mode k40 to get a 10ms interupt

    cheers

    Sheldon

    K22 SERIES
    TMR0-8BIT MODE K22.JPG

    K40 SERIES
    TMR0-8BIT MODE K40.JPG



    Code setup for k22 , with a 10ms interupt
    Code:
     ' ---------- Timer 0 Register Setups ---------------- 
        INTCON2.2 = 1          ' TMR0IP  - TIMER 0 Overflow Interupt Priority - 1 = High Priorty , 0 = Low Priority 
        T0CON.7 = 1             ' Bit7 - TMR0 1= Enable 0 = Stop timer
        T0CON.6 = 0             ' Bit6 - TMR0 8/16bit control 1= 8bit 0=16bit, 
        T0CON.5 = 0             ' Bit5 - TMR0 Clock Source 1=clk on T0CKI pin input 0=Internal clcck (Fosc/4)
        T0CON.4 = 0             ' Bit4 - TMR0 Source Edge Select 1= H/L of TOCKI 0= L/H of TOCKI , 
        T0CON.3 = 0             ' Bit3 - TMR0 PSA select 1=TMR0 Bypass Prescaler 0= CLK Input from Prescaler output      
        T0CON.2 = 0             ' Bit2-0 - Timer0 prescaler Rate Select bits 2-0(set to 1:256)         
        T0CON.1 = 0             ' 000 = 1:2  , 001= 1:4  , 010 = 1:8   , 011 = 1:16      
        T0CON.0 = 0             ' 100 = 1:32 , 101 = 1:64, 110 = 1:128 , 111 = 1:256
    
        TMR0H = $63               ' preset Timer 0 to a 10ms timer
        TMR0L = $C0

    timer 0 setup for k40 results in 500us timer
    Code:
       
       ' ---------- Timer 0 Register Setups ---------------- 
       ' TOCON0 - POR = 0
       ' T0CON1 - POR = 0 
       ' Used for a 10ms timer 
       ' Set for 1:2 prescaler, 1:1 postscaler , use system clock (fosc/4) , preloaded in ISR with 25536  for 10ms 
       ' Note: Interupt Priority level set disable by default IPEN(INTCON.5= 0 )
       '       Timer0 Intertupt Priority = High by default - IPR0.5 = 1
       '       TMR0 Interupt enable bit  - PIE0.5 = 0 disabled  PIE0.5 = 1 Enabled - set/cleared  using DT-INTS-18  
               
        T0CON0.7 = 1             ' Bit7 - TMR0 1= Enable 0 = Stop timer
        T0CON0.6 = 0             ' Bit6 - TMR0 N/A  
        T0CON0.5 = 0             ' Bit5 - TMR0 Output bit ( read  only)                                                                                         
        T0CON0.4 = 0             ' Bit4 - TMR0 8/16bit control 0= 8bit 1=16bit                                                             
        T0CON0.3 = 0             ' Bit3-0 - Timer0 output Postscaler divider Selector bits 3-0      
        T0CON0.2 = 0             ' 0=1:1, 1=1:2, 2=1:3 , 3=1:4, 4=1:5, 5=1:6, 6=1:7          
        T0CON0.1 = 0             ' 7=1:8, 8=1:9, 9=1:10, 10=1:11, 11=1:12, 12=1:13      
        T0CON0.0 = 0             ' 13=1:14, 14=1:15, 15=1:16                        
     
        T0CON1.7 = 0             ' Bit7-5 TMR0 Clock Source Select bits - use Fosc/4  
        T0CON1.6 = 1             ' 000= Ext pin T0CKIPPS(Non Inv),001 = Ext pin T0CKIPPS(Inverted)
        T0CON1.5 = 0             ' 010= Fosc/4 , 011= HFINTOSC, 100= LFINTOSC, 101=SOSC,110-111 = Reserved                                                                                                                 
        T0CON1.4 = 0             ' Bit4 - TMR0 ASYNCH Enable Bit - 1= T0 Input NOT sync to system clock , 0 = T0 Input sync to Fosc/4
        T0CON1.3 = 0             ' Bit3-0 - Timer0 output Prescaler divider Selector bits 3-0      
        T0CON1.2 = 0             ' 0=1:1, 1=1:2, 2=1:4 , 3=1:8, 4=1:16, 5=1:32, 6=1:64          
        T0CON1.1 = 0             ' 7=1:128, 8=1:256, 9=1:512, 10=1:1024, 11=1:2048, 12=1:4096      
        T0CON1.0 = 1             ' 13=1:8192, 14=1:16384, 15=1:32768                        
        
         TMR0H = $63               ' preset Timer 0 to a 10ms timer
        TMR0L = $C0

  2. #2
    Join Date
    May 2013
    Location
    australia
    Posts
    1,479

    Default Re: 18f27k40 - timer0 changes from K22, K80 series 8bit/16bit mode

    posting values to the TMR0H reg when the timer is in 8 bit mode , how is that going to work ?
    normally TMR0H would be cleared

    Code:
    TMR0H = $63               ' preset Timer 0 to a 10ms timer
       TMR0L = $C0
    I cannot simulate any of your numbers in the mcc 4/8/16,64 MHz fosc produce different 8 bit reload values that match none of your figures
    what is your clock source ?
    This is more entertaining than Free to Air TV

  3. #3

    Default Re: 18f27k40 - timer0 changes from K22, K80 series 8bit/16bit mode

    In the K40, TMR0 functions different than it did in previous devices.
    In 8-bit mode TMR0H becomes a programmable period register (similar to how TMR2 works).

    It looks like you're running at 16MHz.
    If you have the timer set for Fosc/4 mode T0CS<2:0> = 010, then at 16MHz TMR0 counts 250ns

    You want to count a total of 10ms/250ns = 40000

    The postscaler settings are 1:1 to 1:16, and the 8-bit count = 1 to 256 (since it reloads on the next count)
    So the largest count x postscaler value = 16x256 = 4096
    You have to use a prescaler that gets you < 4096

    Pick prescaler 1:32 = 40000/32 = 1250, which is < 4096
    Then set count=125 with a 1:10 postscaler -> 1250

    So, to get a 10ms TMR0 8-bit setting try this:
    - set Fosc/4 mode T0CS<2:0> = 010
    - set 8-bit mode T016BIT = 0
    - set prescaler to 1:32 T0CKPS<3:0> = 0101
    - set postscaler to 1:10 T0OUTPS<3:0> = 1001
    - set period count = 125 (load TMR0H with 125-1=124)

  4. #4
    Join Date
    May 2013
    Location
    australia
    Posts
    1,479

    Default Re: 18f27k40 - timer0 changes from K22, K80 series 8bit/16bit mode

    @ fosc=16mhz settings from mcc for a 10mS timer
    pic18f25k22
    Code:
     // Set TMR0 to the options selected in the User Interface
        // TMR0H 0; 
        TMR0H = 0x00;
        // TMR0L 100; 
        TMR0L = 0x64;
     
        // Load TMR0 value to the 8-bit reload variable
        timer0ReloadVal = 100;
        // Clearing IF flag
        INTCONbits.TMR0IF = 0;
        // T0PS 1:256; T08BIT 8-bit; T0SE Increment_hi_lo; T0CS FOSC/4; TMR0ON enabled; PSA assigned; 
        T0CON = 0xD7;
    18f27k40
    Code:
           // Set TMR0 to the options selected in the User Interface
    
        // T0CS FOSC/4; T0CKPS 1:64; T0ASYNC synchronised; 
        T0CON1 = 0x46;
    
        // TMR0H 155; 
        TMR0H = 0x9B;
    
        // TMR0L 0; 
        TMR0L = 0x00;
    
        // Clearing IF flag
        PIR0bits.TMR0IF = 0;
    
        // T0OUTPS 1:1; T0EN enabled; T016BIT 8-bit; 
        T0CON0 = 0x80;
    Last edited by richard; - 22nd July 2018 at 00:32.
    This is more entertaining than Free to Air TV

  5. #5
    Join Date
    Feb 2012
    Location
    PERTH AUSTRALIA
    Posts
    765

    Default Re: 18f27k40 - timer0 changes from K22, K80 series 8bit/16bit mode

    thanks guys

    i am running at 32mhz , so a few changes , its nice have both the prescaller and the postscaller something that timer0 never had , and without it 10ms in 8bit mode functioning in same way as TMR2 would not be possible at 32mhz

    i pointed this out cos i have ported timer0 code from one processor to another , and was not expecting a change in timer0 internal functions when in 8 bit mode

    cheers

    Sheldon

  6. #6
    Join Date
    Feb 2012
    Location
    PERTH AUSTRALIA
    Posts
    765

    Default Re: 18f27k40 - timer0 changes from K22, K80 series 8bit/16bit mode

    oh and when i first setup this original code, back some years back i refereed to this calculator, so dont use this calc for timer 0 for k40


    timer0 calc.JPG

  7. #7

    Default Re: 18f27k40 - timer0 changes from K22, K80 series 8bit/16bit mode

    // T0CS FOSC/4; T0CKPS 1:64; T0ASYNC synchronised;
    // TMR0H 155;
    // TMR0L 0;
    // T0OUTPS 1:1; T0EN enabled; T016BIT 8-bit;
    For the K40 in 8-bit mode I don't see how that gets you 10ms @ 16MHz clock

    Doesn't that get you 156 x 64 x 250ns = 2.496ms ?

  8. #8
    Join Date
    May 2013
    Location
    australia
    Posts
    1,479

    Default Re: 18f27k40 - timer0 changes from K22, K80 series 8bit/16bit mode

    Doesn't that get you 156 x 64 x 250ns = 2.496ms ?
    yes , my bad it needs to be T0CKPS 1:256;

    accidently set fosc to 4mhz when trying to match sheldons settings trying to guess at his fosc and forgot to regenerate the code
    This is more entertaining than Free to Air TV

  9. #9

    Default Re: 18f27k40 - timer0 changes from K22, K80 series 8bit/16bit mode

    it needs to be T0CKPS 1:256
    That'll get you a lot closer... 9.984ms

    If you use the postscaler you can get 10.000ms:

    - set prescaler to 1:32 T0CKPS<3:0> = 0101
    - set postscaler to 1:10 T0OUTPS<3:0> = 1001
    - set period count = 125 (load TMR0H with 125-1=124)
    gets you 32 x 10 x 125 x 250ns = 10.000ms

    For 32MHz, just change the prescaler to 1:64:
    64 x 10 x 125 x 125ns = 10.000ms

  10. #10
    Join Date
    May 2013
    Location
    australia
    Posts
    1,479

    Default Re: 18f27k40 - timer0 changes from K22, K80 series 8bit/16bit mode

    i'm impressed on just how versatile these later generation chips are. there is heaps to learn
    This is more entertaining than Free to Air TV

  11. #11
    Join Date
    Feb 2012
    Location
    PERTH AUSTRALIA
    Posts
    765

    Default Re: 18f27k40 - timer0 changes from K22, K80 series 8bit/16bit mode

    also be aware now of the errata on timer0 k40 series , T0ASYNC bit problem , when FOSC/4 clock source

    k40 errata ver E - timer 0.JPG

Similar Threads

  1. 18f27k40 - adc settings
    By longpole001 in forum PBP3
    Replies: 5
    Last Post: - 12th July 2018, 11:34
  2. PBP for dsPIC 16bit??
    By photoelectric in forum PBP Wish List
    Replies: 33
    Last Post: - 9th July 2014, 08:13
  3. PIC18f2620 Timer0 in 16-bit mode
    By Bruce in forum General
    Replies: 0
    Last Post: - 18th August 2010, 03:08
  4. AD7705 is 15bit or 16bit ADC
    By ravindrajoga in forum Off Topic
    Replies: 1
    Last Post: - 13th July 2007, 14:09
  5. 8bit var holds 16bit data!??
    By mofarngo in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 6th August 2005, 20:33

Members who have read this thread : 17

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts