I was helping a customer with a Prop-2 application and thought it would be interesting to port to the Prop-1. Here's what it does: it monitors four buttons on P0..P3 and when pressed will toggle the associated output on P4..P7. Changes are stored in the EEPROM so that when the unit powers up it returns to the last known state.
The button scanning routine is particularly useful as it scans the buttons and then does a bit of math magic so that the bits of xBtns represent any buttons then went from off to on between scans. By forcing the 0-->1 transition we prevent the oscillating of an output if a button gets stuck on.
' =========================================================================
'
' File...... Rizzo_Relays.BS1
' Purpose...
' Author.... Jon Williams, EFX-TEK
' Copyright (c) 2008 EFX-TEK
' Some Rights Reserved
' -- see http://creativecommons.org/licenses/by/3.0/
' E-mail.... jwilliams@efx-tek.com
' Started...
' Updated... 01 JUL 08
'
' {$STAMP BS1}
' {$PBASIC 1.0}
'
' =========================================================================
' -----[ Program Description ]---------------------------------------------
' -----[ Revision History ]------------------------------------------------
' -----[ I/O Definitions ]-------------------------------------------------
SYMBOL Relay4 = PIN7 ' OUT7 / SETUP = OUT
SYMBOL Relay3 = PIN6 ' OUT6 / SETUP = OUT
SYMBOL Relay2 = PIN5 ' OUT5
SYMBOL Relay1 = PIN4 ' OUT4
SYMBOL Btn4 = PIN3 ' P3.W + P3.R (N.O button)
SYMBOL Btn3 = PIN2 ' P2.W + P2.R (N.O button)
SYMBOL Btn2 = PIN1 ' P1.W + P1.R (N.O button)
SYMBOL Btn1 = PIN0 ' P0.W + P0.R (N.O button)
' -----[ Constants ]-------------------------------------------------------
SYMBOL IsOn = 1 ' for active-high in/out
SYMBOL IsOff = 0
' -----[ Variables ]-------------------------------------------------------
SYMBOL nBtns = B2 ' new buttons
SYMBOL oBtns = B3 ' old buttons
SYMBOL xBtns = B4 ' changed (0 --> 1) buttons
SYMBOL relays = B5 ' current relay outs
SYMBOL lastRelays = B6 ' last relay outs
SYMBOL idx = B7 ' loop controller
SYMBOL rToggle = B8 ' relay toggle bits
' -----[ Initialization ]--------------------------------------------------
Reset:
READ 0, PINS ' get previous setting
DIRS = %11110000 ' make P4-P7 outputs
lastRelays = PINS & %11110000
' -----[ Program Code ]----------------------------------------------------
Main:
GOSUB Scan_Buttons ' scan buttons
rToggle = xBtns * 16 ' shift left by 4 bits
PINS = PINS ^ rToggle ' toggle selected outputs
relays = PINS & %11110000 ' isolate relay bits
IF relays = lastRelays THEN Main ' any changes?
lastRelays = relays ' save for next scan
WRITE 0, relays ' save for power-up
PAUSE 100
GOTO Main
' -----[ Subroutines ]-----------------------------------------------------
Scan_Buttons:
nBtns = %00001111 ' assume pressed
FOR idx = 1 TO 10
nBtns = nBtns & PINS ' scan P0..P3
PAUSE 5
NEXT
xBtns = nBtns ^ oBtns & nBtns ' look for 0 --> 1 changes
oBtns = nBtns ' save for next time
RETURN
' -------------------------------------------------------------------------
' -----[ User Data ]-------------------------------------------------------
Saved_Relays:
EEPROM (%00000000)