0% found this document useful (0 votes)
104 views

Chapter 1 - AVR Microcontrollers

The document provides an introduction to AVR microcontrollers, including their history, the MegaAVR family, key features, architecture, and applications. It discusses the origins and development of AVR, describes some common members of the MegaAVR line, and compares AVR to other microcontroller families like PIC and 8051 in terms of performance, power, and development tools.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
104 views

Chapter 1 - AVR Microcontrollers

The document provides an introduction to AVR microcontrollers, including their history, the MegaAVR family, key features, architecture, and applications. It discusses the origins and development of AVR, describes some common members of the MegaAVR line, and compares AVR to other microcontroller families like PIC and 8051 in terms of performance, power, and development tools.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

DIGITAL SYSTEMS III

S2 2023

Chapter 1
Introduction to AVR Microcontrollers

www.vut.ac.za

1
CONTENTS

1. History of AVRs 1
2. MegaAVR Family 2
3. Whats Special about AVRs 3
4. Basic Peripheral Functions of AVR 4
5. Internal Architecture 5
6. The Arduino Family 6
7. Recommended Readings 7

The contents of this presentation is confidential. ©VUT

2
History of AVR
▪ AVR was developed in the year 1996 by Atmel Corporation.
▪ The architecture of AVR was developed by Alf-Egil Bogen and VegardWollan.
▪ AVR derives its name from its developers and stands for Alf-Egil Bogen Vegard Wollan RISC microcontroller, also known as
Advanced Virtual RISC.
▪ The AT90S8515 was the first microcontroller which was based on AVR architecture however the first microcontroller to hit
the commercial market was AT90S1200 in the year 1997.
▪ AVR microcontrollers are available in three categories:
1. TinyAVR – The TinyAVR family consists of small-sized microcontrollers with limited resources, making them suitable for low-cost and
low-power applications. They typically have a smaller number of pins and are available in compact package options. TinyAVR
microcontrollers are known for their simplicity and ease of use, making them ideal for basic projects with moderate processing
requirements.
2. MegaAVR – The MegaAVR family offers a broader range of microcontrollers with varying feature sets, including a larger number of I/O
pins, more memory options (both program memory and data memory), and enhanced peripherals. MegaAVR microcontrollers provide a
good balance between performance and cost, making them suitable for a wide range of applications, from consumer electronics to industrial
control systems
3. XmegaAVR – XMegaAVR microcontrollers feature a higher number of I/O pins, larger memory capacities, advanced peripherals
3 (such
as high-speed analog-to-digital converters and advanced communication interfaces), and more advanced power management options.
MegaAVR Family
▪ ATmega AVR microcontrollers offer a good balance between performance, features, and cost.
▪ They are well-suited for a wide range of applications, including consumer electronics, industrial automation, robotics,
Internet of Things (IoT) devices, and more.
▪ Their popularity stems from their reliability, ease of use, extensive peripheral options, and the support provided by the
Microchip development ecosystem and community.
▪ The following table shows the comparison between different members of MegaAVR family:

Part Name ROM RAM EEPROM I/0 Pins Timer Interrupts Operation Operating Packaging
Voltage frequency
ATmega8 8KB 1KB 512B 23 3 19 4.5-5.5 V 0-16 MHz 28
ATmega8L 8KB 1KB 512B 23 3 19 2.7-5.5 V 0-8 MHz 28
ATmega16 16KB 1KB 512B 32 3 21 4.5-5.5 V 0-16 MHz 40
ATmega16L 16KB 1KB 512B 32 3 21 2.7-5.5 V 0-8 MHz 40
ATmega32 32KB 2KB 1KB 32 3 21 4.5-5.5 V 0-16 MHz 40
ATmega32L 32KB 2KB 1KB 32 3 21 2.7-5.5 V 0-8 MHz 40

