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

RS232C communication using the PIC18F4550

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



Joined: 12 May 2013
Posts: 7

View user's profile Send private message

RS232C communication using the PIC18F4550
PostPosted: Sun May 12, 2013 8:13 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Mon May 13, 2013 12:32 am     Reply with quote

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

View user's profile Send private message

PostPosted: Mon May 13, 2013 1:16 am     Reply with quote

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. Crying or Very sad

Compiler is not old. (ver 4.140)

This device need special configuration?

Thanks
Ttelmah



Joined: 11 Mar 2010
Posts: 19477

View user's profile Send private message

PostPosted: Mon May 13, 2013 2:33 am     Reply with quote

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

View user's profile Send private message

PostPosted: Mon May 13, 2013 6:33 am     Reply with quote

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

View user's profile Send private message

PostPosted: Mon May 13, 2013 8:07 pm     Reply with quote

Arrow

Last edited by Mally on Mon May 13, 2013 11:10 pm; edited 1 time in total
Mally



Joined: 12 May 2013
Posts: 7

View user's profile Send private message

PostPosted: Mon May 13, 2013 10:37 pm     Reply with quote

Sorry.
It was with reference to the old compiler.
Thank you for your help.
Mally



Joined: 12 May 2013
Posts: 7

View user's profile Send private message

Receive is abnormal
PostPosted: Tue May 14, 2013 7:47 pm     Reply with quote

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

View user's profile Send private message

Re: Receive is abnormal
PostPosted: Tue May 14, 2013 8:40 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Tue May 14, 2013 8:50 pm     Reply with quote

Thank you for your reply.
However, the result was the same. Sad

Why only abnormal at this device ...
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue May 14, 2013 10:39 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Wed May 15, 2013 4:03 am     Reply with quote

It was successfully After the upgrade of MPLAB. Razz

Thank you for your help.
Ttelmah



Joined: 11 Mar 2010
Posts: 19477

View user's profile Send private message

PostPosted: Wed May 15, 2013 8:17 am     Reply with quote

Er. You didn't mention MPLAB anywhere.....
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