Timers !


Results 1 to 3 of 3

Thread: Timers !

Threaded View

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


    Did you find this post helpful? Yes | No

    Default

    If you enable interrupts, then you need an interrupt handler. If you prefer just to monitor the
    timer1 overflow flag, then disable global interrupts.

    Here's one example of monitoring the timer1 overflow flag without interrupts.

    Just replace the LCD port assignments & port settings to match your board. I tested this on
    a LAB-X1 board.

    Note: You can remove the TOGGLE 0 stuff. I used the output for a scope probe.
    Code:
    DEFINE OSC 4
    DEFINE LOADER_USED 1
    
    '=================================================
    'Setting LCD parameters
    '=================================================
     
    ' Define LAB-X1 LCD registers and bits
    Define  LCD_DREG        PORTD
    Define  LCD_DBIT        4
    Define  LCD_RSREG       PORTE
    Define  LCD_RSBIT       0
    Define  LCD_EREG        PORTE
    Define  LCD_EBIT        1
    Define  LCD_COMMANDUS   2000
    Define  LCD_DATAUS      50
    
    ticker VAR WORD
    T1LO CON $F1    ' constant values for 10mS timer1 overflows
    T1HI CON $D8    ' use with 1:1 prescale at 4MHz
    
    ticker = 0      ' clear ticker on power-up
    
    ADCON1 = 7      ' Set PORTA and PORTE to digital
    Low PORTE.2     ' LCD R/W line low (W)
    Pause 1000      ' Wait for LCD to start up
    
    '=================================================
    'Initialisation of PORT's
    '=================================================
    
    PORTB = 0
    TRISB=%11110000
    OPTION_REG.7=0  'Enable Pull-Up's on PORTB
    
    T1CON = 0       '1:1 prescale, internal clock
    PIE1.0 = 1      'enables the tmr1 overflow interrupt 
    PIR1.0 = 0      'resets tmr1 interrupt flag 
    INTCON.6 = 0    'disable peripheral interrupts
    INTCON.7 = 0    'disable global interrupts
    
    '=================================================
    'Main Program
    '=================================================
    
        pause 500 'initialise LCD
    
        LCDOUT $FE, 1, "Hello"
        LCDOUT $FE, $C0, "World"
    
        pause 2000 'displays the above for 5 seconds
    
        LCDOUT $fe,1
        LCDOUT "-- press button"
        
        TMR1H = T1HI   ' load timer1 for 10mS interrupts
        TMR1L = T1LO
        
    waitforstart:
        if PORTB.5 = 0 then 
          LCDOUT $FE, 1, "Timer started"
          T1CON.0 = 1 'starts timer
          goto loop
        endif
        goto waitforstart
        
    loop: ' loop waiting for timer1 overflow
        WHILE PIR1.0=0  ' timer1 overflow?
        WEND            ' no, keep waiting
        PIR1.0 = 0      ' clear overflow flag
        TOGGLE 0        ' 10mS pulse on RB0
        ticker = ticker + 1 ' inc ticker
        LCDout $FE,$C0,DEC5 ticker ' display ticker
        
        ' timer1 is still ticking, so we'll stop it, then add
        ' in our value for 10mS interrupts
        T1CON.0 = 0           ' stop timer1
        TMR1L = TMR1L + T1LO  ' add in low byte
        IF TMR1L < T1LO THEN  ' did low byte overflow?
           TMR1H = TMR1H + 1  ' yes, increment high byte
        ENDIF
        TMR1H = TMR1H + T1HI  ' no, just add in high byte
        T1CON.0=1             ' start timer1 
        GOTO Loop
        
        END
    If you want to use interrupts, here's another version using ON INTERRUPT,
    Code:
    DEFINE OSC 4
    DEFINE LOADER_USED 1
    
    '=================================================
    'Setting LCD parameters
    '=================================================
     
    ' Define LAB-X1 LCD registers and bits
    Define  LCD_DREG        PORTD
    Define  LCD_DBIT        4
    Define  LCD_RSREG       PORTE
    Define  LCD_RSBIT       0
    Define  LCD_EREG        PORTE
    Define  LCD_EBIT        1
    Define  LCD_COMMANDUS   2000
    Define  LCD_DATAUS      50
    
    ticker VAR WORD
    T1LO CON $F1    ' constant values for 10mS interrupts with
    T1HI CON $D8    ' timer1 1:1 prescale and 4MHz osc
    
    ticker = 0      ' clear ticker on power-up
    
    ADCON1 = 7      ' Set PORTA and PORTE to digital
    Low PORTE.2     ' LCD R/W line low (W)
    Pause 1000      ' Wait for LCD to start up
    
    '=================================================
    'Initialisation of PORT's
    '=================================================
    
    PORTB = 0
    TRISB=%11110000
    OPTION_REG.7=0  'Enable Pull-Up's on PORTB
    
    T1CON = 0       '1:1 prescale, internal clock
    PIE1.0 = 1      'enables the tmr1 overflow interrupt 
    PIR1.0 = 0      'resets tmr1 interrupt flag 
    INTCON.6 = 1    'enables all unmasked peripheral interrupts
    INTCON.7 = 1    'enables global interrupts
    
    '=================================================
    'Main Program
    '=================================================
    
        pause 500 'initialise LCD
    
        LCDOUT $FE, 1, "Hello"
        LCDOUT $FE, $C0, "World"
    
        pause 2000 'displays the above for 5 seconds
    
        LCDOUT $fe,1
        LCDOUT "-- press button"
        
        TMR1H = T1HI   ' load timer1 for 10mS interrupts
        TMR1L = T1LO
        
    waitforstart:
        if PORTB.5 = 0 then 
          LCDOUT $FE, 1, "Timer started"
          T1CON.0 = 1 'starts timer
          goto loop
        endif
        goto waitforstart
    
       ON INTERRUPT goto BasicInt
        
    loop: ' loop waiting for interrupt & display updates
        @ NOP
        GOTO Loop
        
        ' update & display ticker on each interrupt
        DISABLE
    BasicInt:
        TOGGLE 0              ' toggle RB0 every 10mS
        ticker = ticker + 1   ' increment ticker
        LCDout $FE,$C0,DEC5 ticker  ' display ticker
        
        ' timer1 is still ticking, so we'll stop it, then add
        ' in our value for 10mS interrupts
        T1CON.0 = 0           ' stop timer1
        PIR1.0 = 0            ' clear overflow flag
        TMR1L = TMR1L + T1LO  ' add in low byte
        IF TMR1L < T1LO THEN  ' did low byte overflow?
           TMR1H = TMR1H + 1  ' yes, increment high byte
        ENDIF
        TMR1H = TMR1H + T1HI  ' no, just add in high byte
        T1CON.0=1             ' start timer1 
        RESUME
        ENABLE
        
        END
    Once you understand how these work, you can go for the big guns, and use Darrel Taylors'
    instant interrupts here http://www.picbasic.co.uk/forum/showthread.php?t=3251

    Then you'll really start having fun...;o}
    Last edited by Bruce; - 22nd August 2007 at 02:06.
    Regards,

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

Similar Threads

  1. Timers
    By mitchf14 in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 15th November 2008, 20:08
  2. Reading Timers
    By kevj in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 27th August 2007, 08:19
  3. hardware timers
    By Adam in forum General
    Replies: 3
    Last Post: - 7th March 2007, 00:10
  4. Availability of PIC timers from PBP
    By coda64 in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 8th February 2006, 07:18
  5. Count Down Timers
    By CocaColaKid in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 28th October 2005, 17:34

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