Skip to content

Instantly share code, notes, and snippets.

@TGNThump
Last active March 8, 2016 15:30
Show Gist options
  • Save TGNThump/f25b722262838648a8d4 to your computer and use it in GitHub Desktop.
Save TGNThump/f25b722262838648a8d4 to your computer and use it in GitHub Desktop.
; Set the frequency of the PICAXE chip to 8 MHz
setfreq m8
; Pause for 1000 clock cycles
pause 1000
; Set the TRISC values
let trisc = 0x0F
; Define varibles and constants
symbol flgs = b0 ; A byte of flags, where each bit represents a boolean flag.
symbol lockflg = 0x00 ; The bit position of the lock flag in the flgs varible.
symbol failflg = 0x01 ; The bit position of the fail flag in the flgs varible.
symbol atmpts = b1 ; The number of attempts since the last successfull attempt.
symbol digit = b2 ; The position of the current code digit in the table.
symbol currin = b3 ; The current input.
symbol lastin = b4 ; The last input.
symbol value = b6 ; The current code digit from the table.
symbol currout = b7 ; The current output of PORTC.
table 0, (%01000001, %10000001, %11000001, %01000010) ; The code.
symbol codeln = 0x03 ; The length of the code - 1.
symbol resetbtn = %01001000 ; The port of the reset button.
symbol enterbtn = %11001000 ; The port of the enter button.
symbol lcd = B.7 ; The port of the LCD display.
symbol alarm = B.6 ; The port of the alarm circuit.
; Stepper Ports
symbol brown = B.5 ; The port of the stepper brown connection.
symbol yellow = B.4 ; The port of the stepper yellow connection.
symbol black = B.3 ; The port of the stepper black connection.
symbol orange = B.2 ; The port of the stepper orange connection.
; Column Outputs
symbol col1 = 0x06 ; The port of the first collumn of the matrix keypad.
symbol col2 = 0x05 ; The port of the second collumn of the matrix keypad.
symbol col3 = 0x04 ; The port of the thrid collumn of the matrix keypad.
main:
; Call the lock function.
gosub lock
; Start a loop that goes until the power or the chip dies.
do
; If the lock flag is set, call the locked function.
if flgs bit lockflg set then
gosub locked
; Else, call the unlocked function.
else
gosub unlocked
endif
debug
loop
end
locked:
; Call the function to read the matrix keypad input.
gosub readin
select case currin
; If the current input is equal to the last input, return.
case lastin
return
; If the current input is equal to the last input, return.
case 0x00
return
; If the current input is equal to the reset button:
case resetbtn
; And the current digit is not zero:
if digit != 0 then
; Increment the atempts count.
inc atmpts
; Call the reset function.
gosub rset
; Call the function to set the LCD message to Locked.
gosub lcdLocked
endif
; If the current input is equal to the enter button:
case enterbtn
; If the fail flag is cear:
if flgs bit failflg clear then
; If the digit count is greater than the number of digits:
if digit > codeln then
; Call the unlock method.
gosub unlock
; Return from the function.
return
endif
endif
; If the digit does not equal zero:
if digit != 0 then
; Increment the attempt count.
inc atmpts
; Call the reset function.
gosub rset
; Call the function to set the LCD message to Locked.
gosub lcdLocked
endif
else
; If the fail flag is clear:
if flgs bit failflg clear then
; Read the current digit from the code table to the value varible.
readtable digit, value
; If the current input does not equal the value:
if currin != value then
; Set the failure flag.
setbit flgs, failflg
endif
endif
; If the fail flag is set:
if flgs bit failflg set then
; If the attempt count is greater than 0x03:
if atmpts >= 0x03 then
; Turn on the alarm.
high alarm
endif
endif
; Increment the digit count.
inc digit
; Add a '*' character to the LCD.
serout lcd, N1200, ("*")
endselect
return
unlocked:
; Call the function to read the matrix keypad input.
gosub readin
select case currin
; If the current input equals the last input, return.
case lastin
return
; If the current input is zero, return.
case 0x00
return
; If the current input is the reset button:
case resetbtn
; Call the lock function.
gosub lock
endselect
return
readin:
; Set the last input to the current input.
let lastin = currin
; Set the current input to 0x00
let currin = 0x00
;col1
; Turn on the column 1 output.
high portc col1
; Call the read function.
gosub readone
; Turn off the column 1 output.
low portc col1
; If the current input is not 0x00:
if currin != 0x00 then
; Add a column identifier to the current input varible.
let currin = currin + %01000000
; Return
return
endif
;col2
; Turn on the column 2 output.
high portc col2
; Call the read function.
gosub readone
; Turn off the column 2 output.
low portc col2
; If the current input is not 0x00:
if currin != 0x00 then
; Add a column identifier to the current input varible.
let currin = currin + %10000000
; Return
return
endif
;col3
; Turn on the column 3 output.
high portc col3
; Call the read function.
gosub readone
; Turn off the column 3 output.
low portc col3
; If the current input is not 0x00:
if currin != 0x00 then
; Add a column identifier to the current input varible.
let currin = currin + %11000000
; Return
return
endif
return
readone:
; Read the portc input.
readportc currin
; Mask the portc input.
let currin = currin & 0x0F
return
rset:
; Reset the current digit to 0x00.
let digit = 0x00
; Turn off the col1, col2 and col3 outputs.
low portc col1
low portc col2
low portc col3
; Clear the failure flag.
clearbit flgs, failflg
; Read the current outputs.
readoutputs currout
; If the alarm is deactivated:
if currout bit 0x06 clear then
; Turn on the alarm
high alarm
; Wait 200ms
call wait100ms
call wait100ms
; Turn off the alarm.
low alarm
endif
return
lcdLocked:
; Send the LCD reset signal.
serout lcd, N1200, (254,1)
; Wait for the LCD to reset.
pause 60
; Set the current LCD position to the start of line 1.
serout lcd, N1200, (254,128)
; Add "Locked" to the LCD display.
serout lcd, N1200, ("Locked")
; Set the current LCD position to the start of line 2.
serout lcd, N1200, (254,192)
return
unlock:
; Call the reset function.
gosub rset
; Clear the lock flag.
clearbit flgs, lockflg
; Turn off the alarm.
low alarm
; Set the attempt count to zero.
let atmpts = 0x00
; Send the LCD reset signal.
serout lcd, N1200, (254,1)
; Wait for the LCD to reset.
pause 60
; Set the current LCD position to the start of line 1.
serout lcd, N1200, (254,128)
; Add "Unlocked" to the LCD display.
serout lcd, N1200, ("Unlocked")
; Set the current LCD position to the start of line 2.
serout lcd, N1200, (254,192)
; Call the stepper unlock function.
gosub stepperUnlock
return
lock:
; Call the reset function.
gosub rset
; Set the lock flag.
setbit flgs, lockflg
; Call the LCD locked function.
gosub lcdLocked
; Call the stepper lock function.
gosub stepperLock
return
stepperLock:
; Call the step1 function.
call step1
; Wait 10ms.
call wait10ms
; Call the step2 function.
call step2
; Wait 10ms.
call wait10ms
; Call the step3 function.
call step3
; Wait 10ms.
call wait100ms
; Call the step4 function.
call step4
; Wait 10ms
call wait100ms
; Call the step1 function.
call step1
; Wait 10ms.
call wait10ms
; Call the step2 function.
call step2
; Wait 10ms.
call wait10ms
; Call the step3 function.
call step3
; Wait 10ms.
call wait100ms
; Call the step4 function.
call step4
; Wait 10ms
call wait100ms
; Call the step1 function.
call step1
; Wait 10ms.
call wait10ms
; Call the step2 function.
call step2
; Wait 10ms.
call wait10ms
; Call the step3 function.
call step3
; Wait 10ms.
call wait100ms
; Call the step4 function.
call step4
; Wait 10ms
call wait100ms
; Call the stepper off function.
call stepOff
return
stepperUnlock:
; Call the step4 function.
call step4
; Wait 10ms.
call wait100ms
; Call the step4 function.
call step3
; Wait 10ms.
call wait100ms
; Call the step2 function.
call step2
; Wait 10ms
call wait100ms
; Call the step1 function.
call step1
; Wait 10ms
; Call the step4 function.
call step4
; Wait 10ms.
call wait100ms
; Call the step4 function.
call step3
; Wait 10ms.
call wait100ms
; Call the step2 function.
call step2
; Wait 10ms
call wait100ms
; Call the step1 function.
call step1
; Wait 10ms
call wait100ms
; Call the step4 function.
call step4
; Wait 10ms.
call wait100ms
; Call the step4 function.
call step3
; Wait 10ms.
call wait100ms
; Call the step2 function.
call step2
; Wait 10ms
call wait100ms
; Call the step1 function.
call step1
; Wait 10ms
call wait100ms
; Call the stepper off function.
call stepOff
return
step1:
low orange
low black
high brown
high yellow
return
step2:
low brown
low orange
high black
high yellow
return
step3:
low brown
low yellow
high orange
high black
return
step4:
low black
low yellow
high brown
high orange
return
stepOff:
low brown
low orange
low black
low yellow
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment