There are two methods for serial data communication (i) Synchronous and (ii) Asynchronous communication. In Synchronous communication method complete block (characters) is sent at a time. It doesn’t require any additional bits (start, stop or parity) to be added for the synchronization of frame. The devices are synchronized by clock. And in asynchronous communication data transmission is done byte by byte i.e., one byte at a time. The additional bits are added to complete a frame.
In synchronous communication the frame consists of data bits while in asynchronous communication the total number of bits in a frame may be more than the data bits.
There are three ways in which serial communication can be done
i. Simplex: Transmission is done in one direction.
ii. Half duplex: Transmission can be done in both the direction but one side at a time.
iii. Full duplex: Transmission can be done in both the direction simultaneously.
Atmega16 is equipped with three different kinds of serial communication peripheral systems:
i. Serial USART
ii. SPI (Serial Peripheral Interface)
iii. TWI (Two wire Interface)
SERIAL USART (universal synchronous asynchronous receiver and transmission/ transmitter):
Serial USART provides full-duplex communication between the transmitter and receiver. Atmega16 is equipped with independent hardware for serial USART communication. Pin-14 (RXD) and Pin-15 (TXD) provide receive and transmit interface to the microcontroller.
Atmega16 USART provides asynchronous mode of communication and do not have a dedicated clock line between the transmitting and receiving end. The synchronization is achieved by properly setting the baud rate, start and stop bits in a transmission sequence.
Start bit and stop bit: These bits are use to synchronize the data frame. Start bit is one single low bit and is always given at the starting of the frame, indicating the next bits are data bits. Stop bit can be one or two high bits at the end of frame, indicating the completion of frame.
Baud Rate: In simple words baud rate is the rate at which serial data is being transferred.
Atmega16 USART has following features:
· Different Baud Rates.
· Variable data size with options ranging from 5bits to 9bits.
· One or two stop bits.
· Hardware generated parity check.
· USART can be configured to operate in synchronous mode.
· Three separate interrupts for RX Complete, TX complete and TX data register empty.
To use the USART of Atmega16, certain registers need to be configured.
UCSR: USART control and status register. It’s is basically divided into three parts UCSRA, UCSRB and UCSRC. These registers are basically used to configure the USART.
UBRR: USART Baud Rate Registers. Basically use to set the baud rate of USART
UDR: USART data register
i. UCSRA: (USART Control and Status Register A)
RXC (USART Receive Complete): RXC flag is set to 1 if unread data exists in receive buffer, and set to 0 if receive buffer is empty.
TXC (USART Transmit complete): TXC flag is set to 1 when data is completely transmitted to Transmit shift register and no data is present in the buffer register UDR.
UDRE (USART Data Register Empty): This flag is set to logic 1 when the transmit buffer is empty, indicating it is ready to receive new data. UDRE bit is cleared by writing to the UDR register.
ii. UCSRB: (USART Control and Status Register B)
RXCIE: RX Complete Interrupt Enable,
When 1 -> RX complete interrupt is enabled.
When 0 -> RX complete interrupt is disabled.
TXCIE: TX Complete Interrupt Enable,
When 1 -> TX complete interrupt is enabled
When 0-> TX complete interrupt is disabled
UDRIE: USART Data Register Empty Interrupt Enable,
When 1 -> UDRE flag interrupt is enabled.
When 0 -> UDRE flag interrupt is disabled.
RXEN: Receiver Enabled,
When 1 -> USART Receiver is enabled.
When 0 -> USART Receiver is disabled.
TXEN: Transmitter Enabled,
When 1 -> USART Transmitter is enabled.
When 0 -> USART Transmitter is disabled.
iii. UCSRC: (USART Control and Status Register C)
The transmitter and receiver are configured with the same data features as configured in this register for proper data transmission.
URSEL: USART Register select. This bit must be set due to sharing of I/O location by UBRRH and UCSRC
UMSEL: USART Mode Select,
When 1 -> Synchronous Operation
When 0 -> Asynchronous Operation
UPM[0:1]: USART Parity Mode, Parity mode selection bits.
USBS: USART Stop Select Bit,
When 0-> 1 Stop Bit
When 1 -> 2 Stop Bits
UCSZ[0:1]: The UCSZ[1:0] bits combined with the UCSZ2 bit in UCSRB sets size of data frame i.e., the number of data bits. The table shows the bit combinations with respective character size.
iv. UDR: (USART Data Register)
The USART Data receive and data transmit buffer registers share the same address referred as USART UDR register, when data is written to the register it is written in transmit data buffer register (TXB). Received data is read from the Receive data buffer register (RXB).
v. UBRRH & UBRRL (USART Baud Rate Registers)
The UBRRH register shares the same I/O address with the UCSRC register, The differentiation is done on the basis of value of URSEL bit.
When URSEL=0; write operation is done on UBRRH register.
When URSEL=1; write operation is done on UCSRC register.
The UBRRH and UBRRL register together stores the 12-bit value of baud rate, UBRRH contains the 4 most significant bits and UBRRL contains the other 8 least significant bits. Baud rates of the transmitting and receiving bodies must match for successful communication to take place.
UBRR register value is calculated by the following formula:
The Connection of MAX232 and ATmega16 is shown in the circuit diagram. The MAX232 is used for level conversion. The reader can refer the component section for further details on MAX 232.The T1IN (pin11) of Max232 is connected to Tx (pin15) of AVR and R1IN(pin12) is connected to Rx(pin14) of AVR. The HyperTerminal software is used to send data to microcontroller via COM port.
Step 1: First step is to select the Baud rate. Baud rate of two devices must match or else they will not be able to synchronize with each other.
#define USART_BAUDRATE 9600
Step2: To set a particular Baud Rate in ATmega16, write the corresponding UDRR value. The UDRR value is calculated by using the formula
#define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1)
Step3: To initialize the USART
i. Turn on transmission and reception circuitry.
ii. Select the number of stop bits.
iii. Set the size of data.
iv. Load the value of UBRR to set the baud rate.
UCSRB |= (1 << RXEN) | (1 << TXEN); // Turn on the transmission and reception circuitry
UCSRC |= (1 << URSEL) | (1<<USBS) | (1 << UCSZ0) | (1 << UCSZ1);
// Use 8-bit character sizes
UBRRL = BAUD_PRESCALE;
// Load lower 8-bits of the baud rate value into the low byte of the UBRR register
UBRRH = (BAUD_PRESCALE >> 8); // Load upper 8-bits of the baud rate value..
// into the high byte of the UBRR register
Step4: Get the data from USART
Monitor the status of RXC (receiver complete) flag . RXC becomes high when it receives the stop bit signal. So if RXC is high it means that the data is loaded into UDR register. Collect the data from UDR or else it might get lost or overwritten with the next incoming data.
unsigned int usart_getch()
while ((UCSRA & (1 << RXC)) == 0);
// Do nothing until data has been received and is ready to be read from UDR
return(UDR); // return the byte