Credits: Al Williams
Adapted with permission from the AWC collaboration site , from the original page
Here is a way to connect a Basic Stamp to a firecracker.
For details on the basic stamp, look at the ASP and our PAK coprocessors at http://www.al-williams.com/awce
Under the hood it is simple to connect the Firecracker to the Stamp. I took a female DB9 connector and wired it like this:
DB9 pin Stamp connection
Using the code is simple.
The example program below uses switches wired to P8, P9, P10, and P11. The switches connect between the pins and ground. Each pin also has a pull up resistor (say 10K or 22K) so the pin reads high when the switch is open and low when you close the switch.
The example program turns off all the devices so they are in a known state. Then when you press a button it toggles the state of the corresponding device. Of course, you could write as fancy a program as you wanted to take the time to write. For example, you could use a light sensor to turn the lights on and off automatically. Or light them according to a timer -- use your imagination.
The Code
' http://www.al-williams.com/awce
' Wire Ground (DB9 pin 5)
' DTR (DB9 pin 4)
' RTS (DB9 pin 7)
rts con 1
dtr con 0
' The Firecracker header (2 bytes)
h1 con $D5
h2 con $AA
' the Firecracker footer (1 byte)
foot con $AD
' byte to send
byt var byte
tmp var byte
i var nib
x var nib
state var bit(4)
house var byte '0=A 1=B...
unit var nib ' 0-15 (must be 0 for bright/dim)
cmd var nib ' 0=off
1=on 2=bright 3=dim
' Sample program
main:
' all off
for x=0 to 3
house=0
unit=x
cmd=0
pause 20
state(x)=0
next
mainloop:
if in11=0 then c0 ' look for buttons on p8-11
if in10=0 then c1
if in9=0 then c2
if in8=0 then c3
goto mainloop
c0:
x=0
goto ccmd
c1:
x=1
goto ccmd
c2:
x=2
goto ccmd
c3:
x=3
ccmd:
unit=x
cmd=state(x)+1
if cmd=1 then gocmd
cmd=0
gocmd:
pause 250
state(x)=cmd
goto mainloop
' End of example program
sendcmd:
byt=h1
gosub sendbyte
byt=h2
gosub sendbyte
read housetbl+house,byt
if unit<9 then lowunit
byt=byt+4
lowunit:
gosub sendbyte
byt=$20
if cmd=0 then addunit
byt=0
if cmd=1 then addunit
byt=$88
if cmd=2 then nounit
byt=$98
if cmd=3 then nounit
' huh???
addunit:
read unittbl+(unit//8),tmp
byt=byt+tmp
nounit:
gosub sendbyte
byt=foot
gosub sendbyte
return
' Send 1 raw byte
sendbyte:
debug hex byt," "
for i=0 to 7
if byt & $80 = $80 then xmit1
gosub send0
nextbit:
pause 1
byt=byt*2
next
return
' Send a 1
xmit1:
gosub send1
goto nextbit
' Send bits (0 or 1)
send0:
low rts
pause 1
high rts
return
send1:
low dtr
pause 1
high dtr
return
' Always reset firecracker first
resetfirecracker:
low rts
low dtr
pause 50 ' reset
high rts
high dtr
pause 50
return
' Data for house and unit codes
housetbl data $60,$70,$40,$50,$80,$90,$A0,$B0,$E0,$F0,$C0,$D0
data $00,$10,$20,$30
unittbl data 0,$10,$8,$18,$40,$50,$48,$58