Sie sind auf Seite 1von 9

EXPERIMENT 7: Introduction to Universal Serial Asynchronous Receive Transmit

(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.

Figure 9.1 The serial port and pin description on the PC

EEEB371 E7-1
PIC18F4550

Figure 9.2: Connection from microcontroller to PC

The connection is from microcontroller on required 3 wires simple if no handshaking is required.


Figure 9.2 illustrate the connection from microcontroller to PC. The logic levels for serial port for RS
232 port where logic 1 represented by -10V and logic 0 represented by +10V. The microcontroller
logic levels are based on TTL or CMOS. ( 5V for logic 1 and 0V for logic 0). To match the logic
level, a level converter hardware ( e.g MAX232 IC) must be used.

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

Dec Hex Character Dec Hex Character Dec Hex Character


32 20 64 40 @ 96 60 `
33 21 ! 65 41 A 97 61 a
34 22 " 66 42 B 98 62 b
35 23 # 67 43 C 99 63 c
36 24 $ 68 44 D 100 64 d
37 25 % 69 45 E 101 65 e
38 26 & 70 46 F 102 66 f
39 27 ' 71 47 G 103 67 g
40 28 ( 72 48 H 104 68 h
41 29 ) 73 49 I 105 69 i
42 2A * 74 4A J 106 6A j
43 2B + 75 4B K 107 6B k
44 2C , 76 4C L 108 6C l
45 2D - 77 4D M 109 6D m
46 2E . 78 4E N 110 6E n
47 2F / 79 4F O 111 6F o
48 30 0 80 50 P 112 70 p
49 31 1 81 51 Q 113 71 q
50 32 2 82 52 R 114 72 r
51 33 3 83 53 S 115 73 s
52 34 4 84 54 T 116 74 t
53 35 5 85 55 U 117 75 u
54 36 6 86 56 V 118 76 v
55 37 7 87 57 W 119 77 w
56 38 8 88 58 X 120 78 x
57 39 9 89 59 Y 121 79 y
58 3A : 90 5A Z 122 7A z
59 3B ; 91 5B [ 123 7B {
60 3C < 92 5C \ 124 7C |
61 3D = 93 5D ] 125 7D }
62 3E > 94 5E ^ 126 7E ~
63 3F ? 95 5F _

EEEB371 E7-3
Table 9.1(b) Non printable ASCII character

Dec Hex Abbr Character Description


0 0 NUL \0 Null character
1 1 SOH Start of Header
2 2 STX Start of Text
3 3 ETX End of Text
4 4 EOT End of Transmission
5 5 ENQ Enquiry
6 6 ACK Acknowledgment
7 7 BEL \a Bell
8 8 BS \b Backspace[t 4][t 5]
9 9 HT \t Horizontal Tab
10 0A LF \n Line feed
11 0B VT \v Vertical Tab
12 0C FF \f Form feed
13 0D CR \r Carriage return[t 6]
14 0E SO Shift Out
15 0F SI Shift In
16 10 DLE Data Link Escape
17 11 DC1 Device Control 1 (oft. XON)
18 12 DC2 Device Control 2
Device Control 3 (oft.
19 13 DC3 XOFF)
20 14 DC4 Device Control 4
21 15 NAK Negative Acknowledgement
22 16 SYN Synchronous Idle
23 17 ETB End of Trans. Block
24 18 CAN Cancel
25 19 EM End of Medium
26 1A SUB Substitute
27 1B ESC \e Escape[t 8]
28 1C FS File Separator
29 1D GS Group Separator
30 1E RS Record Separator
31 1F US Unit Separator
127 7F DEL Delete

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

1. Construct the simple UART circuit below using PROTEUS.

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()
{

// Initialize USART module (8 bit, 9600 baud rate, no parity bit..)


UART1_Init(9600);

UART1_Write('H'); // Send data via USART


UART1_Write('e');
UART1_Write('l');
UART1_Write('l');
UART1_Write('o');
UART1_Write(' '); // send space
UART1_Write('W');
UART1_Write('o');
UART1_Write('r');
UART1_Write('l');
UART1_Write('d');

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.

unsigned char i;// same as unsigned short i;


void main()
{
// Initialize USART module (8 bit, 9600 baud rate, no parity bit..)
UART1_Init(9600);

while(1){ // infinite loop


if (UART1_Data_Ready()==1)
{ // If data is received
i = UART1_Read(); // Read the received data
UART1_Write(i); // Send data via USART
UART1_Write(13); // new line }
}
}

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);

i=125; // 8 bit number


UART1_Write(i);
UART1_Write(13); // new line
NumtoChar(i);
}

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

Convert ASCII character to number (AND with


0x0F)

Send character \n, N, 2, :

no
Serial data ready?

yes

Receive second number/character

Convert ASCII character to number (AND with


0x0F)

Add first and second number

Send character \n, A, n, :

Call function NumtoChar to display


the addition value

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

Das könnte Ihnen auch gefallen