Menu

Timer operations with prescaler

2009-03-28
2013-05-30
  • Edward LaBudde

    Edward LaBudde - 2009-03-28

    Hi all, newbie here.  The discussion about timers drew my attention, as I have some projects that rely on them.  I therefore made a test program to check timer operation for three classes of chips, 12F675, 16F88 and 18F2455.  I was not able to compile the 18F2455 because it gave me a “Error [174] : Unknown opcode "CONFIG" even though no #config was specified.  See my recent post on this.  For this test I used the On Interrupt Timer#Overflow call TimerMark# command and output a pulse on a pin to measure the Overflow frequency with a frequency counter.  I used Santiago’s correction for Timer 0 in the timer.h file, see his post on this, but still had big problems.

    The results were quite surprising.   On the 12F675 the oscillator was set to internal and calibrated to near exact frequency with OSCCAL command.
    The results for Timer 0 were as follows:
    PS1_1/2 Overflow frequency = 1952.8 Hz close to the calculated value of 1960.7
    PS1_1/4 Overflow frequency = 125 Hz!!!
    PS1_1/8 Overflow frequency = 138.2 Hz!!!
    PS1_1/16 Overflow frequency = 3900 Hz!!!
    PS1_1/32 Overflow frequency = 15.1Hz!!!
    PS1_1/64 Overflow frequency = 121 Hz
    PS1_1/128 Overflow frequency = 975 Hz
    PS1_1/256 Overflow frequency = 1955 Hz

    Timer 1 worked ok, but only if there was some activity in the main program.  Had to ad this:
    On Interrupt Timer1Overflow call TimerMark1    '15.25 Hz at /1
    set gpio.2 On    'Required activity to get Interrupt to work on Timer 1
    wait 10 us
    set gpio.2 Off
    goto main
     

    If the line: On Interrupt Timer1Overflow call TimerMark1 was alone, no output pulse appeared, although it would EPWrite a number to the correct address showing it actually had occurred. I tried the 16F88 and got similar, but not the same results
    PS1_1/2 Overflow frequency = 3948.8 Hz close to the calculated value of 3921.5
    PS1_1/4 Overflow frequency = No output at all
    PS1_1/8 Overflow frequency = No output at all
    PS1_1/16 Overflow frequency = 7894 Hz!!!
    PS1_1/32 Overflow frequency = 30Hz!!!
    PS1_1/64 Overflow frequency = 493 Hz
    PS1_1/128 Overflow frequency = 1973 Hz
    PS1_1/256 Overflow frequency = 3948 Hz

    Had the same problem with timer 1, it need the same activity to get the right answer, otherwise it would read 2-5 times the correct rate

    Timer 2  read 7896 for all PS!_/X settings X = 1/4/16

    Somebody tell me what is going on here?  Here is the 12F675-code sample
    'A Timer test for 12F675
    'Chip model
    #chip 12F675, 4
    #config INTRC_OSC_CLKOUT
    'Set the pin directions
    dir gpio.0 Out 'Timer 0 output pin 7
    dir gpio.1 Out 'Timer 1 output pin 6
    dir gpio.2 Out    'pin 5
    dir gpio.4 Out 'pin 3
    'set defines
    #define Pulse0 gpio.0
    #define Pulse1 gpio.1
    osccal = b'00110100'    'Cal to 1.00 MHz Clk output
    'Set inital parameters
    InitTimer0 Osc, PS1_1/2 'Select timer 0 clock, prescale 2,4,8,...256
    InitTimer1 Osc, PS1_1/1 'Select timer 1 clock, prescale 1,2,4,8
    wait 100 ms    'startup delay
    ClearTimer 1
    StartTimer 1

    'Proram
    main:
    'On Interrupt Timer0Overflow call TimerMark0    '1.960.7 Hz at /2
    On Interrupt Timer1Overflow call TimerMark1    '15.25 Hz at /1
    set gpio.2 On    'Required activity to get Interrupt to work on Timer 1 or 2
    wait 10 us
    set gpio.2 Off
    goto main

    Sub TimerMark0
    set pulse0 On    'Mark event for scope
    wait 10 us
    set pulse0 Off
    'EPWrite 0, 100    'mark event capture in Eprom
    Return

    Sub TimerMark1
    set pulse1 On    'Mark event for scope
    wait 200 us
    set pulse1 Off
    'EPWrite 1, 100    'mark event capture in Eprom
    Return

     

     
    • Nobody/Anonymous

      I think Your problem is here:

      InitTimer0 Osc, PS1_1/2

      You are using timer1 prescaler: PS1 for timer0, it should be: PS0_1/2

      You also should put these lines outside main loop:

      'On Interrupt Timer0Overflow call TimerMark0 '1.960.7 Hz at /2
      On Interrupt Timer1Overflow call TimerMark1 '15.25 Hz at /1

      This is not a real problem, but i think this is the correct way, as this lines are for interrupt initialisation, only should be executed one time.

       
    • Edward LaBudde

      Edward LaBudde - 2009-03-29

      Nobody, thanks for your hint on PS0_.  You were correct.  I was so careless to overlook such an error, I copied the lines and forgot to edit them.

      Timer0 and Timer1 will work outside the main loop. 

      However, there is still a problem with Timer2.  It requires activitity to work right either inside or outside.  Also still have the problem of 7843 Hz no matter what the prescale is set to.

      Also note that the 18F2455 problem is with GPASM not GCBasic.
      Installed MPASMWIN and compiles ok with that.

       

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.