You are on page 1of 27

1/17/2017

AVR Microcontroller

Introduction

Lecture 1

Introduction to AVR
The AVR architecture was conceived by two
students at the Norwegian Institute of Technology
(NTH)
Alf-Egil Bogen (A) and
Vegard Wollan (V)
RISC (R)

Advanced Virtual RISC (AVR)

@ Dr Javaid, M.H Fidai 2 / 54

1
1/17/2017

Introduction to AVR
The AVR is a Harvard architecture machine with
separate memories and buses for program and data
AVR or Atmel AVR are RISC based family of
microcontroller produced by Atmel Corporations

@ Dr Javaid, M.H Fidai 3 / 54

Device Architecture
Program Execution
Atmel's AVRs have a pipeline design. This means the next
machine instruction is fetched as the current one is
executing. This concept enables instructions to be
executed in every clock cycle
Most instructions take just one or two clock cycles, making
AVRs relatively fast among the eight-bit microcontrollers.
The AVR family of processors were designed with the
efficient execution of compiled C code in mind and has
several built-in pointers for the task.

@ Dr Javaid, M.H Fidai 4 / 54

2
1/17/2017

Device Architecture
Program ROM
ROM is used to store program. AVR microcontroller has
Flash memory for program storage. The program ROM
size can vary from 1K to 256K, depending on the AVR
controller being used.
Data RAM
RAM is space for data storage. Data RAM space has
three components
General Purpose Registers
I/O Memory
Internal SRAM

EEPROM
AVR also have small amount of EEPROM to store
critical data. Unlike RAM the data stored in EEPROM
memory can save stored data when power is cut off.
@ Dr Javaid, M.H Fidai 5 / 54

Features
Current AVRs offer a wide range of features:
Multifunction, Bi-directional General Purpose I/O ports with
configurable, built-in pull-up resistors
Multiple Internal Oscillators

In-System Programmable using ISP, JTAG, or High Voltage


methods
Optional Boot Code Section with Independent Lock Bits for
Protection
On chip debugging (OCD) support through JTAG or
debugWIRE on most devices
Internal Data EEPROM up to 4 kB

Internal SRAM up to 8 K

@ Dr Javaid, M.H Fidai 6 / 54

3
1/17/2017

Features
8-Bit and 16-Bit Timers
Brownout Detection
Watchdog Timer (WDT)
Multiple Power-Saving Sleep Modes
Motor control (PWM Specific) Controller models
10-Bit A/D Converters, with multiplex of up to 16 channels
Ethernet Controller Support
LCD Controller Support
Low-voltage Devices Operating Down to 1.8v
picoPower Devices
Analog Comparators

@ Dr Javaid, M.H Fidai 7 / 54

Features
A variety of serial interfaces, including
Synchronous/Asynchronous Serial Peripherals (UART/USART)
(used with RS-232, RS-485, and more)
Serial Peripheral Interface Bus (SPI)
Universal Serial Interface (USI) for Two/Three-Wire Synchronous
Data Transfer
IC Compatible Two-Wire Interface (TWI)
CAN Controller Support
USB Controller Support

@ Dr Javaid, M.H Fidai 8 / 54

4
1/17/2017

Why AVR
There are various microcontrollers available from different
vendors like PIC, Motorola , Texas Instruments then why we
choose AVR?
Relatively low cost, ICs available from college Road
Dynamic engineer, IC master, Gharyan Electronics.
Efficient RISC architecture, almost 10 times faster than
CISC
Crystal Clock is not divided and is given directly to the
processor.
8051 divides clock by 12
Microchip divides clock by 4
Motorola divides clock by 3
So generally speaking, an AVR running at 16 MHz
crystal (Max speed of some AVRs) is equivalent to a
192 MHz 8051 or a 64 MHz PIC or a 36 MHz 68HC11
in general, but variations do exist

@ Dr Javaid, M.H Fidai 9 / 54

Why AVR
Although the assembly language of AVR is completely
different from 8051, as that of Motorola and Microchip too,
but:
WINAVR (previously called AVRGCC), A High Quality free
C/C++ compiler is available from the open source (Linux)
community, and it runs in all windows

As AVR is manufactured by same company i.e. Atmel so the


most direct benefit is that its datasheets are similar to
AT89C51 and easy to follow for students who have studied
AT89C51 datasheets

Some AVR devices (e.g. AT90S8515) are Pin compatible to


AT89C51 so previously made PCBs (with some minor
modifications) can be used for testing and learning the
AVR architecture and programming

@ Dr Javaid, M.H Fidai 10 / 54

5
1/17/2017

Why AVR
In system programmable via different methods
A simple low cost trivial to make programming circuit is
required to program most AVRs (Feature also available
in most PIC and some 8051 too)
Latest AVRs support on-chip Boot program this
completely eliminates the need for any external
programming hardware and the AVR can be
programmed through its built-in UART or other
communication ports. (Motorola has been offering this
feature for a long time.)

