PDA

View Full Version : How On Interrupt works?



koossa
- 9th February 2008, 09:25
Good day Picers

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

Bruce
- 9th February 2008, 15:33
http://www.microengineeringlabs.com/resources/samples/x1/pbp/serbufx.bas

sayzer
- 9th February 2008, 15:54
http://www.picbasic.co.uk/forum/ could also be helpful.

:)
____________

koossa
- 21st February 2008, 14:21
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;&nbsp;&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;&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;&nbsp;&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>

Bruce
- 21st February 2008, 14:37
Lookup disable in your manual under ON INTERRUPT. Then lookup RETURN under GOSUB. That will get you started.

koossa
- 21st February 2008, 15:02
Thank you!!

Bruce
- 21st February 2008, 15:13
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.

Archangel
- 21st February 2008, 19:58
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

koossa
- 22nd February 2008, 08:55
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

koossa
- 22nd February 2008, 09:34
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;&nbsp;&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;&nbsp; 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;&nbsp;&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;&nbsp;&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>

Bruce
- 22nd February 2008, 12:26
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;


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

koossa
- 22nd February 2008, 12:51
Thank you very much Bruce!!!

koossa
- 22nd February 2008, 13:50
It is working now!!!
Thank you very much for your help Bruce!!!!