4
What’s special about AVR?
▪ AVR microcontrollers are a popular choice for embedded systems due to their many unique features and
advantages, including:
▪ High Performance: AVR microcontrollers are designed to offer high performance at low power consumption. They are typically based on a
RISC (Reduced Instruction Set Computing) architecture, which allows for faster and more efficient processing.
▪ Low Power Consumption: AVR microcontrollers are designed to operate on very low power, making them ideal for battery-powered and
other low-power applications.
▪ High Integration: AVR microcontrollers typically include a wide range of on-chip peripherals, such as timers, ADCs, DACs, and serial
communication interfaces. This high level of integration reduces the need for external components and simplifies system design.
▪ Flexibility: AVR microcontrollers are highly versatile and can be used in a wide range of applications, from low-cost, low-power applications
to high-performance, complex systems.
▪ Easy to Use: AVR microcontrollers are easy to program and use, thanks to a variety of available software tools and development
environments. They are also compatible with a wide range of third-party development tools and libraries.
▪ Availability: AVR microcontrollers are widely available from multiple vendors, making them easy to source and use in a variety of
applications.
▪ Cost-Effective: AVR microcontrollers are typically priced competitively, making them an affordable option for a wide range of applications.
5
How is AVR different from PIC and 8081 microcontrollers?
▪ 8-bit AVR, PIC, and 8051 are all popular microcontroller families that are widely used in embedded systems. While they share some
similarities, they also have significant differences in terms of their architectures, features, and capabilities.
▪ Each microcontroller family has its own unique strengths and weaknesses. Hence, the choice of microcontroller will depend on the specific
requirements of the application, including factors such as processing power, memory, power consumption, and development tools.
Feature AVR PIC 8051
Architecture They are based on a RISC architecture, which They are based on a modified Harvard architecture, They are based on a CISC (Complex Instruction
allows for faster and more efficient processing. which separates program and data memory to improve Set Computing) architecture, which includes a
performance. large instruction set and supports more complex
operations.
Clock Speed They typically operate at higher clock speeds They generally operate at lower clock speeds than AVR They operate at relatively low clock speeds,
compared to PIC and 8051 microcontrollers. microcontrollers but can be optimized for low-power making them suitable for low-power applications.
applications
Memory: They typically have a larger amount of flash They have a smaller amount of flash memory than AVR They have a relatively small amount of memory
memory compared to PIC and 8051 microcontrollers but can be programmed using a range compared to AVR and PIC microcontrollers.
microcontrollers. of different memory types.

Power They are designed to operate on very low power, They are also designed for low power consumption, and They consume more power compared to AVR and
Consumption making them ideal for battery-powered and can be optimized for specific power-saving modes. PIC microcontrollers due to their larger
other low-power applications. instruction set and slower clock speeds.
Development They are well-supported by a range of They are also well-supported by a range of development They have been in use for many years and are
Tools development tools and environments, including tools and environments, including the MPLAB X IDE supported by a wide range of development tools
the popular Atmel Studio IDE. and environments
Applications They are suitable for a wide range of applications, They are widely used in industrial and automotive They are commonly used in low-cost and low-
from low-cost, low-power applications to high- applications due to their robustness and reliability. 6
power applications, such as home automation
performance, complex systems. and small-scale embedded systems
Internal Architecture
NOTE: The architecture discussed in this module is not intended to represent any specific AVR device, just an 8-bit AVRs
in general.
▪ Internally, an AVR ATmega microcontroller consists of an AVR CPU and various input/output, timing, analog-
to-digital conversion, counter/timer, and serial interface functions, along with other functions depending on
the part number. These features are referred to by Atmel as peripheral functions.
▪ AVRs are built using a modified Harvard Architecture. This means the Flash Program Memory space is on a separate
address bus than the Static Random Access Memory (SRAM). There are two data bus, one that can access all data and the
In/Out data bus with limited access to a small section of memory
▪ The main differences between the AVR microcontroller types lie in the amount of on-board flash memory and available
I/O functions.
▪ All the 8-bit AVR microcontrollers (identified by their parts Numbers) use essentially the same AVR CPU core.
▪ They (8-bit microcontroller) belonging to the family of Reduced Instruction Set Computer (RISC).
▪ In RISC architecture the instruction set of the computer are not only fewer in number but also simpler and faster in
operation.
▪ The other type of categorization is CISC (Complex Instruction Set Computers)
7
A Generic AVR Microcontroller Block
Diagram

8
Basic Peripheral Functions of AVR
▪ The heart of an AVR microcontroller is the 8-bit CPU, but what makes it a truly useful microcontroller is the built-in peripheral functions
integrated into the IC with the CPU logic.
▪ The peripheral functions are controlled by the CPU via an internal high-speed data bus.
▪ All peripheral functions share port pins with the discrete digital I/O capabilities.
▪ The peripheral functions of an AVR device vary from one type to another. The list below is some of the peripheral functions you will find
build-in in the 8-bit AVR microcontroller:
1. Digital I/O Ports - All AVR parts provide bidirectional I/O pins for discrete digital signals
2. Timers /Counters - AVR microcontrollers include several timers that can be used for a variety of timing and control applications, such as generating
interrupts at specified intervals or controlling the duty cycle of PWM signals.
3. Watch dogs timers - AVR microcontrollers typically include a watchdog timer that can be used to reset the microcontroller if it becomes unresponsive or
crashes.
4. Pulse-Width Modulation (PWM) - AVR microcontrollers include one or more PWM channels that can be used for controlling the speed of motors or the
brightness of LEDs.
5. Serial I/O – The serial I/O consist of Universal Synchronous/Asynchronous Receiver/Transmitter (USART), Inter-Integrated Circuit (I2C) and Serial
Peripheral Interface (SPI) used for communication with other devices, such as sensors or other microcontrollers.
6. Interrupts - The AVR microcontroller includes a highly flexible and programmable interrupt controller that can manage multiple interrupts from different
sources.
7. ADC /DAC - AVR microcontrollers typically include one or more ADCs that can be used to measure analog signals, such as temperature or voltage, and
convert them into digital values for processing. Some AVR microcontrollers include DACs that can be used to generate analog signals, such as audio signals or
control signals for motor control.
8. Analog Comparators - compares two analog input voltages and generates a digital output based on the comparison result. The output of the comparator can
be used for various applications, such as detecting changes in a signal level, triggering an interrupt, or controlling a circuit or system based on the input voltage
level. 9
AVR CPU

