16f1826 timer problem solution

Chris
2012-04-12
2013-05-30
  • Chris
    Chris
    2012-04-12

    not sure where this should go, but i couldnt use the timer on my 16f1826 enhanced pic micro. the culprit was in the timer.h file, the 16f1826 has TMR1CS0 and TMR1CS1 because of the capacitive sensors being able to be used as timer inputs (or something like that) im working on modifying the code to work with this chip and will post the updated code on here for anyone interested. this code im going to post shouldnt change function of normal chips but adds the changes needed for TMR1CS0 and TMR1CS1 (i also remembered i added TMR0CS in place of T0SE for timer0, im going to check over and test any other timers on this chip before posting the code, maybe the developer would like to put this code in the updates?

     
  • Chris
    Chris
    2012-04-12

    This code compiled properly but I havent tested it yet on the actual chip, but here it is anyway

    '    Timer control routines for Great Cow BASIC
    '    Copyright (C) 2006-2009 Hugh Considine
    '    This library is free software; you can redistribute it and/or
    '    modify it under the terms of the GNU Lesser General Public
    '    License as published by the Free Software Foundation; either
    '    version 2.1 of the License, or (at your option) any later version.
    '    This library is distributed in the hope that it will be useful,
    '    but WITHOUT ANY WARRANTY; without even the implied warranty of
    '    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    '    Lesser General Public License for more details.
    '    You should have received a copy of the GNU Lesser General Public
    '    License along with this library; if not, write to the Free Software
    '    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    '********************************************************************************
    'IMPORTANT:
    'THIS FILE IS ESSENTIAL FOR SOME OF THE COMMANDS IN GCBASIC. DO NOT ALTER THIS FILE
    'UNLESS YOU KNOW WHAT YOU ARE DOING. CHANGING THIS FILE COULD RENDER SOME GCBASIC
    'COMMANDS UNUSABLE!
    '********************************************************************************
    'Changes:
    ' 10/7/2009: Added AVR support
    ' 04/11/2012: Added support for 16f1826 by bowlofpudding
    'Subroutines:
    ' InitTimer0 (Source, Prescaler)
    ' InitTimer1 (Source, Prescaler)
    ' InitTimer2 (Prescaler, Postscaler)
    ' InitTimer3 (Source, Prescaler)
    ' ClearTimer(TimerNumber)
    ' StartTimer(TimerNumber)
    ' StopTimer(TimerNumber)
    'Some simpler names for the timers (use to read)
    #ifdef PIC
        '#define Timer0 TMR0
        Dim Timer0 Alias TMR0
        '#define Timer1 TMR1L
        'Is now a function, need to ensure read happens in certain order
        '#define Timer2 TMR2
        Dim Timer2 Alias TMR2
    #endif
    #ifdef AVR
        Dim Timer0 Alias TCNT0
        Dim Timer2 Alias TCNT2
    #endif
    'Sources
    #define Osc 1
    #define Ext 2
    #define ExtOsc 3
    'AVR prescaler settings
    #define PS_1 1
    #define PS_8 2
    #define PS_64 3
    #define PS_256 4
    #define PS_1024 5
    'Also worth noting here,
    '0: stop timer
    '6: external, clock on falling
    '7: external, clock on rising
    #define AVR_EXT_TMR 7
    'Timer 0 prescales
    #define PS0_1/2 0
    #define PS0_1/4 1
    #define PS0_1/8 2
    #define PS0_1/16 3
    #define PS0_1/32 4
    #define PS0_1/64 5
    #define PS0_1/128 6
    #define PS0_1/256 7
    #define PS0_2 0
    #define PS0_4 1
    #define PS0_8 2
    #define PS0_16 3
    #define PS0_32 4
    #define PS0_64 5
    #define PS0_128 6
    #define PS0_256 7
    'Timer 1 prescales
    #define PS1_1/1 0
    #define PS1_1/2 16
    #define PS1_1/4 32
    #define PS1_1/8 48
    #define PS1_1 0
    #define PS1_2 16
    #define PS1_4 32
    #define PS1_8 48
    'Timer 2 prescales
    #define PS2_1/1 0
    #define PS2_1/4 1
    #define PS2_1/16 2
    #define PS2_1 0
    #define PS2_4 1
    #define PS2_16 2
    'Timer 3 prescales
    #define PS3_1/1 0
    #define PS3_1/2 16
    #define PS3_1/4 32
    #define PS3_1/8 48
    #define PS3_1 0
    #define PS3_2 16
    #define PS3_4 32
    #define PS3_8 48
    Function Timer1 As Word
        #ifdef PIC
            [byte]Timer1 = TMR1L
            Timer1_H = TMR1H
        #endif
        #ifdef AVR
            [byte]Timer1 = TCNT1L
            Timer1_H = TCNT1H
        #endif
    End Function
    Function Timer3 As Word
        #ifdef PIC
            [byte]Timer3 = TMR3L
            Timer3_H = TMR3H
        #endif
        #ifdef AVR
            [byte]Timer3 = TCNT3L
            Timer3_H = TCNT3H
        #endif
    End Function
    Function Timer4 As Word
        #ifdef AVR
            [byte]Timer4 = TCNT4L
            Timer4_H = TCNT4H
        #endif
    End Function
    Function Timer5 As Word
        #ifdef AVR
            [byte]Timer5 = TCNT5L
            Timer5_H = TCNT5H
        #endif
    End Function
    'Start/Clear/Stop subs
    Sub StartTimer(In TMRNumber)
        #ifdef PIC
            'Timer 0 always runs
            #ifdef Var(T1CON)
                If TMRNumber = 1 Then
                    Set TMR1ON On
                End If
            #endif
            #ifdef Var(T2CON)
                If TMRNumber = 2 Then
                    T2CON = TMR2Pres
                    Set TMR2ON On
                End If
            #endif
            #ifdef Var(T3CON)
                If TMRNumber = 3 Then
                    Set TMR3ON On
                End If
            #endif
        #endif
    
        #ifdef AVR
            'Need to set clock select bits to 0
            #ifndef Var(TCCR0B)
                #ifdef Var(TCCR0)
                If TMRNumber = 0 Then
                    TCCR0 = TMR0Pres
                End If
                #endif
            #endif
            #ifdef Var(TCCR0B)
                If TMRNumber = 0 Then
                    TCCR0B = TCCR0B And 248 Or TMR0Pres
                End If
            #endif
            #ifdef Var(TCCR1B)
                If TMRNumber = 1 Then
                    TCCR1B = TCCR1B And 248 Or TMR1Pres
                End If
            #endif
            #ifdef Var(TCCR2B)
                If TMRNumber = 2 Then
                    TCCR2B = TCCR2B And 248 Or TMR2Post
                End If
            #endif
            #ifdef Var(TCCR3B)
                If TMRNumber = 3 Then
                    TCCR3B = TCCR3B And 248 Or TMR3Pres
                End If
            #endif
            #ifdef Var(TCCR4B)
                If TMRNumber = 4 Then
                    TCCR4B = TCCR4B And 248 Or TMR4Pres
                End If
            #endif
            #ifdef Var(TCCR5B)
                If TMRNumber = 5 Then
                    TCCR5B = TCCR5B And 248 Or TMR5Pres
                End If
            #endif
        #endif
    End Sub
    Sub ClearTimer (In TMRNumber)
        #ifdef PIC
            If TMRNumber = 0 Then
                TMR0 = 0
                SInitTimer0
            End If
            #ifdef Var(T1CON)
                If TMRNumber = 1 then
                    TMR1H = 0
                    TMR1L = 0
                    SInitTimer1
                End If
            #endif
            #ifdef Var(T2CON)
                If TMRNumber = 2 Then
                    TMR2Pres = T2CON
                    TMR2 = 0
                    T2CON = TMR2Pres
                End If
            #endif
            #ifdef Var(T3CON)
                If TMRNumber = 3 then
                    TMR3H = 0
                    TMR3L = 0
                    SInitTimer3
                End If
            #endif
        #endif
        #ifdef AVR
            #ifdef Var(TCNT0)
                If TMRNumber = 0 Then
                    TCNT0 = 0
                End If
            #endif
            #ifdef Var(TCNT1L)
                If TMRNumber = 1 Then
                    TCNT1H = 0
                    TCNT1L = 0
                End If
            #endif
            #ifdef Var(TCNT2)
                If TMRNumber = 2 Then
                    TCNT2 = 0
                End If
            #endif
            #ifdef Var(TCNT3L)
                If TMRNumber = 3 Then
                    TCNT3H = 0
                    TCNT3L = 0
                End If
            #endif
            #ifdef Var(TCNT4L)
                If TMRNumber = 4 Then
                    TCNT4H = 0
                    TCNT4L = 0
                End If
            #endif
            #ifdef Var(TCNT5L)
                If TMRNumber = 5 Then
                    TCNT5H = 0
                    TCNT5L = 0
                End If
            #endif
        #endif
    End Sub
    Sub StopTimer (In TMRNumber)
        #ifdef PIC
            'Timer 0 always runs
            If TMRNumber = 1 Then
                Set TMR1ON OFF
            End If
            #ifdef Var(T2CON)
                If TMRNumber = 2 Then
                    Set TMR2ON OFF
                End If
            #endif
            #ifdef Var(T3CON)
                If TMRNumber = 3 Then
                    Set TMR3ON OFF
                End If
            #endif
        #endif
        #ifdef AVR
            'Need to set clock select bits to 0
            #ifdef Var(TCCR0B)
                If TMRNumber = 0 Then
                    TCCR0B = TCCR0B And 248
                End If
            #endif
            #ifdef Var(TCCR1B)
                If TMRNumber = 1 Then
                    TCCR1B = TCCR1B And 248
                End If
            #endif
            #ifdef Var(TCCR2B)
                If TMRNumber = 2 Then
                    TCCR2B = TCCR2B And 248
                End If
            #endif
            #ifdef Var(TCCR3B)
                If TMRNumber = 3 Then
                    TCCR3B = TCCR3B And 248
                End If
            #endif
            #ifdef Var(TCCR4B)
                If TMRNumber = 4 Then
                    TCCR4B = TCCR4B And 248
                End If
            #endif
            #ifdef Var(TCCR5B)
                If TMRNumber = 5 Then
                    TCCR5B = TCCR5B And 248
                End If
            #endif
        #endif
    End Sub
    'Initialise subs for individual timers
    Sub InitTimer0(In TMR0Source, In TMR0Pres)
        #ifdef PIC
            SInitTimer0
        #endif
        #ifdef AVR
            'Just need to buffer TMR0Pres
            '(And change it for external clock)
            If TMR0Source = Ext Then
                TMR0Pres = AVR_EXT_TMR
            End If
        #endif
    End Sub
    Sub SInitTimer0
        OPTION_REG = OPTION_REG AND 192
        SET OPTION_REG.PSA OFF
    'to fix for 16f1826
    #ifdef T0SE
        if TMR0Source = Osc THEN SET OPTION_REG.T0SE OFF
        if TMR0Source = Ext THEN SET OPTION_REG.T0SE ON
    #endif
    #ifndef T0SE
        if TMR0Source = Osc THEN SET OPTION_REG.TMR0CS OFF
        if TMR0Source = Ext THEN SET OPTION_REG.TMR0CS ON
    #endif
        clrwdt
        OPTION_REG = OPTION_REG OR TMR0Pres
    End Sub
    Sub InitTimer1(In TMR1Source, In TMR1Pres)
        #ifdef PIC
            SInitTimer1
        #endif
        #ifdef AVR
            If TMR1Source = Ext Then
                TMR1Pres = AVR_EXT_TMR
            End If
        #endif
    End Sub
    Sub SInitTimer1
        T1CON = TMR1Pres
    'added for 16f1826
            If TMR1Source = Ext Then
                #ifdef TMR1CS
                    Set TMR1CS On
                #endif
        'added for 16f1826
                #ifndef TMR1CS
                    Set TMR1CS1 On
                #endif
            End If
            If TMR1Source = ExtOsc Then
                #ifdef TMR1CS
                    Set TMR1CS On
                #endif
        'added for 16f1826
                #ifndef TMR1CS
                    Set TMR1CS1 On
                #endif
                Set T1OSCEN On
            End If
        #endif
    End Sub
    Sub InitTimer2 (In TMR2Pres, In TMR2Post)
        #ifdef PIC
            swapf TMR2Post,F
            rrf TMR2Post,W
            andlw 120
            iorwf TMR2Pres,F
        #endif
        #ifdef AVR
            'Some wacky swapping of variable names here
            If TMR2Pres = Ext Then
                TMR2Post = AVR_EXT_TMR
            End If
        #endif
    End Sub
    Sub InitTimer3(In TMR3Source, In TMR3Pres)
        #ifdef PIC
            SInitTimer3
        #endif
        #ifdef AVR
            If TMR3Source = Ext Then
                TMR3Pres = AVR_EXT_TMR
            End If
        #endif
    End Sub
    Sub SInitTimer3
        T3CON = TMR3Pres
        If TMR3Source = Ext Then
            Set TMR3CS On
        End If
        If TMR3Source = ExtOsc Then
            Set TMR3CS On
        End If
    End Sub
    Sub InitTimer4(In TMR4Source, In TMR4Pres)
        #ifdef AVR
            If TMR4Source = Ext Then
                TMR4Pres = AVR_EXT_TMR
            End If
        #endif
    End Sub
    Sub InitTimer5(In TMR5Source, In TMR5Pres)
        #ifdef AVR
            If TMR5Source = Ext Then
                TMR5Pres = AVR_EXT_TMR
            End If
        #endif
    End Sub
    
     
  • Chris
    Chris
    2012-04-14

    I tested the code I modified with a non 16f1826 chip and the way I had the "#ifndef" in there made the compile fail. I've made the required changes and it should now work with the 16f1826, and with any other chip is used to work with. I'm a beginner at C code and really really beginner at understanding ASM codes, I just used my common sense to figure these problems out.

    '    Timer control routines for Great Cow BASIC
    '    Copyright (C) 2006-2009 Hugh Considine
    '    This library is free software; you can redistribute it and/or
    '    modify it under the terms of the GNU Lesser General Public
    '    License as published by the Free Software Foundation; either
    '    version 2.1 of the License, or (at your option) any later version.
    '    This library is distributed in the hope that it will be useful,
    '    but WITHOUT ANY WARRANTY; without even the implied warranty of
    '    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    '    Lesser General Public License for more details.
    '    You should have received a copy of the GNU Lesser General Public
    '    License along with this library; if not, write to the Free Software
    '    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    '********************************************************************************
    'IMPORTANT:
    'THIS FILE IS ESSENTIAL FOR SOME OF THE COMMANDS IN GCBASIC. DO NOT ALTER THIS FILE
    'UNLESS YOU KNOW WHAT YOU ARE DOING. CHANGING THIS FILE COULD RENDER SOME GCBASIC
    'COMMANDS UNUSABLE!
    '********************************************************************************
    'Changes:
    ' 10/7/2009: Added AVR support
    ' 04/11/2012: Added support for 16f1826 by bowlofpudding
    'Subroutines:
    ' InitTimer0 (Source, Prescaler)
    ' InitTimer1 (Source, Prescaler)
    ' InitTimer2 (Prescaler, Postscaler)
    ' InitTimer3 (Source, Prescaler)
    ' ClearTimer(TimerNumber)
    ' StartTimer(TimerNumber)
    ' StopTimer(TimerNumber)
    'Some simpler names for the timers (use to read)
    #ifdef PIC
        '#define Timer0 TMR0
        Dim Timer0 Alias TMR0
        '#define Timer1 TMR1L
        'Is now a function, need to ensure read happens in certain order
        '#define Timer2 TMR2
        Dim Timer2 Alias TMR2
    #endif
    #ifdef AVR
        Dim Timer0 Alias TCNT0
        Dim Timer2 Alias TCNT2
    #endif
    'Sources
    #define Osc 1
    #define Ext 2
    #define ExtOsc 3
    'AVR prescaler settings
    #define PS_1 1
    #define PS_8 2
    #define PS_64 3
    #define PS_256 4
    #define PS_1024 5
    'Also worth noting here,
    '0: stop timer
    '6: external, clock on falling
    '7: external, clock on rising
    #define AVR_EXT_TMR 7
    'Timer 0 prescales
    #define PS0_1/2 0
    #define PS0_1/4 1
    #define PS0_1/8 2
    #define PS0_1/16 3
    #define PS0_1/32 4
    #define PS0_1/64 5
    #define PS0_1/128 6
    #define PS0_1/256 7
    #define PS0_2 0
    #define PS0_4 1
    #define PS0_8 2
    #define PS0_16 3
    #define PS0_32 4
    #define PS0_64 5
    #define PS0_128 6
    #define PS0_256 7
    'Timer 1 prescales
    #define PS1_1/1 0
    #define PS1_1/2 16
    #define PS1_1/4 32
    #define PS1_1/8 48
    #define PS1_1 0
    #define PS1_2 16
    #define PS1_4 32
    #define PS1_8 48
    'Timer 2 prescales
    #define PS2_1/1 0
    #define PS2_1/4 1
    #define PS2_1/16 2
    #define PS2_1 0
    #define PS2_4 1
    #define PS2_16 2
    'Timer 3 prescales
    #define PS3_1/1 0
    #define PS3_1/2 16
    #define PS3_1/4 32
    #define PS3_1/8 48
    #define PS3_1 0
    #define PS3_2 16
    #define PS3_4 32
    #define PS3_8 48
    Function Timer1 As Word
        #ifdef PIC
            [byte]Timer1 = TMR1L
            Timer1_H = TMR1H
        #endif
        #ifdef AVR
            [byte]Timer1 = TCNT1L
            Timer1_H = TCNT1H
        #endif
    End Function
    Function Timer3 As Word
        #ifdef PIC
            [byte]Timer3 = TMR3L
            Timer3_H = TMR3H
        #endif
        #ifdef AVR
            [byte]Timer3 = TCNT3L
            Timer3_H = TCNT3H
        #endif
    End Function
    Function Timer4 As Word
        #ifdef AVR
            [byte]Timer4 = TCNT4L
            Timer4_H = TCNT4H
        #endif
    End Function
    Function Timer5 As Word
        #ifdef AVR
            [byte]Timer5 = TCNT5L
            Timer5_H = TCNT5H
        #endif
    End Function
    'Start/Clear/Stop subs
    Sub StartTimer(In TMRNumber)
        #ifdef PIC
            'Timer 0 always runs
            #ifdef Var(T1CON)
                If TMRNumber = 1 Then
                    Set TMR1ON On
                End If
            #endif
            #ifdef Var(T2CON)
                If TMRNumber = 2 Then
                    T2CON = TMR2Pres
                    Set TMR2ON On
                End If
            #endif
            #ifdef Var(T3CON)
                If TMRNumber = 3 Then
                    Set TMR3ON On
                End If
            #endif
        #endif
    
        #ifdef AVR
            'Need to set clock select bits to 0
            #ifndef Var(TCCR0B)
                #ifdef Var(TCCR0)
                If TMRNumber = 0 Then
                    TCCR0 = TMR0Pres
                End If
                #endif
            #endif
            #ifdef Var(TCCR0B)
                If TMRNumber = 0 Then
                    TCCR0B = TCCR0B And 248 Or TMR0Pres
                End If
            #endif
            #ifdef Var(TCCR1B)
                If TMRNumber = 1 Then
                    TCCR1B = TCCR1B And 248 Or TMR1Pres
                End If
            #endif
            #ifdef Var(TCCR2B)
                If TMRNumber = 2 Then
                    TCCR2B = TCCR2B And 248 Or TMR2Post
                End If
            #endif
            #ifdef Var(TCCR3B)
                If TMRNumber = 3 Then
                    TCCR3B = TCCR3B And 248 Or TMR3Pres
                End If
            #endif
            #ifdef Var(TCCR4B)
                If TMRNumber = 4 Then
                    TCCR4B = TCCR4B And 248 Or TMR4Pres
                End If
            #endif
            #ifdef Var(TCCR5B)
                If TMRNumber = 5 Then
                    TCCR5B = TCCR5B And 248 Or TMR5Pres
                End If
            #endif
        #endif
    End Sub
    Sub ClearTimer (In TMRNumber)
        #ifdef PIC
            If TMRNumber = 0 Then
                TMR0 = 0
                SInitTimer0
            End If
            #ifdef Var(T1CON)
                If TMRNumber = 1 then
                    TMR1H = 0
                    TMR1L = 0
                    SInitTimer1
                End If
            #endif
            #ifdef Var(T2CON)
                If TMRNumber = 2 Then
                    TMR2Pres = T2CON
                    TMR2 = 0
                    T2CON = TMR2Pres
                End If
            #endif
            #ifdef Var(T3CON)
                If TMRNumber = 3 then
                    TMR3H = 0
                    TMR3L = 0
                    SInitTimer3
                End If
            #endif
        #endif
        #ifdef AVR
            #ifdef Var(TCNT0)
                If TMRNumber = 0 Then
                    TCNT0 = 0
                End If
            #endif
            #ifdef Var(TCNT1L)
                If TMRNumber = 1 Then
                    TCNT1H = 0
                    TCNT1L = 0
                End If
            #endif
            #ifdef Var(TCNT2)
                If TMRNumber = 2 Then
                    TCNT2 = 0
                End If
            #endif
            #ifdef Var(TCNT3L)
                If TMRNumber = 3 Then
                    TCNT3H = 0
                    TCNT3L = 0
                End If
            #endif
            #ifdef Var(TCNT4L)
                If TMRNumber = 4 Then
                    TCNT4H = 0
                    TCNT4L = 0
                End If
            #endif
            #ifdef Var(TCNT5L)
                If TMRNumber = 5 Then
                    TCNT5H = 0
                    TCNT5L = 0
                End If
            #endif
        #endif
    End Sub
    Sub StopTimer (In TMRNumber)
        #ifdef PIC
            'Timer 0 always runs
            If TMRNumber = 1 Then
                Set TMR1ON OFF
            End If
            #ifdef Var(T2CON)
                If TMRNumber = 2 Then
                    Set TMR2ON OFF
                End If
            #endif
            #ifdef Var(T3CON)
                If TMRNumber = 3 Then
                    Set TMR3ON OFF
                End If
            #endif
        #endif
        #ifdef AVR
            'Need to set clock select bits to 0
            #ifdef Var(TCCR0B)
                If TMRNumber = 0 Then
                    TCCR0B = TCCR0B And 248
                End If
            #endif
            #ifdef Var(TCCR1B)
                If TMRNumber = 1 Then
                    TCCR1B = TCCR1B And 248
                End If
            #endif
            #ifdef Var(TCCR2B)
                If TMRNumber = 2 Then
                    TCCR2B = TCCR2B And 248
                End If
            #endif
            #ifdef Var(TCCR3B)
                If TMRNumber = 3 Then
                    TCCR3B = TCCR3B And 248
                End If
            #endif
            #ifdef Var(TCCR4B)
                If TMRNumber = 4 Then
                    TCCR4B = TCCR4B And 248
                End If
            #endif
            #ifdef Var(TCCR5B)
                If TMRNumber = 5 Then
                    TCCR5B = TCCR5B And 248
                End If
            #endif
        #endif
    End Sub
    'Initialise subs for individual timers
    Sub InitTimer0(In TMR0Source, In TMR0Pres)
        #ifdef PIC
            SInitTimer0
        #endif
        #ifdef AVR
            'Just need to buffer TMR0Pres
            '(And change it for external clock)
            If TMR0Source = Ext Then
                TMR0Pres = AVR_EXT_TMR
            End If
        #endif
    End Sub
    Sub SInitTimer0
        OPTION_REG = OPTION_REG AND 192
        SET OPTION_REG.PSA OFF
    'to fix for 16f1826
    #ifdef T0SE
        if TMR0Source = Osc THEN SET OPTION_REG.T0SE OFF
        if TMR0Source = Ext THEN SET OPTION_REG.T0SE ON
    #endif
    #ifdef TMR0CS                                               'added for 16f1826
        if TMR0Source = Osc THEN SET OPTION_REG.TMR0CS OFF
        if TMR0Source = Ext THEN SET OPTION_REG.TMR0CS ON
    #endif
        clrwdt
        OPTION_REG = OPTION_REG OR TMR0Pres
    End Sub
    Sub InitTimer1(In TMR1Source, In TMR1Pres)
        #ifdef PIC
            SInitTimer1
        #endif
        #ifdef AVR
            If TMR1Source = Ext Then
                TMR1Pres = AVR_EXT_TMR
            End If
        #endif
    End Sub
    Sub SInitTimer1
        T1CON = TMR1Pres
            If TMR1Source = Ext Then        'added for 16f1826
                #ifdef TMR1CS
                    Set TMR1CS On
                #endif
                #ifdef TMR1CS1
                    Set TMR1CS1 On
                #endif
            End If
            If TMR1Source = ExtOsc Then
                #ifdef TMR1CS               'added for 16f1826
                    Set TMR1CS On
                #endif
        'added for 16f1826
                #ifdef TMR1CS1
                    Set TMR1CS1 On
                #endif
                Set T1OSCEN On
            End If
    End Sub
    Sub InitTimer2 (In TMR2Pres, In TMR2Post)
        #ifdef PIC
            swapf TMR2Post,F
            rrf TMR2Post,W
            andlw 120
            iorwf TMR2Pres,F
        #endif
        #ifdef AVR
            'Some wacky swapping of variable names here
            If TMR2Pres = Ext Then
                TMR2Post = AVR_EXT_TMR
            End If
        #endif
    End Sub
    Sub InitTimer3(In TMR3Source, In TMR3Pres)
        #ifdef PIC
            SInitTimer3
        #endif
        #ifdef AVR
            If TMR3Source = Ext Then
                TMR3Pres = AVR_EXT_TMR
            End If
        #endif
    End Sub
    Sub SInitTimer3
        T3CON = TMR3Pres
        If TMR3Source = Ext Then
            Set TMR3CS On
        End If
        If TMR3Source = ExtOsc Then
            Set TMR3CS On
        End If
    End Sub
    Sub InitTimer4(In TMR4Source, In TMR4Pres)
        #ifdef AVR
            If TMR4Source = Ext Then
                TMR4Pres = AVR_EXT_TMR
            End If
        #endif
    End Sub
    Sub InitTimer5(In TMR5Source, In TMR5Pres)
        #ifdef AVR
            If TMR5Source = Ext Then
                TMR5Pres = AVR_EXT_TMR
            End If
        #endif
    End Sub
    
     
  • Hugh Considine
    Hugh Considine
    2012-04-15

    I'll be happy to add those changes, thanks!

     
  • Chris
    Chris
    2012-04-15

    just to add on a little more support for the 16f1825 someone sent me a working eeprom routine for this chip, because the one included with gcbasic didnt work for my chip. I have tested this as working with my chip, but i dont know if it breaks support for the more standard chips.

    '    EEPROM routines for Great Cow BASIC
    '    Copyright (C) 2006-2007 Hugh Considine
    '    This library is free software; you can redistribute it and/or
    '    modify it under the terms of the GNU Lesser General Public
    '    License as published by the Free Software Foundation; either
    '    version 2.1 of the License, or (at your option) any later version.
    '    This library is distributed in the hope that it will be useful,
    '    but WITHOUT ANY WARRANTY; without even the implied warranty of
    '    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    '    Lesser General Public License for more details.
    '    You should have received a copy of the GNU Lesser General Public
    '    License along with this library; if not, write to the Free Software
    '    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    '********************************************************************************
    'IMPORTANT:
    'THIS FILE IS ESSENTIAL FOR SOME OF THE COMMANDS IN GCBASIC. DO NOT ALTER THIS FILE
    'UNLESS YOU KNOW WHAT YOU ARE DOING. CHANGING THIS FILE COULD RENDER SOME GCBASIC
    'COMMANDS UNUSABLE!
    '********************************************************************************
    'Updates:
    ' 3/5/2006: Program EEPROM read/write added
    ' 16/5/2006: Write mode disabled at end of EPWrite
    ' 23/5/2006: EPWrite and EPRead changed to stop use of temporary vars
    ' 19/8/2006: Program EEPROM altered to take input as word
    ' 9/2/2007: Bug fix: EEPGD bit not set if not present
    ' 5/8/2007: Altered to use alias to access address, data vars
    ' 4/9/2007: Data EEPROM code altered to support AVR
    ' 15/10/2007: Bug fix: FREE bit not set if not present, altered to use IntOn/IntOff to control interrupt
    ' 4/12/2007: Added In/Out to parameters
    ' 17/6/2008: Altered to make EPRead a system sub, to allow use in ReadTable
    ' 19/4/2009: Bugfixes for AVR, allow EEPE instead of EEWE
    sub EPWrite(In EEAddress, In EEDataValue)
    #IFDEF PIC
        'Variable alias
        #IFNDEF Var(EEADRH)
            Dim EEAddress Alias EEADR
        #ENDIF
        #IFDEF Var(EEADRH)
            Dim EEAddress As Word Alias EEADRH, EEADR
        #ENDIF
        #IFDEF Var(EEDATA)
            Dim EEDataValue Alias EEDATA
        #ENDIF
        #IFDEF Var(EEDATL)
            Dim EEDataValue Alias EEDATL
        #ENDIF
    
        'Disable interrupt
        IntOff
    
        'Select data memory
        #IFDEF Bit(EEPGD)
            SET EECON1.EEPGD OFF
        #ENDIF
        #IFDEF Bit(CFGS)
            Set EECON1.CFGS OFF
        #ENDIF
    
        'Start write
        SET EECON1.WREN ON
        EECON2 = 0x55
        EECON2 = 0xAA
        SET EECON1.WR ON
        SET EECON1.WREN OFF
    
        'Wait for write to complete
        WAIT WHILE EECON1.WR ON
        SET EECON1.WREN OFF
    
        'Restore interrupt
        IntOn
    
    #ENDIF
    #IFDEF AVR
        'Variable alias
        #IFDEF Var(EEARH)
            Dim EEAddress As Word Alias EEARH, EEARL
        #ENDIF
        #IFNDEF Var(EEARH)
            #IFDEF Var(EEAR)
                Dim EEAddress Alias EEAR
            #ENDIF
        #ENDIF
        Dim EEDataValue Alias EEDR
    
        'Enable write
        #IFDEF Bit(EEMWE)
            Set EECR.EEMWE On
        #ENDIF
        #IFNDEF Bit(EEMWE)
            #IFDEF Bit(EEMPE)
                Set EECR.EEMPE On
            #ENDIF
        #ENDIF
        'Start write, wait for it to complete
        #IFDEF Bit(EEWE)
            Set EECR.EEWE On
            Wait Until EECR.EEWE Off
        #ENDIF
        #IFNDEF Bit(EEWE)
            #IFDEF Bit(EEPE)
                Set EECR.EEPE On
                Wait Until EECR.EEPE Off
            #ENDIF
        #ENDIF
    
    #ENDIF
    
    end sub
    'EPRead made into system subroutine
    'Constant added to allow it to still be referred to as EPRead
    #define EPRead SysEPRead
    sub SysEPRead(In EEAddress, Out EEDataValue)
    
    #IFDEF PIC
        'Variable alias
        #IFNDEF Var(EEADRH)
            Dim EEAddress Alias EEADR
        #ENDIF
        #IFDEF Var(EEADRH)
            Dim EEAddress As Word Alias EEADRH, EEADR
        #ENDIF
        #IFDEF Var(EEDATA)
            Dim EEDataValue Alias EEDATA
        #ENDIF
        #IFDEF Var(EEDATL)
            Dim EEDataValue Alias EEDATL
        #ENDIF
    
        'Disable interrupt
        IntOff
    
        'Select data memory
        #IFDEF Bit(EEPGD)
            SET EECON1.EEPGD OFF
        #ENDIF
        #IFDEF Bit(CFGS)
            Set EECON1.CFGS OFF
        #ENDIF
    
        'Read
        SET EECON1.RD ON
    
        'Restore interrupt
        IntOn
    #ENDIF
    #IFDEF AVR
        'Variable alias
        #IFDEF Var(EEARH)
            Dim EEAddress As Word Alias EEARH, EEARL
        #ENDIF
        #IFNDEF Var(EEARH)
            #IFDEF Var(EEAR)
                Dim EEAddress Alias EEAR
            #ENDIF
        #ENDIF
        Dim EEDataValue Alias EEDR
    
        'Start read
        Set EECR.EERE On
    
    #ENDIF  
    end sub
    function ReadEP(EEAddress)
    #IFDEF PIC
        'Variable alias
        #IFNDEF Var(EEADRH)
            Dim EEAddress Alias EEADR
        #ENDIF
        #IFDEF Var(EEADRH)
            Dim EEAddress As Word Alias EEADRH, EEADR
        #ENDIF
        #IFDEF Var(EEDATA)
            Dim EEDataValue Alias EEDATA
        #ENDIF
        #IFDEF Var(EEDATL)
            Dim EEDataValue Alias EEDATL
        #ENDIF
    
        'Disable interrupt
        IntOff
    
        'Select data memory
        #IFDEF Bit(EEPGD)
            SET EECON1.EEPGD OFF
        #ENDIF
        #IFDEF Bit(CFGS)
            Set EECON1.CFGS OFF
        #ENDIF
    
        'Read
        SET EECON1.RD ON
        ReadEP = EEDataValue
    
        'Restore interrupt
        IntOn
    #ENDIF
    #IFDEF AVR
        'Variable alias
        #IFDEF Var(EEARH)
            Dim EEAddress As Word Alias EEARH, EEARL
        #ENDIF
        #IFNDEF Var(EEARH)
            #IFDEF Var(EEAR)
                Dim EEAddress Alias EEAR
            #ENDIF
        #ENDIF
        Dim EEDataValue Alias EEDR
    
        'Start read
        Set EECR.EERE On
    #ENDIF
    
    end function
    sub ProgramWrite(In EEAddress, In EEDataWord)
    #IFDEF PIC
        Dim EEAddress As Word Alias EEADRH, EEADR
        #IFDEF Var(EEDATA)
            Dim EEDataWord As Word Alias EEDATH, EEDATA
        #ENDIF
        #IFDEF Var(EEDATL)
            Dim EEDataWord As Word Alias EEDATH, EEDATL
        #ENDIF
    
        'Disable Interrupt
        IntOff
    
        'Select program memory
        SET EECON1.EEPGD ON
        #IFDEF Bit(CFGS)
            Set EECON1.CFGS OFF
        #ENDIF
    
        'Enable write
        SET EECON1.WREN ON
        #ifdef bit(FREE)
            SET EECON1.FREE OFF
        #endif
    
        'Write enable code
        EECON2 = 0x55
        EECON2 = 0xAA
    
        'Start write, wait for it to finish
        SET EECON1.WR ON
        NOP
        NOP
        SET EECON1.WREN OFF
    
        'Enable Interrupt
        IntOn
    #ENDIF
    end sub
    sub ProgramRead(In EEAddress, Out EEDataWord)
        Dim EEAddress As Word Alias EEADRH, EEADR
        #IFDEF Var(EEDATA)
            Dim EEDataWord As Word Alias EEDATH, EEDATA
        #ENDIF
        #IFDEF Var(EEDATL)
            Dim EEDataWord As Word Alias EEDATH, EEDATL
        #ENDIF
    
        'Disable Interrupt
        IntOff
    
        'Select program memory
        SET EECON1.EEPGD ON
        #IFDEF Bit(CFGS)
            Set EECON1.CFGS OFF
        #ENDIF
    
        'Start read, wait for it to finish
        SET EECON1.RD ON
        NOP
        NOP
    
        'Enable interrupt
        IntOn
    end sub
    sub ProgramErase(In EEAddress)
        Dim EEAddress As Word Alias EEADRH, EEADR
    
        'Disable Interrupt
        IntOff
    
        'Select program memory
        SET EECON1.EEPGD ON
        #IFDEF Bit(CFGS)
            Set EECON1.CFGS OFF
        #ENDIF
    
        SET EECON1.WREN ON
        #ifdef bit(FREE)
            SET EECON1.FREE ON
        #endif
        EECON2 = 0x55
        EECON2 = 0xAA
        SET EECON1.WR ON
        NOP
        NOP
        #ifdef bit(FREE)
            SET EECON1.FREE OFF
        #endif
        SET EECON1.WREN OFF
    
        'Enable interrupt
        IntOn
    end sub