Menu

Timer0 optimisation suggestion

Santiago
2009-02-01
2013-05-30
  • Santiago

    Santiago - 2009-02-01

    Hello...

    I had problems when using timer0 functions, when looking to generated asm i could not find the error, and had a look to the timer.h file, then i did some changes to solve the problem and optimize the generated code.

    This is the original timer.h code and the generated asm:
    _____________________________________________________________________________

    'Sources

    #define Osc 1
    #define Ext 2

    #define ExtOsc 3

    Sub SInitTimer0

        OPTION_REG = OPTION_REG AND 192

        SET OPTION_REG.PSA OFF         'not needed... already OFF

        if TMR0Source = Osc THEN SET OPTION_REG.T0SE OFF 'not needed... already OFF

        if TMR0Source = Ext THEN SET OPTION_REG.T0SE ON
        OPTION_REG = OPTION_REG OR TMR0Pres       'this way generated code is lager.

        clrwdt

    End Sub

    ;********************************************************************************

    SINITTIMER0
        movlw    192
        banksel    OPTION_REG
        andwf    OPTION_REG,F
        bcf    OPTION_REG,PSA
        movlw    0
        banksel    TMR0SOURCE
        subwf    TMR0SOURCE,W
        btfss    STATUS, Z
        goto    ENDIF4
        banksel    OPTION_REG
        bcf    OPTION_REG,T0SE
    ENDIF4
        movlw    1
        banksel    TMR0SOURCE
        subwf    TMR0SOURCE,W
        btfss    STATUS, Z
        goto    ENDIF5
        banksel    OPTION_REG
        bsf    OPTION_REG,T0SE
    ENDIF5
        movf    OPTION_REG,W
        banksel    TMR0PRES
        iorwf    TMR0PRES,W
        banksel    OPTION_REG
        movwf    OPTION_REG
        clrwdt
        banksel    STATUS
        return

    ;********************************************************************************

    And this is the code i used in timer.h and the generated asm:
    _____________________________________________________________

    'Sources

    #define Osc 0
    '.................this allow: OPTION_REG.T0SE = Osc
    #define Ext 1

    #define ExtOsc 2

    Sub SInitTimer0

        OPTION_REG = OPTION_REG AND 192

        OPTION_REG.T0SE = Osc
        OPTION_REG = TMR0Pres OR OPTION_REG     'this way generated code is shorter.

        clrwdt

    End Sub

    ;********************************************************************************

    SINITTIMER0
        movlw    192
        banksel    OPTION_REG
        andwf    OPTION_REG,F
        bsf    OPTION_REG,T0SE
        banksel    TMR0PRES
        movf    TMR0PRES,W
        banksel    OPTION_REG
        iorwf    OPTION_REG,F
        clrwdt
        banksel    STATUS
        return

    ;********************************************************************************

    Now timer0 works for me and the code is little more optimized

    Also saw timer1 possible error... not tried it, but looks like Osc mode is missing:

    Sub SInitTimer1

        T1CON = TMR1Pres

        If TMR1Source = Ext Then

            Set TMR1CS On

        End If

        If TMR1Source = ExtOsc Then

            Set TMR1CS On

            Set T1OSCEN On

        End If

    End Sub

     
    • Nobody/Anonymous

      Arcachofo said:
      "Also saw timer1 possible error... not tried it, but looks like Osc mode is missing: "

      Sorry... not needed to do anything in Osc mode.... that was my mistake....

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.