Code:
'* Notes : This is a codelock based on the following components *
'* : PIC18F26K22 @ 64Mhz *
'* : 4D Systems ULCD 32DT diablo *
'* note chksum untested *
'**********************************************************************
#CONFIG
CONFIG FOSC=INTIO67, PLLCFG=OFF, PRICLKEN=OFF, FCMEN=OFF, IESO=OFF
CONFIG PWRTEN=OFF, BOREN=SBORDIS, BORV=190, WDTEN=ON, WDTPS=32768
CONFIG CCP2MX=PORTC1, PBADEN=OFF, CCP3MX=PORTB5, HFOFST=ON, T3CMX=PORTC0
CONFIG P2BMX=PORTB5, MCLRE=EXTMCLR, STVREN=ON, LVP=OFF, XINST=OFF, DEBUG=OFF
CONFIG CP0=OFF, CP1=OFF, CP2=OFF, CP3=OFF, CPB=OFF, CPD=OFF, WRT0=OFF
CONFIG WRT1=OFF, WRT2=OFF, WRT3=OFF, WRTC=OFF, WRTB=OFF, WRTD=OFF, EBTR0=OFF
CONFIG EBTR1=OFF, EBTR2=OFF, EBTR3=OFF, EBTRB=OFF
#ENDCONFIG
define OSC 64
OSCCON = $70 ; 64Mhz
OSCTUNE.6 = 1 ; Enable 4x PLL
while ! osccon2.7 :WEND ; to make sure the pll has stabilised before you run any other code
TRISB = %10111111
TRISC = %10111111
ansela=0
anselb=0
anselc=0
DEFINE HSER_RCSTA 90h ' Enable serial port & continuous receive
DEFINE HSER_TXSTA 24h ' Enable transmit, BRGH = 1
DEFINE HSER_CLROERR 1 ' Clear overflow automatically
DEFINE HSER_SPBRG 138 ' 115200 Baud @ 64MHz, -0.08%
SPBRGH = 0
BAUDCON.3 = 1 ' Enable 16 bit baudrate generator
DEFINE HSER2_RCSTA 90h ' Enable serial port & continuous receive
DEFINE HSER2_TXSTA 24h ' Enable transmit, BRGH = 1
DEFINE HSER2_CLROERR 1 ' Clear overflow automatically
DEFINE HSER2_SPBRG 160 ' 38400 Baud @ 64MHz, -0.08%
SPBRGH2 = 1
BAUDCON2.3 = 1 ' Enable 16 bit baudrate generator
DATAIN var byte[5] ;key pkt
pword var byte[4] ;holds password
pbuff var byte[4] ;ringbuffer must be pow of 2
buff var byte[4] ;buffer to test password input
pbuffinx var byte ;ringbuffer index
chksum var byte ;the key pkt checksum if used
key var byte ;the key just read in
i var byte
;num1 data "070D0000313B" ; this is number 1
;num2 data "070D00003238" ; this is number 2
;num3 data "070D00003339" ; this is number 3
;num4 data "070D0000343E" ; this is number 4
;num5 data "070D0000353F" ; this is number 5
;num6 Data "070D0000363C" ; this is number 6
;num7 Data "070D0000373D" ; this is number 7
;num8 Data "070D00003832" ; this is number 8
;num9 data "070D00003933" ; this is number 9
;num10 data "070D00002A20" ; this is number *
;num11 data "070D0000303A" ; this is number 0
;num12 data "070D00000802" ; this is go
pause 1000
HSERout2 ["ready"] ;debug
arraywrite pword ,["*478"] ;set password
pbuffinx = 0
Start:
gosub readkey
if key != 8 then ;not go key
HSERout2 [key] ;debug
pbuff[pbuffinx] = key ;add key to ringbuffer
pbuffinx = (pbuffinx+1)&3 ;inc ringbuffer index
else ;is go key
for i = 0 to 3 ;read ringbuffer into buffer
buff[i] = pbuff[(pbuffinx+i)&3]
next
HSERout2 [13,10,str buff\4,13,10];debug
ARRAYREAD buff,4,fail,[WAITSTR pword\4] ;test password
goto unlocked
endif
goto start
unlocked:
HSERout2 ["unlocked"] ;debug
pbuffinx = 0 ;may not be needed
goto start
fail:
pbuffinx = 0 ;may not be needed
HSERout2 ["failed"] ;debug
goto start
readkey: ;read key pkt
HSERIN [WAIT($07), str datain\5]
' chksum=7 ;chksum untested proteus vterminal won't allow null chr to be sent
' for i=0 to 4
' chksum = chksum ^ datain[i]
' next
' if chksum then goto readkey ;dud read
key = datain[3]
return
Bookmarks