Today 16:20
Forum: USB Module
Starter: MTH
Views: 178
Replies: 15
Today 09:46
Forum: Proton Plus Compiler v3
Starter: johngb
Views: 34
Replies: 0
+ Reply to Thread
Page 2 of 3 FirstFirst 123 LastLast
Results 11 to 20 of 21

Thread: Using TMR0 with PIC 18F26K226 days old

  1. #11
    Senior Member charliecoultas's Avatar
    Join Date
    Sep 2010
    Posts
    941
    Thumbs Up
    Received: 28
    Given: 26
    Total Downloaded
    1,003.23 MB

    0 Not allowed!

    Default Re: Using TMR0 with PIC 18F26K22

    I think you need: "Symbol GIE INTCON.7" inserting?

  2. #12
    Senior Member barak's Avatar
    Join Date
    Apr 2008
    Posts
    739
    Thumbs Up
    Received: 47
    Given: 11
    Total Downloaded
    548.98 MB

    0 Not allowed!

    Default Re: Using TMR0 with PIC 18F26K22

    Quote Originally Posted by charliecoultas View Post
    I think you need: "Symbol GIE INTCON.7" inserting?
    Sure he will need that and possibly interrupt priority. I'm assuming he will define all the standard items and configure the device. Its just a timer 0 example taken out of working code but is was not tested

  3. #13
    Member Mentcleer's Avatar
    Join Date
    Aug 2014
    Posts
    113
    Thumbs Up
    Received: 2
    Given: 0
    Total Downloaded
    1.41 GB

    0 Not allowed!

    Default Re: Using TMR0 with PIC 18F26K22

    Hi,


    I'm sorry barak I've been testing the code at the moment it does not work, it does not turn on or off the LED.
    adding in the code "Symbol GIE INTCON.7".

    THX

  4. #14
    Senior Member charliecoultas's Avatar
    Join Date
    Sep 2010
    Posts
    941
    Thumbs Up
    Received: 28
    Given: 26
    Total Downloaded
    1,003.23 MB

    0 Not allowed!

    Default Re: Using TMR0 with PIC 18F26K22

    Mentcleer

    If I try to compile your code I get errors. For example, COUNT is a reserved word and my compiler won't accept it.
    How are you compiling your code? What version of the compiler?

    Charlie

  5. #15
    Senior Member barak's Avatar
    Join Date
    Apr 2008
    Posts
    739
    Thumbs Up
    Received: 47
    Given: 11
    Total Downloaded
    548.98 MB

    0 Not allowed!

    Default Re: Using TMR0 with PIC 18F26K22

    Quote Originally Posted by Mentcleer View Post
    Hi,


    I'm sorry barak I've been testing the code at the moment it does not work, it does not turn on or off the LED.
    adding in the code "Symbol GIE INTCON.7".

    THX
    I'll try it and let you know

  6. #16
    Senior Member AlbertoFS's Avatar
    Join Date
    Apr 2005
    Posts
    500
    Thumbs Up
    Received: 61
    Given: 2
    Total Downloaded
    1.22 GB

    0 Not allowed!

    Default Re: Using TMR0 with PIC 18F26K22

    In my Bootloader project in the Wiki, for the test code I used Timer0 to toggle a LED. I was used it in the Low Interrupt Handler.
    CodeTest_uart1_26K22_V30.BAS

    For High Interrupt it could be the Init:
    Code:
    ' Configuration of the Timer0 to toggle a LED every 1.048Sec 
        ' Timer0 is configured As a 16-Bit timer/Counter, 1:256 prescale value: 1.048Sec 
        ' For xtal = 64
        INTCONBits_GIE = 0          ' Disable all interrupts.
        INTCON2.7 = 1               ' All PORTB pull-ups are disabled.
        RCONBits_IPEN = 0           ' Disable priority levels on interrupts.
    
        T0CON = 7                   ' Timer0 is set for 1 second (more or less) with FOSC = 64Mhz.
        INTCON2Bits_TMR0IP = 1      ' Timer0 for High Interrupt
        INTCONBits_TMR0IE = 1       ' Enable the Timer0 interrupts.
        INTCONBits_TMR0IF = 0       ' Clear the Timer0 interrupt flag to begin.
        INTCON.7 = 1                ' Enables all high priority interrupts
        INTCON.6 = 1                ' Enables all peripherals
        TMR0H = 0                   ' Clear the Timer0 counter.
        Nop
        TMR0L = 0
        Nop    
        T0CONBits_TMR0ON = 1        ' Start the Timer0.
        PostScaler = 0
        ' Put output digital for LED9
    For the Interrupt handler:
    Code:
        On_High_Interrupt GoTo ISR_High
     
    ISR_High:
        ' Timer is used to Toggle the LED9 every 40 second. (more or less)
        Context Save
        If INTCONBits_TMR0IF = 1 Then   ' Check the Timer0 Interrupt flag
            INTCONBits_TMR0IF = 0
            Inc PostScaler
            If PostScaler = 40 Then
                PostScaler = 0
                Toggle LED9             ' LED9 = 40 seconds ON, 40 seconds OFF.
            EndIf
        EndI
    I didn't test the code again.
    Do not forget put the LED output to digital, etc...
    Alberto
    Last edited by AlbertoFS; 20th April 2017 at 15:36.
    [U]73's de Alberto ea3agv[/U]

  7. #17
    Senior Member barak's Avatar
    Join Date
    Apr 2008
    Posts
    739
    Thumbs Up
    Received: 47
    Given: 11
    Total Downloaded
    548.98 MB

    0 Not allowed!

    Default Re: Using TMR0 with PIC 18F26K22

    Quote Originally Posted by Mentcleer View Post
    Hi,
    I'm sorry barak I've been testing the code at the moment it does not work, it does not turn on or off the LED.
    adding in the code "Symbol GIE INTCON.7".
    THX
    OK here is a working hardware tested code. All you have to change is device (I only had 25K22) and the number of seconds you want for the LED to turn on (I have it on 10 seconds as I'm not patient enough to wait one minute). Compile it as is so you see that it works and then carefully add or change. The is LED on RC.5

    Code:
    Device = 18F25K22
    Xtal = 64        
    OSCCON  = 0x70                   ' IRCF = 111 = internal osc 16MHz; SCS = 00 for Primary OSC
    Symbol PLLEN = OSCTUNE.6        ' Enable/disable PLL
    '
    PLLEN = 1
    ANSELC = 0     
    TRISC  = 0
    '    
    '----------------------------------------------
    ' example for 18F4xK22 Timer 0
    ' interrupt every 1 sec at FOS 64MHz
    ' every 60 seconds, turn on a LED for 1 second
    '----------------------------------------------
    Symbol GIE    = INTCON.7        ' Global Interrupt Enable
    Symbol TMR0IE = INTCON.5        ' TMR0 Overflow Interrupt enable bit 
    Symbol TMR0IF = INTCON.2        ' TMR0 Overflow Interrupt Flag bit
    T0CON = 7                       ' Timer0 set: off,16bit,Internal Clk,Prescalar 1:256
    Symbol TMR0ON = T0CON.7         ' Timer0 On/Off Control bit    
    Symbol T0Prest = 3036           ' preset value to load in timer for 1 sec overflow @ FOSC 64MHz
    Symbol LED = PORTC.5
    Dim Timer0REG As TMR0L.Word     ' Alias Timer0REG as a word = TMR0L & TMR0H
    Dim SecCntr As Byte             ' counts seconds
    Dim oneminflag As Bit
    Symbol SECONDS = 9              ' test 10 seconds - change to 59 for 1 minute  
    '
    main:
        High LED                    ' test LED
        DelayMS 200
        Low LED
        oneminflag = 0
        SecCntr = 0
        Timer0REG = T0Prest         ' load timer for 1 Sec int
        GIE = 0                     ' disable global interrupts
        TMR0IE = 1                  ' enable tmr0 int
        TMR0IF = 0                  ' clear int flag    
        GIE = 1                     ' enable global interrupts
        TMR0ON = 1                  ' tmr0 on
        While 1 = 1
            LED = oneminflag        ' the LED will be on for 1 second every minute
            DelayMS 10
        Wend
    '
    On_Hardware_Interrupt GoTo Isr
    '          
    Isr:
        Context Save        
        If  TMR0IF = 1 Then         ' the interupt came from timer0 overflow
            Inc SecCntr
            If SecCntr > SECONDS Then    ' do something after so many seconds
                oneminflag = 1
                SecCntr = 0
            EndIf
            If SecCntr = 1 Then
                oneminflag = 0      ' clear flag after 1 sec
            EndIf                       
            Timer0REG = T0Prest     ' load Timer0 register with the preset value for a 1 sec overflow
            TMR0IF = 0              ' clear TMR0 Interrupt flag
        EndIf  
        Context Restore
    '
    Config_Start
      FOSC = INTIO67    ;Internal oscillator block, CLKOUT function on OSC2
      PLLCFG = On    ;Oscillator used directly
      PRICLKEN = On    ;Primary clock enabled
      FCMEN = OFF    ;Fail-Safe Clock Monitor disabled
      IESO = OFF    ;Oscillator Switchover mode disabled
      PWRTEN = OFF    ;Power up timer disabled
      BOREN = SBORDIS    ;Brown-out Reset enabled in hardware only (SBOREN is disabled)
      BORV = 190    ;VBOR set to 1.90 V nominal
      WDTEN = OFF    ;Watch dog timer is always disabled. SWDTEN has no effect.
      WDTPS = 128    ;1:128
      CCP2MX = PORTC1    ;CCP2 input/output is multiplexed with RC1
      PBADEN = OFF    ;PORTB<5:0> pins are configured as digital I/O on Reset
      CCP3MX = PORTC6    ;P3A/CCP3 input/output is mulitplexed with RC6
      HFOFST = On    ;HFINTOSC output and ready status are not delayed by the oscillator stable status
      T3CMX = PORTC0    ;T3CKI is on RC0
      P2BMX = PORTB5    ;P2B is on RB5
      MCLRE = INTMCLR    ;RE3 input pin enabled; MCLR disabled
      STVREN = OFF    ;Stack full/underflow will not cause Reset
      LVP = OFF    ;Single-Supply ICSP disabled
      XINST = OFF    ;Instruction set extension and Indexed Addressing mode disabled (Legacy mode)
      Debug = OFF    ;Disabled
      Cp0 = OFF    ;Block 0 (000800-001FFFh) not code-protected
      CP1 = OFF    ;Block 1 (002000-003FFFh) not code-protected
      CP2 = OFF    ;Block 2 (004000-005FFFh) not code-protected
      CP3 = OFF    ;Block 3 (006000-007FFFh) not code-protected
      CPB = OFF    ;Boot block (000000-0007FFh) not code-protected
      CPD = OFF    ;Data EEPROM not code-protected
      WRT0 = OFF    ;Block 0 (000800-001FFFh) not write-protected
      WRT1 = OFF    ;Block 1 (002000-003FFFh) not write-protected
      WRT2 = OFF    ;Block 2 (004000-005FFFh) not write-protected
      WRT3 = OFF    ;Block 3 (006000-007FFFh) not write-protected
      WRTC = OFF    ;Configuration registers (300000-3000FFh) not write-protected
      WRTB = OFF    ;Boot Block (000000-0007FFh) not write-protected
      WRTD = OFF    ;Data EEPROM not write-protected
      EBTR0 = OFF    ;Block 0 (000800-001FFFh) not protected from table reads executed in other blocks
      EBTR1 = OFF    ;Block 1 (002000-003FFFh) not protected from table reads executed in other blocks
      EBTR2 = OFF    ;Block 2 (004000-005FFFh) not protected from table reads executed in other blocks
      EBTR3 = OFF    ;Block 3 (006000-007FFFh) not protected from table reads executed in other blocks
      EBTRB = OFF    ;Boot Block (000000-0007FFh) not protected from table reads executed in other blocks
    Config_End

  8. #18
    Member Mentcleer's Avatar
    Join Date
    Aug 2014
    Posts
    113
    Thumbs Up
    Received: 2
    Given: 0
    Total Downloaded
    1.41 GB

    0 Not allowed!

    Default Re: Using TMR0 with PIC 18F26K22

    Hi all,

    barak the code works perfectly.
    I would like to know how to calculate the value "3036" of the variable T0Prest.

    I do not know why it gives me "T0Prest" different value.


    Prescaler =256
    TMR0 Value =0
    FOSC = 64M
    Time = 60seg => 60000ms ' Every minut Led On

    4/64000000 = 0,0000000625

    TMR0 Value = 256 - (60000ms/(256*0,0000000625)) =
    256 - 60000ms/0,000016) =
    256 - 3750 =3494


    60000ms = (4/64000000) * (256 - 0)*256
    TMR0 Value= 256 - (60000ms/256*0,0000000625)
    TMR0 Value= 256 - (60000ms/0,000016)
    TMR0 Value= 256 - 3750
    TMR0 Value= 3494

    T0Prest = 3494

    THX.
    Last edited by Mentcleer; 21st April 2017 at 11:44. Reason: Missing aclaration

  9. #19
    Member Mentcleer's Avatar
    Join Date
    Aug 2014
    Posts
    113
    Thumbs Up
    Received: 2
    Given: 0
    Total Downloaded
    1.41 GB

    0 Not allowed!

    Default Re: Using TMR0 with PIC 18F26K22

    Hi all,

    AlbertoFS, I have tried your code modifying some thing but it still does not work.

    I have put the "Context Restore" command at the end of the "Interrupt handler" and declare "PostScaler" As Byte.

    What I have added in "ADD" in explanations.

    Code:
    Device = 18F26K22
    Declare Xtal 64 
    
    ANSELC = 0     
    TRISC  = 0
    
    
    Symbol LED = PORTC.6
    
    
    Dim PostScaler As Byte          'ADD
    
    
        INTCONBits_GIE = 0          ' Disable all interrupts.
        INTCON2.7 = 1               ' All PORTB pull-ups are disabled.
        RCONBits_IPEN = 0           ' Disable priority levels on interrupts.
    
    
        T0CON = 7                   ' Timer0 is set for 1 second (more or less) with FOSC = 64Mhz.
        INTCON2Bits_TMR0IP = 1      ' Timer0 for High Interrupt
        INTCONBits_TMR0IE = 1       ' Enable the Timer0 interrupts.
        INTCONBits_TMR0IF = 0       ' Clear the Timer0 interrupt flag to begin.
        INTCON.7 = 1                ' Enables all high priority interrupts
        INTCON.6 = 1                ' Enables all peripherals
        TMR0H = 0                   ' Clear the Timer0 counter.
        Nop
        TMR0L = 0
        Nop    
        INTCONBits_GIE = 1          'ADD
        T0CONBits_TMR0ON = 1    ' Start the Timer0.
        PostScaler = 0
    
    
        
     On_Interrupt GoTo ISR_High 
     
     Ini:
        
         High LED
     
     GoTo Ini
     
     
    ISR_High:
        ' Timer is used to Toggle the LED9 every 40 second. (more or less)
        Context Save
        If INTCONBits_TMR0IF = 1 Then   ' Check the Timer0 Interrupt flag
            INTCONBits_TMR0IF = 0
            Inc PostScaler
            If PostScaler = 10 Then
                PostScaler = 0
                Toggle LED             ' LED9 = 40 seconds ON, 40 seconds OFF.
            EndIf
        EndIf
        Context Restore              ' ADD

    THX.

  10. #20
    Senior Member barak's Avatar
    Join Date
    Apr 2008
    Posts
    739
    Thumbs Up
    Received: 47
    Given: 11
    Total Downloaded
    548.98 MB

    0 Not allowed!

    Default Re: Using TMR0 with PIC 18F26K22

    Quote Originally Posted by Mentcleer View Post
    Hi all,

    barak the code works perfectly.
    I would like to know how to calculate the value "3036" of the variable T0Prest...
    tic = 1/64 * 4 * 256 = 16uS
    period 1 Sec
    Required timer value 2^16 - (period / tic) = 65536 - 62500 = 3036

    The max. period possible at this clock speed with the prescaler at 256 is 1.048576 Sec = (2^16 * tic)

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. The I2C bus doesn't work in PIC 18F26K22.
    By Mentcleer in forum Proton Plus Compiler v3
    Replies: 1
    Last Post: 22nd August 2016, 14:51
  2. PIC 18F26K22 Problems RE3
    By Mentcleer in forum Proton Plus Compiler v3
    Replies: 11
    Last Post: 16th June 2016, 11:07
  3. 18F26K22 vs 18F46K22
    By towlerg in forum Proton Plus Compiler v3
    Replies: 3
    Last Post: 19th June 2015, 19:21
  4. Analog help on 18F26K22
    By joesaliba in forum Proton Plus Compiler v3
    Replies: 1
    Last Post: 21st February 2015, 19:57
  5. 18f26k22
    By Gerard in forum Oscillator Modules
    Replies: 2
    Last Post: 25th November 2013, 11:06

Members who have read this thread : 35

Actions :  (Set Date)

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

Tags for this Thread

Posting Permissions

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