OK, here is my entire code.
I have a 2*16 LCD, a button and a shaft encoder connected.
Regards
Håkan
DEFINE LOADER_USED 1
' Set X-tal to 4 MHz
DEFINE OSC 4
DEFINE HSER_RCSTA 90h 'Hser receive status init
DEFINE HSER_TXSTA 20h 'Hser transmit status init
DEFINE HSER_BAUD 2400 'Hser baud rate
' Set LCD Data port
DEFINE LCD_DREG PORTB
' Set starting Data bit (0 or 4) if 4-bit bus
DEFINE LCD_DBIT 0
' Set LCD Register Select port
DEFINE LCD_RSREG PORTB
' Set LCD Register Select bit
DEFINE LCD_RSBIT 7
'LCD read/write port
DEFINE LCD_RWREG PORTB
'LCD read/write bit
DEFINE LCD_RWBIT 6
' Set LCD Enable port
DEFINE LCD_EREG PORTB
' Set LCD Enable bit
DEFINE LCD_EBIT 5
' Set LCD bus size (4 or 8 bits)
DEFINE LCD_BITS 4
' Set number of lines on LCD
DEFINE LCD_LINES 2
' Set command delay time in us
DEFINE LCD_COMMANDUS 2000
' Set data delay time in us
DEFINE LCD_DATAUS 50
SPBRG = 25'103 'SET TO 2400bps
On Interrupt Goto myint ' Define interrupt handler
INTCON.6 = 1' = $C0 ' Enable INTE interrupt
PIE1.5 = 1
' Shaft Encoder
clock var PORTD.0
stat var PORTD.1
' Button
exec var PORTD.2
' ICD
txPin var PORTC.6
rxPin Var PORTC.7
' LCD background
LED var PORTD.3
T2400 CON 396'1021
Inverted CON $4000
Baud CON T2400 '+ Inverted
LcdCls CON $01 ' clear the LCD
LcdLine1 CON $80 ' DDRAM address of line 1
LcdLine2 CON $C0 ' DDRAM address of line 2
countPulses VAR Byte
print VAR Bit
a VAR Byte
b VAR Byte
c var byte
idx var byte
turnOff VAR Bit
failSafe VAR Byte
'Init EE
read $00,a
if a <> 0 then
' First digit --> Delay when turnig off
' Second digit --> Card
' Therd digit --> Relay
' ALL system
write 2,$A ' --> Start adr
write 3,$D ' --> Stop adr
write $A, 11
write $B, 12
write $C, 114
write $D, 16
' Main Sound --> Mixerboard, eq, Ampl.
' Sub Sound --> Only Mixer and Eq etc.
' Main Displ --> Display Comp., screens and Projector
' Sub Displ --> Only Display Comp. and Screens
write $00, $1
endif
PAUSE 1000 ' Wait for LCD to startup
countpulses = 1
turnOff = 0
print = 0
high led
main:
GOSUB readpuls
GOSUB writeDisp
GOSUB readExec
GOTO main
'**************************
'**************************
writeDisp:
IF (print) THEN
SELECT case countPulses
CASE 1
lcdout $FE, LcdCls, "Start ALL Syst. "
CASE 2
lcdout $FE, LcdCls, "Main Sound Syst."
CASE 3
lcdout $FE, LcdCls, "Sub Sound Syst. "
CASE 4
lcdout $FE, LcdCls, "Main Disp System"
CASE 5
lcdout $FE, LcdCls, "Sub Disp System "
CASE ELSE
lcdout $FE, LcdCls, " Illigal value "
END SELECT
Print = 0
ENDIF
RETURN
'**************************
'**************************
readExec:
IF (exec = 0) THEN
lcdout $FE, LcdLine2, " Starting Up "
gosub set
GOSUB logOff
lcdout $FE, LcdLine2, " Shuting Down "
gosub clr
lcdout $FE, LcdLine2, " "
ENDIF
RETURN
' '**************************
' '**************************
readPuls:
IF(clock = 0) THEN
IF (stat) THEN
countpulses = ((countpulses - 1)//5)+1'(countpulses - 1) MIN 1'((countpulses - 1)//5)
ELSE
countpulses = ((countpulses + 1)//5)+1'(countpulses + 1) MAX 5'((countpulses + 1)//5)
ENDIF
print = 1
again: IF clock = 0 THEN GOTO again
high led
ENDIF
RETURN
' '**************************
' '**************************
set:
read countpulses*2, a
read countpulses*2+1, b
for idx = a to b
read idx,c
SEROUT2 txPin, baud,[13,(c dig 1),"S",(c dig 0)+48,((13 + (c dig 1) + "S" + 48+(c dig 0))^$FF)+1]
pause 100
SEROUT2 txPin, baud,[13,(c dig 1),"S",(c dig 0)+48,((13 + (c dig 1) + "S" + 48+(c dig 0))^$FF)+1]
pause 1000
next
RETURN
' '**************************
' '**************************
clr:
read countpulses*2, a
read countpulses*2+1, b
for idx = b to a step -1
read idx,c
if ((c dig 2)= 1) then
pause 5000
endif
SEROUT2 txPin, baud,[13,(c dig 1),"C",(c dig 0) + 48,((13 + (c dig 1) + "C" + 48 +(c dig 0))^$FF)+1]
pause 100
SEROUT2 txPin, baud,[13,(c dig 1),"C",(c dig 0) + 48,((13 + (c dig 1) + "C" + 48 +(c dig 0))^$FF)+1]
pause 1000
next
RETURN
'**************************
'**************************
logOff:
lcdout $FE, LcdLine2, " All systems GO "
failsafe = 0
turnOff = 0
logOffloop:
IF (exec = 0) THEN
PAUSE 10
loop1: IF (exec = 0) THEN loop1
IF turnOff THEN
RETURN
ELSE
lcdout $FE, LcdLine2, "OK, to turn OFF?"
PAUSE 200
failsafe = 0
turnOff = 1
ENDIF
ELSE
failSafe = failSafe + 1
IF failSafe > 10 THEN
turnOff = 0
lcdout $FE, LcdLine2, " All systems GO "
PAUSE 10
failsafe = 0
ENDIF
ENDIF
PAUSE 100
GOTO logOffLoop
' Interrupt handler
Disable ' No interrupts past this point
myint:
PIR1.5 = 0
lcdout $FE, LcdLine2, " Connected "
pause 1000
Resume ' Return to main program
Enable
end
Bookmarks