View previous topic :: View next topic |
Author |
Message |
Mally
Joined: 12 May 2013 Posts: 7
|
RS232C communication using the PIC18F4550 |
Posted: Sun May 12, 2013 8:13 pm |
|
|
Hi,
I hope to communicate with a PC and PIC18F4550 at RS232C.
But the PC does not receive anything.
PIC16F877A and PIC16F887 and PIC18F46K20 is OK.
Thanks
Code: | #include <18F4550.h>
#include <stdlib.h>
#fuses HS,NOLVP,NOWDT,PUT
#use delay(clock=20000000)
#use RS232(BAUD=9600, XMIT=PIN_C6, RCV=PIN_C7)
void main()
{
printf("test\n");
} |
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19477
|
|
Posted: Mon May 13, 2013 12:32 am |
|
|
The obvious main problem is the clock.
You are not specifying the CPUDIV fuse value. The default will be CPUDIV4, do your chip will actually be running at 5MHz, not 20MHz.
Also, as shown, only the first three characters will ever print, as the code drops off the end before the transmission completes.
Finally repeat 50*, I must _always_ have 'errors' in a hardware RS232 declaration, unless I handle errors myself. Otherwise if the RX line is floating, or data is sent from the PC, the UART will become hung.
Code: |
#include <18F4550.h>
#include <stdlib.h>
#fuses HS,NOLVP,NOWDT,PUT,CPUDIV1 //fix the fuses
#use delay(clock=20000000)
#use RS232(BAUD=9600, XMIT=PIN_C6, RCV=PIN_C7,ERRORS)
void do_nothing(void)
{
RS232_ERRORS=0;
}
//This does absolutely nothing, except to stop the compiler generating
//an error that RS232_ERRORS is not used....
void main()
{
printf("test\n");
do ;
while (TRUE); //avoid the drop off the end, so the whole message is sent
}
|
Unless you have a hardware problem, or are using a very old compiler, this does correctly generate the message.
Best Wishes |
|
|
Mally
Joined: 12 May 2013 Posts: 7
|
|
Posted: Mon May 13, 2013 1:16 am |
|
|
Thank you very much for taught me.
I tried the code that you have learned, but it did not receive on PC.
I think no problem is hard , because it is normal in other devices.
Compiler is not old. (ver 4.140)
This device need special configuration?
Thanks |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19477
|
|
Posted: Mon May 13, 2013 2:33 am |
|
|
The code I posted works on a 4550, with a 20MHz crystal. Power attached, MCLR pulled up.
Hardware.....
Best Wishes |
|
|
ezflyr
Joined: 25 Oct 2010 Posts: 1019 Location: Tewksbury, MA
|
|
Posted: Mon May 13, 2013 6:33 am |
|
|
Hi,
Is your PIC actually running? Can you connect an LED to one of the I/O pins (thru a suitable current limiting resistor), and flash it at a known rate? Turn the LED On for one second and then Off for one second. Do this repetitively, and verify that the LED flashes, and does so at the correct rate. Until you can do this successfully, forget about RS232.
John |
|
|
Mally
Joined: 12 May 2013 Posts: 7
|
|
Posted: Mon May 13, 2013 8:07 pm |
|
|
Last edited by Mally on Mon May 13, 2013 11:10 pm; edited 1 time in total |
|
|
Mally
Joined: 12 May 2013 Posts: 7
|
|
Posted: Mon May 13, 2013 10:37 pm |
|
|
Sorry.
It was with reference to the old compiler.
Thank you for your help. |
|
|
Mally
Joined: 12 May 2013 Posts: 7
|
Receive is abnormal |
Posted: Tue May 14, 2013 7:47 pm |
|
|
Hello.
I was able to successfully send.
However, receive is abnormal.
It will be forever 1 Byte received.
I send "A" .
forever Returns "A" .
Other devices is normal.
Code: | #include <18F4550.h>
#include <stdlib.h>
#fuses HS,NOLVP,NOWDT,PUT,CPUDIV1 //fix the fuses
#use delay(clock=20000000)
#use RS232(BAUD=9600, XMIT=PIN_C6, RCV=PIN_C7,ERRORS)
void main()
{
char data;
printf("test\n");
while(1){
data=getc();
putc(data);
}
} |
|
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1611 Location: Central Illinois, USA
|
Re: Receive is abnormal |
Posted: Tue May 14, 2013 8:40 pm |
|
|
Mally wrote: | Hello.
I was able to successfully send.
However, receive is abnormal.
It will be forever 1 Byte received.
I send "A" .
forever Returns "A" .
Other devices is normal.
Code: | #include <18F4550.h>
#include <stdlib.h>
#fuses HS,NOLVP,NOWDT,PUT,CPUDIV1 //fix the fuses
#use delay(clock=20000000)
#use RS232(BAUD=9600, XMIT=PIN_C6, RCV=PIN_C7,ERRORS)
void main()
{
char data;
printf("test\n");
while(1){
data=getc();
putc(data);
}
} |
|
you need to TEST to see if a char has been received EVERY time you think one might be in the buffer.
This is done via kbhit()...
so you should have something more like:
Code: |
if ( kbhit() ) {
putc( getc() );
} |
_________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
|
Mally
Joined: 12 May 2013 Posts: 7
|
|
Posted: Tue May 14, 2013 8:50 pm |
|
|
Thank you for your reply.
However, the result was the same.
Why only abnormal at this device ... |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue May 14, 2013 10:39 pm |
|
|
Your hardware is likely bad.
I installed vs. 4.140 and compiled the code that you posted on
"Tue May 14, 2013 5:47 pm" and programmed it into an 18F4550.
I'm using a PicDem2-Plus board (non-Rohs, red board version).
I have a 20 MHz crystal installed. I connected the board to my PC
with a 9-pin serial port cable. I opened a TeraTerm window and
starting typing the alphabet. (Local echo is disabled in TeraTerm).
It works. I get this displayed on the TeraTerm window:
Code: |
test
abcdefghijklmnopqrstu
|
Check your hardware. Get a new 18F4550. |
|
|
Mally
Joined: 12 May 2013 Posts: 7
|
|
Posted: Wed May 15, 2013 4:03 am |
|
|
It was successfully After the upgrade of MPLAB.
Thank you for your help. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19477
|
|
Posted: Wed May 15, 2013 8:17 am |
|
|
Er. You didn't mention MPLAB anywhere..... |
|
|
|