i encounter some problem when using the hardware USART for communication. pls help. my code as below:
include "modedefs.bas"
DEFINE SHIFT_PAUSEUS 20
define OSC 20
DEFINE SER2_ODD 1
TRISA=%11110000
TRISB= %11111011
symbol Addr_Data_In=porta.4
symbol en=porta.3
symbol stb=porta.2
symbol clk=porta.1
symbol data_out=porta.0
SPBRG = 32 ' Set baud rate to 9600
RCSTA = %10010000 ' Enable serial port and continuous receive
TXSTA = %00100000 ' Enable transmit and asynchronous mode
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)
LED VAR PORTD.0 ' Alias LED to PORTD.0
ADCON1=6
protocol_2 var byte
protocol var byte
addr var word
mode var byte
Address_Value var word
digit var byte
i var byte
buffer_size CON 35 ' Sets the size of the ring buffer
buffer VAR BYTE[buffer_size] ' Array variable for holding received characters
index_in VAR BYTE ' Pointer - next empty location in buffer
index_out VAR BYTE ' Pointer - location of oldest character in buffer
bufchar VAR BYTE ' Stores the character retrieved from the buffer
errflag VAR BYTE ' Holds error flags
index_in = 0
index_out = 0
i = 0
INTCON = %11000000 ' Enable interrupts
ON INTERRUPT GoTo serialin ' Declare interrupt handler routine
PIE1.5 = 1 ' Enable interrupt on USART
'================================================= ==================================================
main: call display
CALL ending
display: IF errflag Then error ' Handle error if needed
IF index_in = index_out Then display ' loop if nothing in buffer
GoSub getbuf ' Get a character from buffer
protocol=buffer[1]
if (protocol<>"X") then display
addr=buffer[2]
If (addr<>Address_Value) then display
mode=buffer[3]
if (mode=$30) Then Turn_Off_Led
a29=buffer[4]
a28=buffer[5]
a27=buffer[6]
a26=buffer[7]
a25=buffer[8]
a24=buffer[9]
a23=buffer[10]
a22=buffer[11]
a21=buffer[12]
a20=buffer[13]
a19=buffer[14]
a18=buffer[15]
a17=buffer[16]
a16=buffer[17]
a15=buffer[18]
a14=buffer[19]
a13=buffer[20]
a12=buffer[21]
a11=buffer[22]
a10=buffer[23]
a9=buffer[24]
a8=buffer[25]
a7=buffer[26]
a6=buffer[27]
a5=buffer[28]
a4=buffer[29]
a3=buffer[30]
a2=buffer[31]
a1=buffer[32]
a0=buffer[33]
protocol_2=buffer[34]
if (protocol_2<>"Y") then display
lookup a0,[$FD,$85,$DB,$9F,$A7,$BE,$FE,$95,$FF,$BF,$80],digit
seg1=digit
lookup a1,[$FD,$85,$DB,$9F,$A7,$BE,$FE,$95,$FF,$BF,$80],digit
seg2=digit
lookup a2,[$FD,$85,$DB,$9F,$A7,$BE,$FE,$95,$FF,$BF,$80],digit
seg3=digit
lookup a3,[$FD,$85,$DB,$9F,$A7,$BE,$FE,$95,$FF,$BF,$80],digit
seg4=digit
lookup a4,[$FD,$85,$DB,$9F,$A7,$BE,$FE,$95,$FF,$BF,$80],digit
seg5=digit
lookup a5,[$FD,$85,$DB,$9F,$A7,$BE,$FE,$95,$FF,$BF,$80],digit
seg6=digit
lookup a6,[$FD,$85,$DB,$9F,$A7,$BE,$FE,$95,$FF,$BF,$80],digit
seg7=digit
lookup a7,[$FD,$85,$DB,$9F,$A7,$BE,$FE,$95,$FF,$BF,$80],digit
seg8=digit
lookup a8,[$FD,$85,$DB,$9F,$A7,$BE,$FE,$95,$FF,$BF,$80],digit
seg9=digit
lookup a9,[$FD,$85,$DB,$9F,$A7,$BE,$FE,$95,$FF,$BF,$80],digit
seg10=digit
lookup a10,[$FD,$85,$DB,$9F,$A7,$BE,$FE,$95,$FF,$BF,$80],digit
seg11=digit
lookup a11,[$FD,$85,$DB,$9F,$A7,$BE,$FE,$95,$FF,$BF,$80],digit
seg12=digit
lookup a12,[$FD,$85,$DB,$9F,$A7,$BE,$FE,$95,$FF,$BF,$80],digit
seg13=digit
lookup a13,[$FD,$85,$DB,$9F,$A7,$BE,$FE,$95,$FF,$BF,$80],digit
seg14=digit
lookup a14,[$FD,$85,$DB,$9F,$A7,$BE,$FE,$95,$FF,$BF,$80],digit
seg15=digit
lookup a15,[$FD,$85,$DB,$9F,$A7,$BE,$FE,$95,$FF,$BF,$80],digit
seg16=digit
lookup a16,[$FD,$85,$DB,$9F,$A7,$BE,$FE,$95,$FF,$BF,$80],digit
seg17=digit
lookup a17,[$FD,$85,$DB,$9F,$A7,$BE,$FE,$95,$FF,$BF,$80],digit
seg18=digit
lookup a18,[$FD,$85,$DB,$9F,$A7,$BE,$FE,$95,$FF,$BF,$80],digit
seg19=digit
lookup a19,[$FD,$85,$DB,$9F,$A7,$BE,$FE,$95,$FF,$BF,$80],digit
seg20=digit
lookup a20,[$FD,$85,$DB,$9F,$A7,$BE,$FE,$95,$FF,$BF,$80],digit
seg21=digit
lookup a21,[$FD,$85,$DB,$9F,$A7,$BE,$FE,$95,$FF,$BF,$80],digit
seg22=digit
lookup a22,[$FD,$85,$DB,$9F,$A7,$BE,$FE,$95,$FF,$BF,$80],digit
seg23=digit
lookup a23,[$FD,$85,$DB,$9F,$A7,$BE,$FE,$95,$FF,$BF,$80],digit
seg24=digit
lookup a24,[$FD,$85,$DB,$9F,$A7,$BE,$FE,$95,$FF,$BF,$80],digit
seg25=digit
lookup a25,[$FD,$85,$DB,$9F,$A7,$BE,$FE,$95,$FF,$BF,$80],digit
seg26=digit
lookup a26,[$FD,$85,$DB,$9F,$A7,$BE,$FE,$95,$FF,$BF,$80],digit
seg27=digit
lookup a27,[$FD,$85,$DB,$9F,$A7,$BE,$FE,$95,$FF,$BF,$80],digit
seg28=digit
lookup a28,[$FD,$85,$DB,$9F,$A7,$BE,$FE,$95,$FF,$BF,$80],digit
seg29=digit
lookup a29,[$FD,$85,$DB,$9F,$A7,$BE,$FE,$95,$FF,$BF,$80],digit
seg30=digit
shiftout data_out,clk,MSBFIRST,[seg30,seg29,seg28,seg27,seg26,seg25,_
seg24,seg23,seg22,seg21,seg20,seg19,seg18,seg17,_
seg16,seg15,_
seg14,seg13,seg12,seg11,seg10,seg9,_
seg8,seg7,seg6,seg5,seg4,seg3,seg2,seg1]
high stb
pause 5
low stb
goto display
' Subroutines
Disable ' Don't check for interrupts in this section
getbuf: ' move the next character in buffer to bufchar
index_out = (index_out + 1) ' Increment index_out pointer (0 to 63)
IF index_out > (buffer_size-1) Then index_out = 0 ' Reset pointer if outside of buffer
bufchar = buffer[index_out] ' Read buffer location
Return
error: ' Display error message if buffer has overrun
errflag = 0 ' Reset the error flag
CREN = 0 ' Disable continuous receive to clear overrun flag
CREN = 1 ' Enable continuous receive
GoTo display ' Carry on
serialin: ' Buffer the character received
IF OERR Then usart_error ' Check for USART errors
index_in = (index_in + 1) ' Increment index_in pointer (0 to 63)
IF index_in > (buffer_size-1) Then index_in = 0 'Reset pointer if outside of buffer
IF index_in = index_out Then buffer_error ' Check for buffer overrun
HSerin [buffer[index_in]] ' Read USART and store character to next empty location
IF RCIF Then serialin ' Check for another character while we're here
Resume ' Return to program
buffer_error:
errflag.1 = 1 ' Set the error flag for software
' Move pointer back to avoid corrupting the buffer. MIN insures that it ends up within the buffer.
index_in = (index_in - 1) MIN (buffer_size - 1)
HSerin [buffer[index_in]] ' Overwrite the last character stored (resets the interrupt flag)
usart_error:
errflag.0 = 1 ' Set the error flag for hardware
Resume ' Return to program
ending: END
Bookmarks