Basics of Sending Data Serially in AVR (ATmega32)

 


1. Before you start serial communication you have to enable the USART receiver or USART transmitter by writing one to the RXEN or TXEN bit of UCSRB.

 LDI R16, (1<<TXEN)

 OUT UCSRB, R16

2. As we mentioned before, in the AVR you can use either synchronous or asynchronous operating mode. The UMSEL bit of the UCSRC register selects the USART operating mode. Since we want to use the Asynchronous USART operating mode, we have to set the UMSEL bit to zero/low. Note: By default, UMSEL is Zero/LOW, so instruction (0<<UMSEL) can be skipped 

// NOTE: When Writing to USCRC if    URSEL = 1, data will go to USCRC, if URSEL = 0, Data will go to BRRH, because BRRH and USCRC share the same address.

To set the number of data bits (character size) in a frame you must set the values of the UCSZ1 and USCZ0 bits in the UCSRB and UCSZ2 bits in UCSC. 


LDI R16, (1<<URSEL)|(1<<UCSZ1)|(1<<UCSZ0)|(0<<UMSEL)   

OUT UCSRC, R16


3. Set the Baud Rate in UBBR Register, UBBR is 16-Bit Register  So Value need to upload using Half - Half i.e. using UBBRL and UBBRH. For setting the 9600 baud rate 0x33h value need to load, this can be loaded into the lower half.

LDI R16,0x33

OUT UBRRL,R16

 

4. Check if UDR is empty, If empty send data to UDR.By monitoring the UDRE flag, we make sure that we are not overloading the UDR register. If we write another byte into the UDR register before it is empty, the old byte could be lost before it is transmitted. We conclude that by checking the UDRE flag bit, we know whether or not the AVR is ready to accept another byte to transmit. The UDRE flag bit can be checked by the instruction “SBIS UCSRA, UDRE” or we can use an interrupt, to transfer data serially, and avoid tying down the microcontroller with instructions such as “SBIS UCSRA, UDRE” serially at 9600 baud. 

 again:

 SBIS UCSRA, UDRE    // stay here until it is high(means UDR not empty)

 JMP again

 LDI R16, 'B'       // send letter 'G' in UDR

 OUT UDR, R16

 jmp again




Full Code: 

 // Code to send letter 'G' Serially 

.INCLUDE "M32DEF.INC"        

.ORG 0000                        

    LDI R16, HIGH(RAMEND)           

    OUT SPH, R16

    LDI R16, LOW(RAMEND)

    OUT SPL, R16

 // STEP-1: 

  LDI R16, (1<<TXEN)

  OUT UCSRB, R16

 //STEP-2: 

  LDI R16, (1<<URSEL)|(1<<UCSZ1)|(1<<UCSZ0)|(0<<UMSEL)

  OUT UCSRC, R16

 // Step-3

  LDI R16,0x33

  OUT UBRRL, R16

 //Step-4: 

  again: SBIS UCSRA, UDRE    // stay here until it is high(means UDR not empty)

 JMP again

 LDI R16, 'A'       // send letter 'G' in UDR

 OUT UDR, R16 

 jmp again

 

Comments

Popular posts from this blog

br0 Network Interface Explained

U-Boot SPL vs U-BOOT

dB vs dBm