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 , PIC18F2550 don't working
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Chaud



Joined: 11 Jan 2012
Posts: 39

View user's profile Send private message

RS232 , PIC18F2550 don't working
PostPosted: Wed Feb 01, 2012 11:06 am     Reply with quote

hello i wanna send a simple string via RS232.

I'm using this code:
Code:

#include <18f2550.h>
#use delay(clock=20M)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)

void main(){
 printf("Hello");
}


I'm using serial port connected to MAX233 and then connected to PIC. I'm pretty sure all connections are ok, because i followed 3 different guides.

I'm using RComSerial program to read the string, but it always receives random chars like "xyxy£€" and not "hello".
Any idea whats going on?


Last edited by Chaud on Wed Feb 01, 2012 12:33 pm; edited 1 time in total
temtronic



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

View user's profile Send private message

PostPosted: Wed Feb 01, 2012 12:21 pm     Reply with quote

I don't use that terminal program,but it looks like a mismatch of the comm. parameters.

You should have a delay of say 1/2 second in main() and loop forever as your program only runs once. The terminal buffer could have garbage in it,or setup wrong.

The PIC may need to have additional fuses configured( there are several !).

Quick test of the comm program is to use a loopback connector..
dyeatman



Joined: 06 Sep 2003
Posts: 1933
Location: Norman, OK

View user's profile Send private message

PostPosted: Wed Feb 01, 2012 12:24 pm     Reply with quote

Where are your fuses? Are you sure your PIC is running at the correct
speed? Also, your PIC is going to sleep at the end.

Add an LED/resistor to Pin C5 and try the following code. The LED should
toggle on for 500ms then off 500ms. If the PIC is running at the wrong
speed the LED will blink at a different rate. If it doesn't blink at all the
LED is likely not connected correctly.

Code:
#include <18f2550.h>
#use delay(clock=20M)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)

void main(){
 printf("Hello");

  while(1)   // <<==== add this section to keep it from going to sleep and blink the LED
      {
       output_toggle(pin_c5);
       delay_ms(500);
       }
}

_________________
Google and Forum Search are some of your best tools!!!!
Ttelmah



Joined: 11 Mar 2010
Posts: 19496

View user's profile Send private message

PostPosted: Wed Feb 01, 2012 4:05 pm     Reply with quote

