AVR Atmega
BABotre,Scientist AgriElectronicsGroup, CSIRCentral lElectroEngineering l nicsResearch hInstitute, CSIRCEERI,Pilani. ;bbotre@gmail.com g Email:bhau@ceeri.ernet.in;
Serial communication in ATmega16
ATmega16providesthreesubsystemsforserialcommunication. UniversalSynchronousandAsynchronousReceiverand Transmitter(USART) SerialPeripheralInterface(SPI) TwowireSerialInterface(TWI) Wefocusonthissubsysteminthislecture. Supportsfullduplexmodebetweentwodevices. Typicallyusedinasynchronouscommunication. Startbitandstopbitareusedforeachbyteofdata.
USART:
Serial communication in ATmega16
Serial Peripheral Interface (SPI) The receiver and transmitter share a common clock line. Supports higher data rates The transmitter is designed as the master, the receiver as the slave. Examples of devices using SPI : LCD LCD, high speed analogue analogueto-digital converter. Connect several devices such as microcontrollers and display boards, using a two-wire bus. Up to 128 devices are supported. Each device has a unique address and can exchange data with other devices in a small network
Two-wire serial interface(TWI):
Serial USART An overview
USART S of f the ATmega16 supports Baud rates from 960 bps p to 57.6kpbs Character size : 5 to 9 bits 1 start bit, bit 1 or 2 stop bits, Parity bit (optional: ( ti l even or odd dd parity) Common baud rates are 19200, 9600, 4800, 2400, and 1200 bps
Serial USART: Block Diagram
Serial USART Hardware elements
USART Clock Generator: To provide clock source. To set baud rate using UBRR register. register To send a character through TxD pin. To handle strat/stop bit framing, parity bit, shift register. To receive a character through RxD pin. To perform the reverse operation of the transmitter. To configure, control, and monitor the serial USART.
USART Transmitter:
USART Receiver:
USART Registers:
Serial USART Three groups of registers
USARTBaudRateRegisters UBRRHandUBRRL
USARTControlandStatusRegisters UCSRA UCSRB UCSRC
USARTDataRegisters UDR
Understandingtheseregistersisessentialinusingtheserialport. Therefore,wellstudytheseregistersindepth.
USART Baud Rate Registers
Two8bitregisterstogetherdefinethebaudrate.
Example:FindUBRRregistersforbaudrateof1200bps,assumingsystem clockis1MHz.
UBRR=1000000/(16 1200) 1=51d=0033H. Therefore,UBRRH=00HandUBRRL=33H. Ccode UBRRH=0x00;UBRRL=0x33;
USART Control and Status Register A (UCSRA)
1 to enable multi-processor com mode 1 to double the transmission speed
1 when there is parity error 1 when there is data overrun 1 when there is frame error
1 when USART data register is empty 1 when no new data in transmit buffer (tx complete) 1 when receive buffer has unread data (rx complete)
USART Control and Status Register B (UCSRB)
Tx extra data bit for 9-bit character size Rx extra data bit for 9-bit character size bit UCSZ2 to decide character size
1 to enable USART transmitter: Pin D.1 = TXD pin 1 to enable USART receiver: Pin D.0 = RXD pin
1 to enable USART Data Register Empty Interrupt
1 to enable TX Complete Interrupt, valid only if Global Interrupt Flag = 1 and TXC = 1 1 to enable e ab e RX Complete Co p ete Interrupt, te upt, valid a d only o y if Global G oba Interrupt te upt Flag ag = 1 a and d RXC C=1
USART Control and Status Register C (UCSRC)
Clock polarity, used with synchronous Used with UCSZ2 to select character size
To select stop bit modes: 0 ->1 stop bit, 1 -> 2 stop bits To select parity mode: 00 no parity, 10 even party, 11 odd parity To select USART modes: 0 asynchronous, 1 synchronous Must be set to 1 to write to UCSRC. Note: UCSRC and UBRRH share same location.
Setting character size
Charactersize(5,6,7,8,9)isdeterminedbythreebits bitUCSZ2(inregisterUCSRB), bitUCSZ1andbitUCSZ0(inregisterUCSRC).
Example:Foracharactersizeof8bits,weset UCSZ2=0,UCSZ1=1,and dUCSZ0=1.
USART Data Register
RegisterUDRisthebufferforcharacterssentorreceivedthroughthe serialport.
Tostartsendingacharacter,writeittoUDR.
Toprocessareceivedcharacter,readitfromUDR.
Serial USART Main tasks
Thereare4maintasksinusingtheserialport. Initializingtheserialport. port Sendingacharacter. Receiving gacharacter. Sending/receivingformattedstrings.
Initializing Serial Port - Example
InitializeserialportofATmega16tobaudrate9600bps,doublespeed, noparity,1stopbit,8databits.Assumeaclockspeedof8MHz.
voidUSART_init(void){ UCSRA=0b00000010; UCSRB=0b00011000; UCSRC=0b10000110; //doublespeed,disablemultiproc //EnableTx andRx,disableinterrupts //Asyn mode,noparity,1stopbit,8databits
//indoublespeedmode mode,UBRR=Fclock/(8xbaudrate) 1 UBRR=103; } //Baudrate9600bps,assuming8MHzclock
Sending a character - Example
Cfunctiontosendacharacterthrough port. g ATmega16 g serialp
voidUSART_send(unsignedchardata) { //WaituntilUDREflag=1 while((UCSRA&(1<<UDRE))==0x00){;} //WritechartoUDRfortransmission UDR=data; }
Receiving a character - Example
CfunctiontoreceiveacharacterviaATmega16serialport.
g _ ( ) unsigned charUSART_receive(void) { //WaituntilRXCflag=1 while((UCSRA&(1<<RXC))==0x00){;} //ReadthereceivedcharfromUDR return(UDR); }