Receive Serial Data in the Background
John Couture
Posts: 370
The program below is (I think) the example that Jon Williams provided a while back that is supposed to demonstrate gathering serial data using an interrupt.· I can't get it to work???
Look inside the the Interrupt routine.· There is a label of RX_Buffer .... how does it ever get there?
' =========================================================================
'
'·· File...... INTERRUPT_RTCC.SXB
'·· Purpose... Buffered serial reception using ISR
'·· Author.... (c) Parallax, Inc. -- All Rights Reserved
'·· E-mail.... support@parallax.com
'·· Started...
'·· Updated... 30 MAR 2005
'
' =========================================================================
'
' Program Description
'
'
' This program demonstrates the construction of an ISR to receive serial
' data "in the background" using a 16-byte circular buffer.
'
' Note: Requires SX/B 1.2 or later
'
' Device Settings
'
DEVICE········· SX28, OSCXT2, TURBO, STACKX, OPTIONX
FREQ··········· 4_000_000
'
' IO Pins
'
Sin··VAR·RA.0···' serial in
Blinker··VAR·RB.0···' blinking LED
LEDs··VAR·RC···' eight LEDs (7-segs)
TRIS_LEDs·VAR·TRIS_C
'
' Constants
'
B1200·········· CON···· 64····················· ' 1200 Baud
B2400·········· CON···· 32····················· ' 2400 Baud
B4800·········· CON···· 16····················· ' 4800 Baud
B9600·········· CON···· 8······················ ' 9600 Baud
B19K2·········· CON···· 4······················ ' 19.2 kBaud (max @ 4 MHz)
BitTm·········· CON···· B19K2·················· ' samples per bit
BitTm15········ CON···· 3*BitTm/2·············· ' 1.5 bits
'
' Variables
'
rxCount··VAR·Byte···' bits to receive
rxTimer··VAR·Byte···' bit timer for ISR
rxByte··VAR·Byte·· ·' serial byte
rxHead··VAR·Byte··· ' available slot
rxTail··VAR·Byte··· ' next byte to read
rxBuf··VAR·Byte(16)·' circular buffer
temp1·········· VAR···· Byte··················· ' parameter
'
· INTERRUPT NOPRESERVE
'
' ISR is setup to receive N81, inverted mode.
ISR_Start:
· ASM
··· MOVB C, /Sin·········· ' sample serial input
··· TEST rxCount·········· ' receiving now?
··· JNZ RX_Bit············ ' yes if rxCount > 0
··· MOV W, #9············· ' start + 8 bits
··· SC···················· ' skip if no start bit
··· MOV rxCount, W········ ' got start, load bit count
··· MOV rxTimer, #BitTm15· ' delay 1.5 bits
RX_Bit:
··· DJNZ rxTimer, ISR_Exit·' update bit timer
··· MOV rxTimer, #BitTm··· ' reload bit timer
··· DEC rxCount·········· ·' mark bit done
··· SZ···················· ' if last bit, we're done
··· RR rxByte············· ' move bit into rxByte
··· SZ·····················' if not 0, get more bits
··· JMP ISR_Exit
RX_Buffer:
··· MOV FSR, #rxBuf······· ' get buffer address
··· ADD FSR, rxHead······· ' point to head
··· MOV IND, rxByte······ ·' move rxByte to head
··· INC rxHead··········· ·' update head
··· CLRB rxHead.4········ ·' keep 0 - 15
· ENDASM
ISR_Exit:
· RETURNINT 52············ ' 13 uS @ 4 MHz
' =========================================================================
· PROGRAM Start
' =========================================================================
'
' Subroutine Declarations
'
GETBYTE··SUB····' returns byte from buffer
'
' Program Code
'
Start:
· TRIS_LEDs = %00000000········· ' make LED pins outputs
· OPTION = $88···················' interrupt, no prescaler
Main:
· IF rxHead = rxTail THEN Blink··' if buffer emtpy, skip
· LEDs = GETBYTE················ ' get byte from buffer
Blink:
· TOGGLE Blinker·············· ··' blink LED
· PAUSE 50····················· ·' small pause
· GOTO Main
'
' Subroutine Code
'
' Use: aVar = GETBYTE
' -- if data is in buffer, the next byte is move to 'aVar'
' -- if called when buffer empty, code waits for character to arrive
GETBYTE:
· DO WHILE rxHead = rxTail
··· ' wait while empty
· LOOP
· temp1 = rxBuf(rxTail)······················ ·' get first available
· INC rxTail································· ·' update tail position
· \ CLRB rxTail.4···························· ·' keep 0 - 15
· RETURN temp1
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
John J. Couture
San Diego Miramar College
Look inside the the Interrupt routine.· There is a label of RX_Buffer .... how does it ever get there?
' =========================================================================
'
'·· File...... INTERRUPT_RTCC.SXB
'·· Purpose... Buffered serial reception using ISR
'·· Author.... (c) Parallax, Inc. -- All Rights Reserved
'·· E-mail.... support@parallax.com
'·· Started...
'·· Updated... 30 MAR 2005
'
' =========================================================================
'
' Program Description
'
'
' This program demonstrates the construction of an ISR to receive serial
' data "in the background" using a 16-byte circular buffer.
'
' Note: Requires SX/B 1.2 or later
'
' Device Settings
'
DEVICE········· SX28, OSCXT2, TURBO, STACKX, OPTIONX
FREQ··········· 4_000_000
'
' IO Pins
'
Sin··VAR·RA.0···' serial in
Blinker··VAR·RB.0···' blinking LED
LEDs··VAR·RC···' eight LEDs (7-segs)
TRIS_LEDs·VAR·TRIS_C
'
' Constants
'
B1200·········· CON···· 64····················· ' 1200 Baud
B2400·········· CON···· 32····················· ' 2400 Baud
B4800·········· CON···· 16····················· ' 4800 Baud
B9600·········· CON···· 8······················ ' 9600 Baud
B19K2·········· CON···· 4······················ ' 19.2 kBaud (max @ 4 MHz)
BitTm·········· CON···· B19K2·················· ' samples per bit
BitTm15········ CON···· 3*BitTm/2·············· ' 1.5 bits
'
' Variables
'
rxCount··VAR·Byte···' bits to receive
rxTimer··VAR·Byte···' bit timer for ISR
rxByte··VAR·Byte·· ·' serial byte
rxHead··VAR·Byte··· ' available slot
rxTail··VAR·Byte··· ' next byte to read
rxBuf··VAR·Byte(16)·' circular buffer
temp1·········· VAR···· Byte··················· ' parameter
'
· INTERRUPT NOPRESERVE
'
' ISR is setup to receive N81, inverted mode.
ISR_Start:
· ASM
··· MOVB C, /Sin·········· ' sample serial input
··· TEST rxCount·········· ' receiving now?
··· JNZ RX_Bit············ ' yes if rxCount > 0
··· MOV W, #9············· ' start + 8 bits
··· SC···················· ' skip if no start bit
··· MOV rxCount, W········ ' got start, load bit count
··· MOV rxTimer, #BitTm15· ' delay 1.5 bits
RX_Bit:
··· DJNZ rxTimer, ISR_Exit·' update bit timer
··· MOV rxTimer, #BitTm··· ' reload bit timer
··· DEC rxCount·········· ·' mark bit done
··· SZ···················· ' if last bit, we're done
··· RR rxByte············· ' move bit into rxByte
··· SZ·····················' if not 0, get more bits
··· JMP ISR_Exit
RX_Buffer:
··· MOV FSR, #rxBuf······· ' get buffer address
··· ADD FSR, rxHead······· ' point to head
··· MOV IND, rxByte······ ·' move rxByte to head
··· INC rxHead··········· ·' update head
··· CLRB rxHead.4········ ·' keep 0 - 15
· ENDASM
ISR_Exit:
· RETURNINT 52············ ' 13 uS @ 4 MHz
' =========================================================================
· PROGRAM Start
' =========================================================================
'
' Subroutine Declarations
'
GETBYTE··SUB····' returns byte from buffer
'
' Program Code
'
Start:
· TRIS_LEDs = %00000000········· ' make LED pins outputs
· OPTION = $88···················' interrupt, no prescaler
Main:
· IF rxHead = rxTail THEN Blink··' if buffer emtpy, skip
· LEDs = GETBYTE················ ' get byte from buffer
Blink:
· TOGGLE Blinker·············· ··' blink LED
· PAUSE 50····················· ·' small pause
· GOTO Main
'
' Subroutine Code
'
' Use: aVar = GETBYTE
' -- if data is in buffer, the next byte is move to 'aVar'
' -- if called when buffer empty, code waits for character to arrive
GETBYTE:
· DO WHILE rxHead = rxTail
··· ' wait while empty
· LOOP
· temp1 = rxBuf(rxTail)······················ ·' get first available
· INC rxTail································· ·' update tail position
· \ CLRB rxTail.4···························· ·' keep 0 - 15
· RETURN temp1
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
John J. Couture
San Diego Miramar College
Comments
John,
· If the zero flag is set, then the "SZ" instruction is skip the "JMP ISR_Exit" instruction and you will be at the RX_Buffer: label.
Bean.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Cheap 4-digit LED display with driver IC·www.hc4led.com
Low power SD Data Logger www.sddatalogger.com
"I reject your reality, and substitute my own." Mythbusters
·