PDA

View Full Version : How can I shorten this code - 16 1's and 0's into one number



Christopher4187
- 5th July 2012, 21:14
I've been trying this for a few hours but no luck. I'm using wireless communication and sending 18 separate bytes of data is an issue because the packet is so long. As I shorten it the reliability goes up. Anyhow, I have the following on the TX side (16F876):

b1=PORTA.5 'PIN 1
B2=PORTA.2 'PIN 2
b3=PORTA.1 'PIN 3
b4=PORTA.0 'PIN 4
b5=PORTB.1 'PIN 5
B6=PORTB.5 'PIN 6
b7=PORTB.4 'PIN 7
b8=PORTB.2 'PIN 8
b9=PORTC.0 'PIN 9
B10=PORTC.1' PIN 10
b11=PORTC.2 'PIN 11
b12=PORTC.3 'PIN 12
B13=PORTC.7 'PIN 13
B14=PORTC.5 'PIN 14
b15=PORTC.4 'PIN 15
b16=PORTB.0 'PIN 16

The variables above are switches. When I get ready to send the data, I send out this packet:

HSEROUT [start,b1,b2,b3,b4,B5,b6,B7,B8,B9,B10,B11,B12,B13,B 14,B15,B16,sum.byte0]

On the RX side, the incoming message is decoded like this:

HSERIN 30, xfer, [start, IN1, IN2, IN3, IN4,IN5,in6, in7, IN8, IN9, IN10, IN11, IN12, IN13, IN14, IN15, IN16 ,sum]

My laymans approach would be to do this:

HSEROUT [START, B1, SUM.BYTE0]

B1 could be 17 as an example. On the incoming message, the LED's would light like this:

1000100000000000

Can something like this be done?

FromTheCockpit
- 5th July 2012, 21:23
Before sending you can assign each bit to a word type variable. something Like this
Word_Var.0[0]=b1
Word_Var.0[1]=b2
.
.
And so on
Then just send the Word_Var which is just 2 bytes.

Christopher4187
- 5th July 2012, 23:21
This is a little above my level. I see a potential problem but I'm not 100% sure. I think I can only send max word variables so the sendpacket would be 2 word variables, right? This is what my TX side looks like (I put known numbers in there for now so I know what to expect on the receiving end):


SENDPACKET.0[0]=1
SENDPACKET.0[1]=1
SENDPACKET.0[2]=1
SENDPACKET.0[3]=1
SENDPACKET.0[4]=1
SENDPACKET.0[5]=0
SENDPACKET.0[6]=1
SENDPACKET.0[7]=0
SENDPACKET.0[8]=1
SENDPACKET.0[9]=1
SENDPACKET.0[10]=1
SENDPACKET.0[11]=1
SENDPACKET.0[12]=1
SENDPACKET.0[13]=0
SENDPACKET.0[14]=1
SENDPACKET.0[15]=0

sum = SENDPACKET.0[0]+SENDPACKET.0[1]+SENDPACKET.0[2]+SENDPACKET.0[3]+SENDPACKET.0[4]+SENDPACKET.0[5]+SENDPACKET.0[6]+SENDPACKET.0[7]+SENDPACKET.0[8]+SENDPACKET.0[9]+SENDPACKET.0[10]+SENDPACKET.0[11]+SENDPACKET.0[12]+SENDPACKET.0[13]+SENDPACKET.0[14]+SENDPACKET.0[15]

HSEROUT [9,sendpacket,12]

The RX side looks like this:


IF RCSTA.1 = 1 THEN xfer 'USART overflow error - bad data
HSERIN 30, xfer, [start1, sendpacket,sum]

IF start1 <> 9 THEN xfer

datasum = sendpacket
IF datasum = sum THEN errorflag = 0


xfer:


IF errorflag = 0 THEN
high led




B1 = SENDPACKET.0[0]
B2 = SENDPACKET.0[1]
B3 = SENDPACKET.0[2]
B4 = SENDPACKET.0[3]
B5 = SENDPACKET.0[4]
B6 = SENDPACKET.0[5]
B7 = SENDPACKET.0[6]
B8 = SENDPACKET.0[7]
B1 = SENDPACKET.0[8]
B2 = SENDPACKET.0[9]
B3 = SENDPACKET.0[10]
B4 = SENDPACKET.0[11]
B5 = SENDPACKET.0[12]
B6 = SENDPACKET.0[13]
B7 = SENDPACKET.0[14]
B8 = SENDPACKET.0[15]

ENDIF

Christopher4187
- 6th July 2012, 04:10
Awesome! I got it working now. It's blazing fast and a ton more reliable as my data packet has shrunk from 16 to 2! Thanks!