Timer1 usage in PIC18F252


+ Reply to Thread
Results 1 to 7 of 7
  1. #1
    EShatara's Avatar
    EShatara Guest

    Default Timer1 usage in PIC18F252

    Been diviing into the description of the use of the interrupts for the 18F252 device and want to test out my understanding relative to the relationship between the prescaler and 8/16 bit registers of the timer.

    If I'm using a 4Mhz oscillator and an internal clock for the Timer then my effective frequency going to the 8 or 16 bit counter is either:

    1 Mhz for a Prescale value of 1
    500 Khz for a prscale value of 2
    etc....

    Then this frequency is fed into the counter and when it overflows an interrupt is generated.

    Correct?

    So if I want an interrupt every 0.5 seconds I could use a prescale of 8 giving me a frequency of 125khz (8us period) and preload the 16bit register with 3036 (decimal)

    I reached this figure by dividing 0.5 sec by 8 us and getting 62500 pulses required to overflow the 16bit counter. With no preload, the counter would take 65,536 counts to overflow so I would preload it with 65536-62500

    Correct?

    The other thing I'm not sure about is the ability to configure the timer for 16 bit reads and writes. I still need to read TMR1L and TMR1H separately... don't I?

    I'm sure I'll have more questions when I attempt to program this later on this weekend.

    thanks...Chris

  2. #2
    Join Date
    Sep 2005
    Location
    Campbell, CA
    Posts
    1,107

    Default

    Your math is correct, but be sure you write to any 16bit timer in the order: highbyte, lowbyte. The datasheet explains this.
    Charles Linquist

  3. #3
    EShatara's Avatar
    EShatara Guest

    Default Preload Register on service

    I'm also assuming that I will need to reload the TMR1H/L register as I exit the interrupt service routine. Correct?

  4. #4
    Join Date
    Sep 2005
    Location
    Campbell, CA
    Posts
    1,107

    Default

    Generally no. If you are interrupt driven, and you need an interrupt every 'x' seconds then your ISR would reload the registers with the appropriate values as soon as possible (to insure correct timing). There is no need to reload them upon exit.

    In PBP, I normally run in "pseudo-interrupt" mode. I sit in a tight loop polling PIR1.0 until it goes true, clear that bit, reload TMR1H and TMR1L then enter my main routine. After the routine, I go back to the polling mode.

    I put all my high-priority items (such as checking the serial port interrupt bit) in the tight loop, and all the normal stuff in the main program. I avoid pauses whenever possible and instead rely on the timer to provide timing information.
    Charles Linquist

  5. #5
    EShatara's Avatar
    EShatara Guest

    Default Thanks

    >Generally no. If you are interrupt driven, and you need an interrupt every 'x' >seconds then your ISR would reload the registers with the appropriate >values as soon as possible (to insure correct timing). There is no need to >reload them upon exit.

    That's what I meant...thank you

    >In PBP, I normally run in "pseudo-interrupt" mode. I sit in a tight loop >polling PIR1.0 until it goes true, clear that bit, reload TMR1H and TMR1L >then enter my main routine. After the routine, I go back to the polling >mode.

    I already have a tight loop polling my serial port looking for a charaacter. Are you saying I can use the timer without having it generate an interrupt?

    Hadn't thought about that....That could be quite easy to implement in my code.

    Thanks .... chris

  6. #6
    Join Date
    Sep 2005
    Location
    Campbell, CA
    Posts
    1,107

    Default

    Yes, here is a quick code example:


    FastLoop:
    IF PIR1.0 = 1 THEN
    PIR1.0 = 0
    TMR1H = TimerPreloadValue.Highbyte
    TMR1L = TimerPreloadValue.Lowbyte
    GOSUB MainProgram
    ENDIF
    IF PIR1.5 = 1 THEN
    Gosub KeyboardInputRoutine ' HSERIN clears PIR1.5
    ENDIF

    GOTO FastLoop

    MainProgram:

    'Do something here every time the timer rolls over

    RETURN




    ' This works perfectly as long as your 'Main Program' never takes as long as the timer period. The code sits in the FastLoop waiting for either a keyboard input or a timer timeout. When the timer timeout occurs, then the MainProgram is executed. If you output data in your MainProgram, that data will be output at a regular interval regarless of the length of your program.
    Just remember - stay away from PAUSEs in your main program whenever possible!
    Charles Linquist

  7. #7
    EShatara's Avatar
    EShatara Guest

    Default Thanks again

    Thanks Charles...you've been most helpful....Chris

Similar Threads

  1. Software PWM using Timer1
    By muqasim in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 18th December 2009, 12:49
  2. Help with TIMER1 + SLEEP
    By Yodoad in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 22nd May 2009, 15:07
  3. Time Period of Timer1
    By arnol34 in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 4th May 2007, 00:31
  4. Timer1 and Interupt wierdness
    By mind in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 23rd August 2005, 01:24
  5. Use of Timer1 with 16F819
    By Barry Johnson in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 25th January 2005, 17:25

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts