' Keyboard Tester
' Daryl Owen 7/11/00
' Using the PIC 16C65
'

INCLUDE "modedefs.bas" ' Include serial modes
DEFINE OSC 20

Top:

DEFINE LCD_BITS 4'Set LCD bus size to 4 bits
DEFINE LCD_DREG PortD'Set LCD data port to D
DEFINE LCD_DBIT 0'Set LCD starting data to bit 0
DEFINE LCD_EREG PortD'Set LCD Enable to port D
DEFINE LCD_EBIT 5'Set LCD Enable line to bit 5
DEFINE LCD_Lines 4'Set number of LCD lines to 4
DEFINE LCD_RSREG PortD'Set LCD Register to port D
DEFINE LCD_RSBIT 4'Set LCD Register line to bit 4
DEFINE LCD_COMMANDUS 5000'Set command delay time to 5mS
DEFINE LCD_DATAUS 500'Set data delay time to 500uS

TrisA.0 = 1 'RS485 RX Data port define as Input
TrisA.1 = 0 'RS485 RX Enable port define as Output
TrisA.2 = 0 'RS485 TX Data port define as Ouput
TrisA.3 = 0 'RS485 TX Enable port define as Ouput

XdataA VAR BYTE[20]'Array for all RS485 Transmittions
UnitID VAR BYTE'Unit ID Number or ID Buss Number. This number will be read from EEProm and updated using that value
Keypress VAR BYTE'Keyboard value read from keyboard
AscKey VAR BYTE'This is used to send key scan data and subroutine controls to other subroutines
TempReg VAR BYTE'Misc temp register
Temp2Reg VAR BYTE'Misc temp register
ShiftReg VAR BIT'Stores the shift key status. 1=shift 0=no shift
DumpReg VAR BIT'Temp reg for dumping keypress values after a releasecommand
CursorLocation VAR BYTE'Cursor Location on LCD
CursorMin VAR BYTE'Minimum cursor position
CursorMax VAR BYTE'Maximum cursor position
Chksum VAR BYTE'Checksum value used in RS485 transmition
Command VAR BYTE'Command read from eeprom on how to handle the functionkeys.
MenuAddress VAR BYTE'Menu Address Location in EEProm
UserAddress VAR BYTE'User data storage address location in EEProm



PortA.1 = 1'RS485 Disable RX Port
PortA.3 = 0'RS485 Disable TX Port

CursorLocation = 223
ShiftReg = 0
CursorMin = $D4 + 11
CursorMax = $D4 + 19

PortA.3 = 1'RS485 Enable TX Port
SEROUT PORTA.2,T9600,[13,10," Creative Automation "]
SEROUT PORTA.2,T9600,[13,10," "]
SEROUT PORTA.2,T9600,[13,10,"Universal Data Entry Terminal (UDET)"]
SEROUT PORTA.2,T9600,[13,10," Version 1.0 7/28/2000",13,10]
PortA.3 = 0'RS485 Disable TX Port
PAUSE 1000


Header:
LCDOUT $FE,1, "The DESERT VIPERS MC"
LCDOUT $FE,$C0, " Presents The "
LCDOUT $FE,$94, "ADELANTO GRAND PRIX "
LCDOUT $FE,$D4, "LAST NAME: "

Main:
    GOSUB ScanKeyboard'Gets key pressed keycode
    GOSUB KeyFilters'Filters out unpritable Chrs
    GOSUB ChrtoASCII'Changes keycode into ascii
    GOSUB UpdateLCD
    'Gosub RS485output
GOTO Main

