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>
Include "Modedefs.bas"
DEFINE OSC 4
DE_OR_RE VAR PORTC.5 ' DE and RE Pin of SN75176
LEDPIN VAR PORTD.2 ' LED to confirm PIC is running
DATARECEIVED VAR BYTE
'======== HSEROUT, HSERIN SETTINGS ==========
DEFINE HSER_RCSTA 90h
DEFINE HSER_TXSTA 24h
DEFINE HSER_BAUD 9600
ADCON1 = 7
RCIF VAR PIR1.5 ' Alias RCIF (USART Receive Interrupt Flag)
OERR VAR RCSTA.1 ' Alias OERR (USART Overrun Error Flag)
CREN VAR RCSTA.4 ' Alias CREN (USART Continuous Receive Enable)
Main:
HIGH DE_OR_RE ' Make ready for RX
HSEROUT ["1"] ' Send R1
HIGH LEDPIN
PAUSE 5000
LOW LEDPIN
PAUSE 5000
GOTO MAIN
END
</code>
<b>RX Code</b>
<hr>
<code>
Include "Modedefs.bas"
DEFINE OSC 4
DE_OR_RE VAR PORTC.5 ' DE and RE Pin of SN75176 (RS485)
LEDPIN VAR PORTD.2 ' LED Pin
i VAR BYTE
'======== HSEROUT, HSERIN SETTINGS ==========
DEFINE HSER_RCSTA 90h
DEFINE HSER_TXSTA 24h
DEFINE HSER_BAUD 9600
ADCON1 = 7
RCIF VAR PIR1.5 ' Alias RCIF (USART Receive Interrupt Flag)
OERR VAR RCSTA.1 ' Alias OERR (USART Overrun Error Flag)
CREN VAR RCSTA.4 ' Alias CREN (USART Continuous Receive Enable)
LOW DE_OR_RE ' Make ready for RX
INTCON = %11000000 ' Enable interrupts
ON INTERRUPT GoTo serialin ' Declare interrupt handler routine
PIE1.5 = 1 ' Enable interrupt on USART
DoSomeStuff:
For i = 0 to 10
Pause 2
Next i
GOTO DoSomeStuff
serialin: ' Buffer the character received
GOSUB BLINK
RESUME
BLINK:
HIGH LEDPIN
PAUSE 1000
LOW LEDPIN
PAUSE 1000
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>
Include "Modedefs.bas"
DEFINE OSC 4
DE_OR_RE VAR PORTC.5 ' DE and RE Pin of SN75176
LEDPIN VAR PORTD.2 ' LED to confirm PIC is running
DATARECEIVED VAR BYTE
'======== HSEROUT, HSERIN SETTINGS ==========
DEFINE HSER_RCSTA 90h
DEFINE HSER_TXSTA 24h
DEFINE HSER_BAUD 9600
ADCON1 = 7
RCIF VAR PIR1.5 ' Alias RCIF (USART Receive Interrupt Flag)
OERR VAR RCSTA.1 ' Alias OERR (USART Overrun Error Flag)
CREN VAR RCSTA.4 ' Alias CREN (USART Continuous Receive Enable)
Main:
HIGH DE_OR_RE ' Make ready for RX
HSEROUT ["1"] ' Send R1
HIGH LEDPIN
PAUSE 5000
LOW LEDPIN
PAUSE 5000
GOTO MAIN
END
</code>
<hr>
<b>Code for RX</b>
<hr>
<code>
Include "Modedefs.bas"
DEFINE OSC 4
DE_OR_RE VAR PORTC.5 ' DE and RE Pin of SN75176 (RS485)
LEDPIN VAR PORTD.2 ' LED Pin
i VAR BYTE
DATARECEIVED VAR BYTE
'======== HSEROUT, HSERIN SETTINGS ==========
DEFINE HSER_RCSTA 90h
DEFINE HSER_TXSTA 24h
DEFINE HSER_BAUD 9600
ADCON1 = 7
RCIF VAR PIR1.5 ' Alias RCIF (USART Receive Interrupt Flag)
OERR VAR RCSTA.1 ' Alias OERR (USART Overrun Error Flag)
CREN VAR RCSTA.4 ' Alias CREN (USART Continuous Receive Enable)
LOW DE_OR_RE ' Make ready for RX
INTCON = %11000000 ' Enable interrupts
ON INTERRUPT GoTo SerialDataReceived ' Declare interrupt handler routine
PIE1.5 = 1 ' Enable interrupt on USART
DoSomeStuff:
For i = 0 to 10
Pause 2
Next i
GOTO DoSomeStuff
DISABLE
SerialDataReceived: ' Buffer the character received
HSerin [DATARECEIVED] ' Read USART and store character to next empty location
GOSUB BLINK
IF RCIF Then SerialDataReceived
RESUME
ENABLE
BLINK:
HIGH LEDPIN
PAUSE 1000
LOW LEDPIN
PAUSE 1000
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!!!!
Powered by vBulletin® Version 4.1.7 Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.