Code:
@ DEVICE PIC16F628A , HS_OSC , WDT_OFF , PWRT_ON , MCLR_ON , BOD_ON , LVP_OFF , PROTECT_OFF
'HS 20mhz external, watchdog off, powerup timer on, mclr external, brown out detect on, low volt program off , code protect off
resetplaceholder: 'wordpad, arial black, size 8, reg, 1600x1200 screen, 16f628A code
DEFINE OSC 20 '20mhz
serdata var byte : serialbuffer var byte[255] : bufferpointer var byte
bufferdata var byte : temp var byte
startupholder: goto skipsubs 'skip over all the commonly used subroutines
ON INTERRUPT GOTO INTHANDLER
DISABLE INTERRUPT
INTHANDLER: if pir1.5 = 1 then 'if serial data rx'd
serdata = rcreg 'save serial port data right away
pir1.5 = 0 'reset the RX flag
if ( rcsta.1 = 1 ) or ( rcsta.2 = 1 ) then 'check for err, if frame/overrun error,
rcsta.4 = 0 : rcsta.4 = 1 : serdata = rcreg : serdata = 0
else
if bufferpointer = 255 then goto intfinish 'buffer is full
serialbuffer[bufferpointer] = serdata
: bufferpointer = bufferpointer + 1 'save data in buffer, bump pointer
goto INTHANDLER 're-check serial port just in case another character came in while saving data
'...not likely
endif
endif
intfinish: RESUME
ENABLE INTERRUPT
getcharacterfrombuffer:
if bufferpointer = 0 then return 'no data in buffer in the first place!
buffereddata = serialbuffer[0] 'get oldest data from buffer
for temp = 0 to 254 : serialbuffer[temp] = serialbuffer[temp+1] : next temp 'shift data down one byte
serialbuffer[255]=0 'set last byte in buffer to zero since it's meaningless
bufferpointer = bufferpointer - 1 'one less byte in buffer
return
skipsubs: option_reg=8 : pie1=32 : trisa=0 : porta=0 : trisb=$ef : portb=16 : t1con=0 : t2con=0
cmcon=7 : ccp1con=0 : vrcon=0 : txsta=0 : rcsta=$90 : pir1.5=0 : spbrg=33 : intcon=$e0 '9600 baud
for temp = 0 to 255 : serialbuffer[temp] = 0 : next temp
mainloop: goto mainloop 'do it all over again
END
I modified one of my old programs. This works for me, always has.
I've left the character handling and the modifications of the program to you to make work for your PIC.
Bookmarks