Hello Everyone,

I just wanted to thank you guys, Darrel, Anand and BobK for your help and to show the results of Al Williams Stamp-to-Firecracker code translated into PBP. Maybe someone here that's thinking about getting involved with X10 will find it interesting. It works like a charm and I'm really pleased with the results. I'll do away with the push buttons tomorrow night and connect the LabXT outputs to this circuit and it should work great. Using X10 modules with the LabXT's remotext.bas sample program will make the remote control functions over the telephone really easy to implement.

Thanks Again
jessey


Code:
'************************************************************
'*  Name    : X10_firecracker.BAS                           *
'*  Author  : Al Williams, modifyed by Jessey Montgomery    *
'*  Date    : 1/23/2006                                     *
'*  Version : PBP ver 2.45, MicroCode Studio 2.1.0.7        *
'*          : MPASM v03.60, EPIC Version 2.46 beta          *
'*          : 16F877                                        *
'************************************************************

'-------------------------- NOTES --------------------------'


'                          --------------                     
'                          |   16F877   |                     
'         MCLR/Vpp ------> |1         40| <-----> RB7/PGD     
'          RA0/AN0 <-----> |2         39| <-----> RB6/PGC     
'          RA1/AN1 <-----> |3         38| <-----> RB5         
'    RA2/AN2/Vref- <-----> |4         37| <-----> RB4         
'    RA3/AN3/Vref+ <-----> |5         36| <-----> RB3/PGM     
'        RA4/TOCK1 <-----> |6         35| <-----> RB2         
'       RA5/AN4/SS <-----> |7         34| <-----> RB1         
'       RE0/RD/AN5 <-----> |8         33| <-----> RB0/INT0    
'       RE1/WR/AN6 <-----> |9         32| <-----  Vdd +       
'       RE2/CS/AN7 <-----> |10        31| <-----  Vss -       
'            + Vdd  -----> |11        30| <-----> RD7/PSP7    
'            _ VSS  -----> |12        29| <-----> RD6/PSP6    
'        OSC/CLKIN  -----> |13        28| <-----> RD5/PSP5    
'      OSC2/CLKOUT <------ |14        27| <-----> RD4/PSP4    
'  RC0/T1OSO/TICK1 <-----> |15        26| <-----> RC7/RX/DT   
'   RC1/T1OSI/CCP2 <-----> |16        25| <-----> RC6/TX/CK   
'         RC2/CPP1 <-----> |17        24| <-----> RC5/SDO     
'      RC3/SCK/SCL <-----> |18        23| <-----> RC4/SD1/SDA 
'         RD0/PSP0 <-----> |19        22| <-----> RD3/PSP3    
'         RD1/PSP1 <-----> |20        21| <-----> RD2/PSP2    
'                          --------------              

'-----------------------------------------'
'  Clear All The Variables And Each Port  '
'   Before Setting The TRIS Registers     '
'-----------------------------------------'
 
  Clear     ' Set all ram registers to zero
  PORTA = 0
  PORTB = 0                           
  PORTC = 0   
  PORTD = 0
  PORTE = 0
 
'-----------VSS VDD MCLR Ect. Pins -----------'
 
' MCLR         (Pin 1)
' VDD          (Pin 11)
' VSS          (Pin 12)
' OSC/CLKIN    (Pin 13)
' OSC2/CLKOUT  (Pin 14)
' VSS          (Pin 31)
' Vdd          (Pin 32)
 
 '-----------SET THE TRIS PINS BEING USED-----------'         
 
 '-------------------- PORTA PINS --------------------'
             
  TRISA.0 = 1 '(pin 2) NOT USED  
  TRISA.1 = 1 '(pin 3) NOT USED 
  TRISA.2 = 1 '(pin 4) NOT USED 
  TRISA.3 = 1 '(pin 5) NOT USED 
  TRISA.4 = 1 '(pin 6) NOT USED  
  TRISA.5 = 1 '(pin 7) NOT USED  
 
 '-------------------- PORTB PINS --------------------'
 
  TRISB.0 = 1 '(pin 33) NOT USED   
  TRISB.1 = 1 '(pin 34) NOT USED   
  TRISB.2 = 1 '(pin 35) NOT USED   
  TRISB.3 = 1 '(pin 36) NOT USED  
  TRISB.4 = 1 '(pin 37) .......Pushbutton or input from LabXT 
  TRISB.5 = 1 '(pin 38) .......Pushbutton or input from LabXT    
  TRISB.6 = 1 '(pin 39) .......Pushbutton or input from LabXT  
  TRISB.7 = 1 '(pin 40) .......Pushbutton or input from LabXT 
 
 '-------------------- PORTC PINS --------------------'

  TRISC.0 = 1 '(pin 15) NOT USED
  TRISC.1 = 1 '(pin 16) NOT USED
  TRISC.2 = 1 '(pin 17) NOT USED 
  TRISC.3 = 1 '(pin 18) NOT USED  
  TRISC.4 = 1 '(pin 23) NOT USED 
  TRISC.5 = 1 '(pin 24) NOT USED  
  TRISC.6 = 1 '(pin 25) NOT USED 
  TRISC.7 = 1 '(pin 26) NOT USED     
 
 '-------------------- PORTD PINS --------------------'
 
  TRISD.0 = 0 '(pin 19) ........used to power the firecracker   
  TRISD.1 = 0 '(pin 20) ........used to power the firecracker    
  TRISD.2 = 1 '(pin 21) NOT USED    
  TRISD.3 = 1 '(pin 22) NOT USED  
  TRISD.4 = 1 '(pin 27) NOT USED   
  TRISD.5 = 1 '(pin 28) NOT USED   
  TRISD.6 = 1 '(pin 29) NOT USED   
  TRISD.7 = 1 '(pin 30) NOT USED  
 
 '-------------------- PORTE PINS --------------------'
 
  TRISE.0 = 1 '(pin 8)  NOT USED   
  TRISE.1 = 1 '(pin 9)  NOT USED   
  TRISE.2 = 1 '(pin 10) NOT USED