@ Dr Javaid, M.H Fidai 11 / 54

Why AVR
Most AVRs have built-in 10 to 16-bit PWM channels
generated automatically by timers. Number of channels
ranges from 2 to 6
Most AVRs have built-in 10- bit ADC and MUX with
number of channels ranging from 4 to 8
Large variety of Flash Memory ranging from 1KB to 128
KB
Large variety of SRAM ranging up to 4 KB
Built-in EEPROM support up to 4 KB
I2C and SPI synchronous serial communication for short
distance networking of microcontrollers and other
peripherals DACs, ADCs, Flash memory, MP3 decoder
ICs etc.

@ Dr Javaid, M.H Fidai 12 / 54

6
1/17/2017

Different AVRs

Size

Features

@ Dr Javaid, M.H Fidai 13 / 54

Different AVRs
AVRs are generally classified in four broad groups

tinyAVRs
1-8 kB program memory
8-20-pin package
Limited peripheral set
megaAVRs
4-256 kB program memory
28-100-pin package
Extended instruction set and peripheral set

@ Dr Javaid, M.H Fidai 14 / 54

7
1/17/2017

Different AVRs

XMEGA AVRs
16-384 kB program memory
44-100 pin package

32-bit AVR UC3

Application Specific AVRs

@ Dr Javaid, M.H Fidai 15 / 54

Software Required
Software we are going to use are
1. AVR Studio

2. Win AVR (Packaged with AVR Studio 5 and Above)

3. Extreme Burner-AVR or Pony Prog

For Simulation and Lab Experiments


Proteus 7.1 or above

@ Dr Javaid, M.H Fidai 16 / 54

8
1/17/2017

Software Required
AVR Studio
AVR Studio is the Integrated Development Environment (IDE)
for developing 8-bit AVR applications in Windows
Many other software are also available for the purpose like
MikroC you can use any of them
The greatest advantage of AVR Studio is that it is provided by
Atmel as a freeware
AVR Studio provides a complete set of features including
debugger supporting run control including source and
instruction-level stepping and breakpoints; registers, memory
and I/O views; and target configuration and management as
well as full programming support for standalone
Debugging is mostly not available in other software but they do
have some exciting features which AVR Studio lacks.

@ Dr Javaid, M.H Fidai 17 / 54

Software Required
WinAVR
It is compiler for C/C++
It integrates with AVR Studio
Open Source software can be downloaded from web (It is
packaged with AVR Studio 5 and above so no need for separate
installation)

Pony Prog
Used for burning firmware in the controller
Freeware easily available
Cannot be used with usb programmer

eXtreme burner-AVR
For USB Programmer

@ Dr Javaid, M.H Fidai 18 / 54

9
1/17/2017

Getting Started

Download and Install AVR studio and then WinAVR

1. How to make a new project in AVR Studio

2. Some basic settings of AVR Studio

3. Debugging In AVR Studio

@ Dr Javaid, M.H Fidai 19 / 54

AVR Studio

@ Dr Javaid, M.H Fidai 20 / 54

10
1/17/2017

PonyProg

@ Dr Javaid, M.H Fidai 21 / 54

eXtreme Burner-AVR

@ Dr Javaid, M.H Fidai 22 / 54

11
1/17/2017

Programmers for AVR


Different Programmer Designs are available for in circuit
programming of AVR microcontrollers which can be
connected to a PC via

1.Parallel Port

2.Serial Port

3.USB Port

@ Dr Javaid, M.H Fidai 23 / 54

Parallel Port Programmer

@ Dr Javaid, M.H Fidai 24 / 54

12
1/17/2017

Serial Port Programmer

@ Dr Javaid, M.H Fidai 25 / 54

Serial Port Programmer

The AVR-PG1B is a serial port dongle compatible


with PonyProg programming software. It does not require
any external power supply; it takes power from your target
board.
The dongle attaches to your PC via a standard DB9 serial
port, and connects via ribbon cable to Atmel's
standard 2x5-pin AVR ICSP connector.
Cable length can be up to 10+ft

@ Dr Javaid, M.H Fidai 26 / 54

13
1/17/2017

USB Programmer

Link http://www.fischl.de/usbasp/

@ Dr Javaid, M.H Fidai 27 / 54

USB Programmer
Firmware and Layout of the above programmer
are available on the given link
Required Drivers for the USB Programmers
are also available at the link
Number of programming software are available
for download on the above link which work with
this USB programmer (eXtreme burner-AVR is
one of them)

@ Dr Javaid, M.H Fidai 28 / 54

14
1/17/2017

AVR for this course


