Today 08:13
Forum: Proton Plus Compiler v3
Starter: RGV250
Views: 0
Replies: 10
+ Reply to Thread
Page 1 of 3 123 LastLast
Results 1 to 10 of 21

Thread: Using TMR0 with PIC 18F26K2297 days old

  1. #1
    Member Mentcleer's Avatar
    Join Date
    Aug 2014
    Posts
    116
    Thumbs Up
    Received: 2
    Given: 0
    Total Downloaded
    2.14 GB

    0 Not allowed!

    Default Using TMR0 with PIC 18F26K22

    Hello, all

    Long time I do not use the timers except once in a super easy project and use the code below, every second the Red Led would turn on and off while it lasted a few ms the Green Led.


    Now I want to do a project that every minute makes battery and temperature measurement.
    But with the PIC 18F26K22 there are instructions that do not exist as:
    (OPTION_REG, TMR0)


    I have been looking but I can not clarify with TMR0L or replace OPTION_REG, So that every minute makes a measurement.
    Somebody help me.



    Code:
    Device = 12F675
    Xtal = 4
    
    
    
    
    All_Digital True
    
    
     On_Interrupt GoTo LED_ON_GREEN
     
     OPTION_REG = %10000111
     INTCON = %11100000
     CMCON = 7
     
     TRISIO = $0
     GPIO = %00010000
     
     Dim COUNT As Byte
     Symbol LED_RED   = GPIO.1  ' Out LEd RED
     Symbol LED_GREEN = GPIO.0  ' Out LEd GREEN
     
     TMR0 = 0
     
     Low LED_RED
     Low LED_GREEN
     
     GoTo INI
     
    LED_ON_GREEN:
    Inc COUNT
     If COUNT =40 Then
       Toggle LED_GREEN
       COUNT=0
     End If
    TMR0=0
    INTCONT = %11100000
    Context Restore
    
    
    INI:
     High LED_RED
     DelayMS 1000
     Low LED_RED
     DelayMS 1000
    GoTo INI
    THX.

  2. #2
    Senior Member barak's Avatar
    Join Date
    Apr 2008
    Posts
    743
    Thumbs Up
    Received: 48
    Given: 11
    Total Downloaded
    783.11 MB

    0 Not allowed!

    Default Re: Using TMR0 with PIC 18F26K22

    What is the osc speed in your application?

  3. #3
    Senior Member charliecoultas's Avatar
    Join Date
    Sep 2010
    Posts
    969
    Thumbs Up
    Received: 29
    Given: 27
    Total Downloaded
    1.13 GB

    0 Not allowed!

    Default Re: Using TMR0 with PIC 18F26K22

    After your label LED_ON_GREEN: you need a CONTEXT SAVE, to match your CONTEXT RESTORE later on.

    Charlie

  4. #4
    Senior Member charliecoultas's Avatar
    Join Date
    Sep 2010
    Posts
    969
    Thumbs Up
    Received: 29
    Given: 27
    Total Downloaded
    1.13 GB

    0 Not allowed!

    Default Re: Using TMR0 with PIC 18F26K22

    You can use Timer0, here are some of the settings:

    Symbol INT0IF INTCON.1 'int0 int flag
    Symbol TMR0IF INTCON.2 'TIMER 0 int flag
    Symbol TMR0IE INTCON.5 'TIMER 0 int enable

  5. #5
    Senior Member barak's Avatar
    Join Date
    Apr 2008
    Posts
    743
    Thumbs Up
    Received: 48
    Given: 11
    Total Downloaded
    783.11 MB

    0 Not allowed!

    Default Re: Using TMR0 with PIC 18F26K22

    Quote Originally Posted by charliecoultas View Post
    After your label LED_ON_GREEN: you need a CONTEXT SAVE, to match your CONTEXT RESTORE later on.

    Charlie
    His code is for 16f and he wants to know how to run tmr0 on 18f which does not use the option reg

  6. #6
    Junior Member flosigud's Avatar
    Join Date
    Oct 2013
    Posts
    29
    Thumbs Up
    Received: 2
    Given: 1
    Total Downloaded
    704.57 MB

    0 Not allowed!

    Default Re: Using TMR0 with PIC 18F26K22

    My setup for timer 0 is like this:

    Code:
     Setup Timer 0
    '    ----------------------------------------------------------------------------
            
        Symbol TIMER0 = TMR0L.Word            ' Create a 16-bit variable out of TMR0L
        Symbol TMR0IE = INTCON.4            ' TMR0 Overflow Interrupt Enable
        Symbol TMR0IF = INTCON.2             ' TMR0 Overflow Interrupt Flag
        Symbol TMR0IP = INTCON2.2             ' TMR0 Interrupt Priority bit
        
        Symbol T0PS0 = T0CON.0              ' Timer0 Prescaler Select bits
        Symbol T0PS1 = T0CON.1              ' Timer0 Prescaler Select bits
        Symbol T0PS2 = T0CON.2              ' Timer0 Prescaler Select bits
        Symbol PSA = T0CON.3                ' Timer0 Prescaler Assignment bit
        Symbol T0SE = T0CON.4               ' Timer0 Source Edge Select bit
        Symbol T0CS = T0CON.5               ' Timer0 Clock Source Select bit
        Symbol T08BIT = T0CON.6             ' Timer0 8-bit/16-bit Control bit
        Symbol TMR0ON = T0CON.7             ' Timer0 On/Off Control bit
    
    
        T08BIT = 0                            ' 16 bit
        T0CS = 0                            ' Use internal clock
        PSA = 1                                ' Prescaler off
        
    '    PSA = 0                                ' Prescaler on
    '    T0PS0 = 0                             ' Presaler 
    '    T0PS1 = 0 
    '    T0PS2 = 0
    
    
        TMR0IE = 1
        TMR0ON =1
    Flosi Gu­mundsson
    ReykjavÝk

  7. #7
    Member Mentcleer's Avatar
    Join Date
    Aug 2014
    Posts
    116
    Thumbs Up
    Received: 2
    Given: 0
    Total Downloaded
    2.14 GB

    0 Not allowed!

    Default Re: Using TMR0 with PIC 18F26K22

    Hi all,


    Thanks for the response so fast my osc speed is 64 the internal 16 x4.


    Device = 18F26K22
    Declare Xtal 64

    FOSC = INTIO67 ' HS oscillator (high power > 16 MHz)
    PLLCFG = On ' Oscillator multiplied by 4
    PRICLKEN = On


    -----------------------------------------------------------------------

    Well I think the modifications would be these using the example of the LEDs, correct me if it is wrong.

    Code:
    Code:
    Device = 18F26K22
    
    
    Config_Start
     FOSC = INTIO67   ' Internal Oscillator
     PLLCFG = On         ' Oscillator multiplied by 4
     PRICLKEN = On     ' Primary clock enabled
     FCMEN = OFF           ' Fail-Safe Clock Monitor disabled
     IESO = OFF            ' Internal/External Oscillator Switchover mode disabled
     PWRTEN = OFF 'On           ' Power up timer enabled
     BOREN = SBORDIS 'OFF       ' Brown-out Reset enabled in hardware only (SBOREN is disabled)
     BORV = 190            ' Brown Out Reset Voltage set to 1.90 V nominal
     WDTEN = Off           ' Watch dog timer is always disabled. SWDTEN has no effect.
     WDTPS = 128           ' Watchdog Timer Postscale 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 multiplexed with RC6 
     HFOFST = On           ' HFINTOSC output and ready status are not delayed by the oscillator stable status
     T3CMX = PORTC0        ' Timer3 Clock Input (T3CKI) is on RC0
     P2BMX = PORTB5        ' ECCP2 B (P2B) is on RB5                   (EXTMCLR = Enable MCLR/Disable RE Input)
     MCLRE = INTMCLR       ' MCLR pin enabled, RE3 input pin disabled  (INTMCLR = Disable MCLR/Enabled RE Input)
     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
    
    
    ANSELC = 00000000     ' Set port C all Digital
    TRISC   = 00000001     ' Set port C I/O
    
    
    
    
     On_Interrupt GoTo LED_ON_GREEN
     
     T0CON    = 11000111      ' = OPTION_REG
     INTCON  = 11100000
     
     
     Dim COUNT As Byte
     
    
    
     Symbol TMR0ON = T0CON.7     ' Timer0 On/Off Control bit 
    
    
     Symbol LED_RED     = PORTC.5  ' Out LEd RED
     Symbol LED_GREEN = PORTC.6  ' Out LEd GREEN
    
    
    
    
    
    
     TMR0ON = 0              ' Timer0 On
     
     Low LED_RED
     Low LED_GREEN
     
     GoTo INI
     
    LED_ON_GREEN:
    Context Save
    Inc COUNT
     If COUNT =40 Then
       Toggle LED_GREEN
       COUNT=0
     End If
    TMR0ON =0                ' Timer0 On
    INTCONT = 11100000
    Context Restore
    
    
    
    
    INI:
     High LED_RED
     DelayMS 1000
     Low LED_RED
     DelayMS 1000
    GoTo INI
    THX
    Last edited by Mentcleer; 19th April 2017 at 07:25. Reason: Missing aclaration

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

    0 Not allowed!

    Default Re: Using TMR0 with PIC 18F26K22

    Hello,

    I've tested the code and it does not work ... any suggestions?


    In the code are put the% of behind the binaries.

    THX.

  9. #9
    Senior Member barak's Avatar
    Join Date
    Apr 2008
    Posts
    743
    Thumbs Up
    Received: 48
    Given: 11
    Total Downloaded
    783.11 MB

    0 Not allowed!

    Default Re: Using TMR0 with PIC 18F26K22

    Quote Originally Posted by Mentcleer View Post
    Hi all,
    Thanks for the response so fast my osc speed is 64 the internal 16 x4.
    In this clock speed there is no way to get a timer to do 1 minute! The maximum possible timer period is 1.04858 Second at prescaler = 1:256.
    If you want 60 seconds you need to count 60, 1 second interrupts. I will post some code for you to see.

  10. #10
    Senior Member barak's Avatar
    Join Date
    Apr 2008
    Posts
    743
    Thumbs Up
    Received: 48
    Given: 11
    Total Downloaded
    783.11 MB

    0 Not allowed!

    Default Re: Using TMR0 with PIC 18F26K22

    Code:
    '----------------------------------------------
    ' example for 18F4xK22 Timer 0
    ' interrupt every 1 sec at FOS 64MHz
    ' every 60 seconds, turn on a LED for 1 second
    '----------------------------------------------
    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           ' one minute flag
    '
    main:
        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 > 59 Then    ' one minute
                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

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 since 25th July 2017, 01:34 : 0

Actions :  (Set Date)  (Clear 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