L3 Serial Ports - USART and Serial Communication ECET 209...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: USART and Serial Communication ECET 209 – Lecture 3 Introduction to Microcontrollers Overview • STDIO • Alternate functions & the USART • ASCII data ECET 209 Purdue University 2 Advantages to C • Stated on the first day that there are several advantages to using the C programming language – Standard Input and Output Functions • Allow us to use the built in functions to utilize the serial port to transmit serial data to the PC ECET 209 Purdue University 3 STDIO • Standard C functions that allow a method of sending or gathering information from your program. • Ability to format numeric and text information. ECET 209 Purdue University 4 The printf() function • One of the standard I/O functions that allows us to send text information to the terminal window through the serial USART. • For example: printf(“Hello World”); ECET 209 Purdue University 5 Hello World ECET 209 Purdue University 6 Alternate Functions • Many Port Pins have alternate (specialized) functions. • One example is the serial port. • Specifically used to communicate with the PC. ECET 209 Purdue University 7 Serial Data Connection ` ECET 209 Purdue University 8 ECET 209 Purdue University 9 Typical data communication Txd Personal Computer ECET 209 Rxd Rxd Txd Purdue University Modem Serial Printer ATmega Dev. Board 10 Serial Data Transmission • Data is sent one bit at a time • From a Parallel in / Serial out shift register • To a Serial in / Parallel out shift register ECET 209 Purdue University 11 ECET 209 Shift Register Shift Register Simplified Serial Transfer Purdue University 12 Simplified Serial Transfer Serial Data Shift Register Parallel Data In Shift Register Load Data Parallel Data Out Clock Sync Logic ECET 209 Purdue University Clock 13 PC serial ports e.g. COM1, COM2 • Standard RS-232 interface • Asynchronous transmission • Protocol must be the same on both transmitter and receiver • Mostly used to send ASCII characters ECET 209 Purdue University 14 What is RS-232 • Standard developed years ago to standardize data communication between devices • Specs logic levels: – ZERO – ONE +3V -3V up to +24V down to -24 V • Includes synchronous and asynch modes ECET 209 Purdue University 15 Asynchronous Data Format • Idle Line defined as Logic “1” • Requires START bit (Logic “0”) • Specified number of data bits (5 - 8 typical) – Least significant bit is sent first • • • • • Optional Even or Odd Parity bit Specified number of stop bits ( 1 - 2) Specified rate of transmission (baud rate) Baud rate = bits/sec Time for each bit = 1/Baud rate ECET 209 Purdue University 16 Serial data protocol TTL Levels 1 IDLE IDLE 0 START ECET 209 D0 D1 D2 D3 D4 D5 D6 D7 D8 STOP Purdue University 17 EXAMPLE: ASCII “5” PROTOCOL: 8 DATA BITS, NO PARITY, 1 STOP BIT, 9600 BAUD 8-BIT ASCII “5” = 35H = 0011 0101 Note: TTL Levels START D0 D1 D2 D3 D4 D5 D6 D7 STOP 1 0 ECET 209 Purdue University 18 RS -232 LEVELS START D0 D1 D2 D3 D4 D5 D6 D7 STOP +5 TTL 0V 0101011001 +12 RS-232 -12 ECET 209 Purdue University 19 Initializing the USART? • Just as before (with the I/O Ports), we have to enable the USART function of the microcontroller ECET 209 Purdue University 20 Configuration • Configuring the USART: At a minimum, you MUST – set the baud rate – enable the transmitter and/or receiver – specify the number of data bits ECET 209 Purdue University 21 Configuring the USART • The baud rate is controlled through the UBRRL and UBRRH registers. • Two options for determining the value to load into these registers – Select the value from a table – Calculate the value ECET 209 Purdue University 22 ECET 209 Purdue University 23 Manual Calculation • The data sheets supply the following equation BAUD = Fclk / ( 16 * (UBRR + 1) ) (rearranged) UBRR = (Fclk / (BAUD * 16)) - 1 ECET 209 Purdue University 24 UBRR = (Fclk / (BAUD * 16)) - 1 ECET 209 Purdue University 25 Manual Calculation • Solve the equation UBRR = (6,000,000 / (9600 * 16)) – 1 UBRR = 38.0625 ECET 209 Purdue University 26 Configuration • UBRR = 38.0625 gets rounded to 38 • Assign the rounded value to UBRRL and UBRRH – set UBRRL equal to 38 – set UBRRH equal to 0 ECET 209 Purdue University 27 C Code • The C code to set the baud rate then becomes: UBRRL = 38; UBRRH = 0; // set the baud rate to 9600 // with 6MHz clock • If you prefer hexadecimal numbers UBRRL = 0x26; ECET 209 Purdue University 28 Manual Configuration • Set the baud rate • Enable the transmitter and receiver • Specify the number of data bits ECET 209 Purdue University 29 Enable TX and RX • Bits to enable both the transmitter and receiver are located in the UCSRB register. Bit 7 6 5 4 3 2 1 0 RXCIE TXCIE UDRIE RXEN TXEN UCSZ2 RXB8 TXB8 Read/Write R/W R/W R/W R/W R/W R/W R/W R/W Initial Value 0 0 0 0 0 0 0 0 ECET 209 Purdue University UCSRB 30 C Code • The C code to set the TXEN and RXEN UCSRB = 0x18; ECET 209 // enable TX and RX Purdue University 31 Manual Configuration • Set the baud rate • Enable the transmitter and receiver • Specify the number of data bits ECET 209 Purdue University 32 Number of Data Bits • The UCSRC data register controls the number of data bits in the transmission. UCSRC = 0x86; ECET 209 // select 8 data bits Purdue University 33 The C Initialization UBRRL = 38; UBRRH = 0; // SET THE BAUD RATE // 9600 BAUD, W/ 6MHz UCSRB = 0x18; // ENABLE TX & RX UCSRC = 0x86; // SET 8 DATA BITS ECET 209 Purdue University 34 Questions? ECET 209 Purdue University 35 Digital I/O vs. Serial Port • Cannot be both (they share the same pins) • When the USART is enabled, the internal circuitry “disconnects” the pin from the digital interface circuitry. ECET 209 Purdue University 36 Disable TX and RX • The BootLoader leaves the TX and RX enabled! • Must always disable these bits located in the UCSRB register to use PORTD as general I/O Bit 7 6 5 4 3 2 1 0 RXCIE TXCIE UDRIE RXEN TXEN UCSZ2 RXB8 TXB8 Read/Write R/W R/W R/W R/W R/W R/W R/W R/W Initial Value 0 0 0 0 0 0 0 0 ECET 209 Purdue University UCSRB 37 The C Code • PORTD configured as an input with the internal pull-up resistors on. DDRD = 0x00; // configure PORTD for Input PORTD = 0xFF; // turn on the pull-up resistors UCSRB = 0x00; // disable the USART ECET 209 Purdue University 38 ECET 209 Purdue University 39 printf() • The printf() function allows us to output text with embedded numerical data also. • for instance: printf(“The value is %d”, value); – assuming value was 25, then ECET 209 Purdue University 40 The value is 25 ECET 209 Purdue University 41 Using STDIO • The STDIO functions allow us to perform calculations and send the results out of the serial port (you will do this in Lab 2) ECET 209 Purdue University 42 Using printf() For example degree_C = 5 * (degree_F – 32) / 9; printf(“The temp in C is %d”, degree_C); ECET 209 Purdue University 43 STDIO for Lab #2 printf(“The temp in C is %d\n\r”, degree_C); • %d outputs the next argument as a signed decimal integer • %u outputs the next argument as an unsigned decimal integer • %x outputs the next argument as an unsigned hexadecimal integer using lower case letters • %X outputs the next argument as an unsigned hexadecimal integer using upper case letters • %c outputs the next argument as an ASCII character ECET 209 Purdue University 44 What is ASCII Data? • American Standard Code for Information Interchange (ASCII) – Method for encoding humanly readable characters – Uses 7 bits to represent • Upper and Lower case letters • Numbers and Symbols • Punctuation and Control Characters ECET 209 Purdue University 45 ECET 209 Purdue University 46 0x20 ECET 209 Purdue University 47 0x30 ECET 209 Purdue University 48 Lab 2 • Under Procedure 2 – Display ASCII data on the Terminal – Set the toggles for a value between 0x30 and 0x70 ECET 209 Purdue University 49 ...
View Full Document

This note was uploaded on 02/22/2012 for the course ECET 209 taught by Professor Richardson during the Fall '08 term at Purdue University-West Lafayette.

Ask a homework question - tutors are online