• The 8-bit AVR microcontroller is built around a Harvard


architecture CPU, which consists of a powerful ALU (Arithmetic
Logic Unit), a set of general-purpose registers, and a range of
addressing modes. Here are some key features of the 8-bit AVR
CPU:
• RISC Architecture:The AVR CPU uses a Reduced Instruction Set
Computer (RISC) architecture, which means that it has a small number
of simple instructions that execute quickly and efficiently.
• Efficient Instruction Execution:The AVR CPU executes most
instructions in a single clock cycle, allowing it to achieve high
performance with low power consumption.
• General-Purpose Registers:The AVR CPU has 32 general-purpose
registers, which can be used for storing data and performing
calculations. These registers are organized into three banks, each of
which contains 16 registers.
• Addressing Modes:The AVR CPU supports a variety of addressing
modes, including direct, indirect, indexed, and relative addressing
modes. These modes provide flexibility in accessing memory and 10

operands, and can improve code efficiency.


AVR CPU

• The 8-bit AVR microcontroller is built around a Harvard architecture CPU,


which consists of a powerful ALU (Arithmetic Logic Unit), a set of general-
purpose registers, and a range of addressing modes. Here are some key
features of the 8-bit AVR CPU:
• Interrupt Handling: The AVR CPU has a highly efficient interrupt
handling system, which can respond to an interrupt in just a few clock
cycles. The interrupt system allows the CPU to respond quickly to
external events, such as sensor readings or user input.
• Low Power Consumption: The AVR CPU is designed to operate at
low power levels, which makes it well-suited for battery-powered
applications. The CPU can be configured to enter various sleep modes
to reduce power consumption when the microcontroller is not
performing any processing.
• Program and Data Memory: The AVR CPU has separate program
memory and data memory spaces, which allows it to fetch and execute
instructions from program memory while storing data in a separate
data memory space.
11
AVR CPU Core Components
• Below is the discussion of AVR CPU components and their functions:
• Registers – The CPU uses registers to store information on temporarily basis which could be data to be processed, or an address
pointing to the data which is to be fetched into the CPU or a configuration data to guide the CPU on how to perform operations.
• The fast-access Register file contains 32 x 8-bit General Purpose Working registers with a single clock cycle access time.
• Six of the 32 registers can be used as three 16-bit indirect address register pointers for Data Space addressing, enabling efficient address calculations.
• One of these address pointers can also be used as an address pointer for lookup tables in Flash program memory. These added function registers are
the 16-bit X-,Y-, and Z-registers.
• Arithmetic Logic Unit (ALU) - The ALU supports arithmetic and logic operations between registers or between a constant and a
register.
• Single clock cycle access time allows single-cycle ALU operations.
• In a typical ALU operation, two operands are output from the Register file, the operation is executed and the result is stored
back in the Register file in one clock cycle. Single register operations can also be executed in the ALU. After an arithmetic
operation, the Status register is updated to reflect information about the result of the operation.
• Program flow is provided by conditional and unconditional jump and call instructions, able to directly address the whole address
space.
• Most AVR instructions have a single 16-bit word format and every program memory address contains a 16- or 32-bit
instruction.
12
AVR CPU Core Components
▪ Below is the discussion of AVR CPU components and their functions:
▪ Memory - The memory spaces in the AVR architecture are all linear and regular memory maps.
▪ Program Flash memory space is divided into two sections, the Boot Program section and the Application Program section. Both
sections have dedicated Lock bits for write and read/write protection. The Store Program Memory (SPM) instruction that
writes into the Application Flash memory section must reside in the Boot Program section.
▪ During interrupts and subroutine calls, the return address in the Program Counter (PC) is stored on the stack. The stack is
effectively allocated in the general data SRAM and consequently, the stack size is only limited by the total SRAM size and the
usage of the SRAM.
▪ All user programs must initialize the Stack Pointer (SP) in the Reset routine (before subroutines or interrupts are executed).
The SP is read/write accessible in the I/O space. The data SRAM can easily be accessed through the five different addressing
modes supported in the AVR architecture.
▪ The I/O memory space contains 64 addresses for CPU peripheral functions as Control registers, Serial Peripheral Interface
(SPI), and other I/O functions. The I/O Memory can be accessed directly, or as the Data Space locations following those of the
Register file, 0x20 - 0x5F. In addition, this device has extended I/O space from 0x60 - 0xFF in SRAM.

