View previous topic :: View next topic |
Author |
Message |
Chaud
Joined: 11 Jan 2012 Posts: 39
|
RS232 , PIC18F2550 don't working |
Posted: Wed Feb 01, 2012 11:06 am |
|
|
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: 9243 Location: Greensville,Ontario
|
|
Posted: Wed Feb 01, 2012 12:21 pm |
|
|
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: 1934 Location: Norman, OK
|
|
Posted: Wed Feb 01, 2012 12:24 pm |
|
|
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: 19539
|
|
Posted: Wed Feb 01, 2012 4:05 pm |
|
|
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
|
|
Posted: Wed Feb 01, 2012 4:59 pm |
|
|
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
|
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Wed Feb 01, 2012 6:12 pm |
|
|
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
|
|
Posted: Wed Feb 01, 2012 6:18 pm |
|
|
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
Last edited by Chaud on Wed Feb 01, 2012 6:43 pm; edited 1 time in total |
|
|
dyeatman
Joined: 06 Sep 2003 Posts: 1934 Location: Norman, OK
|
|
Posted: Wed Feb 01, 2012 6:31 pm |
|
|
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
|
|
Posted: Wed Feb 01, 2012 6:41 pm |
|
|
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
|
|
Posted: Wed Feb 01, 2012 6:57 pm |
|
|
dyeatman it's ok bro , thanks for your help!!
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
|
|
Posted: Wed Feb 01, 2012 7:19 pm |
|
|
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
|
|
Posted: Wed Feb 01, 2012 7:25 pm |
|
|
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
|
|
Posted: Thu Feb 02, 2012 11:43 am |
|
|
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
|
|
Posted: Thu Feb 02, 2012 12:59 pm |
|
|
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 |
|
|
|