All Atmega controllers are compatible with each
other and code of one works on the other
Controller we are going to use for this course is
Atmega16
Reasons
Readily available on college road and is cheaper
It is available in Proteus version 7.1
EasyAVR5A development kits in the department have
this controller

@ Dr Javaid, M.H Fidai 29 / 54

Atmega16

@ Dr Javaid, M.H Fidai 30 / 54

15
1/17/2017

ATmega16 Features
High-performance, Low-power AVR 8-bit Microcontroller
Advanced RISC Architecture
131 Powerful Instructions Most Single-clock Cycle
Execution
32 x 8 General Purpose Working Registers
Up to 16 MIPS Throughput at 16 MHz

@ Dr Javaid, M.H Fidai 31 / 54

ATmega16 Features
Nonvolatile Program and Data Memories
16K Bytes of In-System Self-Programmable Flash
program memory (ROM)
Endurance: 10,000 Write/Erase Cycles
512 Bytes EEPROM (Data Memory)
Endurance: 100,000 Write/Erase Cycles
1K Byte Internal SRAM (Data Memory )
Programming Lock for Software Security

@ Dr Javaid, M.H Fidai 32 / 54

16
1/17/2017

ATmega16 Features
Peripheral Features
Two 8-bit Timer/Counters with Separate Prescalers and Compare
Modes
One 16-bit Timer/Counter with Separate Prescaler, Compare Mode,
and Capture Mode
Real Time Counter with Separate Oscillator
Four PWM Channels
8-channel, 10-bit ADC
8 Single-ended Channels
7 Differential Channels in TQFP Package Only
2 Differential Channels with Programmable Gain at 1x, 10x, or 200x
I2C Two-wire Serial Interface (TWI)
Programmable Serial USART
Master/Slave SPI Serial Interface
Programmable Watchdog Timer with Separate On-chip Oscillator
On-chip Analog Comparator

@ Dr Javaid, M.H Fidai 33 / 54

ATmega16 Features
Special Microcontroller Features
Power-on Reset and Programmable Brown-out Detection
Internal Calibrated RC Oscillator
External and Internal Interrupt Sources
Six Sleep Modes: Idle, ADC Noise Reduction, Power-save, Power-
down, Standby
and Extended Standby
JTAG (IEEE std. 1149.1 Compliant) Interface
Boundary-scan Capabilities According to the JTAG Standard
Extensive On-chip Debug Support
Programming of Flash, EEPROM, Fuses, and Lock Bits through the
JTAG Interface

@ Dr Javaid, M.H Fidai 34 / 54

17
1/17/2017

ATmega16 Features
I/O and Packages
32 Programmable I/O Lines
40-pin PDIP, 44-lead TQFP, and 44-pad MLF
Operating Voltages
2.7 - 5.5V for ATmega16L
4.5 - 5.5V for ATmega16
Speed Grades
0 - 8 MHz for ATmega16L
0 - 16 MHz for ATmega16

@ Dr Javaid, M.H Fidai 35 / 54

Atmega16 Instruction set


Atmega16 has an extensive dataset
consisting of 131 instructions.
Instructionset.pdf

@ Dr Javaid, M.H Fidai 36 / 54

18
1/17/2017

Fuse Bits
There are few parameters in the chip which are used to configure it
before it can be used for external environment. These parameters
are set with the use of FUSE BITS

New Microcontrollers (such as AVR) are coming with lots of inbuilt


peripherals, various additional features and have the capability of
working on internal and external clock options

In order to select desired clock option and enable/disable particular


peripheral microcontrollers have FUSE BITS

The fuse bit determines the behavior of the chip, whether it has a
boot loader, what is the speed and voltage it runs at, the Watchdog
Timer conditions, etc.

@ Dr Javaid, M.H Fidai 37 / 54

Fuse Bits
Once the fuse bits are set for a particular configuration, the
controller can be used again and again (program can be burned
again and again)

There is no need to set the fuse bits every time using the controller
till the time you want to use it under the same configuration

Fuse bits need to be changed only in case you want to change the
initial configuration of the controller.

@ Dr Javaid, M.H Fidai 38 / 54

19
1/17/2017

Fuse Bits
Atmega16 has two fuse bytes
Fuse high byte
Fuse low byte
These two bytes have the bits which
Enable/Disable on-circuit Debugging
Enable/Disable JTAG
Enable/Disable SPI Interface
Enable/Disable Brown-out detection
Selects the clock source (speed of the controller)

@ Dr Javaid, M.H Fidai 39 / 54

Fuse Bits
The fuses are read as logical zero, 0, if they
are programmed
If the fuse bit is set 0 it means it is enabled
If the fuse bit is set 1 it means it is disabled

@ Dr Javaid, M.H Fidai 40 / 54

20
1/17/2017

Fuse Bits
Fuse High Byte

@ Dr Javaid, M.H Fidai 41 / 54

Fuse Bits
Fuse Low Byte