ScanKeyboard:
    DumpReg = 0
    
    DumpKeys:'NOTE
    
    AscKey = 0
    Keypress = 0
    TrisB.1 = 1 : PortB.1 = 1 'Release Clock Line for Com from Keyboard to PIC
    GOSUB ClockCycle'Dumps start bit
    FOR TempReg = 0 TO 7
        GOSUB ClockCycle
        IF PortB.0 = 1 THEN 'Calculates bit value
            LOOKUP TempReg,[1,2,4,8,16,32,64,128],Temp2Reg
            Keypress = Keypress + Temp2Reg'Adds the bit values together
        ENDIF
    NEXT TempReg
    
    GOSUB ClockCycle'Dumps parity bit
    GOSUB ClockCycle'Dumps stop bit
    TrisB.1 = 0 : PortB.1 = 0 'Set Clock Line Low and hold Com from Keyboard to PIC

    IF Keypress = $F0 THEN'If key release command then go again
        DumpReg = 1'Set so that next key will also get dumped
        GOTO DumpKeys'Rerun loop
    ENDIF

    IF Keypress=$58 || Keypress=$12 || Keypress=$59 && DumpReg = 1 THEN'Turn off ShiftReg
        ShiftReg = 0'Sets the Shift Reg for Shift
        AscKey = 255'Will not print anything on LCD
    ENDIF

    IF DumpReg = 1 THEN'If this loop was to dump chr then send a 255 to next
        DumpReg = 0 '???????
        Keypress = 255
    ENDIF

    IF Keypress=$58 || Keypress=$12 || Keypress=$59 THEN'Turn on ShiftReg
        ShiftReg = 1'Sets the Shift Reg for Shift
        AscKey = 255'Will not print anything on LCD
    ENDIF
RETURN

UpdateLCD:'This will convert the key scan codes to ASCCII
    IF AscKey <> 255 THEN
        LCDOUT $FE,CursorLocation, Keypress
        CursorLocation = CursorLocation + 1
        GOSUB CheckCurPos
    ENDIF
RETURN

ClockCycle:'Waits for the clock to cycle from keyboard
    HoldClockLow:
        IF PortB.1 = 0 THEN HoldClockLow
    HoldClockHi:
        IF PortB.1 = 1 THEN HoldClockHi
RETURN

ChrtoASCII:'Converts keycode into ASCII
    IF AscKey = 254 THEN SkipASCII'No Conversion

    IF AscKey <> 255 THEN
        AscKey = 255
        LOOKDOWN Keypress,[69,22,30,38,37,46,54,61,62,70,28,50,33,35,36,43,52,51,67,59,66,75,58,49,68,77,21,45,27,44,60,42,29,34,53,26,41,78,85,93,84,91,76,82,65,73,74],AscKey
        LOOKUP AscKey,["0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ -+\[];',./"],Keypress
    ENDIF
SkipASCII:
RETURN

KeyFilters:
    IF Keypress = $66 THEN'Backspace key pressed
        LCDOUT $FE,CursorLocation," "'Prints a blank
        CursorLocation = CursorLocation - 1'Moves cursor back 1 spaces. Note:The LCD cursor location will have a blank printed from the ASCII lookuptable.
        GOSUB CheckCurPos
        LCDOUT $FE,CursorLocation," "'Prints a blank
        AscKey = 255'Will not print anything on LCD
    ENDIF

    IF Keypress=$1E && ShiftReg = 1 THEN
        Keypress="@"
        AscKey = 254
    ENDIF
RETURN

CheckCurPos:'Verifies cursor is within limits
    IF CursorLocation > CursorMax THEN
        CursorLocation = CursorMax
    ENDIF
    IF CursorLocation < CursorMin THEN
        CursorLocation = CursorMin
    ENDIF
RETURN

GOTO Top


'SendRS485:
'    Gosub CalcChkSum
'    PortA.3 = 1'RS485 Enable TX Port
'    Serout PORTA.2,N9600,[13,10,#UnitID DIG 2,#UnitID DIG 1,#UnitID DIG
'    0,":",XdataA(0),XdataA(1),XdataA(2),XdataA(3),XdataA(4),XdataA(5),XdataA(6),
'    XdataA(7),XdataA(8),XdataA(9),XdataA(10),XdataA(11),XdataA(12),XdataA(13),Xd
'    ataA(14),XdataA(15),XdataA(16),XdataA(17),XdataA(18),XdataA(19),":",Chksum]
'    PortA.3 = 0'RS485 Disable TX Port
'Return