EXPERIMENT 7: Introduction To Universal Serial Asynchronous Receive Transmit (Usart) Objective
EXPERIMENT 7: Introduction To Universal Serial Asynchronous Receive Transmit (Usart) Objective
EXPERIMENT 7: Introduction To Universal Serial Asynchronous Receive Transmit (Usart) Objective
(USART)
Objective:
To understand and apply USART command for sending and receiving data
Introduction
Universal Serial Asynchronous Receive Transmit (USART) a.k.a UART or RS -232, is one
of the easiest communication protocols for a device to communicate with PC. It is very useful
for seeing what happen inside the microcontroller, for example the reading from Analogue
to Digital Converter (ADC). Even when current laptop does not have a serial port, the USB to
serial converter with virtual port driver easily solve the problem. The serial port pin
description is shown in Figure 9.1.
EEEB371 E7-1
PIC18F4550
Usually data are sending and receive through USART in bytes. The bytes usually are represent ASCII
characters as shown in Table 9.1(a) and Table 9.1(b).
EEEB371 E7-2
Table 9.1(a) ASCII Printable Characters
EEEB371 E7-3
Table 9.1(b) Non printable ASCII character
EEEB371 E7-4
Library Routines
UARTx_Init to set the baudrate
1 - if data is ready for reading
UARTx_Data_Ready
0 - if there is no data in the receive register
1 - if the data has been transmitted
UARTx_Tx_Idle
0 - otherwise
UARTx_Read Returns the received byte.
Reads characters received via UART until the
UARTx_Read_Text
delimiter sequence is detected
UARTx_Write data to be sent
Sends text via UART. Text should be zero
UARTx_Write_Text
terminated.
Procedure
Hello World
EEEB371 E7-5
2. Write the source code in MikroC Compiler, build it and download the Hex file into the
microcontroller and simulate the PROTEUS program. Print out the source file and write down
your observation. UART1_Write is used to send a character.
void main()
{
3. Write the source code in MikroC Compiler, build it and download the Hex file into the
microcontroller and simulate the PROTEUS program. Print out the source file and write down
your observation. UART1_Write_Text is used to send a text.
void main()
{
char *output ="HELLO WORLD";
// Initialize USART module (8 bit, 9600 baud rate, no parity bit..)
UART1_Init(9600);
UART1_Write(13); // new line
UART1_Write_Text(output);
}
EEEB371 E7-6
Simple Send-Receive a character Program
4. Write the source code in MikroC Compiler, build it and download the Hex file into the
microcontroller and simulate the PROTEUS program. Print out the source file and write down
your observation.
5. Click the Virtual Terminal and type any character. Observe and record the response. Try with
another two different characters. Write down the result/response on the worksheet.
Conversion to ASCII
6. Write the following program, build, download and run the program.
unsigned char i ; // same as unsigned short i;
void NumtoChar(char a)
{
unsigned char digit[3];
digit[0]=a/100; // get the hundredth digit
digit[0]+=0x30; // convert to ASCII
a=a%100; // get the remainder
digit[1]=a/10; // get the tenth digit
digit[1]+=0x30; // convert to ASCII
digit[2]=a%10; // get the remainder
digit[2]+=0x30; // convert to ASCII
UART1_Write(digit[0]);
UART1_Write(digit[1]);
UART1_Write(digit[2]);
}
void main()
{
// Initialize UART1 module (8 bit, 9600 baud rate, no parity bit..)
UART1_Init(9600);
EEEB371 E7-7
7. Observe and record the response on the Virtual Terminal. Identify the output for
Usart_Write(i) and NumtoChar(i).
8. Based on the sample code above and flow chart given, write a program to receive two single
digit numbers, add the two numbers and display the results.
The response should be as follows: (Bold - send by controller, Italic - is typed by the user)
N1: 1
N2: 2
AN: 003
EEEB371 E7-8
Flow chart:
Send character N, 1, :
no
Serial data ready?
yes
Receive first character
no
Serial data ready?
yes
9. Write, build, download and run the program. Demonstrate the output to your instructor.
10. Printout the C program written in procedure 8. Turn off your board.
EEEB371 E7-9