CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

RS232 Sending and receiving bytes

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
global



Joined: 01 Feb 2005
Posts: 21
Location: Paris

View user's profile Send private message

RS232 Sending and receiving bytes
PostPosted: Thu Apr 21, 2005 1:41 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Apr 21, 2005 4:18 am     Reply with quote

For individual bytes use getc to get a char, and putch to output a char. (or is it getch and putc?! Confused )
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

View user's profile Send private message

PostPosted: Thu Apr 21, 2005 4:21 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Apr 21, 2005 6:55 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Apr 21, 2005 8:21 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Apr 21, 2005 10:04 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Apr 22, 2005 3:55 am     Reply with quote

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: 1907

View user's profile Send private message

PostPosted: Fri Apr 22, 2005 8:48 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Apr 22, 2005 8:58 am     Reply with quote

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

View user's profile Send private message Send e-mail

PostPosted: Fri Apr 22, 2005 9:06 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Apr 22, 2005 11:11 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Apr 22, 2005 11:59 am     Reply with quote

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

View user's profile Send private message Send e-mail

PostPosted: Fri Apr 22, 2005 1:34 pm     Reply with quote

or maybe he is still trying to do this

Code:

 printf("01100010\n");
Very Happy
global



Joined: 01 Feb 2005
Posts: 21
Location: Paris

View user's profile Send private message

PostPosted: Mon Apr 25, 2005 6:19 am     Reply with quote

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.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group