Timer SubRoutine


Results 1 to 8 of 8

Threaded View

  1. #5


    Did you find this post helpful? Yes | No

    Default Re: Timer SubRoutine

    Maybe try using the TMR0 interrupt. You'll need to configure the TRIS and ANSEL registers to your specific peripherals. It's basically a clock that runs in the background. Every 60th of a second it goes to the dualcounter subroutine (or whatever subroutine you tell it to go to) without you having to call it so that you're free to write code to do other things. The last three bits of the OPTION_REG set up the prescaler which is what tells it how often to interrupt. I'm not exactly sure, but I think 1:64 is about 1/60th of a second. You may need to tweak that a bit though.

    Code:
    #config 
        __CONFIG _CP_OFF & _WDTE_OFF & _BOREN_OFF & _PWRTE_ON & _INTRC_OSC_NOCLKOUT & _MCLRE_OFF & _IOSCFS_8MHZ
    #endconfig
    
    ADCON0 = %00000000
    ADCON1 = %00110000
    ANSEL = %00000110
    define ADC_SAMPLEUS 50
    define ADC_BITS 8
    DEFINE ADC_CLOCK 3
    CM1CON0 = %00000000     
    CM2CON0 = %00000000     
    TRISA = %00001111        
    TRISC = %00000000
       
        
        ' Set TMR0 interrupt prescaler to 1:64
        OPTION_REG = %10000101        ' Set TMR0 configuration and enable PORTB pullups
        INTCON = %10100000           ' Enable TMR0 interrupts
        On Interrupt Goto dualcounter
    
    1minutecounter var word
    5minutecounter var word
    
    MAIN:
       
         ' insert main code here
    
         if 1minutecounter > 3600 then
              ' insert code to handle 1 minute timer here
              1minutecounter = 0          ' clear counter
         endif
    
         if 5minutecounter > 18000 then
              ' insert code to hand 5 minute time here
              5minutecounter = 0          'clear counter
         endif
    
    goto MAIN
    
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''INTERRUPT'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    
    Disable                                 ' Disable interrupts during interrupt handler
    dualcounter:                                    ' Interrupt routine to handle each timer tick      
        1minutecounter = 1minutecounter + 1            ' add 1 on every interrupt
        5minutecounter = 5minutecounter + 1
    tiexit: 
       INTCON.2 = 0                             ' Reset timer interrupt flag
       Resume
    enable
    end
    Last edited by keithv; - 13th September 2016 at 21:17.

Similar Threads

  1. Subroutine placement - must they come first?
    By BrianT in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 28th August 2012, 12:10
  2. Pulsin within subroutine
    By Wesley in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 10th January 2009, 23:05
  3. subroutine with hserin
    By volcane in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 19th December 2007, 02:56
  4. subroutine question
    By maus in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 11th September 2006, 21:36
  5. subroutine for picbasic
    By jojokatada in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 19th February 2005, 18:58

Members who have read this thread : 0

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