SERIAL COMMUNICATION IN ATMEGA 16 Suren Kumar @ embedautomation.com
Lets get started Wiki:- S erial  Port  is a serial communication physical interface through which information transfers in...
Basics of Serial Communication <ul><li>Computers transfer data in two ways: parallel and serial. </li></ul><ul><li>In para...
Shift Registers <ul><li>In digital circuits, a  shift register  is a group of flip flops set up in a linear fashion which ...
Modem <ul><li>Modem (from modulator-demodulator) is a device that modulates an analog carrier signal to encode digital inf...
Synchronous and Asynchronous <ul><li>Synchronous serial communications </li></ul><ul><li>- The clocks of the sender and re...
Serial Communication Terminology <ul><li>Baud rate: the number of bits sent per second (bps). Baud rate is the number of s...
Data Framing Example
Pin Out of Serial Port
Serial Communication in ATMEGA 16 <ul><li>ATmega16 provides three subsystems for serial communications. </li></ul><ul><li>...
Serial Communication in ATMEGA 16 <ul><li>Max 232:-  As we know for serial port we require TIA/EIA-232-F voltage level i.e...
Schematic of ATMEGA 16 with Serial Port
Serial USART – An Overview <ul><li>USART of the ATmega16 supports </li></ul><ul><li>- baud rates from 960bps up to 57.6kbp...
Block Diagram of USART - Datasheet
Serial USART – Hardware Elements <ul><li>USART Clock Generator: </li></ul><ul><li>- to provide clock source. </li></ul><ul...
USART Baud Rate Registers The UBRRH Register shares the same I/O location as the UCSRC Register. See the “Accessing UBRRH/...
USART Baud Rate Registers Example: Find UBRR registers for baud rate of 1200bps, assuming system clock is 1MHz. Ans: UBRR ...
USART Control & Status Register A  <ul><li>Read the registers USCRA,USCRB,USCRC from the DataSheet. There is no substitute...
USART Data Register <ul><li>Register UDR is the buffer for characters sent or received through the serial port. </li></ul>...
Serial USART – Main Tasks <ul><li>Initializing the serial port. </li></ul><ul><li>Sending a character. </li></ul><ul><li>R...
Initializing Serial Port <ul><li>Set USART communication parameter (data bit, stop bit, parity bit) </li></ul><ul><li>Enab...
Initializing Serial Port - Example <ul><li>Initialize serial port of ATmega16 to baud rate 1200, no parity, 1 stop bit, 8 ...
Sending a character <ul><li>Has UDRE flag been set to 1? (Register UCSRA) </li></ul><ul><li>Write the character to registe...
Receiving a Character  <ul><li>Has RXC flag been set to 1? (Register USCRA) </li></ul><ul><li>Read the received character ...
Upcoming SlideShare
Loading in...5
×

Serial Communication In Atmega 16

31,821

Published on

Published in: Technology
4 Comments
16 Likes
Statistics
Notes
No Downloads
Views
Total Views
31,821
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
0
Comments
4
Likes
16
Embeds 0
No embeds

No notes for slide

