Advertisement

Asynchronous serial communication

, Posted by ADMIN at 11:13 AM

Description

The PIC16F628A microcontroller has a built in Universal Synchronous Asynchronous Receiver Transmitter (USART) hardware that allows to communicate with a wide range of serial devices such as memory chips, LCDs, personal computers, etc. The USART module has two modes of operation: synchronous (requires a synchronized clock between the transmitter and receiver) and asynchronous (no synchronization clock required). As the asynchronous mode is more popular, we will focus today's lab session on this and will establish a two way serial data link between the PIC microcontroller and a PC.

Required Theory

Serial communications are used in microcontroller-based systems, mostly due to the scarcity of available I/O pins. Besides for long distance communications, serial data transfer is more simple and cost effective as the required hardware connections in the data link can be reduced to three (Tx, Rx, and Gnd).

There are two different types of serial communications: synchronous and asynchronous. The key challenge in a serial data link is to maintain the synchronization between the transmitter and the receiver. The asynchronous method uses a start and stop bit protocol to synchronize the two ends. Each character byte is sent in a frame consisting of a start bit , followed by the character bits, followed (optionally) by a parity bit, and finalized by one or more stop bits. The sender and receiver have to be initialized to use the same data rate, number of data bits, and number of stop bits.

In idle condition, the transmit output is at logic high. When the transmitter is ready to send a character byte, it signals the receiver by pulling the transmit line low for one clock period. This is the start bit and it tells the receiver that a frame follows. The receiver reads the number of character bits expected according to the adopted protocol until the line is pulled to logic high by the transmitter (one or more stop bits), and that is the end of the frame. The whole process is repeated every time the transmitter has to send a character byte. This form of serial transmission is called asynchronous because the receiver resynchronizes itself to the transmitter every time the data is sent using the start bit. However, within each frame the two parties are synchronized.

On the other hand, the synchronous serial communication transmit characters in blocks with no framing bits surrounding them. The transmitter and receiver are synchronized with a separate clock line or, in some cases, the clock signal is contained in the transmitted characters. In both the types of serial communications, the rate at which the data is sent and received is known as the baud rate.

The USART module inside the PIC16F628A microcontroller supports both types of serial communications but it is best suited for the asynchronous method. In asynchronous mode, RB2 acts as a data transmit (TX) output, and RB1 as data receive (RX) input. A byte of serial data is sent as a string of 10 bits; a start bit, eight data bits, and a stop bit, as shown below.

The PC's serial port (also known as COM port) uses the RS232-C standard for serial communication. This standard specifies the electrical, mechanical, functional signal and procedural specifications of the serial communication interface. A logic high for RS232-C is a signal voltage in the range of -3 V to -15 V (typically -12 V), and a logic low is between +3 V to +15 V (typically +12 V). So unlike the PIC microcontroller's logic levels, an RS232-C high is a negative voltage, and a low is a positive voltage. The table below shows the standard connections for RS232-C, for 25-pin, 9-pin and RJ-45 connectors. For details on each of these signal pins, you can find tons of literature online. For this experiment, we are implementing a minimal serial interface between the PIC microcontroller and a PC by using only the TX, RX, and GND signals.

RS232 signal pins in a DB-9 female connector

We are going to use asynchronous mode to communicate with an RS232-C serial port on the PC. Since the PIC16F628A already has a built in hardware (USART) that supports asynchronous serial communication, so all that is required is an external level shifter to translate TTL signals from PIC to RS232-C levels, and vice-versa. This can be achieved by using a MAX232 chip made by Maxim. The chip requires a few external capacitors for its internal charge pumps to generate +12 V and -12 V required for the RS232-C communication. A simple way to send and receive bytes through the PC's serial port is by using the application named HyperTerminal that comes with Windows operating system. You can open the HyperTerminal application window through Start → Menu → Programs → Accessories → Communications→ Hyperterminal. You can create a connection with your serial port (e.g. COM1), choose a baud rate, number of bits, parity setting, etc. When HyperTerminal connects to the serial port, whatever character you type is sent (as ASCII) through the serial port. The received characters are also displayed on the screen.

Circuit Setup

The circuit setup for this experiment is shown below. It has the basic setup circuit from PIC16F628A breadboard module and a level shfter using MAX232 chip. The MAX232 requires four external capacitors (each 1 uF) for its internal charge pumps. A MAX232 is actually a dual driver/receiver and we are just using one. For more details on the MAX232 chip, read the datasheet. On PC's side, only three lines are connected (Tx, Rx, and Ground) to the COM port through a 9-pin connector.

Software

As usual, the program is developed with the MikroC Pro for PIC compiler. The MikroC compiler provides UART library that supports asynchronous serial communication in full duplex mode (that means transmit and receive simultaneously). This makes the programming lot easier. For example, if you want to initialize the hardware UART module of PIC16F628A with the data rate of 9600 baud, you just need to write UART1_Init(9600). The example code given here establishes a two way asynchronous serial link between the PIC16F628A microcontroller and the PC. The microcontroller sends the message 'Type in a Number' that is displayed on the Hyperterminal window. When you enter any character from the keyboard, it will be sent to the microcontroller through the COM port. The PIC microcontroller will read it and send it back to the PC, which will be again displayed on the Hyperterminal window. The Hyperterminal setttings for this should be

Bits per second: 9600, Data Bits: 8, Parity: None, Stop bits: 1, Flow control: None.

/*
Lab 8: Hardware UART
MCU: PIC16F628A
External 4MHz Crystal, MCLR Enabled, PWRT Enabled, WDT OFF
Copyright @ Rajendra Bhatt
Dec 12, 2010
*/

void newline(){
UART1_Write(13); // Carriage Return
UART1_Write(10); // Line Feed
}

void main() {
unsigned char MyError, Temp;
CMCON = 7; // Disable Comparators
TRISB = 0b00000010;
UART1_Init(9600);
Delay_ms(100);
UART1_Write_Text("Testing UART! ");
newline();

do {
UART1_Write_Text("Type in a Number: ");
while(!UART1_Data_Ready());
Temp = UART1_Read();
newline();
UART1_Write_Text("You entered: ");
UART1_Write(Temp);
newline();
} while(1);
} // End main()

Download HEX file

Output

Here's a snapshot of the output from the Hyperterminal window on my Windows XP machine. In order to display what you typed you need to turn on the 'Echo typed character locally' option on the settings.


--
With Regards,

s.m.sethupathy,
sms communication,
Tanjore -1.

mobile :9944 186 173           
      www.questionpaperlink.co.cc
      www.sethu-panguvarthagam.blogspot.com





Currently have 0 comments: