+ Reply to Thread
Results 1 to 6 of 6
  1. #1
    Join Date
    Dec 2003
    Location
    Storrs, Ct.
    Posts
    91

    Default TMR1 interrupt question

    Thank you Darrel Taylor for steering me in this direction with your Instant Interrupts - Revisited thread. I tried your LED blink with a 16f876 and it worked great.
    I now would like to add some other functions to the main program and would expect the LED to continue merrily on it's way while the program executes.
    I found, however, this wasn't the case.
    I'm trying to use the interrupt handler to increment a VAR which I then use with a case select for the output to the LCD.
    The LED skips a beat occasionally and the output to the LCD is very inconsistent, as if only part of the output reaches the LCD before being interrupted.
    Is this the wrong application for the interrupt?
    Am I way off base here?
    Pardon the code as most of it is cut and paste snippetts from other programs.


    DEFINE LOADER_USED 1 'Only required if bootloader used to program PIC
    define OSC 4

    INCLUDE "modedefs.bas"
    INCLUDE "DT_INTS-14.bas" ' Base Interrupt System
    INCLUDE "ReEnterPBP.bas" ' Include if using PBP interrupts

    'OPTION_REG.7 = 0
    'ADCON1 = 7 ' PORTA digital

    PORTA = %00000000 'Set all INPUTS to 0
    TRISA = %00111111 'Set PORTA RA0-RA5 to input
    PORTB = %00000000 'Set all outputs to 0 (off)
    TRISB = %00000000 'Set PORTB to all output
    PORTC = %00000000 'Set all outputs to 0 (off)
    TRISC = %00000000 'Set PORTC to all output

    '::::::::::::::::::::::::::::::::::::::::::::::::: :::::::::::::::::::::::::::::
    ' Alias pins
    '::::::::::::::::::::::::::::::::::::::::::::::::: :::::::::::::::::::::::::::::
    scl VAR PORTA.1 'clock line for temp sensors
    sda VAR PORTA.0 'data line for temp sensors
    Relay1 var PORTB.7 'relay one is connected here
    Relay2 var PORTB.6 'relay two is connected here
    Relay3 var PORTB.5 'relay three is connected here
    Relay4 var PORTB.4 'relay four is connected here
    Relay5 var PORTB.3 'relay five is connected here
    Relay6 var PORTB.2 'relay six is connected here
    Relay7 var PORTB.1 'relay seven is connected here
    Relay8 var PORTC.5 'relay eight is connected here
    RST VAR PORTC.1 'reset line for RTC
    IO VAR PORTC.3 'data line for RTC
    SCLK VAR PORTC.2 'clock line for RTC
    led1 var PORTC.0 'heartbeat LED
    '::::::::::::::::::::::::::::::::::::::::::::::::: :::::::::::::::::::::::::::::
    ' Define Variables
    '::::::::::::::::::::::::::::::::::::::::::::::::: :::::::::::::::::::::::::::::
    rtcyear VAR BYTE 'year value to and from RTC BCD
    rtcday VAR BYTE 'day value to and from RTC BCD
    rtcmonth VAR BYTE 'month value to and from RTC BCD
    rtcdate VAR BYTE 'date value to and from RTC BCD
    rtchr VAR BYTE 'hour value to and from RTC BCD
    rtcmin VAR BYTE 'minute value to and from RTC BCD
    rtcsec VAR BYTE 'seconds value to and from RTC BCD
    rtccontrol VAR BYTE '12/24 hour select for RTC 24hr selected
    k var byte 'holding variable for conversions
    display VAR byte 'count var for serout, udp, and lcd update
    powerfail var bit 'lets me know the power failed
    decyear VAR BYTE 'decimal year value from siteplayer
    decday var byte 'decimal day value from siteplayer
    decmonth VAR BYTE 'decimal month value from siteplayer
    decdate VAR BYTE 'decimal date value from siteplayer
    dechr VAR BYTE 'decimal hour value from siteplayer
    decmin VAR BYTE 'decimal minute value from siteplayer
    decsec VAR BYTE 'decimal seconds value from siteplayer

    '((((((((((((((((((((((((((((((((((((((()))))))))) )))))))))))))))))))))))))))))
    ' Clear LCD and pause to start
    '((((((((((((((((((((((((((((((((((((((()))))))))) )))))))))))))))))))))))))))))

    Pause 5000
    Serout2 PORTC.4,84,[$fe, 1]
    Pause 1000
    Serout2 PORTC.4,84,[$fe, 1]
    Pause 1000
    Serout2 PORTC.4,84,[$fe,1,$fe,128,"START"]
    Pause 1500
    'powerfail = 1

    ASM
    INT_LIST macro ; IntSource, Label, Type, ResetFlag?
    INT_Handler TMR1_INT, _ToggleLED1, PBP, yes
    endm
    INT_CREATE ; Creates the interrupt processor

    INT_ENABLE TMR1_INT ; enable external (INT) interrupts
    ENDASM

    T1CON = $31 ; Prescaler = 8, TMR1ON
    @ INT_ENABLE TMR1_INT ; enable Timer 1 interrupts

    '((((((((((((((((((((((((((((((((((((((()))))))))) )))))))))))))))))))))))))))))
    ' Main program loop - updates the LCD and siteplayer with the time
    'and status of temperatures and pumps
    '((((((((((((((((((((((((((((((((((((((()))))))))) )))))))))))))))))))))))))))))

    mainloop:

    GoSub gettime ' Reads time from the DS1302 RT clock
    'GoSub showtime ' update siteplayer with the real time
    GoSub showonlcd

    goto mainloop
    '((((((((((((((((((((((((((((((((((((((()))))))))) )))))))))))))))))))))))))))))
    ' Subroutine to read time from RTC
    '((((((((((((((((((((((((((((((((((((((()))))))))) )))))))))))))))))))))))))))))
    gettime:
    RST = 1 ' Ready for transfer
    ShiftOut IO, SCLK, LSBFIRST, [$bf] ' Read all 8 RTC registers in burst mode
    ShiftIn IO, SCLK, LSBPRE, [ rtcsec, rtcmin, rtchr, rtcdate, rtcmonth, rtcday, rtcyear, rtccontrol]
    RST = 0 ' Reset RTC
    Return

    '((((((((((((((((((((((((((((((((((((((()))))))))) )))))))))))))))))))))))))))))
    'Subroutine to convert from Hex to Dec
    '((((((((((((((((((((((((((((((((((((((()))))))))) )))))))))))))))))))))))))))))
    h2d: ' Convert Hex coded time data -> decimal data
    K = (K & $F )+((K>>4)*10)
    Return
    '((((((((((((((((((((((((((((((((((((((()))))))))) )))))))))))))))))))))))))))))
    'Subroutine to convert from Dec to Hex
    '((((((((((((((((((((((((((((((((((((((()))))))))) )))))))))))))))))))))))))))))
    d2h: ' Convert Decimal -> Hex coded time data
    K = (K DIG 1) * $10 + (K DIG 0)
    Return
    '---[INT - interrupt handler]---------------------------------------------------
    ToggleLED1:
    TOGGLE LED1
    DISPLAY = DISPLAY + 1
    @ INT_RETURN

    '((((((((((((((((((((((((((((((((((((((()))))))))) )))))))))))))))))))))))))))))
    'Subroutine to display time on LCD and write current time to Siteplayer
    '((((((((((((((((((((((((((((((((((((((()))))))))) )))))))))))))))))))))))))))))
    showtime:
    K = rtcyear
    GoSub h2d
    decyear = K
    K = rtcmonth
    GoSub h2d
    decmonth = K
    K = rtcdate
    GoSub h2d
    decdate = K
    K = rtchr
    GoSub h2d
    dechr = K
    K = rtcmin
    GoSub h2d
    decmin = K
    K = rtcsec
    GoSub h2d
    decsec = K
    'SerOut2 PORTC.6,84, [$00, $82, $12, dechr, rtcday, decmin]
    'SerOut2 PORTC.6,84, [$00, $81, $17,decyear, decsec]
    'SerOut2 PORTC.6,84, [$00, $81, $1e, decmonth, decdate]

    RETURN
    '((((((((((((((((((((((((((((((((((((((()))))))))) )))))))))))))))))))))))))))))
    'Subroutine to display time on LCD
    '((((((((((((((((((((((((((((((((((((((()))))))))) )))))))))))))))))))))))))))))
    showonlcd:
    select case display
    case 11
    Serout2 PORTC.4,84, [$fe, 1]
    Serout2 PORTC.4,84, [$fe, 129, HEX2 rtchr, ":", HEX2 rtcmin,_
    ":", HEX2 rtcsec," ","DAY",HEX2 rtcday]
    Serout2 PORTC.4,84, [$fe, 196, HEX2 rtcmonth,_
    "/", HEX2 rtcdate, "/" , HEX2 rtcyear]

    case 21
    Serout2 PORTC.4,84, [$fe, 1]
    Serout2 PORTC.4,84, [$fe,128,"slot 2"]
    case 31
    Serout2 PORTC.4,84, [$fe, 1]
    Serout2 PORTC.4,84, [$fe,128,"slot 3"]
    case 41
    Serout2 PORTC.4,84, [$fe, 1]
    Serout2 PORTC.4,84, [$fe,128,"slot 4"]
    case 51
    Serout2 PORTC.4,84, [$fe, 1]
    Serout2 PORTC.4,84, [$fe,128,"slot 5"]
    case 61
    Serout2 PORTC.4,84, [$fe, 1]
    Serout2 PORTC.4,84, [$fe,128,"slot 6"]
    display = 0
    end select
    if display > 61 then display = 0
    RETURN
    '((((((((((((((((((((((((((((((((((((((()))))))))) )))))))))))))))))))))))))))))
    '
    '((((((((((((((((((((((((((((((((((((((()))))))))) )))))))))))))))))))))))))))))
    goto mainloop
    end


    Thanks in advance for any input you may have.

  2. #2
    Join Date
    Dec 2003
    Location
    Storrs, Ct.
    Posts
    91

    Talking Got it. The old disable interrupt trick.

    INTCON.7 = 0 before the section to be executed and
    INTCON.7 = 1 after the section to be executed worked fine

  3. #3
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959

    Default

    Hi ronjodu,

    I'm glad you figured it out.

    The way I like to put it is ...

    The only problem with Interrupts, is that they Interrupt things.

    And, any statement that does it's timing in software (SerIn/Out, OWin/out, Debug, Freqout, Pulsin/out, etc) will have that timing disturbed by the interrupts. Synchronous commands like LCDOUT, I2C and ShifIn/Out don't care.

    Had you been using a paralel interface to the LCD, it wouldn't have a problem.

    However, As you've found out with Serout, 1 easy way is to just turn off GIE before those types of statements. But that can cause you to lose interrupts. I'm sure at some point you'll be doing more than just blinking lights, and the problem will become more evident.

    If you use the USART on the 16F876 with HSEROUT, then you won't need to turn off the interrupts while sending serial data, and you won't lose any interrupts.

    HTH,
      Darrel

  4. #4
    Join Date
    Dec 2003
    Location
    Storrs, Ct.
    Posts
    91

    Default Hserout

    I have tried the HSEROUT for the LCD and had trouble making it work. Serout2 was much easier to get working at the time. I guess I'll have to reinvestigate the HSEROUT option again.
    Is there an easy way for a beginner to tell which statements are affected by an interrupt and which are not. (trial and error perhaps?)

  5. #5
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959

    Default

    These PBP statements will be affected by the use of interrupts.
    DEBUG/IN
    DTMFOUT
    FREQOUT
    NAP
    OWIN/OUT
    PAUSE/US
    POT
    PULSIN/OUT
    PWM
    RCTIME
    SERIN/OUT
    SLEEP
    SOUND
    WRITE
    WRITECODE
    XIN/OUT
    I think I got them all. If anyone sees one I missed, please chime in.
    <br>
    DT

  6. #6
    Join Date
    Dec 2003
    Location
    Storrs, Ct.
    Posts
    91

    Default Thanks again

    Thanks again, I've included a note in my manual on each statement.

Similar Threads

  1. Interrupt question
    By ultiblade in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 26th November 2009, 20:12
  2. Can't ID interrupt source with this IntHandler??
    By jellis00 in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 3rd June 2009, 02:35
  3. Help with Analog Interrupt
    By brid0030 in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 13th February 2008, 18:14
  4. USART interrupt not interrupting right
    By Morpheus in forum mel PIC BASIC Pro
    Replies: 12
    Last Post: - 6th March 2005, 01:07
  5. Interrupt question
    By stone in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 25th January 2005, 23:11

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