@ Dr Javaid, M.H Fidai 42 / 54

21
1/17/2017

Fuse Bits
It is really important to set the fuse bits correctly clearing
or writing the fuse bits accidentally may cause weird
controller behavior.

As from the table given on the previous slides few bits


are programmed by default.

For normal operations (Speaking about NERC or to start


with) we are only concerned with the bits which are
highlighted in the above tables.
Clock Source Select Bits(Controller Speed)
JTAG (Only because this bit is enabled by default which engages
the pins of Port C so in order to use Port C for I/O purposes this
bit needs to be unprogrammed)

@ Dr Javaid, M.H Fidai 43 / 54

Atmega16 Clock Sources


Atmega16 has following clock source options
External Crystal/Ceramic Resonator
External Low-frequency Crystal
External RC Oscillator
Calibrated Internal RC Oscillator
External Clock
Clock source is selected by the Fuse Bits as
shown in the table on next slide

@ Dr Javaid, M.H Fidai 44 / 54

22
1/17/2017

Fuse Bits for Clock Select

Calibrated Internal RC Oscillator

@ Dr Javaid, M.H Fidai 45 / 54

Fuse Bits for Clock Select


SOURCE Frequency CKOPT CKSEL3 CKSEl2 CKSEL1 CKSEL0
(MHz)

Ext. Clock - - 0 0 0 0

Int. Osc 1 1 0 0 0 1

Int. Osc 2 1 0 0 1 0

Int. Osc 4 1 0 0 1 1

Int. Osc 8 1 0 1 0 0

Ext. RC Osc <=0.9 - 0 1 0 1

Ext. RC Osc 0.9-3.0 - 0 1 1 0

Ext. RC Osc 3.0-8.0 - 0 1 1 1

Ext. RC Osc 8.0-12.0 - 1 0 0 0

Low Freq. Crystal 32.768 KHz - 1 0 0 1


(Watch Crystal=32.378 kHz)

SUT 01 determines the startup time and does not effect the operating frequency setting.

46 / 54

23
1/17/2017

Fuse Bits for Clock Select


SOURCE Frequency CKOPT CKSEL3 CKSEl2 CKSEL1 CKSEL0
(MHz)
Ext. Resonator 0.4-.9 0 1 0 1 -

Ext. Resonator 0.9-3.0 0 1 1 0 -

Ext. Resonator 3.0-8.0 0 1 1 1 -

Ext. Crystal 0.4-0.9 0 1 0 1 -

Ext. Crystal 0.9-3.0 0 1 1 0 -

Ext. Crystal 3.0-8.0 0 1 1 1 -

Ext. 1<=max 1 101,110,111 -


Crystal/Resonator
CKSEL0 and SUT01 determines the startup time and CKSEL0 does not effect the operating
frequency setting.
With CKOPT programmed 101,110,111 any of these combinations can be used.

47 / 54

Setting the Fuse Bits


What value should be written to the Fuse Bytes
of Atmega16(assuming it is currently at default
factory settings) such that we can use all of its
Ports for I/O purposes where the controller runs
at 4MHz on internal RC Oscillator

@ Dr Javaid, M.H Fidai 48 / 54

24
1/17/2017

Setting the Fuse Bits


Fuse High Byte

Here we have to disable JTAG so its bit should be unprogrammed i.e


set to 1. Rest of the bits should be kept unchanged so the number to
be loaded to the Fuse high byte is 11011001 or D9.
@ Dr Javaid, M.H Fidai 49 / 54

Setting the Fuse Bits


Calibrated Internal RC Oscillator

Since the controller frequency should be 4 MHz so the value of the Fuse
Low Byte should be 11100011 or E3
@ Dr Javaid, M.H Fidai 50 / 54

25
1/17/2017

Setting the Fuse Bits


So Fuse High Byte = D9
Fuse Low Byte = E3
Now comes the question of how to set these fuse bytes
practically.
In all the programming software there is a separate
option provided for setting the fuse bits/fuse bytes

IMPORTANT: Fuse bytes have nothing to do with the Hex


File (Program Code) loaded in the controller

@ Dr Javaid, M.H Fidai 51 / 54

Setting the Fuse Bits (eXtreme Burner-AVR)

@ Dr Javaid, M.H Fidai 52 / 54

26
1/17/2017

Setting the Fuse Bits (Pony Prog)

@ Dr Javaid, M.H Fidai 53 / 54

AVR Fuse bits


Tutorial AVR Fuses
http://playwithrobots.com/robotics-pool/avr/fuse

http://www.ladyada.net/learn/avr/fuses.html

http://embedderslife.wordpress.com/2012/08/20/fu
se-bits-arent-that-scary/
Calculator
http://www.engbedded.com/fusecalc

@ Dr Javaid, M.H Fidai 54 / 54

27

You might also like