13
Internal Architecture - Memory

▪ The AVR memory architecture has two main memory spaces, the Data Memory and the Program Memory space. In
addition, the ATmega328P features an EEPROM Memory for data storage.
▪ All three memory spaces are linear and regular and they as follows:
▪ Flash Memory - The flash memory is used to store program code
▪ SRAM (static random-access memory) - the SRAM is used to hold transient data such as program variables and the stack
▪ EEPROM Memory- can hold data that needs to persist between software changes and power cycles.
▪ The flash and EEPROM can be loaded externally, and both will retain their contents when the AVR is powered off. The
SRAM is volatile, and its contents will be lost when the AVR loses power.

14
Internal Architecture - Control Registers

▪ In addition to 32 general-purpose registers in the CPU, AVR devices also have a number of control registers that
determine how the I/O ports, timers, communications interfaces, and other features will behave.
▪ Control registers (separate from the CPU registers) are used to configure the operation of the peripherals.
▪ The list below discuss in brief groups of control registers available in an 8-bit AVR microcontroller:
▪ General Purpose Input/Output (GPIO) Registers: These registers control the input and output pins of the microcontroller. Each
GPIO register controls eight pins, and each bit in the register corresponds to a specific pin. By setting or clearing the appropriate bits in the
GPIO registers, the microcontroller can control the state of the pins.
▪ Timer/Counter Registers: AVR microcontrollers have several timers/counters that can be used to measure time intervals or generate
precise timing signals. Each timer/counter has several Control Registers that control its operation, including the timer/counter value,
prescaler, interrupt flags, and control modes.
▪ Interrupt Control Registers: These registers control the operation of the interrupt system in the microcontroller. They allow the
programmer to enable or disable specific interrupts, set the interrupt priority level, and control the global interrupt enable flag.
▪ Power Management Registers: These registers control the power management features of the microcontroller. They allow the
programmer to control the sleep modes, enable/disable specific peripherals, and control the clock sources.
▪ Watchdog Timer Control Register: This register controls the operation of the Watchdog Timer (WDT). It allows the programmer to
enable/disable the WDT, set the timeout period, and control the interrupt and reset modes.
▪ Analog-to-Digital Converter (ADC) Registers: These registers control the operation of the ADC in the microcontroller. They allow
the programmer to select the ADC input channel, set the reference voltage, and control the start and stop of the conversion process.
▪ USART Control Registers: These registers control the operation of the USART (Universal Synchronous/Asynchronous
Receiver/Transmitter) in the microcontroller. They allow the programmer to set the baud rate, control the transmitter and receiver
operation, and control the interrupt flags. 15
Internal Architecture - Digital I/O Ports

▪ 8-bit AVR digital I/O refers to the ability of these microcontrollers to read digital signals from external devices and control digital
output signals to control external devices
▪ These I/O pins are typically organized in ports, with each port consisting of eight pins that can be accessed and controlled
simultaneously
▪ A port is an 8-bit register wherein some or all of the bits are connected to physical pins on the AVR device package.
▪ Different types of AVR devices have different numbers of ports, ranging from one for the ATTINY13-20SQ up to nine for the
ATmega649.
▪ Ports are labelled as A, B, C, and so on.
▪ Each pin of a port is controlled by internal logic that manages the signal direction, the state of an internal pull-up resistor, timing,
and other functions.
▪ Because of the sophisticated logic used to control functionality, an AVR port can perform many different functions—some of them
simultaneously.
▪ When a port is configured as an output it is still possible to read data from it, and an output can be used to trigger an interrupt.

16
Internal Architecture - 8-Bit Timer/ Counter

▪ Timer/Counter is one of the essential peripheral features of 8-bit AVR microcontrollers.


▪ They provide timing and counting capabilities to the microcontroller, allowing precise control of time-dependent
events, such as generating a pulse, measuring a period, or controlling the frequency of an external signal.
▪ The timer/counter modes of operation are as follows:
▪ Normal mode: In this mode, the timer counts from an initial value to the maximum value, generating an interrupt when it
overflows.
▪ CTC (Clear Timer on Compare Match) mode: In this mode, the timer counts from an initial value to a value set by the
user. When the timer value matches the compare value, it generates an interrupt and resets to the initial value.
▪ PWM (Pulse Width Modulation) mode: In this mode, the timer generates a PWM signal with a variable duty cycle. The
duty cycle is set by changing the compare value during operation.
▪ Input Capture mode: In this mode, the timer captures the value of an external event, such as a pulse or frequency, on a
specified pin. The timer value is stored in a register and can be used for further processing.

