View previous topic :: View next topic |
Author |
Message |
davidbue
Joined: 05 Nov 2007 Posts: 28 Location: Denmark
|
Read Hex as bit stream and send through a single pin. |
Posted: Mon Jun 09, 2008 10:57 am |
|
|
Ok. Here's the problem that I just can't seem to solve:
I've got a HEX, example 0x6F.
0x6F = 01101111 in binary.
I want to read the hex and toggle a pin high and low according to the binary pattern.
The output on the pin, as for 0x6F should be like:
output_low(pin_b3);
delay_ms(1);
output_high(pin_b3);
delay_ms(1);
output_high(pin_b3);
delay_ms(1);
output_low(pin_b3);
delay_ms(1);
output_high(pin_b3);
delay_ms(1);
output_high(pin_b3);
delay_ms(1);
output_high(pin_b3);
delay_ms(1);
output_high(pin_b3);
-following the bit-pattern In other words, I'm imagining the code to look like this:
int8 Hexdata;
Hexdata = 0x6F;
while ('Reading binary stream from Hexdata'){
if('current binary number is 1'){
output_high(pin_b3);
}else{
output_low(pin_b3);
}
delay_ms(1);
}
How the heck do I do that? I'm not a total novice, but I feel so dumb, having tried to solve this problem for days.
I'll mail you a postcard, if you're the first to come up with a solution that I can understand! |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Mon Jun 09, 2008 11:51 am |
|
|
You say you are reading the data from a serial bit stream and you want to send it through a single pin. Sending data through a single pin is often referred to as serial communication. Now what I don't understand is that you are capable of reading the serial data but you don't know how to transmit serial data?
Your pseudo code is crude but should do the job. So what is the exact part you don't understand?
Sending data out of a serial pin is a very common job, most PIC processors even have specialized hardware for this: SPI, I2C and UART. |
|
|
asmallri
Joined: 12 Aug 2004 Posts: 1636 Location: Perth, Australia
|
|
Posted: Mon Jun 09, 2008 12:55 pm |
|
|
Code: | int8 Hexdata = 0x6f;
int8 count;
for (count = 0; count < 8; count++)
{
if (Hexdata & 0x01)
output_high(pin_b3)
else
output_low(pin_b3);
Hexdata >>= 1;
delay_ms(1)
} |
_________________ Regards, Andrew
http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!! |
|
|
crystallattice Guest
|
|
Posted: Mon Jun 09, 2008 1:20 pm |
|
|
or you could use the shift functions like the shift_left for example |
|
|
davidbue
Joined: 05 Nov 2007 Posts: 28 Location: Denmark
|
Solved the problem. |
Posted: Mon Jun 09, 2008 2:05 pm |
|
|
Hi everybody!
I solved the problem myself using the following:
Code: |
int8 c;
c = 0x6F;
for (currentbit = 0; currentbit < 8; currentbit++){
if( bit_test(c,currentbit) ){ //If this bit is 1
output_high(pin_b5);
}else{
output_low(pin_b5);
}
delay_ms(1);
}
|
The postcard goes to asmallri as he was the first with a similar solution. Please send me a PM with the address to which you want your postcard with a scenic view of the capital of Denmark.
/David |
|
|
|