EXPERIMENT 8: Introduction to Universal Serial Asynchronous Receive Transmit (USART) Objective: Introduction To understand and apply USART command for sending and receiving data 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 8.1. Figure 8.1 The serial port and pin description on the PC Straight through Serial Cable Level Converter Figure 8.2: Connection from microcontroller to PC EEEB371 E8-1
The connection is from microcontroller on required 3 wires simple if no handshaking is required. Figure 8.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 send and receive through USART in bytes. The bytes usually are represent ASCII characters as shown in Table 8.1(a) and Table 8.1(b). Table 8.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 E8-2
Table 8.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 E8-3
Procedure Hello World 1. Write the following program, build, download and run the program. void main() // Initialize USART module (8 bit, 9600 baud rate, no parity bit..) Usart_Init(9600); Usart_Write( H ); Usart_Write( e ); Usart_Write( l ); Usart_Write( l ); Usart_Write( o ); Usart_Write( ); Usart_Write( W ); Usart_Write( o ); Usart_Write( r ); Usart_Write( l ); Usart_Write( d ); // Send data via USART // send space 2. On the MikroC program, go to Tools>Usart Terminal. Make sure the setting is the same as your program s USART initialization. Then, click Connect. You may need to reset the microcontroller to rerun the program. After getting the response, Disconnect. Write down the result/response on the worksheet. Simple Send-Receive Program 3. Write the following program, build, download and run the program. EEEB371 E8-4
unsigned char i;// same as unsigned short i; void main() // Initialize USART module (8 bit, 9600 baud rate, no parity bit..) Usart_Init(9600); while(1) // infinite loop if (Usart_Data_Ready()) // If data is received i = Usart_Read(); // Read the received data Usart_Write(i); // Send data via USART 4. Click Connect at the Usart Terminal. Under the communication edit box, type any character and click Send. Observe and record the response. Try with another two different characters. Write down the result/response on the worksheet. After getting the response, Disconnect. Conversion to ASCII 5. 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; Usart_Write(digit[0]); Usart_Write(digit[1]); Usart_Write(digit[2]); // convert to ASCII void main() // Initialize USART module (8 bit, 9600 baud rate, no parity bit..) Usart_Init(9600); i=125; Usart_Write(i); Usart_Write('\n'); NumtoChar(i); // 8 bit number // new line EEEB371 E8-5
6. Observe and record the response on the USART Terminal. Identify the output for Usart_Write(i) and NumtoChar(i). 7. Based on the sample code above and flow char 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 type by user) N1: 1 N2: 2 AN: 003 EEEB371 E8-6
Flow chart: Send character N, 1, : Serial data ready? no yes Receive first character Convert ASCII character to number 0x0F) (AND with Send character \n, N, 2, : Serial data ready? no yes Receive second number/character Convert ASCII character to number 0x0F) (AND with Add first and second number Send character \n, A, n, : Call function NumtoChar to display the addition value 8. Write, build, download and run the program. Demonstrate the output to your instructor. 9. Printout the C program written in procedure 8. Turn off your board. EEEB371 E8-7