Lecture 6 – Introduction to the
AVR Microcontrollers
2k18-Telecom
What is a Microcontroller
Integrated chip that typically contains integrated CPU, memory (RAM
ROM), I/O ports on a single Chip.
System on a single Chip/ small computer on a single chip
Designed to execute a specific task to control a single system
Smaller & Specified (design cost)
Differs from
Microprocessor
general-purpose chip
Used to design multi purpose computers or
devices Require Multiple chips to handle various
tasks
Typically Microcontroller embedded inside some device
Microcontrollers are important part of Embedded systems 3
What is a Development Board
• A printed circuit board
designed to facilitate
work with a particular
microcontroller.
• Typical components include:
• power circuit
• programming interface
• basic input; usually buttons and LEDs
• I/O pins
3
The Arduino Development Board
Arduino (The name is an Italian , meaning “strong friend”) is an
open- source platform used for building electronics projects. Arduino consists
of both a physical programmable circuit board (often referred to as a
microcontroller) and a piece of software, or IDE (Integrated Development
Environment) that runs on your computer, used to write and upload computer
code to the physical board.
4
Arduino Microcontroller Boards
Microcontroller ATmega328 5 V
Operating Voltage
Input Voltage 7-12 V
(recommended) 6-20 V
Input Voltage (limits)
Digital I/O Pins 14 (of which 6 provide PWM output) 6
Analog Input Pins 40 mA
DC Current per I/O Pin 50 mA
DC Current for 3.3V Pin 32 KB (ATmega328) of which 2 KB used by
bootloader
Flash Memory
2 KB (ATmega328)
SRAM 1 KB (ATmega328)
EEPROM 16 MHz
Clock Speed
AVR Microcontroller
AVR stand for?
Advanced Virtual RISC,
the founders are Alf Egil Bogen, Vegard Wollan
RISC
AVR architecture was conceived by two
students at Norwegian Institute of Technology
(NTH) and further refined and developed at Atmel
Norway (Atmel AVR).
1
3
AVR Microcontroller
AVR Micro controllers is Family of RISC
Microcontrollers from Atmel.
There are multiple architectures
RISC (Reduced Instruction Set Computer)
CISC (Complex Instruction Set Computer)
1
4
RISC Microcontroller
Reduced Instruction Set Computers Advantages
Fast Execution of Instructions due to simple
instructions for CPU.
RISC chips require fewer transistors, which makes
them cheaper to design and produce.
Emphasis on software
Single-clock , reduced instruction only
Register to register: “LOAD" and "STORE“ are
independent instructions
Spends more transistors on memory registers
15
Atmega328 Overview
Internal
memories
32KB Flash
1KB EEPROM
2KB SRAM
8-bit CPU
Timer/Counter
Serial Peripheral
Universal Synchronous Interface
and Asynchronous
serial Receiver and
Transmitter (Serial)
GPIO 2-wire Serial
Interface
12
AVR
Architecture
AVR
Architecture
Clocks and Power
Beyond scope of this
course
AVR
Architecture
CPU
Details coming
AVR
Architecture
Harvard architecture
Flash – programmemory
32K
SRAM – data memory
2K
EEPROM
For long-term data
On I/O data bus
AVR Microcontroller
The AVR is a Harvard architecture CPU.
Harvard Architecture
Computer architectures that used physically separate storage
and signal pathways for their instructions and data.
CPU can read both an instruction and data from memory
at the same time that makes it faster.
von Neumann architecture
CPU can Read an instruction or data from/to the memory.
Read, Write can`t occur at the same time due to same
memory and signal pathway for data and instructions.
17
Memory
Flash (32K) (15-bit addresses)
Program memory – readonly
Non-volatile
Allocate data to Flash using PROGMEM keyword
see documentation
SRAM (2K)
Temporary values, stack,etc.
Volatile
Limited space!
EEPROM (1K)
Long-term data
see documentation on EEPROM library
AVR CPU
Instruction Fetch
and Decode
AVR CPU
ALU Instructions
ALU has three types of operations
• Arithmetic (+,-)
• Logic (and, or)
• Bit Function (to check if bit is
high or low)
AVR CPU
I/O and special
functions
AVR Register File
32 8-bit GP registers
Part of SRAM memory space
Special Addressing Registers
X,Y and Z registers
16-bit registers made using registers 26 – 31
Support indirect addressing
AVR Memory
Program memory – Flash
Data memory - SRAM
Addressing Modes
Direct register
addressing
Addressing Modes
Direct I/O addressing
Direct data memory addressing
Addressing
Modes
Direct data memory with displacement addressing
Addressing
Modes
Indirect data memory addressing
Addressing
Modes
Indirect data memory addressing with pre-decrement
Addressing
Modes
Indirect data memory addressing with post-increment
Addressing
Modes
Program memory addressing (constant data)
Addressing
Modes
SRAM Read/Write Timing
Stack Pointer Register
Special register in I/O space [3E, 3D]
Enough bits to address data space
Initialized to RAMEND (address of highest memory address)
Instructions that use the stack pointer
Program Status Register
(PSR)
\
Status bits set by instructions/Checked by Branch/Skip
instructions
I – Global interruptenable
T – Flag bit
H – Half carry (BCD arithmetic)
S – Sign
V – Overflow
N – Negative
Z – Zero
C – Carry
Simple 2-Stage Pipeline
Branch/Skip??
Single-Cycle ALU Instructions
Most instructions execute in one cycle
Makes program timing calculations (relatively) easy
No cachemisses
1 clock/instruction
JMP, CALL – Direct Program Memory Addressing
Addressing
Modes
Addressing Modes
IJMP, ICALL – Indirect program memory addressing
RJMP, RCALL – Relative program memory addressing
Addressing
Modes
Arithmetic
Instructions
Logical Instructions
Jump and Call Instructions
Skip and Branch Instructions
Skip and Branch (cont)
Move,Load
Store
Load/Store Program
Memory
Move, I/O, Push/Pop
Shift and Bit Instructions
Bit Instructions (cont)
AVR
Architecture
Three timers
Very flexible
Choose clock rate
Choose “roll-over”value
Generate interrupts
Generate PWM signals
(represent 8-bit value with
using a clock signal)
Arduino Timing
Functions
delay(ms)
wait for ms milliseconds before continuing
delayMicroseconds(us)
wait for us microseconds before continuing
unsigned long millis( )
return number of milliseconds since program started
unsigned long micros( )
return number of microseconds since program started
resolution of 4 microseconds
AVR
Architecture
Interface to pins
Each pin directly
programmable
Program direction
Program value
Program pull-ups
Some pins are special
Analog vs. Digital
Clocks
Reset
I/O Ports
3 8-bit Ports (B, C, D)
Each port controlled by 3 8-bit registers
Each bit controls one I/O pin
DDRx – Direction register
Defines whether a pin is an input (0) or and output (1)
PINx – Pin inputvalue
Reading this “register” returns value of pin
PORTx – Pin outputvalue
Writing this register sets value of pin
Pin Circuitry
Pin Input
DDRx = 0
off
PO RT x
PINx
Note:Takes a clock cycle for data output to be reflected
on the input
Synchronization
Timing
Pin
Output
DDRx = 1
on
PO RT x
PINx
Pin Input – PORT controls
pullup
DDRx = 0
off
PO RT x
PINx
I/O
Ports
Pullups
If a pin is an input (DDRxi = 0):
PORTxi = 0 – pin is floating
PORTxi = 1 – connects a pullup to thepin
Keeps pin from floating if noone driving
Allows wired-OR bus
Individual bits can be set cleared using bit-ops
A bit can be toggled by writing 1 to PINxi
SBI instruction e.g.
I/O
Protection
Arduino Digital and Analog
I/OPins
Digital pins:
Pins 0 – 7: PORT D [0:7]
Pins 8 – 13: PORT B [0:5]
Pins 14 – 19: PORT C [0:5] (Arduino analog pins 0 – 5)
digital pins 0 and 1 are RX and TX for serialcommunication
digital pin 13 connected to the base board LED
Digital Pin I/O Functions
pinMode(pin, mode)
Sets pin to INPUT or OUTPUT mode
Writes 1 bit in the DDRx register
digitalWrite(pin, value)
Sets pin value to L O W or HIGH (0 or 1)
Writes 1 bit in the PORTx register
int value = digitalRead(pin)
Reads back pin value (0 or 1)
Read 1 bit in the PINx register
Arduino Analog I/O
Analog input pins: 0 – 5
Analog output pins: 3,5,6,9,10, 11 (digital pins)
Analog input functions
int val = analogRead(pin)
Converts 0 – 5v. voltage to a 10-bit number (0 – 1023)
Don’t use pinMode
analogReference(type)
Used to change how voltage is converted (advanced)
Analog output
analogWrite(pin, value)
value is 0 – 255
Generates a PWM output on digital pin (3, 5, 6, 9, 10,11)
@490Hz frequency
AVR
Architecture
Analog inputs
Convert voltage to a
10-bit digital value
Can provide reference
voltages
PWM – Pulse Width Modulation
Use one wire to represent a multi-bit value
A clock with a variable dutycycle
Duty cycle used to represent value
We can turn it into a analog voltage using an integrating filter
Port Special Functions
Lots of special uses for pins
Clock connections
Timer connections
e.g. comparator output for PWM
Interrupts
Analog references
Serial bus I/Os
USART
PCI
Reading and Writing Pins Directly
Only one pin can be changed using the Arduino I/O
functions
Setting multiple pins takes time and instructions
To change multiple pins simultaneously, directly read/write
the pin registers
DDR{A/B/C}
PORT{A/B/C}
PIN{A/B/C}
e.g. to set all digital pins 0 – 7 to a value:
PORTD = B01100101;
AVR
Architecture
Special I/O support
Serial protocols
Uses special pins
Uses timers
Beyond scope of this
course
Arduino C Programs
Arduino calls these “sketches”
Basically C with libraries
Program structure
Header: declarations,includes, etc.
setup()
loop()
Setup is like Verilog initial
executes once when program starts
loop() is like Verilog always
continuously re-executed when the end is reached
Blink Program
int ledPin = 13; // LED connected to digital pin 13
// The setup() method runs once, when the sketch starts
void setup() {
// initialize the digital pin as an output:
pinMode(ledPin, OUTPUT);
}
// the loop() method runs over and over again,
// as long as the Arduino has power
void loop()
{
digitalWrite(ledPin, HIGH); // set the LED on
delay(1000); // wait for a second
digitalWrite(ledPin, LOW); // set the LED off
delay(1000); // wait for a second
}
The Arduino C++ Main Program
int main(void)
{
init();
setup();
for (;;)
loop();
return 0;
}
Arduino Serial I/O
Communication with PC via USB serial line
Use the Serial Monitor in the IDE
Or set up a C or Java (or you-name-it) interface
Example Serial library calls
Serial.begin(baud-rate)
9600 default
Serial.println(string)
int foo = Serial.read()
Read one byte (input data is buffered)
See documentation for more
Example Program