17
Internal Architecture - 16-Bit Timer/ Counter

▪ The 16-bit timer/counter is similar to the 8-bit version, but with an extended count range.
▪ It is true 16-bit logic, which allows for 16-bit variable period PWM generation.
▪ The module also features two independent output comparison circuits, double buffered output comparison
registers, and an input capture circuit with noise cancelling.
▪ In addition to PWM generation the 16-bit timer/counter can be used for high resolution external event capture,
frequency generation, and signal timing measurement. It has

18
Internal Architecture - Timer / Counter Prescaler

▪ The prescaler is essentially a divider circuit that generates a derivative of the system I/O clock at f/8,
f/64, f/256, or f/1024, which are referred to as taps.
▪ The use o a prescaler allows the range of a timer/counter to be extended to more closely match the rate at
which an external event occurs, and also increases the time in between timer/counter overflows and
resets.
▪ In an AVR device one or more counters may share the same prescaler logic, but with different settings.
▪ One timer/counter might use the f/64 tap, whereas another might use the f/1024 tap.

19
Internal Architecture - Analog Comparators
▪ The analog comparator section of an AVR microcontroller is used to compare the input voltages on the AIN0
and AIN1 pins.
▪ Although AIN0 is defined as the positive input and AIN1 as the negative, this refers to the relationship between
them, not the actual polarity of the input voltages.
▪ When AIN0 is greater than AIN1, the comparator logic sets the comparator flag ACO.
▪ The output of the comparator can be configured to trigger the input capture function of one of the
timer/counter modules, and it can also generate an interrupt specific to the comparator.
▪ The interrupt event can be configured to trigger on comparator output rise, fall, or toggle.
▪ The analog comparator circuit can do more than just compare the voltages on the AIN0 and AIN1 inputs.
▪ The input of the analog comparator may also be configured such that the AIN1 input can be compared to the
internal bandgap reference voltage, or AIN0 can be compared to the output of the ADC multiplexer (and this
voltage is still available to the input of the ADC).
▪ The unusual symbols with four arrows are analog gates. How a gate will respond to a control input is indicated
by the inversion circle—when the inverting control input is used it will pass an analog signal when the control is
low, and otherwise it will pass a signal when it is high.

20
Internal Architecture - Analog-to-Digital Converter

▪ 8-bit AVR microcontrollers provide analog-to-digital conversion (ADC) and digital-to-analog conversion (DAC)
capabilities, allowing them to interface with analog signals.
▪ These features are essential in many applications, such as sensor interfacing, audio processing, and power management.
▪ The ADC can be configured to sample the analog signal at a specific rate and resolution, providing accurate and reliable
digital data.
▪ The AVR ADC uses successive approximation conversion, where it converts the analog signal into a binary value by
comparing it with a reference voltage. The accuracy of the ADC depends on the reference voltage, resolution, and sampling
rate.
▪ Most AVR microcontrollers contain the resolution of 8-bit, 10-bit, or 12-bit analog-to-digital converter.
▪ The actual number of available inputs depends largely on the physical package.
▪ Each input is selected one at a time via an internal multiplexer—they are not all active simultaneously.
▪ In addition, some of the I/O pins used by the ADC input multiplexer may also be assigned to other functions.
▪ The typical conversion time for a 10-bit AVR ADC in free-running mode, while still maintaining full resolution, is around
65 microseconds (μs) per sample.
21
Internal Architecture - Serial I/O
▪ The ATmega provides three primary forms of serial interface:
▪ synchronous/asynchronous serial (USART),
▪ SPI master/slave synchronous,
▪ A byte-oriented two-wire interface similar to the Philips I2C (Inter-Integrated Circuit) standard.
▪ USART – It is a versatile serial communication protocol that supports both synchronous and asynchronous modes of
operation.
▪ The protocol allows devices to exchange data in a full-duplex fashion using two wires: TXD (Transmit Data) and RXD (Receive Data).
▪ USART supports several configurable parameters, such as baud rate, parity, stop bits, and data word length.
▪ This function can be used to implement an RS-232 or RS-485 interface, or used without external interface logic for chip-to-chip
communications.
▪ The USART module includes a transmit buffer, a receive buffer, and control registers that configure the USART module.
▪ To use the USART module in an AVR microcontroller, the programmer needs to configure the USART control registers to set the
USART mode (synchronous or asynchronous), baud rate, parity, stop bits, and data word length.
▪ In the asynchronous mode, the AVR microcontroller generates a start bit, followed by the data bits, and optionally a parity bit and one
or more stop bits.
▪ The receiving device samples the incoming signal at the center of each data bit period and uses the start bit to synchronize with the transmission.
▪ In the synchronous mode, the AVR microcontroller sends and receives data in a synchronous fashion using a clock signal.
▪ The transmitting device generates the clock signal and uses it to synchronize the data transmission with the receiving device.
▪ The USART module can be configured to support several clock modes, such as the normal mode, double-speed mode, and clock phase and polarity modes.
22
Internal Architecture - Serial I/O
▪ SPI – It is a synchronous serial communication protocol that allows devices to exchange data in a
full-duplex fashion.
▪ The protocol uses four wires to transmit data: MOSI (Master Out Slave In), MISO (Master In Slave Out), SCK (Serial Clock), and SS
(Slave Select). The SS line is used to select the slave device with which the master wants to communicate.
▪ The SPI peripheral logic of the AVR supports all four standard SPI modes of operation.
▪ To use the SPI module in an AVR microcontroller, the programmer needs to configure the SPI control registers to set the SPI mode
(master or slave), clock frequency, clock polarity, and clock phase.
▪ Once the SPI module is configured, the programmer can use the SPI functions provided by the AVR libraries to send and receive data.
▪ TWI /I2C – It is a synchronous serial communication protocol that allows multiple devices to share the same two
wires to exchange data.
▪ The protocol uses two wires: SDA (Serial Data) and SCL (Serial Clock) to transmit and receive data.
▪ I2C supports several configurable parameters, such as clock speed, addressing modes, and data word length.
▪ The AVR microcontrollers have a dedicated I2C hardware module that makes it easy to interface with other devices that support the I2C
protocol. The I2C module includes a shift register, a clock generator, and control registers that configure the I2C module.
▪ In the master mode, the AVR microcontroller generates the clock signal on the SCL line and controls the SDA line to transmit and
receive data from the slave devices. The AVR microcontroller can initiate a communication with a specific slave device by sending its
unique address on the bus.
▪ The I2C module also supports several addressing modes, such as 7-bit and 10-bit addressing modes.
▪ In the slave mode, the AVR microcontroller waits for the master to initiate the communication by sending its address on the bus. Once
the AVR microcontroller detects its address, it can respond to the master by sending or receiving data.
▪ The TWI interface can achieve transfer speeds of up to 400 kHz with multi-master bus arbitration and has the ability to generate a
wakeup condition when the AVR is in sleep mode

