This article shows how to get started with PIC18F4550 microcontroller USART module using CCS PIC C compiler.
PIC18F4550 microcontroller has one (1) USART (Universal Synchronous/Asynchronous Receive/Transmit) module. This module can work in USRT mode or UART mode. In this topic we are going to use the USART module as UART (Universal Asynchronous Receive/Transmit) to transmit and receive data between the microcontroller and the computer.
Using Multiple RS-232 Ports on a PIC ® MCU or PIC ® DSC. In order to use multiple RS-232 connections on a PIC ® MCU or PIC ® DSC, multiple #USE RS232 pre-processor directives will need to be setup using the STREAM option. A #USE RS232 pre-processor directive will remain in effect for GETC, PUTC, PRINTF and KBHIT functions until another #USE. The next step is setting up the PIC chip. For some reason, I can’t get results that make any sense out of the PIC. Here’s my code written with the CCS C compiler for the midrange PIC devices: // PIC code #include #fuses INTRCIO, NOWDT, NOBROWNOUT, PUT, NOMCLR #use delay (clock=4000000) #use rs232 (baud=9600, xmit=PINC1, rcv=PIN.
PIC18F4550 UART connection circuit schematic:
Pin RC6 (TX) and pin RC7 (RX) are used for the UART (serial) communication between the microcontroller and the computer. To change between TTL logic levels (5V) and RS232 signals (+/-12V), an IC is needed which is max232.
Don’t connect TX and RX pins directly to an RS232 serial port which may damage your microcontroller.
In this example the PIC18F4550 microcontroller uses its internal oscillator and MCLR pin function is disabled.
PIC18F4550 UART example CCS C code:
This is the full C code of this example.
Serial Rs232 Cable
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 | // CCS C UART example for PIC18F4550 microcontroller #include <18F4550.h> #use delay(clock = 8000000) #use rs232(uart1, baud = 9600) // Initialize UART module charmessage[]='PIC18F4550 microcontroller UART example'; voidmain(){ setup_oscillator(OSC_8MHZ);// Set internal oscillator to 8MHz printf('Hello world!');// UART write putc(13);// Go to first column textsize=strlen(message);// Number of characters in message putc(message[j]); } putc(10);// Start a new line if(kbhit()){// If data has been received putc(i);// Send it back } |
PIC18F4550 UART example video:
The following video shows communication between the computer and PIC18F4550 using UART module.
Comunicacion Serial Rs232 Pic Ccs En
Why is the RS-232 not working right?
- The PIC® MCU is Sending Garbage Characters.
- Check the clock on the target for accuracy. Crystals are usually not a problem but RC oscillators can cause trouble with RS-232. Make sure the #USE DELAY matches the actual clock frequency.
- Make sure the PC (or other host) has the correct baud and parity setting.
- Check the level conversion. When using a driver/receiver chip, such as the MAX 232, do not use INVERT when making direct connections with resistors and/or diodes. You probably need the INVERT option in the #USE RS232.
- Remember that PUTC(6) will send an ASCII 6 to the PC and this may not be a visible character. PUTC('A') will output a visible character A.
- The PIC® MCU is Receiving Garbage Characters.
- Check all of the above.
- Nothing is Being Sent.
- Make sure that the tri-state registers are correct. The mode (standard, fast, fixed) used will be whatever the mode is when the #USE RS232 is encountered. Staying with the default STANDARD mode is safest.
- Use the following main() for testing:
Check the XMIT pin for activity with a logic probe, scope or whatever you can. If you can look at it with a scope, check the bit time (it should be 1/BAUD). Check again after the level converter.
- Nothing is being received. First be sure the PIC® MCU can send data. Use the following main() for testing: When connected to a PC typing A should show B echoed back. If nothing is seen coming back (except the initial 'Start'), check the RCV pin on the PIC® MCU with a logic probe. You should see a HIGH state and when a key is pressed at the PC, a pulse to low. Trace back to find out where it is lost.
- The PIC® MCU is always receiving data via RS-232 even when none is being sent.
- Check that the INVERT option in the USE RS232 is right for your level converter. If the RCV pin is HIGH when no data is being sent, you should NOT use INVERT. If the pin is low when no data is being sent, you need to use INVERT.
- Check that the pin is stable at HIGH or LOW in accordance with A above when no data is being sent.
- When using PORT A with a device that supports the SETUP_PORT_A function make sure the port is set to digital inputs. This is not the default. The same is true for devices with a comparator on PORT A.
- Compiler reports INVALID BAUD RATE.
- When using a software RS232 (no built-in UART), the clock cannot be really slow when fast baud rates are used and cannot be really fast with slow baud rates. Experiment with the clock/baud rate values to find your limits.
- When using the built-in UART, the requested baud rate must be within 3% of a rate that can be achieved for no error to occur. Some parts have internal bugs with BRGH set to 1 and the compiler will not use this unless you specify BRGH1OK in the #USE RS232 directive.