View previous topic :: View next topic |
Author |
Message |
global
Joined: 01 Feb 2005 Posts: 21 Location: Paris
|
RS232 Sending and receiving bytes |
Posted: Thu Apr 21, 2005 1:41 am |
|
|
I am trying to write a driver for a PIC 18LF458. I wanted to send bytes and read bytes using RS232.
I use a simple printf : printf("12345678\n");
It seems working, but i ant really do is to send binary data on one byte :
like this : printf("10011010\n"); on the Tx
Is there something to convert ASCII to binary ?
I read that printf function returned ASCII data.
Otherwise i need to read on the Rx, if i want to read a byte which function should i use ? Is gets() ok ? Or another function ?
Then i should compare the bit one by one to know which state it is.
The states are supposed to be 0 or 1 for example to indicate that a led is on or off.
I wondered if i use masks and "if"s saying for example, if bit 1 is 0 do something, else do something.
Should i use "case" ?
How can i isolate bits one by one ? Do i have to use (bit0 & 0x01) for example to isolate the bit 0?
Can the group please help me to choose how to write my code ?
Many thanks. |
|
|
Gizz
Joined: 15 Jun 2004 Posts: 21 Location: Nottingham UK
|
|
Posted: Thu Apr 21, 2005 4:18 am |
|
|
For individual bytes use getc to get a char, and putch to output a char. (or is it getch and putc?! )
ANDing the char with 0x01, 0x02 is indeed the way to go, to check bits.
Code: |
char c;
while (kbhit())
{
c = getc();
if (c & 0x01 != 0)
{
// ...
}
if (c & 0x02 != 0)
{
// ...
}
}
|
|
|
|
global
Joined: 01 Feb 2005 Posts: 21 Location: Paris
|
|
Posted: Thu Apr 21, 2005 4:21 am |
|
|
I forgot to say that what i receive is something that is sent on the Rx by another component.
It should send something like 01101011. And i should be able to read this with the PIC ...
Do you see what i mean ? |
|
|
Gizz
Joined: 15 Jun 2004 Posts: 21 Location: Nottingham UK
|
|
Posted: Thu Apr 21, 2005 6:55 am |
|
|
No, I don't see what you mean.
Do you mean that...
1) the other component sends a series of ASCII characters that are either 1's or 0's
2) it sends a single ASCII character that is made up of 8 bits which are 01101011
3) it simply sends a random 8 bit pattern on the serial line of 01101011 with no stop, start, parity??!!
What is the application? What is the other component?
You know that sending a number with putch will send a bit pattern down the wire? For example, putch(0x6B) will send 0x6B as binary =
01101011
Though you will get stop and start bits and maybe parity bit as well on the wire itself.
Similarly doing a getc() will produce the value of 0x6B if this bit pattern is read in by the serial port. |
|
|
global
Joined: 01 Feb 2005 Posts: 21 Location: Paris
|
|
Posted: Thu Apr 21, 2005 8:21 am |
|
|
What i want to do is to send un byte that will be in binary format with te PIC when the other component reads it.
Maybe i should use a char on one byte to send it to use putch();
It is supposed to be states of sensors.
For example, if bit0 is 1, it means that the light sensor sees light, if it is 0 it doesn't see light.
So there are sensors that are giving indications. These bits are send as one byte by the other component.
I should be able to read as well read a byte that is send to the PIC in a binary format.
I hope it is clearer !
Thanks. |
|
|
Humberto
Joined: 08 Sep 2003 Posts: 1215 Location: Buenos Aires, La Reina del Plata
|
|
Posted: Thu Apr 21, 2005 10:04 am |
|
|
Quote: |
Is there something to convert ASCII to binary ?
|
ASCII is a way to translate and understand a binary number, but esentially is the same.
To convert ASCII to binary, using the right formatting output will do the job:
printf("%X", char_rcved);
The binary 0x41 by ASCII convention is translated and interpreted as an 'A' character.
To transmit the 'A' what you really is sending is 0x41 = 0b01000001
Quote: |
It should send something like 01101011. And i should be able to read this with the PIC ...
|
If you want to send those bits, (01101011)
putc(0x6B) will send them.
In the receiver side you can rebuild the pattern bit_testing and shifting the received char.
Keep well,
Humberto |
|
|
global
Joined: 01 Feb 2005 Posts: 21 Location: Paris
|
|
Posted: Fri Apr 22, 2005 3:55 am |
|
|
Hello,
thanks a lot, i worked things out !
Only one problem left :
When i try to do this :
Code: |
data = getc();
if(data == 0x62) // ... do something (i know what i do is working because i temporarly got it working by putting this if as a comment)
|
This is not working.
Is my comparison well written or not ? Am i doing something wrong ?
Thanks a lot ! |
|
|
newguy
Joined: 24 Jun 2004 Posts: 1909
|
|
Posted: Fri Apr 22, 2005 8:48 am |
|
|
There's nothing wrong with coding the "if" like that, but you can make things much more readable if you put in the actual character that you're looking for. For instance, instead of
Code: | if (data == 0x62) { ... } |
try
Code: | if (data == 'b') { ... } |
Note the single quote marks around the 'b'. Much better readability this way, with the added bonus that you don't have to look up the ASCII codes for whatever character(s) you're looking for.
I suspect that the problem lies in what follows your if statement. Post that. |
|
|
global
Joined: 01 Feb 2005 Posts: 21 Location: Paris
|
|
Posted: Fri Apr 22, 2005 8:58 am |
|
|
Hello,
i am just doing this :
Code: |
int8 data;
while(1)
{
data = getc();
if(data == 0x62)
{
output_high(LED_2);
delay_ms(50);
output_low(LED_2);
delay_ms(50);
putc(0x58); // send a value to the other component to allow it to re-send 0x62 again
}
}
|
It is working if i dim if(data == 0x62), so the getc() function takes a value but it is not 0x62 i think because the if is not working.
BUT i know that the other component is sending 0x62 once and then wait for the PIC to send something and then re-send 0x62. |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Fri Apr 22, 2005 9:06 am |
|
|
If getc() doesn't return 0x62 then it is quite obvious why the if statement never gets executed. Come on man, think about it. You need to figure out why getc isn't returning the right value. Wrong baud rate, wrong #use delay, ect.. |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Fri Apr 22, 2005 11:11 am |
|
|
If getc() isn't getting 0x62 you need to find out what it IS getting. That will go a long way towards telling you what is wrong. _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Apr 22, 2005 11:59 am |
|
|
Or, it may be because the Caps Lock key is on, and he's getting 'B' instead of 'b'. |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Fri Apr 22, 2005 1:34 pm |
|
|
or maybe he is still trying to do this
Code: |
printf("01100010\n");
| |
|
|
global
Joined: 01 Feb 2005 Posts: 21 Location: Paris
|
|
Posted: Mon Apr 25, 2005 6:19 am |
|
|
Hello there,
thanks for your help.
I found out ! It was the baud rate, i think the other component was at 9600 and i used 19200 !
I Thank you all !
Now i need to use interrupts :
for example if a sensor, which is on a pin on the PIC, returns 1, then i'll have to send an alarm or something ...
Could someone help me to write something to manage this ?
Many thanks. |
|
|
|