23
Internal Architecture - Interrupts
▪ Interrupts are events that occur independently of the main program flow and require immediate attention.
▪ They can come from various sources, such as external signals, internal timers, or hardware peripherals.
▪ Interrupts are handled by the Interrupt Service Routine (ISR), which is a specific function that is executed when an interrupt occurs.
▪ 8-bit AVR microcontrollers have two types of interrupts: external interrupts and internal interrupts.
▪ External interrupts are triggered by signals on the external pins of the microcontroller. The AVR microcontroller has two external interrupt
pins (INT0 and INT1) that can be configured to trigger interrupts on a rising edge, falling edge, or any change.
▪ Internal interrupts are triggered by various internal sources such as timers, UART, and SPI.
▪ To use interrupts in AVR microcontrollers, the following steps are required:
▪ Enable interrupts globally by setting the global interrupt flag in the SREG register.
▪ Configure the specific interrupt source by setting the corresponding registers.
▪ Write the ISR code to handle the interrupt.
▪ Return from the ISR using the RETI instruction.

External Interrupts Internal Interrupts


Port Pins : INT0, INT1 and INT2 EEPROM
Analog Comparator
ADC
UART/SPI/TWI or I2C
24
Timers
Internal Architecture - Watchdog Timer

▪ The Watchdog Timer (WDT) is a hardware feature available in many microcontrollers, including the 8-bit AVR microcontrollers.
▪ The WDT is used to automatically reset the microcontroller if it gets stuck in an infinite loop or experiences a software or hardware
failure. This is particularly useful in safety-critical applications, where a failure of the microcontroller could have serious consequences.
▪ WDT is controlled by a separate timer that runs independently of the main program.
▪ The WDT can be configured to generate an interrupt or reset the microcontroller if the timer is not reset within a specified time period.
The WDT can be configured to use different time periods, ranging from 16 milliseconds to 8 seconds.
▪ To use the WDT in an AVR microcontroller, the following steps are required:
▪ Enable the WDT by setting the appropriate bits in the WDT Control Register (WDTCSR).
▪ Select the WDT timeout period by setting the appropriate bits in the WDTCSR.
▪ Reset the WDT timer periodically in the main program using the watchdog_reset() function.
▪ If the WDT generates an interrupt, execute the ISR to handle the event.

