PDA

View Full Version : How do I detect a polarity change on an input?



jessey
- 12th January 2008, 22:00
Hello,

I'm looking to write a bit of code that will only execute when there's a change of state, either from high to low or vise versa of some of the inputs on my 16f877 and I just can't seem to wrap my head around on how to accomplish that.

What I'm doing is:
I'm using a LAB-XT Telephony Experimenter's Board to generate the high and low signals that will be sent to various inputs of my 16f877.

I've taken apart an X10 remote control and have soldered multiple wires to the circuit board and will use transistors to simulate button presses via the 16f877 to turn On/Off various transceivers and receivers modules located through-out my house. When the 877 is first turned on then it looks to see what the polarity of the voltages were before the power was turned off (saved in the eeprom) and jumps to a subroutine to restore the last known state then the program jumps to the mainloop to poll the inputs for any polarity changes that will be generated by the Telephony Experimenter's Board.


This is the code that I want to be able to execute in the mainloop but the way it is, it'll just keep GOSUBing and will be repeatedly turning either On or Off the X10 modules depending on the polarity. Anyone have any suggestions? Any help will be greatly appreciated.


' start of program...
mainloop:

'If request 1 changes states then poll the 2 If-Then's below
IF A_Request_For_1 = To_Turn_On THEN GOSUB Turn_On_Relay_1
IF A_Request_For_1 = To_Turn_Off THEN GOSUB Turn_Off_Relay_1
'ENDIF

'If request 2 changes states then poll the 2 If-Then's below
IF A_Request_For_2 = To_Turn_On THEN GOSUB Turn_On_Relay_2
IF A_Request_For_2 = To_Turn_Off THEN GOSUB Turn_Off_Relay_2
'ENDIF

'If request 3 changes states then poll the 2 If-Then's below
IF A_Request_For_3 = To_Turn_On THEN GOSUB Turn_On_Relay_3
IF A_Request_For_3 = To_Turn_Off THEN GOSUB Turn_Off_Relay_3
'ENDIF

' ect, ect.........

GOTO mainloop


Thanks
jessey

BobK
- 13th January 2008, 15:48
Hi Jessey,

