Lab 11
Lab 11
Serial Communication
Objectives:
➢ Getting introduced to serial port of microcontroller
➢ Serially interfacing microcontroller with PC
Hardware:
➢ PIC18F452
➢ RS-232
➢ Max 232
➢ PC with serial port
Introduction:
Synchronous communication:
When using the synchronous communication – the information is transmitted from the
transmitter to the receiver:
• In sequence
• Bit after Bit
• With fixed baud rate
• And the clock frequency is transmitted along with the bits
That means that the transmitter and the receiver are synchronized between them by the
same clock frequency.
Asynchronous communication:
When using the asynchronous communication - the transmitter and the receiver refraining
to transmit long sequences of bits because there is not a full synchronization between the
transmitter and receiver. In this case, the information is divided into frames, in the size of
byte. Each one of the frame has:
“Start” bit marks the beginning of a new frame.
“Stop” bit marks the end of the frame.
Frames of information must not necessarily be transmitted at equal time space, since
they are independent of the clock.
To communicate with external components such as computers or microcontrollers, the
PIC microcontroller uses a component called USART (Universal Synchronous
Asynchronous Receiver Transmitter0). This component can be configured as:
• A Full-Duplex asynchronous system that can communicate with peripheral
devices, such as CRT terminals and personal computers
• A Half-Duplex synchronous system that can communicate with peripheral
devices, such as A/D or D/A integrated circuits, serial EEPROMs, etc.
LIBRARY of UART:
Task # 1:
Write a code to transmit a string “Serial Communication” serially. Perform it in Proteus
using virtual Terminal and on Hardware via RS232 and Max232 using HyperTerminal.
Code:
void main()
{
UART1_Init(9600); //Initialize UART module at 9600 bps
Delay_ms(100); //Wait for UART module to stabilize
while(1) //Infinite loop
{
UART1_Write_Text("Serial Communication");
Delay_ms(1000);
}
}
Schemtic:
Task # 2:
Write a code to Receive a data (byte) serially and then transmit back to display it
on Hyper Terminal screen. Perform it in Proteus using virtual Terminal and on Hardware
via RS232 and Max232 using HyperTerminal.
Code:
void main()
{
char data1;
UART1_Init(9600); //Initialize UART module at 9600 BPS
Delay_ms(100); //Wait for UART module to stabilize
while(1)
{
while(UART1_Data_Ready()==0); //If data is ready then
data1= UART1_Read(); //Receive the data(byte) and store it in data1
UART1_Write(data1); //Transmit the received data(byte) data1
}
}
Schemtic:
Press the character you want to transmit and then pic microcontroller receive this and
then send it back. Heper Terminal / Virtual Terminal will display this character on
Screen after receiving.
Lab Task # 3:
Write a code to Receive a string serially until “.” is found and then transmit back to display
it on Hyper Terminal screen. Perform it in Proteus using virtual Terminal and on Hardware
via RS232 and Max232 using Hyper Terminal.
Code:
void main()
{
char output[255];
UART1_Init(9600); // Initialize UART module at 9600 bps
Delay_ms(100); // Wait for UART module to stabilize
while (1)
{
while(UART1_Data_Ready()==0); //Check to see if data is ready or not
{
UART1_Read_Text(output,".",255); // Reads text until '.' is found
UART1_Write_Text(output); //Transmit the text back which is received before "."
}
}
}
Lab Task # 4:
Write a code to Receive a string serially until “.” is found and then count the number of
characters in the input entered and then transmit it back to display it on Hyper Terminal
screen. Perform it in Proteus using virtual Terminal.