25
The Arduino Family
▪ The Arduino family of boards use processors developed by the Atmel Corporation of San Jose, California.
▪ The common element among all of them is the Arduino runtime AVR-GCC library that is supplied with the Arduino development
environment, and the on-board bootloader firmware that comes preloaded on the microcontroller of every Arduino board.
▪ Each has a specific intended niche to fill.
▪ Most of the Arduino designs utilize the 8-bit AVR series of microcontrollers, with the Due being the primary exception with its ARM
Cortex-M3 32-bit processor.
▪ Although an Arduino board is, as the Arduino team states, just a basic Atmel AVR development board, it is the Arduino software
environment that sets it apart.
▪ In addition to the various board types designed or sanctioned by Arduino.cc, there are many devices that are either hardware compatible or
software compatible.
▪ What makes these devices Arduino compatible is that they incorporate the Arduino bootloader (or something that works like it), and they
can be programmed with the Arduino IDE by selecting the appropriate compatible Arduino board type from the IDE’s drop-down list.

26
Arduino Constrains
▪ Limited Memory – The AVR MCU just doesn’t have a whole lot of memory available for program storage and variables, and many of the
AVR parts don’t have any way to add more. That being said, the ATmega32 and ATmega128 types can use external memory, but then the
I/O functions for those pins are no longer readily available. Arduino boards were not designed to accommodate external memory, since
one of the basic design assumptions was that the AVR chip itself would have the necessary I/O and that the user would be running a
relatively short program
▪ Limited speed - The Arduino CPU clock rate is typically between 8 and 20 MHz.While this may sound slow, you should bear in mind
two key facts: first, the AVR is a very efficient RISC (reduced instruction set computer) design, and second, things in the real world
generally don’t happen very quickly from a microcontroller’s perspective.
▪ Electrical power. Since the Arduino hardware is actually nothing more than a PCB for an AVR IC to sit on, there is no buffering between
the microcontroller and the external world.You can perform a fast “charcoal conversion” of an AVR (in other words, overheat the IC and
destroy it) if some care isn’t taken to make sure that you aren’t sourcing or sinking more current than the device can handle. Voltage is also
something to consider, since some of the AVR types have 3.3V I/O, whereas others are 5V tolerant. Connecting 5V transistor-transistor
logic (TTL) to a 3.3V device usually results in unhappy hardware, and the potential for some smoke.

27
Arduino Features and Capabilities

Board name Processor VCC (V) Clock (MHz) AIN pins DIO pins PWM pins USB
ArduinoBT ATmega328 5 16 6 14 6 None
Duemilanove ATmega168 5 16 6 14 6 Regular
Diecimila ATmega168 5 16 6 14 6 Regular
Esplora ATmega32U4 5 16 - - - Micro
Ethernet ATmega328 5 16 6 14 4 Regular
Fio ATmega328P 3.3 8 8 14 6 Mini
Leonardo ATmega32U4 5 16 12 20 7 Micro
LilyPad ATmega168V 2.7–5.5 8 6 14 6 None
Mega ATmega1280 5 16 16 54 15 Regular
Micro ATmega32U4 5 16 12 20 7 Micro
Nano ATmega168 5 16 8 14 6 Mini-B
Pro (328) ATmega328 5 16 6 14 6 None
Uno ATmega328 5 16 6 14 6 Regular
Yún ATmega32U4 5 16 12 20 7 Host (A)

28
Programming the Arduino and AVR Microcontrollers
▪ Arduino is best known for its hardware, but you also need software to program that hardware.
▪ Both the hardware and the software are called “Arduino.” The combination enables you to create projects that sense and control the physical
world.
▪ The software is free, open source, and cross-platform. The boards (Hardware) are inexpensive to buy, or you can build your own (the
hardware designs are also open source).
▪ Software programs, called sketches, are created on a computer using the Arduino integrated development environment (IDE). The IDE
enables you to write and edit code and convert this code into instructions that Arduino hardware understands. The IDE also transfers those
instructions to the Arduino board (a process called uploading).
▪ The Arduino board is where the code you write is executed. The board can only control and respond to electricity, so specific components
are attached to it to enable it to interact with the real world.
▪ These components can be sensors, which convert some aspect of the physical world to electricity so that the board can sense it, or
actuators, which get electricity from the board and convert it into something that changes the world.
▪ Examples of sensors include switches, accelerometers, and ultrasound distance sensors. Actuators are things like lights and LEDs, speakers,
motors, and displays.

