Timer SubRoutine


Closed Thread
Results 1 to 8 of 8

Hybrid View

  1. #1


    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 22:17.

  2. #2


    Did you find this post helpful? Yes | No

    Default Re: Timer SubRoutine

    what speed is the PIC clock? (thats what drives the timers in most cases)
    George

  3. #3


    Did you find this post helpful? Yes | No

    Default Re: Timer SubRoutine

    Quote Originally Posted by towlerg View Post
    what speed is the PIC clock? (thats what drives the timers in most cases)
    I believe the 16F616 has options for either 4MHz or 8MHz internal clock.

  4. #4


    Did you find this post helpful? Yes | No

    Default Re: Timer SubRoutine

    Using the Mikro timer calculator @ 8MHz
    Code:
    'Timer1
    'Prescaler 1:4; TMR1 Preload = 15536; Actual Interrupt Time : 100 ms
     
    'Place/Copy this part in declaration section
    sub procedure InitTimer1()
      T1CON	 = 0x21
      TMR1IF_bit	 = 0
      TMR1H	 = 0x3C
      TMR1L	 = 0xB0
      TMR1IE_bit	 = 1
      INTCON	 = 0xC0
    end sub
     
    sub procedure Interrupt()
      if (TMR1IF_bit) then
        TMR1IF_bit = 0
        TMR1H	 = 0x3C
        TMR1L	 = 0xB0
        'Enter your code here
      end if
    end sub
    Of course the syntax is wrong but the values are correct. I couldn't get 1 second so I did 0.1 second. Same principle count to 10 for one sec and 50 for five sec.
    George

Similar Threads

  1. Subroutine placement - must they come first?
    By BrianT in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 28th August 2012, 13:10
  2. Pulsin within subroutine
    By Wesley in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 11th January 2009, 00:05
  3. subroutine with hserin
    By volcane in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 19th December 2007, 03:56
  4. subroutine question
    By maus in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 11th September 2006, 22:36
  5. subroutine for picbasic
    By jojokatada in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 19th February 2005, 19: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