USART Serial Port in AVR Microcontrollers: (Chapter 11 of The Mazidi's Book)
USART Serial Port in AVR Microcontrollers: (Chapter 11 of The Mazidi's Book)
USART Serial Port in AVR Microcontrollers: (Chapter 11 of The Mazidi's Book)
Contents
Serial communication
Serial communication programming in C
University of Tehran 2
University of Tehran 3
Data transmission
University of Tehran 5
RS232
University of Tehran 6
RS232
Requires 3 pins:
RXD= receive, TXD: transmit, GND= ground
The RXD and TXD pins are cross-connected
University of Tehran 7
COM ports
University of Tehran 8
University of Tehran 9
USART
USART: Universal Synchronous/Asynchronous
Receiver Transmitter
An standard IC that can provide both
synchronous and asynchronous communication
It is controlled by some AVR registers
University of Tehran 10
University of Tehran 11
USART
University of Tehran 12
Parity bit
A way to detect error during data transmission
Due to external noises
University of Tehran 13
Transmission rate
University of Tehran 14
University of Tehran 15
UDR
University of Tehran 16
UDR
University of Tehran 17
UCSR
University of Tehran 18
UCSRA
University of Tehran 19
UCSRA- continue
University of Tehran 20
UCSRA- continue
University of Tehran 21
UCSRB
University of Tehran 22
UCSRB- continue
University of Tehran 23
UCSRC
UCSRC- continue
University of Tehran 25
UCSR
Character size of the transmitted and received
data- is same for both directions
University of Tehran 26
UBRR
X= UBBR[0-11]
University of Tehran 27
University of Tehran 28
Sampling
University of Tehran 29
USART in Tx mode
University of Tehran 30
USART in Rx mode
University of Tehran 31
Synchronous mode
University of Tehran 32
Synchronous mode
USART programming in C
Send a sequence of numbers started from 0
every 350ms to TXD pin
Check RXD pin and if the received number is
0x55 set PD.6 (bit 6 of port D) to 1
0x66 set PD.6 to 0
University of Tehran 34
University of Tehran 35
USART programming in C
main()
{
int a=0;
DDRD.6=1;
UCSRA=0x0;
UCSRB=0x98; //10011000 (RXIE=1, RXEN=1, TXEN=1)
UCSRC=0x86;// 10000110 (URSEL=1,asynch, no parity, one stop bit, 8 bit)
UBRRH=0;// just set a rate that guarantees the data transfer can be completed before 350ms
UBRRL=0x08;
Interrupt [USART_RXC] usart_rx_isr()
#asm(sei);
{
while(1)
char data;
{
data=UDR;
UDR= a++;
if(data==0x55)
delay_ms(350);
PORTD.6=1;
if(data==0x66)
}
PORTD.6=0;
}
}
University of Tehran 36
USART in PCs
University of Tehran 37