View previous topic :: View next topic |
Author |
Message |
stoyanoff
Joined: 20 Jul 2011 Posts: 375
|
RS232 communication with PC problem |
Posted: Sun Nov 04, 2012 1:49 am |
|
|
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
|
|
Posted: Sun Nov 04, 2012 5:00 am |
|
|
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'.
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: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: 9225 Location: Greensville,Ontario
|
|
Posted: Sun Nov 04, 2012 5:14 am |
|
|
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
|
|
Posted: Sun Nov 04, 2012 8:25 am |
|
|
I`ll try! Thanks! |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Nov 04, 2012 7:48 pm |
|
|
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: 19504
|
|
Posted: Mon Nov 05, 2012 2:53 am |
|
|
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
|
|
Posted: Tue Nov 06, 2012 9:20 am |
|
|
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
|
|
Posted: Tue Nov 06, 2012 11:47 am |
|
|
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 |
|
|
|