Probably what you want to do is set up flags for each input and make them a bit in the PICs EEPROM. (By storing them in the PIC's EEPROM, you can restart your program pretty much where your left off.) For example, let's call your first switch Input0. We'll call the flag for this switch In0. The same setup would be used for the other switches. You can also set a flag for "new start" and "restart" and have a routine check for the status of this bit before continuing on with the program.

When your system is first setup, In0 will be set to 0. When the switch condition = 0 and the flag = 0 nothing would be done as it was already setup as normal. Now your loop that checks the inputs would "scan" all the switches. When there is a difference between Input(x) and Flag(x) your IF..Then sequence would select the appropriate subroutine to go to. If the current loop value (either 1 or 0) is the same as the flag, then nothing really needs to be done. If there is a change the program would then handle it, change the status flag and resume the normal program.

I do most of my programs like this and for me it works quite well.

HTH,

BobK

I know there must be several ways to handle this type of programming but I like to keep my programs real easy to follow. Maybe someone else will jump in here during the day an enlighten you AND ME! When I get off of work this evening I see if I can send you a sample of code that I used.

Darrel Taylor
- 13th January 2008, 19:47
This doesn't use EEPROM, but I think it does the original request.


' start of program...

LastRequest_1 VAR BYTE : LastRequest_1 = 255
LastRequest_2 VAR BYTE : LastRequest_2 = 255
LastRequest_3 VAR BYTE : LastRequest_3 = 255

mainloop:

'If request 1 changes states then poll the 2 If-Then's below
if A_Request_For_1 <> LastRequest_1 then
LastRequest_1 = A_Request_For_1
IF A_Request_For_1 = To_Turn_On THEN GOSUB Turn_On_Relay_1
IF A_Request_For_1 = To_Turn_Off THEN GOSUB Turn_Off_Relay_1
ENDIF

'If request 2 changes states then poll the 2 If-Then's below
if A_Request_For_2 <> LastRequest_2 then
LastRequest_2 = A_Request_For_2
IF A_Request_For_2 = To_Turn_On THEN GOSUB Turn_On_Relay_2
IF A_Request_For_2 = To_Turn_Off THEN GOSUB Turn_Off_Relay_2
ENDIF

'If request 3 changes states then poll the 2 If-Then's below
if A_Request_For_3 <> LastRequest_3 then
LastRequest_3 = A_Request_For_3
IF A_Request_For_3 = To_Turn_On THEN GOSUB Turn_On_Relay_3
IF A_Request_For_3 = To_Turn_Off THEN GOSUB Turn_Off_Relay_3
ENDIF

' ect, ect.........

GOTO mainloop

BobK
- 14th January 2008, 01:49
Hi Jessey,
Here are the code snippets I created to handle the inputs to my project. I used a 4051 with 8 inputs feeding into a window comparator. The two outputs of the window comparator gave me one signal for loop open condition and another for loop shorted condition. PortC pins 5,6, and 7 were the bcd out to select one of 8 inputs to be tested by the micro. First is tested for an open loop, then it tested for a shorted loop. If either condition was already handled then a bit flag is set when the condition is first handled and the bit flag gets reset when the reset of the condition is handled. I did this code 3 years ago and was my first big project so the coding is rather crude and I'm sure it can be done some other way but it did the job and has been working great in the field since so I guess that's all that matters.

loop0op:
portC.5 = 0: portC.6 = 0: portC.7 = 0 'set portC to look at 1st loop
pause delay 'take a short break
If portA.0 = 0 Then G0on 'if loop is open turn led on
If portA.0 = 1 then G0off 'if loop is normal turn led off
goto loop0sh 'go test for shorts

loop0sh:
If portA.1 = 0 then R0on 'if loop is shorted turn led on
If portA.1 = 1 then R0off 'if loop is normal turn led off
Goto loop1op 'goto next loop


G0on:
If Bit0 = 1 then loop0sh 'if already open goto next test
Portb.0 = 1 'turn led on
Bit0 = 1 'set flag that loop is open
Goto loop0sh 'go test for shorts

G0off:
If Bit0 = 0 then loop0sh 'If loop is normal goto short test
Portb.0 = 0 'turn led off
Bit0 = 0 'reset flag
Goto loop1op 'goto next test

R0on:
If Bit8 =1 then loop1op 'if already tripped goto next loop
PortD.0 = 1 'turn led on
B2 = 120 'set apt #
gosub Sendata 'send apt # to main display board
Pause 10
Bit8 = 1 'set flag that we're tripped
Goto loop1op 'goto next test

R0off:
PortD.0 = 0 'turn led off
pause 10
if bit8 = 0 then loop1op 'if flag already cleared then next loop
B2 = 280 'restore code for 1st zone
gosub Sendata 'send restore report to main board
Bit8 = 0 'clear flag so we're back to normal
Goto loop1op 'goto next test


Hope this helps you!

BobK

jessey
- 14th January 2008, 16:05
Thanks BobK and Darrel,

I tried Darrels code snip and it works great to lite Led's and extinguish them when I phone my LabXT board and enter the correct DTMF's from my cell phone, now all I have to do is connect the wires from my X10 transmitter when I get off work tonight. Thanks a lot Darrel, I really appericate your input, could you please explain the logic as to how the <> works and why you set each byte variable to 255? I'd sure be interested to be able to understand how and why that works.

Also thanks for your code snip BobK, I'll go over it in detail when I get home tonight. That's great guys....

Thanks Again
jessey

CocaColaKid
- 14th January 2008, 18:51
The <> just means the two values are not equal.

Darrel Taylor
- 14th January 2008, 20:10
Right, it's not equal to.
By means of being either Less Than or Greater Than, just like >= would be greater than or Equal to.

It's initialized to 255 to make sure it always triggers on the first pass.
If it were left at 0 (assuming there's a CLEAR statement), and the first Request was also a 0, then the code wouldn't execute and the output state would be left Undefined.

hth,

jessey
- 15th January 2008, 07:34
Very Clever Darrel,

Thanks for the explanation and the code, I totally understand it now. It took me a few minutes to fully get it and once I did I could see with-out a doubt that it's fool proof.

Thanks Again
jessey

ardhuru
- 17th January 2008, 09:15
Jessey, although your original query has been answered, I thought I would add my opinion as well.

When you speak of the X10 transmitter I assume you are referring to an X10 RF transmitter, right?

In which case, why not get rid of the X10 remote altogether, and drive an RF transmitter module directly with your 877? The RF protocol used by X10 is well documented (its actually the NEC IR protocol) and I could send you the URL(s). Besides the hardware being much simpler, you could send as many different commands and addresses as you wish without having to add more transistors.

Regards,

Anand

ardhuru
- 17th January 2008, 09:16
Forgot to add, there are readymade basic snippets on the web that directly let a stamp drive an X10 firecracker as well; would achieve the same objective you seek.

Regards,

Anand

jessey
- 19th January 2008, 06:31
Thanks Anand,

I've been away visiting with a friend without a computer (hard to believe now a days). Now that's something that I'd definitely be interested in investigating. I surely would appericate receiving the URL's if you could please send them to me.

Thanks Again
jessey

ardhuru
- 19th January 2008, 18:23
Hi Jessey,

To begin with, here's Dave excellent page on the X10 RF protocol http://www.arkis.net/rf_protocol.htm.

This would give you a pretty good starting point.

The other approach would be to use a CM17A (Firecracker) module; this would eliminate the need for a separate RF transmitter. Theres loads of basic software available to drive it; one such page is at http://www.awce.com/firecracker.htm

Hope this helps!

Regards,

Anand

jessey
- 3rd February 2008, 00:22
Hello Anand,

Thanks for the links, I've had some time now to ponder over the firecracker stamp code and have some questions if anyone can help me. I've never had any experience using the Stamp so it'll definitely be a learning experience for me. I ordered a firecracker and expect to have it in a week or so. It sure would be nice to be able to get rid of all the reed relays and transistors that I'm using now on my modified X10 remote control.

I've checked the archives for any conversion tips for converting Stamp code to PBP and I did manage to find someone quoting that there are no NIB's in PBP so I changed all the NIB's to BYTE variables as the thread recommended.

I also found that the stamp code didn't declare the port pins for the push buttons so I declared them as well. Another thing I did was to change the rts & dtr from CON statements to declaring them as port variables to enable setting them high and low in the PBP code for the transmitter (firecracker) and I'm also assuming that all I have to do for the ground of the transmitter is to tie it to the VSS of my 16F877.

What I don't understand is that when I try to compile the code I get 3 errors:
"error.macro and?bbl not found in macro file", is this some sort of an include file that I'm missing or something that I have to add or declare somewhere?

Also I get two bad expressions, one for "READ housetbl+house,byt" and the other for "READ unittbl+(unit//8),tmp", is there a conversion for these two that will work for PBP? Could it be that the Data statement is wrong for PBP and possibly it needs an @ sign in there?

Any help in understanding this code will be greatly appreciated.

Thanks
jessey



'************************************************* ***********
'* Name : FIRECRACKER.BAS *
'* Author : Jessey Montgomery *
'* Date : Feb. 2nd, 2008 *
'* 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) NOT USED
TRISB.5 = 1 '(pin 38) NOT USED
TRISB.6 = 1 '(pin 39) NOT USED
TRISB.7 = 1 '(pin 40) NOT USED

'-------------------- 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) .......Pushbutton or input from LabXT
TRISC.5 = 1 '(pin 24) .......Pushbutton or input from LabXT
TRISC.6 = 1 '(pin 25) .......Pushbutton or input from LabXT
TRISC.7 = 1 '(pin 26) .......Pushbutton or input from LabXT

'-------------------- 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 _HS_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

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

' Define Variables Here
' ---------------------
' 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

'these push buttons weren't declared
in11 VAR PORTC.4 ' (pin 23)..Pushbutton or input from LabXT
in10 VAR PORTC.5 ' (pin 24)..Pushbutton or input from LabXT
in9 VAR PORTC.6 ' (pin 25)..Pushbutton or input from LabXT
in8 VAR PORTC.7 ' (pin 26)..Pushbutton or input from LabXT


' Firecracker Interface (Al Williams)
' 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
rts VAR PORTD.0 ' pin 19
dtr VAR PORTD.1 ' pin 20

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

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

gosub resetfirecracker

' Sample program
main:
' all off
for x=0 to 3
house=0
unit=x
cmd=0
gosub sendcmd
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:
gosub sendcmd
pause 250
state(x)=cmd
goto mainloop

' 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
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
' should there be an @ somewhere below for pbp?
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

Darrel Taylor
- 3rd February 2008, 01:24
Hi Jessey,

Can't comment on the FireCracker thingy, but I beleive the and?bbl problem is in the
&nbsp; &nbsp; if byt & $80 = $80 then xmit1
Statement.

Putting parenthesis around the bitwise AND should fix it.
&nbsp; &nbsp; if (byt & $80) = $80 then xmit1
<br>

Darrel Taylor
- 3rd February 2008, 01:27
Oh, and putting the EEPROM Data at the top of the program instead of the bottom, should fix the other error.

hth,

jessey
- 3rd February 2008, 05:01
Thanks Darrel,

That worked out great, now the boring part is that I have to wait until my firecracker makes it here to see how it works as is before I start modifying the program to interface to my LabXT telephone board.

Thanks Again
jessey

jessey
- 7th February 2008, 03:47
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




'************************************************* ***********
'* 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

jessey
- 28th May 2008, 07:35
Hello Everyone,

I'm trying to compile the program X10_firecracker.bas contained in my last post here dated the 6th February 2008 to a 18F452 and am getting these two warnings below?

Warning[202] c\pbp\pond controller~1.mac67:Argument out of range. Least significant bits used.

Warning[202] c\pbp\pond controller~1.mac83:Argument out of range. Least significant bits used.

When I comment out these two lines of code below in the program then it compiles:
READ housetbl+house,byt
READ unittbl+(unit//8),tmp

Would anyone have any suggestions as to why it compiles for the 877 but not for the 452 as I am at a total loss on this one? Any help will be greatly appreciated.

Thanks
jessey

Archangel
- 28th May 2008, 08:44
Hi Jessey,
It compiles without errors if you use PBL to compile. Gotta have ver 2.5 for that.
JS

mister_e
- 28th May 2008, 14:41
I have the same compilation eror here... solved if i used...

READ (0+housetbl+house),byt
READ (0+unittbl+(unit//8)),tmp

HTH

jessey
- 28th May 2008, 23:04
Many thanks Joe S and mister e,

I think I'll be upgrading to 2.50 pretty soon and in the meantime thanks a lot mister e for that work around. Very much appreciated!

Thanks Again
jessey

mister_e
- 28th May 2008, 23:25
I had the problem with PBP 2.50... anyways, it always worth to upgrade.