DEFINE OSC 4         ' 4MHz crystal
 
@ __CONFIG _XT_OSC &_BODEN_OFF &_PWRTE_ON &_WDT_ON &_CP_OFF &_LVP_OFF
' turn off the Crossing page boundary warnings
@ errorlevel -306 

 ADCON1 = 7   ' Set PORTA and PORTE to digital

' 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

' This is a link to the orginal code below, 
' http://www.awce.com/firecracker.htm

' Define Variables Here
' ---------------------
 k VAR BYTE ' used in If-Then's instead of using GOTO's
 K = 0      
' byte to send 
 byt var byte  
 tmp var byte
 i var BYTE 'was a nib
 x var BYTE 'was a nib
 state var bit(4)
 house var byte '0=A 1=B...
 unit var BYTE  ' 0-15  (must be 0 for bright/dim) 'was a nib
 cmd var BYTE  ' 0=off 1=on 2=bright 3=dim 'was a nib

'Thanks to Darrel Taylor for coming up with this code routine 
'for the mainloop...
'Added these var's to prevent receivers from constantly 
'turning On/Off, in the mainloop, the push button 
'IF-THEN's are only (looked at) executed when there's 
'a change of state i.e. whenever a button is pressed.....
 LastRequest_1  VAR BYTE 
 LastRequest_1 = 255      
 LastRequest_2  VAR BYTE  
 LastRequest_2 = 255      
 LastRequest_3  VAR BYTE 
 LastRequest_3 = 255     
 LastRequest_4  VAR BYTE  
 LastRequest_4 = 255     

 in11 VAR PORTB.4 ' (pin 37)..Pushbutton or input from LabXT
 in10 VAR PORTB.5 ' (pin 38)..Pushbutton or input from LabXT
 in9  VAR PORTB.6 ' (pin 39)..Pushbutton or input from LabXT
 in8  VAR PORTB.7 ' (pin 40)..Pushbutton or input from LabXT


' Firecracker Interface (Al Williams)
' http://www.al-williams.com/awce
' These are the 3 connections to the firecracker are below...
' Wire Ground (DB9 pin 5)
rts VAR PORTD.0 ' pin 19 connects to DB9 pin 4 of the firecracker  
dtr VAR PORTD.1 ' pin 20 connects to DB9 pin 7 of the firecracker 

' The Firecracker header (2 bytes)
h1 con $D5
h2 con $AA

' the Firecracker footer (1 byte)
foot con $AD

gosub resetfirecracker

 mainloop:
  if in11 <> LastRequest_1 then
    LastRequest_1 = in11
    IF in11 = 0 THEN '...Push Button (pin 37) or input from LabXT  
      unit = 0 : cmd = 0
      gosub sendcmd : pause 250
    ENDIF
    if in11 = 1 then  
      unit = 0 : cmd = 1
      gosub sendcmd : pause 250
    ENDIF
  ENDIF

  if in10 <> LastRequest_2 then
    LastRequest_2 = in10
    IF in10 = 0 THEN '...Push Button (pin 38) or input from LabXT  
      unit = 1 : cmd = 0
      gosub sendcmd : pause 250
    ENDIF
    if in10 = 1 then  
      unit = 1 : cmd = 1
      gosub sendcmd : pause 250
    ENDIF
  ENDIF

  if in9 <> LastRequest_3 then
    LastRequest_3 = in9
    IF in9 = 0 THEN '...Push Button (pin 39) or input from LabXT  
      unit = 2 : cmd = 0
      gosub sendcmd : pause 250 
    ENDIF
    if in9 = 1 then  
      unit = 2 : cmd = 1
      gosub sendcmd : pause 250 
    ENDIF
  ENDIF

  if in8 <> LastRequest_4 then
    LastRequest_4 = in8
    IF in8 = 0 THEN '...Push Button (pin 40) or input from LabXT  
      unit = 3 : cmd = 0
      gosub sendcmd : pause 250 
    ENDIF
    if in8 = 1 then  
      unit = 3 : cmd = 1
      gosub sendcmd : pause 250 
    ENDIF
  ENDIF

 IF k = 0 THEN mainloop ' continue checking for button presses...
' End of example program

' Send command
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
  IF k = 0 THEN 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

END