Saturday, November 12, 2011

Serial Communication With PIC18F452

In this tutorial I will explain how to practically do a simple communication over RS232 interface. In this tutorial i ll send and receive the data using serial port, in two devices. Also you can control your home appliances using PC serial port with the help of PIC. RS232 is serial interface that means that data is transferred BIT by BIT at a time. Since data is transferred BIT by BIT so we need only a single wire two send data and an another one to receive data. One more common wire (called GND) is required between two separate circuit to enable current flow. So a total of three wire are required for communication.

RS232 can be used to communicate between a variety of devices. Like your MCU and a GSM module or a PC. In this tutorial we will demonstrate a link between a PIC18F452 MCU and a standard PC. On PC we will run a terminal program like RealTerm or Hyperterminal

A terminal program is used to send and receive text data. So any text send by the MCU will be visible on Terminal Screen and Any keypress you make on the PC keyboard will be send over RS232 to your MCU. This configuration is the simplest setup to test and understand RS232 communication. When you have enough knowledge you can replace the Terminal with your own PC end software for sending receiving data.

In this tutorial i will only talk about the simulation in Proteus, using its own terminal. If you want to communicate it with the PC then you have to convert the TTL to RS232 logic, for this see the level converter

Hyper Terminal in windows (if using windows 7 then download RealTerm) is located in communication folder. Create the new connection and set the properties as follow:
  • Bits per second: 57600
  • Data bits: 8
  • Parity: None
  • Stop bits: 1
  • Flow Control: None
Code of serial communication is written in form of library to work with USART. This keeps the USART code separate from the application code. The same library can be used in many other project that requires USART communication. The functions available in the library are discussed below.

void USARTInit()
This function initializes the internal USART of the PIC18F452 microcontroller. This must be called before data can be sent or received.
void USARTWriteByte(char ch)
Writes a byte of data to the USART.

void USARTWriteString(const char *str)
This function will send a string of character to the serial port.

unsigned char USARTReadByte()
Wait until a byte is received from USART/Serial Port and return the value read from the USART.

Downloads: Code Files

Serial Communication
Virtual Terminal of Proteus

No comments:

Post a Comment