Hi, Ive been working on this serial code that will accept bytes into the uart. it uses a different chip, the 16f628, but it uses circular buffers, an interrupt, and a start and a stop character. If you find anything wrong with it, let me know, I havent fully tested it yet but this should get you started.
'************************************************* ***************
'* Name : bufferdesign.BAS *
'* Author : David Kinsler *
'* Notice : Copyright (c) 2006 *
'* : All Rights Reserved *
'* Date : 7/6/2006 *
'* Version : 1.1 *
'* Notes : BUFFER with interrupt v1.1 w/ buffer overrun *
'* : resetting AND no endfound variable *
'************************************************* ***************
'Defines area
'set OSC freq
Define OSC 20
' Set receive register to receiver enabled
DEFINE HSER_RCSTA 90h
' Set transmit register to transmitter enabled
DEFINE HSER_TXSTA 20h
' Set baud rate
DEFINE HSER_BAUD 9600
'clear flag automatically when serial hardware buffer overflows
DEFINE HSER_CLROERR 1
INTCON.7 = 1 ' Enable Global Interrupt Handler
INTCON.6 = 1 ' Enable USART Receive Interrupt
PIE1.5 = 1
pause 100 'safe startup time
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''
on interrupt goto INTERRUPTROUTINE
ENABLE
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''
'Declare variables and counters
commandsize con 10
serbuffsize con 10
BuffSize con 30 'buffer size
Buffer var byte[BuffSize] 'buffer array
SerBuff var byte[serbuffsize] 'used in interrupt to catch incomming data
StChr con "$" 'determines beginning of command
EndChr con "^" 'determines end of command
FirstChr var byte 'holds position of first character in buffer
LastChr var byte 'holds position of last character in buffer
FirstSrlChrPos var byte 'holds position of first serial character
LastSrlChrPos var byte 'holds positon of last serial character
StartFound var bit 'boolean for testing if stchr found
Chr Var Byte 'holds inbound character
Temp var byte[commandsize]
i var word 'counter for blinking led's
x var byte
y var byte 'used to store index of temp
'EndFound var byte 'found the end of the command String?
SrlReceived var bit 'bit says weather this is something in serial buffer or not to transfer
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''
'Initialize variables and counters
FirstChr = 0
LastChr = 0
FirstSrlChrPos = 0
LastSrlChrPos = 0
StartFound = 0
SrlReceived = 0
'endfound = 0
x = 0
y = 0
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''
MAIN:
'do something to like blink and led
high PORTB.4
for i = 0 to 1000
PAUSE 1
NEXT I
LOW PORTB.4
FOR I = 0 TO 1000
PaUSE 1
NEXT I
If SrlReceived = 1 Then
gosub TRANSFERBUFFER
gosub FINDCOMMAND
ENDIF
GOTO MAIN
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''
DISABLE 'disables interrupt
INTERRUPTROUTINE:
Hserin [SerBuff[LastSrlChrPos]]
hserout [serBuff[lastsrlchrpos]]
'when the SerBuff is read into the larger array later, you increment FirstSrlChrPos
'as you read it
lastSrlChrPos = lastSrlChrPos + 1 'increment last character received position
If LastSrlChrPos = serbuffsize then 'if position = serbuffsize after inc, then position = 0
LastSrlChrPos = 0 'making a circular buffer, this also insures FirstSrlChrPos
endif
If LastSrlChrPos = FirstSrlChrPos then
'SERBUFF BUFFER OVERUN, reset serial buffer data
HSEROUT ["Serial Buffer overrun, clearing serial buffer", 10]
LastSrlChrPos = 0
FirstSrlChrpos = 0
srlreceived = 0
goto SRLBUFFOVERRUN
endif
SrlReceived = 1 'new data in serial buffer
SRLBUFFOVERRUN:
Resume 'resumes where left off
ENABLE 'reneables the interrupt
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''
TRANSFERBUFFER:
while FirstSrlChrPOS <> LastSrlChrPos 'while not equal, buffer has not been totaly read.
Buffer[LastChr] = SerBuff[FirstSrlChrPOS]
LastChr = LastChr + 1 'FirstChr is incremented in the PROCESSCOMMAND routine
if LastChr = buffsize then
LastChr = 0
endif
disable 'makes sure interrupt can not change lastchr while clearing
If LastChr = FirstChr Then
'DO SOMETHING ABOUT BUFFER OVERRUN : ADD CODE HERE
HSEROUT ["Transfer Buffer overrun, clearing transfer buffer", 10, 13]
FirstChr = 0
LastChr = 0
goto ENDEDTRANFER
enable 'turns interrupt back on
endif
FirstSrlChrPos = FirstSrlChrPos + 1 'inc serial buffer start Position
if FirstSrlChrPos = serbuffsize then 'if its 10, cycle it back to 0
FirstSrlChrPos = 0 'this creates a circular buffer
endif
wend
ENDEDTRANSFER:
SrlReceived = 0 'all data read from serial buffer
RETURN 'go back to gosub in main
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''
FINDCOMMAND:
'find the start character in buffer
If Buffer[FirstChr] <> StChr then
while FirstChr <> LastChr And StartFound = 0
If Buffer[FirstChr] = StChr then
x = FirstChr
TEmp[0] = Buffer[x]
y = 1
StartFound = 1
else
FirstChr = FirstChr + 1
if FirstChr = 30 then
FirstChr = 0
endif
endif
wend
Return
else
if startfound = 0 then
x = firstChr
startfound = 1
endif
'we have now found the start character, do not advance firstchr until you
'have the whole thing in the temp variable
While X <> LastChr 'you have not already found the end of the command
if x <> FirstChr and Buffer[x] = StChr then
'MISSED COMMAND AND NEW ONE SENT!!!!!!!!!!!!!!!!!
'START NEW COMMAND AT THIS POINT
FirstChr = x
y = 0
startfound = 1
endif
Temp[y] = Buffer[x] 'read next character into temp
if Buffer[x] = EndChr then
goto PROCESSCOMMAND 'Whole command found, goto process it
else 'keep counting
y = y + 1 'inc y for temp
X = x + 1
If x = 30 then
x = 0
endif
endif
wend
'eNDFOUND = 0 'BREAK out of while after end is found and reset endfound
return
endif
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''
PROCESSCOMMAND:
hserout [10, 13, "RECEIVED COMMAND: ", STR temp\y + 1, 10, 13]
FirstChr = x + 1 'remove found commmand from the buffer
if FirstChr = 30 then
FirstChr = 0
endif
y = 0
x = 0 'remove the temp counter
startfound = 0 'reset startfound
goto main
Bookmarks