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 communication with PC problem

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



Joined: 20 Jul 2011
Posts: 375

View user's profile Send private message

RS232 communication with PC problem
PostPosted: Sun Nov 04, 2012 1:49 am     Reply with quote

Hi! I want to connect a microcontroller with PC. I'm using pic18F66J60(3.3V Vcc) and MAX232 (5V Vcc) with voltage divider on the entrance and exit. I made a simple application from this site: http://www.codeproject.com/Articles/8605/Serial-Communication-using-C-and-Whidbey

Next I made a simple program for my controller:
Code:

#include <18F66J60.h>
#fuses HS,NOWDT
#use delay(clock=25M)
#USE RS232 (baud=9600, xmit=PIN_C6,rcv=PIN_C7,BITS=8,PARITY=N)
void main()
{
   char RSdata;
   *0xF93=0x00;
   RSData=0;
   while(1)
   {
      if(kbhit())
      {
         RSdata=getc();
         output_b(RSdata);
         RSdata=0;
      }
   }
}

So I set the application baud rate to 9600 and stop bits to 1, parity N.
The controller reacts only when I send data but it receives different data. If I send 14 I receive 50 for example. Can you tell me what`s happening??
Thanks!
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Sun Nov 04, 2012 5:00 am     Reply with quote

Most likely cause of your problem is that you are mixing ASCII text and its binary representation. When you type a text on your PC then this is stored internally according to the ASCII table codings. That means a character '4' will be stored as the decimal value '52'.

Code:
*0xF93=0x00;
Technically correct, but difficult to read and very specific for your processor. CCS provides a function to do the same which is easier to read and portable to other processors:
Code:
set_tris_b(0x00);
But, even better is to remove this whole line as it has no effect. On every input/output instruction, like output_b(), the compiler will automatically set the TRIS register for you. Less code often leads to fewer errors.

Note: the default compiler behaviour where the TRIS registers are automatically managed is convenient but comes at the cost of slightly larger code size and a tiny slower execution speed. For most applications this is no problem but default compiler behaviour can be overruled through the '#use fixed_io' and similar settings.
temtronic



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

View user's profile Send private message

PostPosted: Sun Nov 04, 2012 5:14 am     Reply with quote

also...
when using the PIC's hardware UART always add 'errors' to the use rs232(...options...)

this will allow your program to continue if you send it serial data too fast and get an 'overrun' condition.

hth
jay
stoyanoff



Joined: 20 Jul 2011
Posts: 375

View user's profile Send private message

PostPosted: Sun Nov 04, 2012 8:25 am     Reply with quote

I`ll try! Thanks!
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Nov 04, 2012 7:48 pm     Reply with quote

Quote:

I'm using pic18F66J60(3.3V Vcc) and MAX232 (5V Vcc) with voltage
divider on the entrance and exit.

Why not use a MAX3232 or equivalent chip (such as SP3232), which runs
at both 3.3v and at 5v ? Just run the PIC and the MAX3232 at the same
voltage. The CMOS-to-RS232 level converter (MAX3232) will
automatically work at both voltages. There are no voltage dividers required.
Ttelmah



Joined: 11 Mar 2010
Posts: 19498

View user's profile Send private message

PostPosted: Mon Nov 05, 2012 2:53 am     Reply with quote

and as a comment on the original post, it talks about "voltage divider on the entrance and exit". Assuming this means TX & RX, you most certainly do _not_ want any form of voltage divider from the TX pin of the PIC. You need every volt you can get from this, possibly a pull up resistor, to make sure it swings as high as possible. It is a 'potentially unreliable solution' throughout....

Best Wishes
stoyanoff



Joined: 20 Jul 2011
Posts: 375

View user's profile Send private message

PostPosted: Tue Nov 06, 2012 9:20 am     Reply with quote

The problem was in the dataformat! Thanks!
Ttelman has a right!
I want to ask another thing. As I said I have an application same as represented in the link above. I have a LCD connected to the PIC and PicKit 3 programmer/debugger.
I maid a simple program:
Code:

   while(1)
   {
      
      if(kbhit())
      {
         RSdata=getc();
         printf(RSdata);
         delay_ms(2000);
         RSdata=0;
      }
      
   }

In this case if set a break point inside the if() operator(on printf() for example), and then when I run the debugger and send data the program doesn`t break and if does the value in the watch window for RSdata is always 0x00. When I turn on the LCD and watch there the recieved data everything is OK every time.
Why does the debugger make these errors???
Thanks!
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Tue Nov 06, 2012 11:47 am     Reply with quote

RSdata can only show correctly all the time if it's global.

If it's local then the RAM space will be used for other variables and it will make nonsense.

Mike
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