PDA

View Full Version : Changing bits in a byte for multiple output



tazntex
- 11th August 2008, 18:30
Hello,
I've been playing with this project for sometime and a lot of ya'll have been a great help.
I am trying to get my transmitter side working and I would like to be able to press more than one button at a time to get multiple outputs to come on if needed. with the below example everything works fine, one button at a time and If I press the button the output stays on until I release the button but if I press more than one button everything comes on and goes off and acts crazy:
INCLUDE "MODEDEFS.BAS"
@ DEVICE PIC16F628a,XT_OSC
@ DEVICE pic16F628a, WDT_OFF
@ DEVICE pic16F628a, PWRT_ON
@ DEVICE pic16F628a, MCLR_ON
@ DEVICE pic16F628a, BOD_ON
@ DEVICE pic16F628a, LVP_OFF
@ DEVICE pic16F628a, CPD_OFF
@ DEVICE pic16F628a, PROTECT_OFF
define osc 4
CMCON=%00000111
trisb = %11111111
trisa = %00010111
chk_sum var byte
dgood var porta.2
serpin var porta.3
nokey con %11111111
synch con 254
address con %00000001
keyin var portb
keydata var byte
E VAR PORTB.0
D VAR PORTB.1
G VAR PORTB.2
A VAR PORTB.3
B VAR PORTB.4
C VAR PORTB.5
F VAR PORTB.6
na VAR PORTB.7
keydata1 var byte
PreAmble CON $A5
findkey:

if A = 0 then
keydata.1=1
keydata.2=1
keydata.4=1
keydata.6=1
endif

if B = 0 then
keydata.0=1
keydata.3=1
keydata.4=1
keydata.6=1
endif

if C = 0 then
keydata.1=1
keydata.3=1
keydata.4=1
keydata.6=1
endif

if D = 0 then
keydata.0=1
keydata.2=1
keydata.5=1
keydata.6=1
endif

if E = 0 then
keydata.1=1
keydata.2=1
keydata.5=1
keydata.6=1
endif


if F = 0 then
keydata.0=1
keydata.3=1
keydata.5=1
keydata.6=1 s
endif

if G = 0 then
keydata.1=1
keydata.3=1
keydata.5=1
keydata.7=1 s
endif
keydata1=keydata
low dgood
pause 100
chk_sum = (address + address)
chk_sum = chk_sum + (keydata + keydata)

serout serpin,N2400,[$55,$55,$55,$55,$55,$AA,$AA,$AA,$AA,$AA,synch,addr ess,address,keydata1,keydata1,chk_sum]
if keyin = nokey then x2
goto findkey

x2:
high dgood
keydata1 = %00000000
keydata = %00000000
serout serpin,N2400,[$55,$55,$55,$55,$55,$AA,$AA,$AA,$AA,$AA,synch,addr ess,address,keydata1,keydata1,chk_sum]
goto findkey

end

If I do it like this:

INCLUDE "MODEDEFS.BAS"
@ DEVICE PIC16F628a,XT_OSC
@ DEVICE pic16F628a, WDT_OFF
@ DEVICE pic16F628a, PWRT_ON
@ DEVICE pic16F628a, MCLR_ON
@ DEVICE pic16F628a, BOD_ON
@ DEVICE pic16F628a, LVP_OFF
@ DEVICE pic16F628a, CPD_OFF
@ DEVICE pic16F628a, PROTECT_OFF
define osc 4
CMCON=%00000111
trisb = %11111111
trisa = %00010111
chk_sum var byte
dgood var porta.2
serpin var porta.3
nokey con %11111111
synch con 254
address con %00000001
keyin var portb
keydata var byte
E VAR PORTB.0
D VAR PORTB.1
G VAR PORTB.2
A VAR PORTB.3
B VAR PORTB.4
C VAR PORTB.5
F VAR PORTB.6
na VAR PORTB.7
PreAmble CON $A5 ' 10100101 preamble
findkey:
if A = 0 then sw0
if B = 0 then sw1
if C = 0 then sw2
if D= 0 then sw3
if E = 0 then sw4
if F = 0 then sw5
if G = 0 then sw6
goto findkey

sw0:

keydata = %01010110
goto dout

sw1:
keydata = %01011001
goto dout
sw2:

keydata = %01011010
goto dout

sw3:

keydata = %01100101
goto dout

sw4:

keydata = %01100110
goto dout

sw5:

keydata = %01101001
goto dout

sw6:

keydata = %01101010
goto dout

dout:
low dgood
pause 100
chk_sum = (address + address)
chk_sum = chk_sum + (keydata + keydata)
goto transmit

transmit:

serout serpin,N2400,[$55,$55,$55,$55,$55,$AA,$AA,$AA,$AA,$AA,synch,addr ess,address,keydata,keydata,chk_sum]
if keyin = nokey then x2
goto findkey

x2:
high dgood
keydata = %00000000
serout serpin,N2400,[$55,$55,$55,$55,$55,$AA,$AA,$AA,$AA,$AA,synch,addr ess,address,keydata,keydata,chk_sum]
goto findkey
end

Then the output selected on the receiver will come on and stay on until I press the next button in which the first output goes off and the second comes on, and so on. So what is the difference between just changing a bit in the byte and sending it serially or changing the whole byte each time? Is there a better way to detects several buttons pressed at the same time and then send the data out?

Thanks

skimask
- 11th August 2008, 18:46
You're making this WAYYYY too hard.
If you've got all of the keys on portB, then just send the whole portB value, all 8 bits, all the keys, all at once, and keep sending them. Even if you don't have all of the buttons on PortB, get all of the buttons states and put them into the bits of a single byte variable and send that.
Get that working so it's infallible, or at least as infallible as you can tolerate it.
Then do all of the hard work at the receiver end figuring out which lights to light or whatever.
I'm thinking you won't get anywhere until that happens...

tazntex
- 11th August 2008, 19:03
Thanks Skimask, your right I have made this difficult but the reason I was not sending out the whole PORTB is that I am trying to use a simple manchester encoding. My 0 is a 01 an a 1 being 10.
Thanks

skimask
- 11th August 2008, 19:10
Thanks Skimask, your right I have made this difficult but the reason I was not sending out the whole PORTB is that I am trying to use a simple manchester encoding. My 0 is a 01 an a 1 being 10.
Thanks
I don't think I would worry about encoding anything at the moment. You're sending short packets. As long as you've got preamble and a sync'ing character, you should be good to send the raw port data, in short bursts anyways.
i.e. SEROUT datapin , mode , whatever , $55,$AA,$FE,raw_portb_data (assuming portb.7 isn't actually used)
Just keep resending it as long as a key is pressed and code the receiver to look for the same thing that you're sending.