Use of Timer1 with 16F819


Closed Thread
Results 1 to 3 of 3
  1. #1
    Barry Johnson's Avatar
    Barry Johnson Guest

    Default Use of Timer1 with 16F819

    I am having difficulty getting the following program to work. PIC is the 16F819. Using an ext. osc running at 16 MHz. When I run the program, it prints the "starting" and "running," then nothing more. This program has no actual purpose other than to help me understand how to proper use Timer1. Once running, I intend to use it in an actual project. I loaded the TMR1 register with $00FF as an arbitrary value. I know having a print command in the interrupt handler isn't wise, but it is just for debug at present.

    Any guidance will be most appreciated.

    Thanks,
    Barry


    ' PicBasic Pro program that uses the Timer1
    ' interrupt for a "real-time clock."
    ' PIC is a 16F819

    include "modedefs.bas" 'include serout defines

    DEFINE OSC 16

    loops VAR WORD

    ' initialize interrupts
    INTCON = $00 ' disable and clear global interrupts, peripheral interrupts

    PIR1 = $00 ' clear interupt flags

    INTCON = $C0 ' enable global and peripheral interupts
    PIE1 = $01 ' Enable TMR1 overflow interrupt

    TMR1H = $00 ' load timer1 with $00FF
    TMR1L = $FF
    TRISB = %00000010 'All port b output except pin 1 (RX) is input

    T1CON = %00000011 ' Turn on Timer1, prescaler = 1, ext. clock on

    serout 2, T2400, ["starting", 10, 13]

    on interrupt goto inthand

    Pause 500
    serout 2, T2400, ["running", 10, 13]

    loops = 0


    loop: ' endless loop with 1 ms pause
    pause 1
    goto loop


    inthand:
    PIR1 = $00 ' clear interrupt flags
    PIE1 = $01 ' enable TMR1 interrupt
    TMR1H = $00 ' load timer1 with $00FF
    TMR1L = $FF
    loops = loops + 1 ' increment counter of interrupts
    serout 2, T2400, ["loops = ", #loops, 10, 13]
    resume

    End

  2. #2
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    If you're trying to use the internal clock (Fosc/4) for the TMR1 clock source, then you'll need T1CON = %00000001 VS T1CON = %00000011.

    You'll also want to place DISABLE just prior to entering your int handler.

    With ONINT you can do pretty much whatever you like in the int handler.

    You can tinker with this code below. It's tested on a 16F627A @20MHz.
    Code:
    @ DEVICE HS_OSC,LVP_OFF,WDT_OFF,MCLR_OFF
    DEFINE OSC 16 ' Tested at 20
    include "modedefs.bas" 'include serout defines
    
        Loops   VAR WORD ' Loops value
        loops   =   0    ' Start clear
        
        TRISB = %00000010 'All port b output except pin 1 (RX) is input
    
        INTCON = 0
        PIR1 = $00        ' clear interupt flags
    
        serout 2, T2400, ["starting", 13, 10] ' Before interrupts are enabled
    
        TMR1H = $00     ' load timer1 with $00FF
        TMR1L = $FF
        T1CON = %00000001 ' TMR1 on, prescaler=1, clock=(Fosc/4)
        INTCON = $C0    ' Enable global and peripheral interupts
        PIE1 = $01      ' Enable TMR1 overflow interrupt 
    
        on interrupt goto inthand
    
    loop: ' endless loop with 1 ms pause
        pause 1
        goto loop
    
        DISABLE
    inthand:
        loops = loops + 1 ' increment counter of interrupts
        serout 2, T2400, ["loops = ", #loops, 13, 10]
        TMR1H = $00       ' load timer1 with $00FF
        TMR1L = $FF
        PIR1 = $00        ' clear interrupt flags
        RESUME
        ENABLE
    
    End
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  3. #3
    Barry Johnson's Avatar
    Barry Johnson Guest


    Did you find this post helpful? Yes | No

    Default

    Thanks Bruce. I understand now what my error was and the code works.

Similar Threads

  1. Software PWM using Timer1
    By muqasim in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 18th December 2009, 11:49
  2. RPM With Timer1
    By mel4853 in forum mel PIC BASIC Pro
    Replies: 14
    Last Post: - 29th June 2009, 21:50
  3. Help with TIMER1 + SLEEP
    By Yodoad in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 22nd May 2009, 15:07
  4. Time Period of Timer1
    By arnol34 in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 4th May 2007, 00:31
  5. Timer1 and Interupt wierdness
    By mind in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 23rd August 2005, 01:24

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

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