Reputation: 1
I have got a raspberry pi bit banging at 1.25MBaud on the GPIO pins. The raspberry pi is to emulate an end system in a RS485 communications network. So hopefully that is the physical layer working :)
I am now struggling with the application layer. The c code libraries require Ascii for example "Hello, world!". It would be desirable for me to pass a binary stream ie "111010111010001010111", there will be 5814 bits :) so I have been researching how to convert binary to ascii however only found complicated c++ answers. Does anyone have a simple solution? Thanks for your help!
Paul
Upvotes: 0
Views: 256
Reputation: 3891
For move 7-bits ASCII-char to the port bit-by-bit, you can use:
void toPort(unsigned char x) {
x |= 0200; // Set unused high-bit to 1
for ( ; ; ) {
char bit = x & 1;
x >>= 1;
if(x == 0) return;
push_to_port(bit);
}
}
Upvotes: 0
Reputation: 8830
Apparently you don't need to break the string into bits the API does it for you....
Anyway if you did:
Each char in your "hello world\n" string is a byte when encoded in ASCII. ASCII actually only used the lowest 7 bits. If you want to pick apart a char into bits I would use the following code as a start.
char ch = 'h';
for (i=0; i<7; i++) {
bool b = (ch & 1 == 1);
ch >>= 1;
// set the bit value b off to the pin....
}
you would need a loop around this to process whole strings.
Upvotes: 1