Professional Documents
Culture Documents
We examine each of them and show how they are used in full-
duplex serial data communication.
UBRR (USART Baud Rate Register)
a given crystal frequency, the value loaded into the UBRR
For
decides the baud rate.
The relation between the value loaded into UBBR and the Fosc
(frequency of oscillator connected to the XTAL1 and XTAL2 pins)
is dictated by the following formula:
For U2X = 1
Replace 16 with 8
Example:
where X is the value we load into the UBRR register.
To get the X value for different baud rates we can solve the
equation as follows:
Example:
Different Values of UBRR
Fosc =7.372800 MHz = 7372800 Hz
U2X = 0 U2X = 1
Baud Rate (bps)
UBRR % Error UBRR % Error
110 4188.09 0.00 8377.18 0.00
300 1535.00 0.00 3071.00 0.00
1,200 383.00 0.00 767.00 0.00
2,400 191.00 0.00 383.00 0.00
4,800 95.00 0.00 191.00 0.00
9,600 47.00 0.00 95.00 0.00
19,200 23.00 0.00 47.00 0.00
38,400 11.00 0.00 23.00 0.00
57,600 7.00 0.00 15.00 0.00
115,200 3.00 0.00 7.00 0.00
230,400 1.00 0.00 3.00 0.00
460,800 Out of Range Out of Range 1.00 0.00
921,600 Out of Range Out of Range Out of Range Out of Range
MAX232 Connection Diagram
USART Programming using Polling
#include <avr/io.h>
void Tx( unsigned char data ){
while ( !(UCSRA & (1<<UDRE)) ); // wait until UDR is empty
UDR = data;
}
unsigned char Rx(){
while ( !(UCSRA & (1<<RXC)) ); // wait until UDR is empty
return UDR;
}
void init_s(){
UCSRB = (1<<RXEN) |(1<<TXEN);
UCSRC = (1<<UCSZ1)|(1<<UCSZ0);
UBRRL = 51;
}
int main(){
DDRB = 0XFF; DDRD = 0XFE;
init_s();
while(1){
PORTB = Rx(); Tx(++PORTB);
}
}
USART Programming Using Interrupt
#include <avr/io.h>
#include <avr/interrupt.h>
void Tx( unsigned char data ){
while ( !(UCSRA & (1<<UDRE)) ); // wait until UDR is empty
UDR = data;
}
ISR(USART_RXC_vect){
PORTB = UDR; Tx(++PORTB);
}
void init_s(){
DDRD = DDRD & 0xFE;
UCSRB = (1<<RXEN )|(1<<TXEN )|(1<<RXCIE);
UCSRC = (1<<UCSZ1)|(1<<UCSZ0)|(1<<URSEL);
UBRRL = 51;
sei();
}
int main(){
DDRB = 0XFF;
init_s();
while(1);
}
ATmega16 interrupt subsystem
The ATmega16 has 21 interrupts:
1 reset interrupt
3 external interrupts
8 timer interrupts
3 serial port interrupts
1 ADC interrupt
1 analogue comparator interrupt
1 SPI interrupt
1 TWI interrupt
2 memory interrupts
Table 10-1: Interrupt Vector Table for the ATmega32 AVR
Interrupt ROM Location (Hex)
Reset 0000
External Interrupt request 0 0002
External Interrupt request 1 0004
External Interrupt request 2 0006
Time/Counter2 Compare Match 0008
Time/Counter2 Overflow 000A
Time/Counter 1 Capture Event 000C
Time/Counterl Compare Match A 000E
Time/Counter 1 Compare Match B 0010
Time/Counterl Overflow 0012
Time/CounterO Compare Match 0014
Time/CounterO Overflow 0016
SPI Transfer complete 0018
USART, Receive complete 001A
USART, Data Register Empty 001C
USART, Transmit Complete 001E
ADC Conversion complete 0020
EEPROM ready 0022
Analog Comparator 0024
Two-wire Serial Interface (I C)
2
0026
Store Program Memory Ready 0028
Interrupt Vector Name for the ATmega32/ATmegal6
in WinAVR
Interrupt Vector Name in WinAVR
External Interrupt request 0 INT0_vect
External Interrupt request 1 INT1_vect
External Interrupt request 2 INT2_vect
Time/Counter2 Compare Match TIMER2_COMP_vect
Time/Counter2 Overflow TIMER2_OVF_vect
Time/Counterl Capture Event TIMER1_CAPT_vect
Time/Counterl Compare Match A TIMER1_COMPA_vect
Time/Counterl Compare Match B TIMER1_COMPB_vect
Time/Counterl Overflow TIMER1_OVF_vect
Time/Counter0 Compare Match TIMER0_COMP_vect
Time/Counter0 Overflow TIMER0_OVF_vect
SPI Transfer complete SPI_STC_vect
USART, Receive complete USARTO_RX_vect
USART, Data Register Empty USARTO_UDRE_vect
USART, Transmit Complete USARTO_TX_vect
ADC Conversion complete ADC_vect
EEPROM ready EE_RDY_vect
Analog Comparator ANALOG_COMP_vect
Two-wire Serial Interface TWI_vect
Store Program Memory Ready SPM_RDY_vect
Enabling and disabling an interrupt
Upon reset, all interrupts are disabled (masked),
meaning that none will be responded to by the
microcontroller if they are activated.
The interrupts must be enabled (unmasked) by
software in order for the uc to respond to them.
The D7 bit of the SREG (Status Register) is responsi-
ble for enabling and disabling the interrupts globally.
Bit D7 D6 D5 D4 D3 D2 D1 D0
OCIE2 TOIE2 TICIE1 OCIE1A OCIE1B TOIE1 OCIE0 TOIE0
#include "avr/io.h"
#include "avr/interrupt.h"