View previous topic :: View next topic |
Author |
Message |
aswilli
Joined: 21 Mar 2006 Posts: 7 Location: Las Cruces, NM
|
USART is not working. Please Help!! |
Posted: Mon Apr 03, 2006 5:51 pm |
|
|
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
|
|
Posted: Mon Apr 03, 2006 6:04 pm |
|
|
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
|
|
Posted: Mon Apr 03, 2006 8:02 pm |
|
|
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
|
|
Posted: Wed Apr 05, 2006 1:53 pm |
|
|
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
|
|
Posted: Wed Apr 05, 2006 2:06 pm |
|
|
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
|
|
Posted: Wed Apr 05, 2006 2:56 pm |
|
|
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
|
|
Posted: Wed Apr 05, 2006 3:15 pm |
|
|
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
|
|
Posted: Wed Apr 05, 2006 3:30 pm |
|
|
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
|
|
Posted: Sun Apr 09, 2006 4:04 pm |
|
|
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
|
PIC to PC RS232 |
Posted: Sun Apr 09, 2006 9:16 pm |
|
|
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. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Apr 09, 2006 10:23 pm |
|
|
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
|
|
Posted: Sun Apr 09, 2006 10:31 pm |
|
|
Yikes! You learn something new everyday - Guess I'll be putting a few resistors in my boards... |
|
|
|