' PIC serial Interface
' 1999 by Marco D'Onofrio
' donomark@tin.it

'If you want to send special commands , you can use the standard controls for Hitachi HD4780
'Command Operation 
'$FE, 1 Clear display
'$FE, 2 Return home (beginning of first line)
'$FE, $0C Cursor off
'$FE, $0E Underline cursor on
'$FE, $0F Blinking cursor on
'$FE, $10 Move cursor left one position
'$FE, $14 Move cursor right one position
'$FE, $C0 Move cursor to beginning of second line



'Serial input and output speed is 9600
'LCD data speed is set to 9600
'You can modify each parameters

'Fast test
'Connect your pic to an LCD display as indicated in PBP manual (LCDOUT)
'Connect the pic to your computer as indicated in PBP manual (debug and serin)
'Open a Terminal Program (as PIC TERMINAL) . Select 9600 bps and the right Com port.
'Type standard characters or commands
'Enjoy (:

define debug_mode 1
DEFINE DEBUG_BAUD 9600


include "modedefs.bas"

ing var byte
cmd var byte


pause 1000
lcdout "Serial display"
lcdout $fe,$c0
lcdout "Ready"
start:

  
serin 1,N2400,ing

if ing="$" then   'If a $ character is received,then the pic go to the command section 
goto command
else
lcdout ing 'display anything is received from remote control
endif
goto start



command:
'The command syntx is the same of PBP manual.
'It is possible to change the commands syntax in order to save memory space or to reduce transmission time
'For ex. instead of 
' $fe,$c0 
'you could use
'$0   
'If it has not been sent a valid command character,then the Pic display the character and goto to start


'Check for $fe,  syntax
cmd=ing 'copy $ in cmd (see later why)
serin 1,N2400,ing
if ing="f" then
serin 1,N2400,ing
if ing="e" then
serin 1,N2400,ing
if ing="," then
serin 1,N2400,ing

'Now check for every single command

if ing="1" then 

lcdout $fe,1
goto start
endif

if ing="2" then
lcdout $fe,2

goto start
endif

if ing="$" then
goto ext_command
endif

endif
endif
endif

'if no valid command is received then Seld must send to the LCD the $ and the new character
lcdout cmd
lcdout ing


goto start

ext_command:

serin 1,N2400,ing
if ing="0" then 
serin 1,N2400,ing
if ing="c" then
	lcdout $fe,$0c
	goto start
endif

if ing="e" then
	lcdout $fe,$0e
	goto start
endif

if ing="f" then
	lcdout $fe,$0f
	goto start
endif

lcdout ing
goto start
endif

if ing="1" then
serin 1,N2400,ing
if ing="0" then
	lcdout $fe,$10
	goto start
endif
if ing="4" then 
	lcdout $fe,$14
	goto start
endif
endif

if ing="c" then
serin 1,N2400,ing
if ing="0" then
	lcdout $fe,$c0
	goto start
endif
endif


lcdout ing

goto start