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 support@ccsinfo.com

USART is not working. Please Help!!

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



Joined: 21 Mar 2006
Posts: 7
Location: Las Cruces, NM

View user's profile Send private message

USART is not working. Please Help!!
PostPosted: Mon Apr 03, 2006 5:51 pm     Reply with quote

Hello,

I am new to the world of microcontroller programming and I am having trouble to get my simple USART working. I am using a PIC18F4550 and trying to implement a very basic piece of code to output "Hello" to the Com port. I am looking at the output using hyperterminal and getting a string of gibberish 5 characters long instead of "Hello". Any help would be great.

Here is my code:

Code:
#include <18f4550.h>

#fuses INTRC,NOLVP,NOWDT,PUT
#use delay (clock=4000000)

#use rs232(baud=9600, xmit=PIN_C6, rcv = PIN_C7,ERRORS)

void main() {
   //setup_oscillator(OSC_4MHZ);
   while(TRUE){
      printf("Hello");
   }

}



Thanks,
Aaron
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Apr 03, 2006 6:04 pm     Reply with quote

Are you using a MAX232-type chip between the PIC and your PC ?
You should have one. If you don't, this could explain the gibberish
characters.
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Mon Apr 03, 2006 8:02 pm     Reply with quote

You have to set the osc freq. You have that line commented out. Make sure hyperterminal is set to 9600.
aswilli



Joined: 21 Mar 2006
Posts: 7
Location: Las Cruces, NM

View user's profile Send private message

PostPosted: Wed Apr 05, 2006 1:53 pm     Reply with quote

I was not aware I needed an external chip to drive the RS 232 from this pic. Does Maxim make this MAX232-type chip? What specifically does this chip do? I was under the impression that the PIC itself had a RS 232 port built in and it was not necessary do have an external chip.

Thanks for your help.
rberek



Joined: 10 Jan 2005
Posts: 207
Location: Ottawa, Canada

View user's profile Send private message

PostPosted: Wed Apr 05, 2006 2:06 pm     Reply with quote

The PIC drives the RS232 protocol, but does not drive the +/- 15V necessary to signal it to a PC, nor does it invert the data. The MAX232 or variants does these functions. Maxim makes them, as does TI.

The MAX232 runs off of a 5V supply and uses 4 external caps. The MAX233 uses no external caps. The MAX3232 runs off of 3.3v.

If you were sending/receiving RS232 to/from another device on the board (i.e a GPS engine), you would not need these devices.
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Wed Apr 05, 2006 2:56 pm     Reply with quote

The PIC has a UART. It does not become RS232 until you add an RS232 transceiver chip. If you add an RS485 transceiver then it becomes an RS485 port. Add 2 and you get an RS422 port.
aswilli



Joined: 21 Mar 2006
Posts: 7
Location: Las Cruces, NM

View user's profile Send private message

PostPosted: Wed Apr 05, 2006 3:15 pm     Reply with quote

That brings me to another question, Is there a way on a chip with a single USART output, to have another USART on two other pins running through some software technique or something else similar?

Thanks
sjbaxter



Joined: 26 Jan 2006
Posts: 141
Location: Cheshire, UK

View user's profile Send private message Visit poster's website

PostPosted: Wed Apr 05, 2006 3:30 pm     Reply with quote

Yes you can.

The pins defined in the #uses rs232 statement cause the compiler to use either the builtin hardware UART (if the PIC has a hardware UART) or a software implementation of a UART (using bit-banging).

You can define more than one #uses rs232 in your application, but if you do you need to add the 'stream=' option. Then, when you then use the printf, getc, putc, etc functions you specify which stream you want to send/receive the data on.

Have a look in the CCS C Compiler documentation and search for STREAM and/or #USES RS232. Also do a search on this forum, you'll find plenty of examples.

A thread discussing a hardware and software UART can be found at:

http://www.ccsinfo.com/forum/viewtopic.php?t=25567&highlight=stream
_________________
Regards,
Simon.
aswilli



Joined: 21 Mar 2006
Posts: 7
Location: Las Cruces, NM

View user's profile Send private message

PostPosted: Sun Apr 09, 2006 4:04 pm     Reply with quote

Thanks very much for the help. I ordered some of these chips and will attempt to get it working. Thanks.
UFAnders



Joined: 13 Apr 2005
Posts: 36
Location: Michigan

View user's profile Send private message Send e-mail Visit poster's website AIM Address

PIC to PC RS232
PostPosted: Sun Apr 09, 2006 9:16 pm     Reply with quote

Look at the #use rs232 portion of the manual - there is an option called "INVERT" that will allow your PIC to talk directly with a generic COM port (in this case on the PC) without any true RS-232 level convertors. NOTE: this only works for software (not the PIC hardware UART) UARTs.

Try this as a test:
Code:
#use rs232(baud=9600, xmit=PIN_C0, rcv = PIN_C1, INVERT)

Also note that this is not a real RS-232 solution - the voltage levels will be waaay out of spec. It will function as a convenient debugging feature for protoboard work, though. Smile
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Apr 09, 2006 10:23 pm     Reply with quote

Quote:

There is an option called "INVERT" that will allow your PIC to talk
directly with a generic COM port (in this case on the PC) without
any true RS-232 level convertors.

If you do this, you should put a resistor between the Tx pin of the
DB-9 on the PC (pin 3) and the Rx pin on your PIC. That's because
the RS232 driver in your PC puts out levels of about +/- 10v.
Your PIC has clamp diodes on the i/o pins to protect against voltages
that go below ground or above Vdd. The resistor will limit the amount
of current going through the input clamp diodes on the PIC to a
safe level, so they don't burn up. If they do burn up, at a minimum
the PIC input will likely be destroyed.

Look at the schematic on this page:
http://www.rentron.com/PIC16F84.htm
(Scroll down a bit to see it)
Notice the 22K resistor between the RS232 connector on the PIC and
the Rx pin (RA4 in this case) on the PIC.
UFAnders



Joined: 13 Apr 2005
Posts: 36
Location: Michigan

View user's profile Send private message Send e-mail Visit poster's website AIM Address

PostPosted: Sun Apr 09, 2006 10:31 pm     Reply with quote

Yikes! You learn something new everyday - Guess I'll be putting a few resistors in my boards... Embarassed
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