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

PIC 18f4520 and rs232

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



Joined: 30 Jan 2014
Posts: 2

View user's profile Send private message

PIC 18f4520 and rs232
PostPosted: Thu Jan 30, 2014 3:59 am     Reply with quote

Hi, i'm on a project where i need to communicate between a Xbee and a pic 18f4520, so i need to use the rs232.
I plugged the pic to my pc to try if the rs232 works. I can send data, but i can't receive ...
I can write with printf from the pic, but i don't really know how to receive. scanf doesn't exist, gets doesn't work.
I searched for 2 days on google, but i didn't find anything, so i hope you can help me. I just need to receive a string or a character and store it in a buffer.
Code:

#include <18f4520.h>

#define BUFFER 50

#fuses XT,NOWDT,NOLVP
#use delay(clock=4M)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, stream=PC)

void main()
{
   unsigned char i = 0;
   char buff[BUFFER];
   
   for(i=0;i<=BUFFER;i++)
      buff[i] = 0x00;
   
   gets(buff);       
}
ezflyr



Joined: 25 Oct 2010
Posts: 1019
Location: Tewksbury, MA

View user's profile Send private message

PostPosted: Thu Jan 30, 2014 7:12 am     Reply with quote

Hello,

With all due respect, if you've 'searched for 2 days' and not found any help then may I suggest that embedded development is NOT a good hobby or
profession for you?

I did a forum search using the word 'Xbee', and found NINETY THREE threads here on the forum that discuss the Xbee is some way or another.
About half of them had some good/relevant information in them. There is also at least one thread that is being discussed RIGHT NOW in the forum
that is related to Xbee communications!

My point? My point is that help is out there if you make any attempt at all to find it, but if you can't/won't then I suspect you are in for a lot of frustration.

Good Luck!

John
temtronic



Joined: 01 Jul 2010
Posts: 9245
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Thu Jan 30, 2014 7:25 am     Reply with quote

re :Xbee communications....
There are 2 different methods
1) the default simple,dumb and slow(1 Xbee<->PIC or PC)
2) the better,faster method using API(network,any# of xbees !!)

You need to tell us which mode you're using.I'm using the API mode.

Your program will never work if using API mode as the data stream from Xbee is NOT <CR> delimited.

You should tell us the Xbee module you're using. All Xbees are 3V devices but if attached to a 'board', it might be 5V compatible.Also they are TTL units NOT RS232, so hardware configuration is critical !

hth
jay
rikotech8



Joined: 10 Dec 2011
Posts: 376
Location: Sofiq,Bulgariq

View user's profile Send private message

PostPosted: Thu Jan 30, 2014 7:27 am     Reply with quote

First think, in every cycle you assign the value of 0x00 to youre buffer.
If you want to receive string trough RS232 the function 'gets(string)' is very apropriate. But, you have to debug the data you have received. for example:

Code:

//processor config...

#define led pin_a0
#include <string.h>
void main()
{
   char test_str[7]="LED_ON";
   char string[30];
   gets(string);
   if(strcmp(str,test_str))  //if both strings(str & test_str) match
       output_high(led);
   else                            // if they dont match
        output_low(led);     
}

also see
https://drive.google.com/file/d/0B_EYtdS7J5eKZ1RaeEt4a2hlQnM/edit?usp=sharing
chapter 6.6
_________________
A person who never made a mistake never tried anything new.
Cotino



Joined: 30 Jan 2014
Posts: 2

View user's profile Send private message

PostPosted: Fri Jan 31, 2014 9:06 am     Reply with quote

Indeed, programming on pic is not a hobby, basically i program in c++, but i don't chose my assignments.

I need a simple communication. At 5pm i send a message to the pic to activate the sensors.

The problem is that i get stuck inside the gets, i run the debugger then send a message, but it never get past the gets.
I tried getc, it works but only detects the first char.
ezflyr



Joined: 25 Oct 2010
Posts: 1019
Location: Tewksbury, MA

View user's profile Send private message

PostPosted: Fri Jan 31, 2014 10:48 am     Reply with quote

Hi Cotino,

Many CCS programmers, including myself, avoid the 'gets()' function because you can get 'trapped' in the function (as you are finding!) if the
incoming data is not what you expect. At a minimum you must realize that 'gets()' will wait (forever, if necessary) for a 'CR' to be received.

A better way to do things is to read from the UART character-by-character, and then parse and process the data as it arrives. The *best* technique is
to use an 'interrupt driven circular receive buffer' as demonstrated in the example program 'ex_sisr.c'.

Good Luck!

John
Humberto



Joined: 08 Sep 2003
Posts: 1215
Location: Buenos Aires, La Reina del Plata

View user's profile Send private message

PostPosted: Fri Jan 31, 2014 12:45 pm     Reply with quote

As said hundreds of times, use the "errors" operator whenever making use of hardware UART. This is mandatory when using the
hardware UART, unless your code is able to handle errors. Without this, if a character is received into the UART buffer waiting to be read,
with the next char arived the UART will hung because the buffer has only 2 char deep.

Humberto
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