View previous topic :: View next topic |
Author |
Message |
christoph Guest
|
output of an 8bit integer as ascii on rs232 |
Posted: Thu Jan 19, 2006 2:54 am |
|
|
hello!
iīd like to print out the status of the whole port_b using input_b(). this returns a 8bit integer. now i will send this value over rs232 in ascii format.
i searched in the posts for integer to char conversion but i canīt find the correct answer.
can anybody help me?
thanks a lot in advance |
|
![](templates/subSilver/images/spacer.gif) |
kypec
Joined: 20 Sep 2003 Posts: 54
|
|
Posted: Thu Jan 19, 2006 4:01 am |
|
|
Look at the built-in printf() function and its arguments. |
|
![](templates/subSilver/images/spacer.gif) |
christoph Guest
|
|
Posted: Thu Jan 19, 2006 5:06 am |
|
|
okay...thank you! as you can see iīm beginner in programming.
i have read the help about printf but i īstill canīt get it working...
would you post me an example? |
|
![](templates/subSilver/images/spacer.gif) |
kypec
Joined: 20 Sep 2003 Posts: 54
|
|
Posted: Thu Jan 19, 2006 5:16 am |
|
|
Code: | printf("%u",input_b()); //outputs 8-bit value of PORTB as ASCII string consisting of 1 to 3 characters
printf("%03u",input_b()); //outputs 8-bit value of PORTB as ASCII string with fixed length = 3 characters with leading zeros |
|
|
![](templates/subSilver/images/spacer.gif) |
christoph Guest
|
|
Posted: Thu Jan 19, 2006 6:18 am |
|
|
thank you!!
i tried you code but with this i donīt get for example "10011100" representing the the port status return - i get the decimal value in ascii-text.
i want to havefor RS232 in ascii: for exampe "Input-Status PortB is 10011100"
so i need a conversion from the 8bit integer to a char-array with 8 records.
you know what i mean? iīm right? |
|
![](templates/subSilver/images/spacer.gif) |
christoph Guest
|
|
Posted: Thu Jan 19, 2006 6:19 am |
|
|
decimal value between 0 and 255 to be correct. |
|
![](templates/subSilver/images/spacer.gif) |
kypec
Joined: 20 Sep 2003 Posts: 54
|
|
Posted: Thu Jan 19, 2006 6:45 am |
|
|
Code: | int8 bit_position,port_value;
port_value=input_b();
printf("Input-Status PortB is ");
for (bit_position=8;bit_position>0;bit_position--) {
if (port_value & 0b10000000) putc('1');
else putc('0');
port_value<<=1;
}
putc(13); //carriage return character alias END OF LINE
|
|
|
![](templates/subSilver/images/spacer.gif) |
christoph Guest
|
|
Posted: Thu Jan 19, 2006 8:47 am |
|
|
thank you... this is the way i tried!
with your code it is "more careful" |
|
![](templates/subSilver/images/spacer.gif) |
|