How On Interrupt works?


Closed Thread
Results 1 to 13 of 13
  1. #1

    Default How On Interrupt works?

    Good day Picers

    Where can I get a simple explanation or sample how "On Interrupt" works with Serial communication (HSERIN / HSEROUT)?

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


    Did you find this post helpful? Yes | No

  3. #3
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default

    http://www.picbasic.co.uk/forum/ could also be helpful.


    ____________
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  4. #4


    Did you find this post helpful? Yes | No

    Default

    Bruce, thank you very much for your sample file.
    I have tried to create an easier implementation, just to see if it is working on my circuit.
    I have a TX circuit and an RX circuit, using RS485.

    Below is my code. The LED on the TX is blinking, but not on the RX side.
    Any idea what I am doing wrong?

    <b>TX Code</b>
    <hr>
    <code>
    &nbsp;&nbsp;Include "Modedefs.bas"

    &nbsp;&nbsp;DEFINE&nbsp;&nbsp;&nbsp;&nbsp; OSC 4

    &nbsp;&nbsp;DE_OR_RE&nbsp;&nbsp;&nbsp;&nbsp;VAR PORTC.5&nbsp;&nbsp;&nbsp;&nbsp; ' DE and RE Pin of SN75176
    &nbsp;&nbsp;LEDPIN&nbsp;&nbsp;&nbsp;&nbsp; VAR PORTD.2&nbsp;&nbsp;&nbsp;&nbsp; ' LED to confirm PIC is running
    &nbsp;&nbsp;DATARECEIVED&nbsp;&nbsp;VAR BYTE
    &nbsp;&nbsp;
    &nbsp;&nbsp;'======== HSEROUT, HSERIN SETTINGS ==========
    &nbsp;&nbsp;DEFINE HSER_RCSTA 90h
    &nbsp;&nbsp;DEFINE HSER_TXSTA 24h
    &nbsp;&nbsp;DEFINE HSER_BAUD 9600

    &nbsp;&nbsp;ADCON1 = 7

    &nbsp;&nbsp;RCIF&nbsp;&nbsp;VAR&nbsp;&nbsp;PIR1.5& nbsp;&nbsp;&nbsp;&nbsp;' Alias RCIF (USART Receive Interrupt Flag)
    &nbsp;&nbsp;OERR&nbsp;&nbsp;VAR&nbsp;&nbsp;RCSTA.1 &nbsp;&nbsp;&nbsp;&nbsp;' Alias OERR (USART Overrun Error Flag)
    &nbsp;&nbsp;CREN&nbsp;&nbsp;VAR&nbsp;&nbsp;RCSTA.4 &nbsp;&nbsp;&nbsp;&nbsp;' Alias CREN (USART Continuous Receive Enable)
    &nbsp;&nbsp;
    Main:
    &nbsp;&nbsp;HIGH DE_OR_RE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ' Make ready for RX
    &nbsp;&nbsp;HSEROUT ["1"]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp; ' Send R1
    &nbsp;&nbsp;HIGH LEDPIN
    &nbsp;&nbsp;PAUSE 5000
    &nbsp;&nbsp;LOW LEDPIN
    &nbsp;&nbsp;PAUSE 5000
    GOTO MAIN

    END
    </code>


    <b>RX Code</b>
    <hr>
    <code>
    &nbsp;&nbsp;Include "Modedefs.bas"
    &nbsp;&nbsp;&nbsp;&nbsp;
    &nbsp;&nbsp;DEFINE&nbsp;&nbsp;&nbsp;&nbsp; OSC 4

    &nbsp;&nbsp;DE_OR_RE&nbsp;&nbsp;&nbsp;&nbsp;VAR PORTC.5&nbsp;&nbsp;&nbsp;&nbsp; ' DE and RE Pin of SN75176 (RS485)
    &nbsp;&nbsp;LEDPIN&nbsp;&nbsp;&nbsp;&nbsp; VAR PORTD.2&nbsp;&nbsp;&nbsp;&nbsp; ' LED Pin
    &nbsp;&nbsp;i&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p; VAR BYTE

    &nbsp;&nbsp;'======== HSEROUT, HSERIN SETTINGS ==========&nbsp;&nbsp;
    &nbsp;&nbsp;DEFINE HSER_RCSTA 90h
    &nbsp;&nbsp;DEFINE HSER_TXSTA 24h
    &nbsp;&nbsp;DEFINE HSER_BAUD 9600

    &nbsp;&nbsp;ADCON1 = 7

    &nbsp;&nbsp;RCIF&nbsp;&nbsp;VAR&nbsp;&nbsp;PIR1.5& nbsp;&nbsp;&nbsp;&nbsp;' Alias RCIF (USART Receive Interrupt Flag)
    &nbsp;&nbsp;OERR&nbsp;&nbsp;VAR&nbsp;&nbsp;RCSTA.1 &nbsp;&nbsp;&nbsp;&nbsp;' Alias OERR (USART Overrun Error Flag)
    &nbsp;&nbsp;CREN&nbsp;&nbsp;VAR&nbsp;&nbsp;RCSTA.4 &nbsp;&nbsp;&nbsp;&nbsp;' Alias CREN (USART Continuous Receive Enable)

    &nbsp;&nbsp;LOW DE_OR_RE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;' Make ready for RX
    &nbsp;&nbsp;INTCON = %11000000&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp ;&nbsp;&nbsp;&nbsp;' Enable interrupts
    &nbsp;&nbsp;ON INTERRUPT GoTo serialin&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' Declare interrupt handler routine
    &nbsp;&nbsp;PIE1.5 = 1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;& nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' Enable interrupt on USART

    DoSomeStuff:
    &nbsp;&nbsp;For i = 0 to 10
    &nbsp;&nbsp;&nbsp;&nbsp;Pause 2
    &nbsp;&nbsp;Next i
    &nbsp;&nbsp;GOTO DoSomeStuff
    &nbsp;&nbsp;
    serialin:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp ;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;& nbsp;' Buffer the character received
    &nbsp;&nbsp;GOSUB BLINK
    &nbsp;&nbsp;RESUME
    &nbsp;&nbsp;
    BLINK:
    &nbsp;&nbsp;HIGH LEDPIN
    &nbsp;&nbsp;PAUSE 1000
    &nbsp;&nbsp;LOW LEDPIN
    &nbsp;&nbsp;PAUSE 1000
    &nbsp;&nbsp;RESUME

    END

    </code>

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


    Did you find this post helpful? Yes | No

    Default

    Lookup disable in your manual under ON INTERRUPT. Then lookup RETURN under GOSUB. That will get you started.
    Regards,

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

  6. #6


    Did you find this post helpful? Yes | No

    Default

    Thank you!!

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


    Did you find this post helpful? Yes | No

    Default

    You're welcome, but you'll want to consider a few other things here too.

    1. RCIF is only cleared after a read of RCREG. To clear RCIF, you will want to read this data
    with HSERIN or X = RCREG until RCIF is cleared.

    2. Values you use in hardware USART defines are not loaded into USART registers if there is
    not at least one instance of HSERIN or HSEROUT used somewhere within your program.

    Just inserting these defines is not configuring the hardware USART for you. Just FYI.
    Regards,

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

  8. #8
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Bruce View Post
    You're welcome, but you'll want to consider a few other things here too.

    1. RCIF is only cleared after a read of RCREG. To clear RCIF, you will want to read this data
    with HSERIN or X = RCREG until RCIF is cleared.

    2. Values you use in hardware USART defines are not loaded into USART registers if there is
    not at least one instance of HSERIN or HSEROUT used somewhere within your program.

    Just inserting these defines is not configuring the hardware USART for you. Just FYI.
    Hi Bruce,
    That's good info! Thanks, How is that book coming? Sign me up for a copy
    JS
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  9. #9


    Did you find this post helpful? Yes | No

    Default

    Where in the Picbasic Manual can I get more info on what <b>INTCON</b> means and how to determine its value?

    Thank you
    Koossa

  10. #10


    Did you find this post helpful? Yes | No

    Default

    Here is my new code, but still the RX LED does not work.

    <hr>
    <b>Code for TX</b>
    <hr>
    <code>
    &nbsp;&nbsp;Include "Modedefs.bas"

    &nbsp;&nbsp;DEFINE&nbsp;&nbsp;&nbsp;&nbsp; OSC 4

    &nbsp;&nbsp;DE_OR_RE&nbsp;&nbsp;&nbsp;&nbsp;VAR PORTC.5&nbsp;&nbsp;&nbsp;&nbsp; ' DE and RE Pin of SN75176
    &nbsp;&nbsp;LEDPIN&nbsp;&nbsp;&nbsp;&nbsp; VAR PORTD.2&nbsp;&nbsp;&nbsp;&nbsp; ' LED to confirm PIC is running
    &nbsp;&nbsp;DATARECEIVED&nbsp;&nbsp;VAR BYTE
    &nbsp;&nbsp;
    &nbsp;&nbsp;'======== HSEROUT, HSERIN SETTINGS ==========
    &nbsp;&nbsp;DEFINE HSER_RCSTA 90h
    &nbsp;&nbsp;DEFINE HSER_TXSTA 24h
    &nbsp;&nbsp;DEFINE HSER_BAUD 9600

    &nbsp;&nbsp;ADCON1 = 7

    &nbsp;&nbsp;RCIF&nbsp;&nbsp;VAR&nbsp;&nbsp;PIR1.5& nbsp;&nbsp;&nbsp;&nbsp;' Alias RCIF (USART Receive Interrupt Flag)
    &nbsp;&nbsp;OERR&nbsp;&nbsp;VAR&nbsp;&nbsp;RCSTA.1 &nbsp;&nbsp;&nbsp;&nbsp;' Alias OERR (USART Overrun Error Flag)
    &nbsp;&nbsp;CREN&nbsp;&nbsp;VAR&nbsp;&nbsp;RCSTA.4 &nbsp;&nbsp;&nbsp;&nbsp;' Alias CREN (USART Continuous Receive Enable)
    &nbsp;&nbsp;
    Main:
    &nbsp;&nbsp;HIGH DE_OR_RE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ' Make ready for RX
    &nbsp;&nbsp;HSEROUT ["1"]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp; ' Send R1
    &nbsp;&nbsp;HIGH LEDPIN
    &nbsp;&nbsp;PAUSE 5000
    &nbsp;&nbsp;LOW LEDPIN
    &nbsp;&nbsp;PAUSE 5000
    GOTO MAIN

    END
    </code>


    <hr>
    <b>Code for RX</b>
    <hr>
    <code>
    &nbsp;&nbsp;Include "Modedefs.bas"
    &nbsp;&nbsp;&nbsp;&nbsp;
    &nbsp;&nbsp;DEFINE&nbsp;&nbsp;&nbsp;&nbsp; OSC 4

    &nbsp;&nbsp;DE_OR_RE&nbsp;&nbsp;&nbsp;&nbsp;VAR PORTC.5&nbsp;&nbsp;&nbsp;&nbsp; ' DE and RE Pin of SN75176 (RS485)
    &nbsp;&nbsp;LEDPIN&nbsp;&nbsp;&nbsp;&nbsp; VAR PORTD.2&nbsp;&nbsp;&nbsp;&nbsp; ' LED Pin
    &nbsp;&nbsp;i&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p; VAR BYTE
    &nbsp;&nbsp;DATARECEIVED&nbsp;&nbsp;VAR BYTE

    &nbsp;&nbsp;'======== HSEROUT, HSERIN SETTINGS ==========&nbsp;&nbsp;
    &nbsp;&nbsp;DEFINE HSER_RCSTA 90h
    &nbsp;&nbsp;DEFINE HSER_TXSTA 24h
    &nbsp;&nbsp;DEFINE HSER_BAUD 9600

    &nbsp;&nbsp;ADCON1 = 7

    &nbsp;&nbsp;RCIF&nbsp;&nbsp;VAR&nbsp;&nbsp;PIR1.5& nbsp;&nbsp;&nbsp;&nbsp;' Alias RCIF (USART Receive Interrupt Flag)
    &nbsp;&nbsp;OERR&nbsp;&nbsp;VAR&nbsp;&nbsp;RCSTA.1 &nbsp;&nbsp;&nbsp;&nbsp;' Alias OERR (USART Overrun Error Flag)
    &nbsp;&nbsp;CREN&nbsp;&nbsp;VAR&nbsp;&nbsp;RCSTA.4 &nbsp;&nbsp;&nbsp;&nbsp;' Alias CREN (USART Continuous Receive Enable)

    &nbsp;&nbsp;LOW DE_OR_RE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;' Make ready for RX
    &nbsp;&nbsp;INTCON = %11000000&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp ;&nbsp;&nbsp;&nbsp; ' Enable interrupts
    &nbsp;&nbsp;ON INTERRUPT GoTo SerialDataReceived ' Declare interrupt handler routine
    &nbsp;&nbsp;PIE1.5 = 1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;& nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ' Enable interrupt on USART

    DoSomeStuff:
    &nbsp;&nbsp;For i = 0 to 10
    &nbsp;&nbsp;&nbsp;&nbsp;Pause 2
    &nbsp;&nbsp;Next i
    &nbsp;&nbsp;GOTO DoSomeStuff


    &nbsp;&nbsp;DISABLE&nbsp;&nbsp;
    SerialDataReceived:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;& nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;&nbsp;&nbsp;' Buffer the character received
    &nbsp;&nbsp;HSerin [DATARECEIVED]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp ;' Read USART and store character to next empty location
    &nbsp;&nbsp;GOSUB BLINK
    &nbsp;&nbsp;IF RCIF Then SerialDataReceived
    &nbsp;&nbsp;RESUME
    &nbsp;&nbsp;ENABLE
    &nbsp;&nbsp;
    &nbsp;&nbsp;
    BLINK:
    &nbsp;&nbsp;HIGH LEDPIN
    &nbsp;&nbsp;PAUSE 1000
    &nbsp;&nbsp;LOW LEDPIN
    &nbsp;&nbsp;PAUSE 1000
    &nbsp;&nbsp;RETURN

    END
    </code>

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


    Did you find this post helpful? Yes | No

    Default

    Information on INTCON will be found in the PIC data sheet. Not your PBP manual.

    Do NOT GOSUB to another routine outside the DISABLE / ENABLE block in your interrupt handler.

    After the ENABLE, PBP begins placing more code to jump to the interrupt check routine. So what happens is you jump out to BLINK, you land smack on code that sees the interrupt has not been serviced, and bingo, you're sent right back to SerialDataReceived before code in the BLINK routine can execute.

    Keep all code in your interrupt handler in the protected area between the DISABLE/ENABLE block, or simply remove ENABLE. If SerialDataReceived and BLINK are the last routines in program memory, then you don't even need the ENABLE. This tells PBP to start inserting code before & after each PBP statement to test for the interrupt condition.

    Try this;
    Code:
     DISABLE  
    SerialDataReceived:                ' Buffer the character received
      HSerin [DATARECEIVED]        ' Read USART and store character to next empty location
      IF RCIF Then SerialDataReceived
      HIGH LEDPIN
      PAUSE 1000
      LOW LEDPIN
      PAUSE 1000
      RESUME
    
      END
    Last edited by Bruce; - 22nd February 2008 at 12:47.
    Regards,

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

  12. #12


    Did you find this post helpful? Yes | No

    Default

    Thank you very much Bruce!!!

  13. #13


    Did you find this post helpful? Yes | No

    Default

    It is working now!!!
    Thank you very much for your help Bruce!!!!

Similar Threads

  1. Won't go back to SLEEP after 1st Interrupt
    By jellis00 in forum mel PIC BASIC Pro
    Replies: 32
    Last Post: - 29th June 2009, 09:00
  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. Interrupt Problem
    By Kamikaze47 in forum mel PIC BASIC Pro
    Replies: 15
    Last Post: - 16th November 2005, 20:58
  5. USART interrupt not interrupting right
    By Morpheus in forum mel PIC BASIC Pro
    Replies: 12
    Last Post: - 6th March 2005, 01:07

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