Menu

Timer test on PIC18F1220 failed

Help
Capaction
2011-01-10
2013-05-30
  • Capaction

    Capaction - 2011-01-10

    I am expecting a 10s flash beat and get 250ms ?

    #chip 18f1220,8   
    #Config LVP = OFF, MCLRE = On, PWRT = On, WDT = off, BOR = Off, OSC = INTIO1

    Set PCFG4 = 1 'pin  8 configured as digital port B0
    Set PCFG5 = 1 'pin  9 configured as digital port B1
    Set PCFG6 = 1 'pin 10 configured as digital port B4
    DIR PortB IN
    Set NOT_RBPU OFF 'enable PullUps all PortB

    Dir PortA.4 OUT
    #Define LED PortA.4
    #Define LEDon  PortA.4 = 0
    #Define LEDoff PortA.4 = 1

    Dim TenthSeconds As Word
    TenthSeconds= 0

    'Clock pulse frequency 8/4=2MHz
    'Divided by 4 by prescaler (PS1_1/4), timer incremented at frequency of 0.50Mhz i.e. every 2 us
    'If cleared then timer overflows after 65535 * 2 us = every 131.07ms

    InitTimer1 OSC,PS1_1/4
    ClearTimer 1
    StartTimer 1
    On  Interrupt Timer1Overflow Call IncCounter

    Do
    Again:
    LEDoff
    If TenthSeconds < 100 Then Goto Again  '0,1x100=10s
    TenthSeconds = 0                                   'reset counter
    LEDon
    Wait 250 ms
    LEDoff
    Loop

    Sub IncCounter
    'Timer will overflow after 131.07ms
    'We need it to overflow after 100.00 so the timer must have a value loaded into it
    'Need to load TMR1IE and TMR1IF with value that they will have after 31.07ms
    'then it will count from 131.07 and take 100.00 ms on overflow
    'value = 31070 / 2=15535
    Dim TempSet  As  Word  Alias TMR1IE,TMR1IF
    TempSet= 15535
    TenthSeconds = TenthSeconds + 1
    End Sub

     
  • gcha44

    gcha44 - 2011-01-10

    Hi,
    To load TMR1 timer you must use TMR1L and TMR1H  , not TMR1IE and TMR1IF

    TMR1E is bit0 of PIE1 and TMR1IF is bit0 of PIE1 registers.

    Best regards
    GC

     
  • gcha44

    gcha44 - 2011-01-10

    Sorry , TMR1IF is bit0 of PIR1 register .

    To load TMR1 with 15535 , you can only  write :
    TMR1L=175
    TMR1H=60                      ' 60*256=15360 + 175= 15535
    and to initialise  TMR1 , you must enter  these two values after InitTimer1  ,otherwise the first delay (ledoff) will be too long
    GC

     

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.