PDA

View Full Version : How to send a binary string on an output?



Robson
- 25th July 2007, 20:35
Hello,
i have now a problem how to send a binary string (150-200 bits long!) over an output pin eg. RB.0 without parity and stop bits.
Iīm using a 16F628A and my osc is working on 4MHz. Itīs also possible to change with 8MHz if the freq is to slow.
1 state is 1ms long that means if the string is "11010110001111100101" then takes this sending operation 20ms.
I want to realize a communication between my steering wheel control and a JVC headunit. The connection is working over a 2way headphone plug. Normal level is set to HIGH.
Here an example for Vol+ "00000000011111011011011011010101011010101101010101 01101111111111111111111011011011011010101011010101 10101010101101111111111111111111011011011011010101 0110101011010101010110"
Is it possible to send such a string over an output pin or is it necessary to split the string?

Thanks
Robson

keithdoxey
- 25th July 2007, 20:46
First thoughts are

1. declare a bit array of the appropriate size

2. declare your output pin

3. set the bits of the array to the desired state

Then transmit it something like this.....


For x = 0 to {number_of_bits-1)
If bitarray[x] = 1 then
outputpin=1
else
outputpin=0
endif
pause 1 ' wait for 1ms - might need changing for pauseus 990 or similar
Next


I might have the state of the pin wrong for your application but you should get the idea

Robson
- 25th July 2007, 21:28
First thoughts are

1. declare a bit array of the appropriate size

2. declare your output pin

3. set the bits of the array to the desired state

Then transmit it something like this.....


For x = 0 to {number_of_bits-1)
If bitarray[x] = 1 then
outputpin=1
else
outputpin=0
endif
pause 1 ' wait for 1ms - might need changing for pauseus 990 or similar
Next


I might have the state of the pin wrong for your application but you should get the idea

ok but how to define or declare an array like this?

bitarray VAR BIT[200]
bitarray = %1000000000111110110110110110101010110101011010101 01011011111111111111111110110110110110101010110101 01101010101011011111111111111111110110110110110101 0101101010110101010101101

The compiler returns with "Numeric overflow, value truncated."

keithdoxey
- 25th July 2007, 21:53
How do you arrive at the values you want to send ?

Are there a series of bytes that you are converting into the binary array ?

If so declare an array of a suitable number of bytes and set each byte

eg

bytearray VAR BYTE[25]

bytearray[0] = {whatever the value is}
bytearray[1] = {whatever the next value is}
etc


Then transmit those bytes



For MyByte = 0 to 24
ThisByte=bytearray[MyByte]
For MyBit = 0 to 7 ' or 7 to 0 depending on the order !!!!
If ThisByte.0[MyBit] = 1 then
OutputPin=1
Else
OutputPin=0
End if
Pause 1
Next
Next




Without knowing the protocol it is difficult to decide how to set the values.

Havent tried the code above so offer no guarantee that it would work !!!

Your question was how to transmit a binary string, not how to create it in the first place :)

Dont suppose you have any info on the protocol SONY use for stalk controllers do you ?

Just wish I could stop my Sony head unit from resetting the iPod EVERY time you turn it on. SWMBOs JVC doesnt. :(

Robson
- 25th July 2007, 22:23
How do you arrive at the values you want to send ?
I have scanned the signal with a digital scope.

The output is LOW active. I have split the array to different blocks. Each button if pressed begins with the first string. Look below

1000000000111110 ' Init sequence (every button)

110110110110101010110 ' sequence 01

101011010101010110 ' Instruction for Vol+

11111111111111111110 ' preparing for 2nd block

110110110110101010110 ' sequence 01

101011010101010110 ' Instruction for Vol+

11111111111111111110 ' preparing for 3rd block

110110110110101010110 ' sequence 01

101011010101010110 ' Instruction for Vol+

and now the signal stay at HIGH till another button is pressed.
Many blocks like Init sequence, sequence 01, preparing for next block are in all instruction of each button. Only a few bits are different in the instruction line.
But this String must be sent as one data without interrupts or delays. Exactly from every bit 1ms.
Every instruction will be sent 3 times.
When you will paint same blocks in different colours you will better see, how it works. But itīs not possible to mark with colours

Bruce
- 25th July 2007, 22:31
Here's a super easy way to do this. Create command tables
and simply GOSUB to one & output your command bit-stream.

This looks-up X directly onto PORTB.0, pauses, then gets the
next bit.


X VAR BYTE

PORTB.0=0 ' set low on boot (change as required here)
TRISB.0=0 ' RB0 = output

Main:
GOSUB Command1
GOTO Main

Command1:
FOR X = 0 TO 171 ' 172 bits total
LOOKUP X,[0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,1,1,0,1,1,0,1,1,0,1, 1,0,1,0,1,_
0,1,0,1,1,0,1,0,1,0,1,1,0,1,0,1,0,1,0,1,0,1,1,0,1, 1,1,1,1,1,_
1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,0,1,1,0,1,1,0,1,1, 0,1,0,1,0,_
1,0,1,1,0,1,0,1,0,1,1,0,1,0,1,0,1,0,1,0,1,1,0,1,1, 1,1,1,1,1,_
1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,0,1,1,0,1,1,0,1,1,0, 1,0,1,0,1,_
0,1,1,0,1,0,1,0,1,1,0,1,0,1,0,1,0,1,0,1,1,0],PORTB.0
PAUSE 1 ' Note: If this is too long, use PAUSEUS
NEXT X
RETURN

END
You can have up to 255 entries 'or bits' per table with a 14-bit core device.

It does eat-up a boat-load of program memory, but it's ultra simple to use.

Robson
- 25th July 2007, 22:35
255 Bytes are almost enough.
Thanks a lot Bruce.
Howīs about the underscores "_" in the array? Need to use them?

Bruce
- 25th July 2007, 22:53
This isn't actually an array. Lookup creates a table, and uses the
index pointer var (in this case the X) as the offset into the table.

The underscores are optional. It just looks a lot better than having
your constant table scrolling WAY off to the right side.

keithdoxey
- 25th July 2007, 22:54
I have scanned the signal with a digital scope.

Many blocks like Init sequence, sequence 01, preparing for next block are in all instruction of each button.


In that case create separate arrays for

InitSeq
Seq01
NextBlock



Only a few bits are different in the instruction line.


Create separate arrays for each command or one large array with all commands and just point to a segment of it eg bits 34-57 (havent worked out the numbers!)



But this String must be sent as one data without interrupts or delays. Exactly from every bit 1ms.


When you need to send a command just call the component parts as required eg

SendInitSeq
SendSeq01
SendCommand
SendNextBlock
SendSeq01
SendCommand
SendNextBlock
SendSeq01
SendCommand

The PIC should be able to read and process the values quickly enough not to cause problems, if you see that your output pulses are taking slightly too long then change the pause to pauseus and shorten slightly



Every instruction will be sent 3 times.


Just add a loop and go round it 3 times. Presumabley there must be some idle time between the 3 instructions so dont forget to add that as a pause



When you will paint same blocks in different colours you will better see, how it works. But itīs not possible to mark with colours
I pasted it into a text editor and broke it into 4 bit chunks and you can see the patterns you refer to.

I have built a universal remote control that reads different values from different variables on the fly and that works just fine so I doubt you will have too many problems.

I seem to remember seeing something by either Darryl or Melanie that showed how you can access individual bits of a byte or word array as well but cant remember where it was eg byte0 was bits 0-7 byte1 was bits 8-15 but I could be wrong.