29
Bootloaders
▪ The AVR family of microcontrollers provides reserved space in the on-board flash memory space for a bootloader. Once the MCU has
been configured to use the bootloader, the address of this special memory space is the first place the AVR MCU will look for instructions
when it is powered up (or rebooted). So long as the bootloader is not overwritten with an uploaded program, it will persist in memory
between onoff power cycles.
▪ Reserving the bootloader location typically involves enabling an internal switch, or fuse, with a special programming device that
communicates with the MCU through an ICSP or JTAG interface. The MCU examines the fuse configuration (nonvolatile configuration
bits) at startup to determine how the flash memory is organized and if space has been reserved for some type of bootloader or other
startup code.
▪ A key feature of the AVR devices is their ability to load program code into their internal flash memory via a bootloader and a serial
interface.
▪ Arduino boards—both the official products and the software-compatible boards— come with an Arduino-type bootloader already loaded
into the MCU. The Arduino bootloader implements a specific protocol that allows it to recognize the Arduino IDE and perform a program
data transfer from the development host to the target board.

30
Cross-Compiling with the Arduino IDE
▪ Like for most single-board microcontroller systems, programs for an Arduino and its AVR MCU are developed on another system of some
type (Windows, Linux, or Mac) and then transferred, or uploaded, to the AVR microcontroller on the Arduino board.
▪ The development system is referred to as the development host, and the Arduino or other MCU-based device is called the target.
▪ This technique of creating software for one type of processor on a different type of system is referred to as cross-compiling, and it is really
the only way to create compiled software for microcontroller targets.
▪ In cross-compiling technique, small microcontrollers like the AVR (or any small 8-bit device, for that matter) simply don’t have the
resources to compile and link something like a C or C++ program. So a more capable machine with resources such as a fast CPU, large-
capacity disk drives, and lots of memory is used to do the compiling and linking, and then the finished program is transferred to the target
for execution.
▪ This type of development process has been around for quite a long time, and has been used whenever it was necessary to create software
for a target machine that didn’t have the capability to compile code for itself.
▪ In some cases the host system might even have an emulator available for the target that allows the developer to load and test programs in a
simulated environment. An emulator might not provide a 100% perfect simulation of the target MCU and its actual environment, but it
can still be a useful tool for checking the basic functionality of the software before it is actually uploaded to the real MCU.

31
Libraries
▪ The libraries supplied with the Arduino IDE cover a lot of things, but not everything.
▪ Unless someone has put in the time and effort to create a library for a specific sensor or interface, then you will need to supply the code
yourself. This is particularly often the case when working with custom or uncommon sensors or shields that require special functions
▪ In the Arduino IDE environment libraries usually exist as source code until they are needed in a sketch. So what is actually happening when
a sketch is compiled is that the sketch (and any tab files), any necessary libraries, and the runtime code are all compiled at the same time
and linked into a single binary executable image.
▪ when the Arduino IDE encounters an include statement that refers to a library already registered with the IDE it will generate the
necessary build steps and linker options to incorporate the library automatically. However, in order for this to work the IDE must know
about the library in advance.

32
Arduino Applications
▪ With the preceding constraints in mind, here are just a few possible applications for an Arduino:
▪ Real-world monitoring – This include Automated weather station, Lightning detector, Sun tracking for solar panels, Background
radiation monitor, Automatic wildlife detector, Home or business security system, etc
▪ Small-scale control - Model rockets, Model aircraft, Quadrotor UAVs, Simple CNC for small machine tools, etc
▪ Small-scale automation - Automated greenhouse, Automated aquarium, Laboratory sample shuttle robot, Precision thermal
chamber, Automated electronic test system, etc
▪ Performance art - Dynamic lighting control, Dynamic sound control, Kinematic structures, Audience-responsive artwork

33
General Facts about AVR Microcontrollers
▪ The AVR family of devices was one of the first to incorporate on-board flash memory for program storage, instead of the one-time
programmable ROM (read-only memory), EPROM (erasable programmable read-only memory), or EEPROM (electrically erasable
programmable read-only memory) found on other microcontrollers. This makes reprogramming an AVR microcontroller simply a matter
of loading new program code into the device’s internal flash memory.
▪ Atmel makes many different types of AVR microcontrollers, which allows hardware designers to pick the part that meets their specific
needs and reduce the number of unused pins and wasted space on a printed circuit board. Some, like the tinyAVR parts, come in small
surface-mount packages with as few as six pins. Each has one or more discrete digital I/O ports, which can be programmed to perform
multiple functions

34
Further Reading

Source Authors Section to Read


Arduino: A Technical Reference J. M. Hughes Chapter 2 – The AVR Microcontroller
- A Handbook for Technicians, Chapter 5 – Programming the AVR and Arduino
Engineers, and Makers microcontrollers

https://www.engineersgarage. Akshay Daga AVR Microcontroller : All You Need To Know


com/avr-microcontroller-all-
you-need-to-know-part-1-
46/
https://www.microchip.com/e Microchip
n-us/education/developer-
help/learn-products/mcu-
mpu/8bit-avr

35
THANK YOU

Andries Po tgieter Blvd. Vanderbij lpark, 1900, So uth Africa | T 098 008 8900 | E [email protected]. za www.vut.ac.za
36

You might also like