Beware of using 'clock=20M', unless you have an external _clock_ at 20Mhz.
To use a _crystal_, you either need to set the HS fuse (dyeatman's comment 'where are your fuses'), or to have:

#use delay(crystal=20M)

which tells the compiler to enable the crystal oscillator.
Since the chip defaults to having the fail safe clock monitor enabled, it'll drop back to the internal RC clock, which can't do 20Mhz, so won't be running at 20MHz....

Best Wishes
Chaud



Joined: 11 Jan 2012
Posts: 39

View user's profile Send private message

PostPosted: Wed Feb 01, 2012 4:59 pm     Reply with quote

hello, full code with #fuses is there:

Code:
#include <18f2550.h>
#Device ADC=10
#fuses HS,NOLVP,NOWDT,PUT
#use delay(clock=20M)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
void main()
{
 printf("hello");
 
  while(1)
      {
       output_toggle(PIN_C5);
       delay_ms(500);
       }
}



And the LED isnt blinking, its always off, what's going on?

My crystal is 20MHz
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Feb 01, 2012 5:58 pm     Reply with quote

Look in the Overview section of the 18F2550 data sheet. Find the pin
descriptions section. Look at the available options for Pin C5.
http://ww1.microchip.com/downloads/en/DeviceDoc/39632e.pdf
You want it to be an Output pin. Check if it can do that.
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Wed Feb 01, 2012 6:12 pm     Reply with quote

or without crystal what do you get if you try THIS ??

Code:

#include <18f2550.h>
#Device ADC=10
#fuses INTRC_IO,NOLVP,NOWDT,PUT
#use delay(clock=8000000,internal)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
void main() {
    setup_oscillator(OSC_8MHZ|OSC_PLL_OFF);

 
  while(1)
      {
       output_toggle(PIN_C5);
       delay_ms(500);
       printf("hello\r\n");  // expell each as a LINE of its own     
      }
}

PLEASE DO as PCM_PGM sez RE pin _C5
BUT.... what do you get for RS232 output NOW ???
Chaud



Joined: 11 Jan 2012
Posts: 39

View user's profile Send private message

PostPosted: Wed Feb 01, 2012 6:18 pm     Reply with quote

PCM programmer wrote:
Look in the Overview section of the 18F2550 data sheet. Find the pin
descriptions section. Look at the available options for Pin C5.
http://ww1.microchip.com/downloads/en/DeviceDoc/39632e.pdf
You want it to be an Output pin. Check if it can do that.


Thanks, C5 is only input, i changed to C2 and its working, and the toogle is doing right, so i think everything is OK

Now back to RS232, one question: do i need to use MAX233 with pic?

EDIT: asmboy, didnt see your post, sorry, cannot test it right now, will do asap and let you know, thanks for help Smile


Last edited by Chaud on Wed Feb 01, 2012 6:43 pm; edited 1 time in total
dyeatman



Joined: 06 Sep 2003
Posts: 1933
Location: Norman, OK

View user's profile Send private message

PostPosted: Wed Feb 01, 2012 6:31 pm     Reply with quote

My apologies on C5. I tried to stay away from the ADC pins and got
snagged by something that is usually a problem on A4... I was in a
place where I couldn't get to the datasheet to double check.
_________________
Google and Forum Search are some of your best tools!!!!
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Wed Feb 01, 2012 6:41 pm     Reply with quote

Quote:

do i need to use MAX233 with pic?

YES - IF wanting to match the serial TTL levels of the PIC to - say a PC
or any genuine RS-232 device
Chaud



Joined: 11 Jan 2012
Posts: 39

View user's profile Send private message

PostPosted: Wed Feb 01, 2012 6:57 pm     Reply with quote

dyeatman it's ok bro , thanks for your help!! Smile

asmboy wrote:
or without crystal what do you get if you try THIS ??

Code:

#include <18f2550.h>
#Device ADC=10
#fuses INTRC_IO,NOLVP,NOWDT,PUT
#use delay(clock=8000000,internal)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
void main() {
    setup_oscillator(OSC_8MHZ|OSC_PLL_OFF);

 
  while(1)
      {
       output_toggle(PIN_C5);
       delay_ms(500);
       printf("hello\r\n");  // expell each as a LINE of its own     
      }
}

PLEASE DO as PCM_PGM sez RE pin _C5
BUT.... what do you get for RS232 output NOW ???


he keeps sending garbage , something like "|0xx|" every 500ms
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Wed Feb 01, 2012 7:19 pm     Reply with quote

1- does the led blink ON and OFF at a combined 1 second rate??
2- add ,ERRORS to your #USE rs232 setup
3- are you using a MAX232 or equivalent level translator ??
4- is the PC receive baud FOR SURE 9600
Chaud



Joined: 11 Jan 2012
Posts: 39

View user's profile Send private message

PostPosted: Wed Feb 01, 2012 7:25 pm     Reply with quote

asmboy wrote:
1- does the led blink ON and OFF at a combined 1 second rate??
2- add ,ERRORS to your #USE rs232 setup
3- are you using a MAX232 or equivalent level translator ??
4- is the PC receive baud FOR SURE 9600


1- Yes, I cannot be sure, but i think yes.
2- Like this? #use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7,ERRORS) -> same result
3- I'm using MAX233
4- Yes it is

I tested kbhit(), and it works
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Thu Feb 02, 2012 11:43 am     Reply with quote

I don't have that chip to play with
but it has a LOT of oscillator fuse complexity in the setup.

Based on the fact that the data you DO get is short -
compared to "hello\r\n" being sent -

I think the PLL clock multiplier circuit must be on ...

I assume there is a fuse setting that will keep it off.

The answer will be in the datasheet somewhere.

Can you test your design with a simpler PIC chip ???
Chaud



Joined: 11 Jan 2012
Posts: 39

View user's profile Send private message

PostPosted: Thu Feb 02, 2012 12:59 pm     Reply with quote

asmboy wrote:
I don't have that chip to play with
but it has a LOT of oscillator fuse complexity in the setup.

Based on the fact that the data you DO get is short -
compared to "hello\r\n" being sent -

I think the PLL clock multiplier circuit must be on ...

I assume there is a fuse setting that will keep it off.

The answer will be in the datasheet somewhere.

Can you test your design with a simpler PIC chip ???


Well I have 3 PIC's, 18F2550, 18F4550 and 18LF4550, so I guess the 18F2550 is more simpler of them :D, what do you mean with simpler? 16F one??

Can you explain to me what #fuses are? I'm using it, but I don't know why, and which ones I should use, etc. Since all hardware is correctly connected, baud rates are the same, etc, it is probably a #fuse or osc problem isn't it??

What #fuses should I use in PIC18F2550 with 20Mhz crystal + RS232??

My crystal is 20 Mhz, so i guess my clock delay should be 20M, about fuses i just copied from somewhere, if you can give me some lights about #fuses would be awesome.

Once again, thanks for help
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
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