Serial Communication In Atmega 16

  1. 1. SERIAL COMMUNICATION IN ATMEGA 16 Suren Kumar @ embedautomation.com
  2. 2. Lets get started Wiki:- S erial Port is a serial communication physical interface through which information transfers in or out one bit at a time (contrast parallel port). Throughout most of the history of personal computers, data transfer through serial ports connected the computer to devices such as terminals and various peripherals. While such interfaces as Ethernet, FireWire, and USB all send data as a serial stream, the term &quot;serial port&quot; usually identifies hardware more or less compliant to the RS-232 standard, intended to interface with a modem or with a similar communication device.
  3. 3. Basics of Serial Communication <ul><li>Computers transfer data in two ways: parallel and serial. </li></ul><ul><li>In parallel data transfer, several data bits are transferred </li></ul><ul><li>simultaneously, e.g. to printers and hard disks. </li></ul><ul><li>In serial data transfer, a single data bit is transferred at one time. </li></ul><ul><li>Advantages of serial communications: longer distances, easier to synchronize, fewer IO pins, and lower cost. </li></ul><ul><li>Serial communications often require </li></ul><ul><li>- Shift registers: convert a byte to serial bits and vice versa. </li></ul><ul><li>- Modems: modulate/demodulate serial bits to/from audio tones. </li></ul>
  4. 4. Shift Registers <ul><li>In digital circuits, a shift register is a group of flip flops set up in a linear fashion which have their inputs and outputs connected together in such a way that the data is shifted down the line when the circuit is activated. </li></ul>
  5. 5. Modem <ul><li>Modem (from modulator-demodulator) is a device that modulates an analog carrier signal to encode digital information, and also demodulates such a carrier signal to decode the transmitted information. </li></ul><ul><li>The goal is to produce a signal that can be transmitted easily and decoded to reproduce the original digital data. </li></ul><ul><li>Modems can be used over any means of transmitting analog signals, from driven diodes to radio. </li></ul>
  6. 6. Synchronous and Asynchronous <ul><li>Synchronous serial communications </li></ul><ul><li>- The clocks of the sender and receiver are synchronized. </li></ul><ul><li>- A block of characters, enclosed by synchronizing bytes, is sent at a time. </li></ul><ul><li>- Faster transfer and less overhead. </li></ul><ul><li>Asynchronous serial communications </li></ul><ul><li>- The clocks of the sender and receiver are not synchronized. </li></ul><ul><li>- One character (8 or 7 bits) is sent at a time, enclosed between a start bit and one or two stop bits. A parity bit may be included. </li></ul>
  7. 7. Serial Communication Terminology <ul><li>Baud rate: the number of bits sent per second (bps). Baud rate is the number of signal changes per second. </li></ul><ul><li>Parity bit: a single bit that is sent together with data bits to make the total number of 1’s even (for even parity) or odd (for odd parity);used for error checking. </li></ul><ul><li>Start bit: to indicate the start of a character. Its typical value is 0. </li></ul><ul><li>Stop bit: to indicate the end of a character. Its typical value is 1. </li></ul>
  8. 8. Data Framing Example
  9. 9. Pin Out of Serial Port
  10. 10. Serial Communication in ATMEGA 16 <ul><li>ATmega16 provides three subsystems for serial communications. </li></ul><ul><li>- Universal Synchronous & Asynchronous Serial Receiver & Transmitter (USART) </li></ul><ul><li>- Serial Peripheral Interface (SPI) </li></ul><ul><li>- Two-wire Serial Interface (TWI) </li></ul><ul><li>USART: </li></ul><ul><li>- Supports full-duplex mode between a receiver and transmitter. </li></ul><ul><li>- Typically used in asynchronous communication. </li></ul><ul><li>- Start bit and stop bit are used for each byte of data. </li></ul>
  11. 11. Serial Communication in ATMEGA 16 <ul><li>Max 232:- As we know for serial port we require TIA/EIA-232-F voltage level i.e. it can accept ±30-V. </li></ul><ul><li>MAX232 has a capacitive voltage generator to supply TIA/EIA-232-F voltage levels from a single 5-V supply. </li></ul><ul><li>Each receiver converts TIA/EIA-232-F inputs to 5-V TTL/CMOS levels. </li></ul>
  12. 12. Schematic of ATMEGA 16 with Serial Port
  13. 13. Serial USART – An Overview <ul><li>USART of the ATmega16 supports </li></ul><ul><li>- baud rates from 960bps up to 57.6kbps, </li></ul><ul><li>- character size: 5 to 9 bits, </li></ul><ul><li>- 1 start bit, </li></ul><ul><li>- 1 or 2 stop bits, </li></ul><ul><li>- parity bit (optional, can be even or odd parity). </li></ul>
  14. 14. Block Diagram of USART - Datasheet
  15. 15. Serial USART – Hardware Elements <ul><li>USART Clock Generator: </li></ul><ul><li>- to provide clock source. </li></ul><ul><li>- to set baud rate using UBRR register. </li></ul><ul><li>USART Transmitter: </li></ul><ul><li>- to send a character through TxD pin. </li></ul><ul><li>- to handle start/stop bit framing, parity bit, shift register. </li></ul><ul><li>USART Receiver: </li></ul><ul><li>- to receive a character through RxD pin. </li></ul><ul><li>- to perform the reverse operation of the transmitter. </li></ul><ul><li>USART Registers: </li></ul><ul><li>- to configure, control and monitor the serial USART. </li></ul>
  16. 16. USART Baud Rate Registers The UBRRH Register shares the same I/O location as the UCSRC Register. See the “Accessing UBRRH/ UCSRC Registers” on page 155 section which describes how to access this register. • Bit 15 – URSEL: Register Select :- This bit selects between accessing the UBRRH or the UCSRC Register. It is read as zero when reading UBRRH. The URSEL must be zero when writing the UBRRH. • Bit 14:12 – Reserved Bits :- These bits are reserved for future use. For compatibility with future devices, these bit must be written to zero when UBRRH is written. • Bit 11:0 – UBRR11:0: USART Baud Rate Register :- This is a 12-bit register which contains the USART baud rate. The UBRRH contains the four most significant bits, and the UBRRL contains the 8 least significant bits of the USART baud rate. Ongoing transmissions by the transmitter and receiver will be corrupted if the baud rate is changed. Writing UBRRL will trigger an immediate update of the baud rate prescaler.
  17. 17. USART Baud Rate Registers Example: Find UBRR registers for baud rate of 1200bps, assuming system clock is 1MHz. Ans: UBRR = 1000000/(16 × 1200) ─ 1 = 51d = 0033H. Therefore, UBRRH = 00H and UBRRL = 33H. C code UBRRH = 0x00; UBRRL = 0x33;
  18. 18. USART Control & Status Register A <ul><li>Read the registers USCRA,USCRB,USCRC from the DataSheet. There is no substitute other than reading datasheet to learn microcontroller </li></ul>
  19. 19. USART Data Register <ul><li>Register UDR is the buffer for characters sent or received through the serial port. </li></ul><ul><li>To start sending a character, write it to UDR. </li></ul><ul><li>unsigned char data; </li></ul><ul><li>data = ‘a’; </li></ul><ul><li>UDR = data; // start sending character </li></ul><ul><li>To check a received character, read it from UDR. </li></ul><ul><li>unsigned char data; </li></ul><ul><li>data = UDR; // this will clear UDR </li></ul>
  20. 20. Serial USART – Main Tasks <ul><li>Initializing the serial port. </li></ul><ul><li>Sending a character. </li></ul><ul><li>Receiving a character. </li></ul><ul><li>Sending/receiving formatted strings. </li></ul>
  21. 21. Initializing Serial Port <ul><li>Set USART communication parameter (data bit, stop bit, parity bit) </li></ul><ul><li>Enable transmitter and reciever </li></ul><ul><li>Set USART for asynchronous mode </li></ul><ul><li>Set Baud Rate </li></ul>
  22. 22. Initializing Serial Port - Example <ul><li>Initialize serial port of ATmega16 to baud rate 1200, no parity, 1 stop bit, 8 data bits. Assume a clock speed of 1MHz. </li></ul><ul><li>void USART_init(void){ </li></ul><ul><li>// Normal speed, disable multi-proc </li></ul><ul><li>UCSRA = 0b00000000; </li></ul><ul><li>// Enable Tx and Rx, disable interrupts </li></ul><ul><li>UCSRB = 0b00011000; </li></ul><ul><li>// Asynchronous mode, no parity, 1 stop bit, 8 data bits </li></ul><ul><li>UCSRC = 0b10000110; </li></ul><ul><li>// Baud rate 1200bps, assuming 1MHz clock </li></ul><ul><li>UBRRL = 0x33; </li></ul><ul><li>UBRRH = 0x00; </li></ul><ul><li>} </li></ul>
  23. 23. Sending a character <ul><li>Has UDRE flag been set to 1? (Register UCSRA) </li></ul><ul><li>Write the character to register UDR for transmission </li></ul><ul><li>Student Task: Write C code to send a character through the Serial Port: </li></ul>
  24. 24. Receiving a Character <ul><li>Has RXC flag been set to 1? (Register USCRA) </li></ul><ul><li>Read the received character from register UDR </li></ul><ul><li>unsigned char USART_receive(void){ </li></ul><ul><li>// wait until RXC flag is set to logic 1 </li></ul><ul><li>……………………………… . </li></ul><ul><li>return (UDR); // Read the received character from UDR </li></ul><ul><li>} </li></ul>

×