Good day Picers
Where can I get a simple explanation or sample how "On Interrupt" works with Serial communication (HSERIN / HSEROUT)?
Good day Picers
Where can I get a simple explanation or sample how "On Interrupt" works with Serial communication (HSERIN / HSEROUT)?
http://www.picbasic.co.uk/forum/ could also be helpful.
____________
"If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte
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& nbsp; ' 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"] &n bsp; ' 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 &nbs p; VAR BYTE
'======== HSEROUT, HSERIN SETTINGS ==========
DEFINE HSER_RCSTA 90h
DEFINE HSER_TXSTA 24h
DEFINE HSER_BAUD 9600
ADCON1 = 7
RCIF VAR PIR1.5& nbsp; ' 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 &n bsp; ' Make ready for RX
INTCON = %11000000   ; ' Enable interrupts
ON INTERRUPT GoTo serialin ' Declare interrupt handler routine
PIE1.5 = 1 & nbsp; ' Enable interrupt on USART
DoSomeStuff:
For i = 0 to 10
Pause 2
Next i
GOTO DoSomeStuff
serialin:   ; & nbsp;' Buffer the character received
GOSUB BLINK
RESUME
BLINK:
HIGH LEDPIN
PAUSE 1000
LOW LEDPIN
PAUSE 1000
RESUME
END
</code>
Lookup disable in your manual under ON INTERRUPT. Then lookup RETURN under GOSUB. That will get you started.
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.
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.
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
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& nbsp; ' 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"] &n bsp; ' 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 &nbs p; 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& nbsp; ' 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 &n bsp; ' Make ready for RX
INTCON = %11000000   ; ' Enable interrupts
ON INTERRUPT GoTo SerialDataReceived ' Declare interrupt handler routine
PIE1.5 = 1 & nbsp; ' Enable interrupt on USART
DoSomeStuff:
For i = 0 to 10
Pause 2
Next i
GOTO DoSomeStuff
DISABLE
SerialDataReceived: & nbsp; &nb sp; ' 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>
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.
Bookmarks