0% found this document useful (0 votes)
27 views76 pages

Chapter Two

Uploaded by

Yohannes Dereje
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views76 pages

Chapter Two

Uploaded by

Yohannes Dereje
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 76

www. Micro Digital Ed.

com
BIHE university

Chapter Two : MCU Hardware


Overview: Ports, Registers, GPIO, Analog I/O,
ADC/DAC

SE4033: Real time and Embedded Systems

B.Sc(SE), 2023
By Mikru L
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Lesson-2 Outline
www. Micro Digital Ed. com
BIHE university

• Most common microcontrollers


• Criteria for choosing a Microcontroller
• Some facts about AVR µc’s
• AVR internal architecture
• Atmel Atmega32 highlights
• AVR different groups
• Registers
• Ports
• GPIO
• Analog I/O
• ADC

AVR Microcontroller and Embedded


SEng4033: Real System
time andUsing © 2011
Embedded System By Mikru L. JiT-JU Pearson Higher Education,
2
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Microcontroller
www. Micro Digital Ed. com
BIHE university

 A microcontroller is a tiny data processing devices which


is primarily designed to perform specific functions within
a large system.
 Which usually includes the following on a single chip
Components :
 microprocessor (CPU).
 A small amount of RAM.
 Programmable ROM and/or flash memory.
 Parallel and/or serial I/O.
 Timers and signal generators.
 Analog to Digital (A/D) and/or Digital to Analog (D/A)
conversion.
 Often used to run dedicated code that controls one or
more tasksandinEmbedded
AVR Microcontroller
Assembly and C
the System
operation ofandaEmbedded
Using Real time
SEng4033: device Systemor
By a
© 2011
system.
Pearson
Mikru Higher Education,
L. JiT-JU 3
Upper Saddle River, NJ 07458. • All Rights
Microcontroller
www. Micro Digital Ed. com
BIHE university

• Devices that utilize microcontrollers include


• Microcontrollers are widely being used in a variety
of electronic devices such as
• Medical instruments, manufacturing machinery,
robotics, automobiles, home appliances, Mobile
Phones, computer Systems etc. to automate their
operation.
• Microcontrollers usually must have low-power
requirements (~. 05 - 1 W as opposed to ~10 - 50 W
for general purpose desktop CPUs) since many
devices they control are battery-operated.
SEng4033: Real time and Embedded
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
4
Assembly and C System By Mikru L. JiT-JU Upper Saddle River, NJ 07458. • All Rights
Most common microcontrollers
8-bit microcontrollers
www. Micro Digital Ed. com
BIHE university

• Atmel AVR
• Microchip’s PIC
• Freescale Semiconductor’s HCS08
• Intel’s 8051
• Zilog’s Z8
32-bit microcontrollers
• AVR32
• ARM
• PIC32

AVR Microcontroller and Embedded


SEng4033: Real System
time andUsing © 2011
Embedded System By Mikru L. JiT-JU Pearson Higher Education,
5
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Most common microcontrollers
www. Micro Digital Ed. com
BIHE university

AVR Microcontroller and Embedded


SEng4033: Real System
time andUsing © 2011
Embedded System By Mikru L. JiT-JU Pearson Higher Education,
6
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Criteria for choosing a Microcontroller
www. Micro Digital Ed. com
BIHE university

1. Meet the task at hand efficiently and cost


effectively.
a) Speed- highest speed µc supports
b) Packaging- DIP, QFP, etc., space required while
assembling
c) Power consumption- critical for battery-powered
products
d) Amount of RAM & ROM on the chip
e) I/O –number of I/O pins and the time on the chip
f) Ease of upgrade to higher performance, lower
power consumption versions
g) Cost per unit.

AVR Microcontroller and Embedded


SEng4033: Real System
time andUsing © 2011
Embedded System By Mikru L. JiT-JU Pearson Higher Education,
7
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Criteria for choosing a Microcontroller
www. Micro Digital Ed. com
BIHE university

2. Availability of development products, such


as, Assembler, debugger, code-efficient C
language compiler, emulator, technical
support, both in-house and outside
expertise, third-party vendor support

3. Ready availability of µc in quantities both


present and future.

AVR Microcontroller and Embedded


SEng4033: Real System
time andUsing © 2011
Embedded System By Mikru L. JiT-JU Pearson Higher Education,
8
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Some facts about AVR µc’s
 What is AVR?
www. Micro Digital Ed. com
BIHE university

 Alf-Egil Bogen and Vegard Wollan (Norwegian


Institute of Technology (NTH))
 AVR ≡ Advanced Virtual RISC
 Except for AVR32, all other AVR’s are 8-bit µc’s.
 In terms of software Atmel µc’s are not 100%
compatible.
 AVR is an 8-bit RISC single-chip µc with Harvard
architecture.
 Atmel have all dedicated massive resources to
ensure wide and timely availability of their
products.
 They are stable, mature and single sourced.
AVR Microcontroller and Embedded
SEng4033: Real System
time andUsing © 2011
Embedded System By Mikru L. JiT-JU Pearson Higher Education,
9
Assembly and C Upper Saddle River, NJ 07458. • All Rights
AVR internal architecture
www. Micro Digital Ed. com
BIHE university

40 PIN DIP
(XCK/T0) PB0 1 40 PA0 (ADC0)
(T1) PB1 2 39 PA1 (ADC1)
(INT2/AIN0) PB2 3 38 PA2 (ADC2)
RAM EEPROM Timers
(OC0/AIN1) PB3 4 MEGA32 37 PA3 (ADC3)
PROGRAM (SS) PB4 5 36 PA4 (ADC4)
ROM (MOSI) PB5 6 35 PA5 (ADC5)
(MISO) PB6 7 34 PA6 (ADC6)
Program (SCK) PB7 8 33 PA7 (ADC7)
Bus Bus RESET 9 32 AREF
CPU
VCC 10 31 AGND
GND 11 30 AVCC
XTAL2 12 29 PC7 (TOSC2)
XTAL1 13 28 PC6 (TOSC1)
(RXD) PD0 14 27 PC5 (TDI)
Interrupt Other (TXD) PD1 15 26 PC4 (TDO)
OSC Ports
Unit Peripherals (INT0) PD2 16 25 PC3 (TMS)
(INT1) PD3 17 24 PC2 (TCK)
(OC1B) PD4 18 23 PC1 (SDA)
I/O (OC1A) PD5 19 22 PC0 (SCL)
PINS
(ICP) PD6 20 21 PD7 (OC2)

AVR Microcontroller and Embedded


SEng4033: Real System
time andUsing © 2011
Embedded System By Mikru L. JiT-JU Pearson Higher Education,
10
Assembly and C Upper Saddle River, NJ 07458. • All Rights
AVR different groups
www. Micro Digital Ed. com
BIHE university

• Classic AVR
– e.g. AT90S2313, AT90S4433
• Mega
– e.g. ATmega8, ATmega32, ATmega128
• Tiny
– e.g. ATtiny13, ATtiny25
• Special Purpose AVR
– e.g. AT90PWM216,AT90USB1287

AVR Microcontroller and Embedded


SEng4033: Real System
time andUsing © 2011
Embedded System By Mikru L. JiT-JU Pearson Higher Education,
11
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Atmel Atmega32 Port
An 8-bit microcontroller featuring:
www. Micro Digital Ed. com
BIHE university

• 3 separate on-chip memories (Harvard architecture)


– 2KB SRAM (for data – volatile; data lost on power off)
– 1KB EEPROM (for persistent data storage – holds data after power off)
– 32KB Flash (organized as 16K of 16-bit words for persistent program code)

• Native data size is 1 byte (SRAM and EEPROM)

• 16-bit data addressing


– Up to 64 KB (216 bytes) of data memory can be accessed

• 8-pin I/O ports named A, B, C, and D, program-configurable as:


– Digital input (for reading discrete external signals on each pin (0v or 5v) as
data values 0 or 1)
– Digital output (for writing binary data values as discrete output signals (0v or
5v)
– Analog input (for reading continuous external signals (0v-5v) as data values)
– Serial/Parallel (for reading or writing streams of bytes)
– Pulse accumulator (for counting #changes of external signals)

AVR Microcontroller and Embedded


SEng4033: Real System
time andUsing © 2011
Embedded System By Mikru L. JiT-JU Pearson Higher Education,
12
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Atmel Atmega32 Port
www. Micro Digital Ed. com  Central Processing Unit BIHE university

 Arithmetic Logic Unit (ALU) performs


the actual arithmetic, logical, and bit-
functions
 Memory – SRAM, EEPROM, Flash
 Clock circuit – internal/external
 I/O – Input/Output; video, serial,
parallel, USB, SCSI, etc.

AVR Microcontroller and Embedded


SEng4033: Real System
time andUsing © 2011
Embedded System By Mikru L. JiT-JU Pearson Higher Education,
13
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Atmel Atmega32 IO Pin
www. Micro Digital Ed. com

1. Vital Pins:
BIHE university

1. Power
• VCC
• Ground
2. Crystal
• XTAL1
• XTAL2
3. Reset
2. I/O pins
• PORTA, PORTB,
PORTC, and PORTD
3. Internal ADC pins
• AREF, AGND, AVCC

AVR Microcontroller and Embedded


SEng4033: Real System
time andUsing © 2011
Embedded System By Mikru L. JiT-JU Pearson Higher Education,
14
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Registers
www. Micro Digital Ed. com
BIHE university

• AVR’s CPU
– ALU
– 32 General Purpose
registers (R0 to R31)
– PC register
– Instruction decoder
– SREG

AVR Microcontroller and Embedded


SEng4033: Real System
time andUsing © 2011
Embedded System By Mikru L. JiT-JU Pearson Higher Education,
15
Assembly and C Upper Saddle River, NJ 07458. • All Rights
GPIO
 AVR ATmega32 has 32 pins constituting four ports. The ports
www. Micro Digital Ed. com
BIHE university

are listed below :


 1. PORT A
 2. PORT B
 3. PORT C
 4. PORT D.
 Each port has 8 pins.
 The pins of these four ports can be used as general-purpose
inputs/outputs.
 These pins can be configured as input or output using the three
I/O registers for each port. These registers are listed below :
DDRx: Data Direction Registers
 These are 8-bit registers.
 These are used to configure the pins of the ports as input or output.
 Writing a one to the bits in this register sets those specific pins as
output
AVR pins. and Embedded System Using
Microcontroller © 2011 Pearson Higher Education,
16
Upper Saddle River, NJ 07458. • All Rights
Assembly and C SEng4033: Real time and Embedded System By Mikru L. JiT-JU
GPIO
• Writing a zero to the bits in this register sets those specific pins
www. Micro Digital Ed. com
BIHE university

as input pins.
• All bits in these registers can be read as well as written to.
• The initial value of these bits is zero.
PORTx: Data Registers
• These are 8-bit registers.
• These are used to put the pins of the ports in
a logic HIGH or logic LOW state.
• Writing a one to the bits in this register
puts a HIGH logic (5V) on those pins.
• Whereas writing a zero to the bits in this
register puts a LOW logic (0V) on those pins.
• All bits in these registers can be read as
well as written to.
SEng4033: Real time and Embedded
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
17
• The initial value of these bits is zero.
Assembly and C System By Mikru L. JiT-JU Upper Saddle River, NJ 07458. • All Rights
GPIO
www. Micro Digital Ed. com
BIHE university

PINx: Input Pins Address Registers


•These are 8-bit registers.
•These are used to read the values on the specific
pins of the port.
•These bits are read-only bits and cannot be written
to.

SEng4033: Real time and Embedded


AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
18
Assembly and C System By Mikru L. JiT-JU Upper Saddle River, NJ 07458. • All Rights
Example 1
www. Micro Digital Ed. com
BIHE university

SEng4033: Real time and Embedded


AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
19
Assembly and C System By Mikru L. JiT-JU Upper Saddle River, NJ 07458. • All Rights
ADC
www. Micro Digital Ed. com
BIHE university

• Analog-to-digital converters(ADC) are among the


most widely used devices for data acquisition
• Digital computer use binary (discrete), but
physical world everything is analog (continuous).
• Thus a device called transducer(sensor) is needed to
convert physical quantities (i.e. temperature,
pressure, etc) to electrical (voltage/current) signal.
• ADC is used to translate the analog signals to digital
signal.
AVR Microcontroller and Embedded
SEng4033: Real System
time andUsing © 2011
Embedded System By Mikru L. JiT-JU Pearson Higher Education,
20
Assembly and C Upper Saddle River, NJ 07458. • All Rights
ADC
• Atmega32 has 10 bit ADC, which means we will
www. Micro Digital Ed. com
BIHE university

get digital output 0 to 1023.


i.e. When the input is 0V, the digital output
will be 0V & when input is 5V (and
Vref=5V),
we will get the highest digital output
corresponding to 1023 steps, which is
5V.
• So controller ADC has 1023 steps and
• Step size with Vref=5V : 5/1023 = 4.88 mV.
• Step size with Vref=2.56 : 2.56/1023 = 2.5 mV.
• So Digital data output will be Dout = Vin / step
size. 5/4.88= Dout is 1023 or 2.5V/4.88 = 51221
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
SEng4033: Real time and Embedded System By Mikru L. JiT-JU
Assembly and C Upper Saddle River, NJ 07458. • All Rights
AVR simplest connection
www. Micro Digital Ed. com
BIHE university

• external crystal,
• external RESET button,
• additional components for AD converter
AVR Microcontroller and Embedded
SEng4033: Real System
time andUsing © 2011
Embedded System By Mikru L. JiT-JU Pearson Higher Education,
22
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Driving Loads – source or sink configurations
www. Micro Digital Ed. com
BIHE university

• An output pin not connected to anything is not of


much interest. To do something useful it must be
connected to another device, referred here simply
as a “load”.
• A real world load could be an LED, a lamp, a
transistor, or some other circuit element.
AVR Microcontroller and Embedded
SEng4033: Real System
time andUsing © 2011
Embedded System By Mikru L. JiT-JU Pearson Higher Education,
23
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Driving Loads – source or sink configurations
www. Micro Digital Ed. com
BIHE university

• Generally there are two ways to connect an output pin


to a load.
1. The first is where the microcontroller supplies the current
to drive the device. The microcontroller is referred to as the
source. Current flows from the microcontroller power to the
output pin, through the load and to ground.
2. A microcontroller I/O pin can be set by the program to
operate as an output. The output voltage will be close to
the supply voltage when the output is set to a logical 1.
The output voltage will be close to 0 volts when the pin is
set to a logical 0.
3. The microcontroller pin can also “sink” the current as
shown in Figure. Here the current flows from the power
supply through the load and through the output pin to
ground. It is important that the load be connected to the
same power supply line as the microcontroller, otherwise
destroy the IC.
AVR Microcontroller and Embedded
SEng4033: Real System
time andUsing © 2011
Embedded System By Mikru L. JiT-JU Pearson Higher Education,
24
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Digital input – pull up or pull down resistor
www. Micro Digital Ed. com
BIHE university

• Sometimes you want an input to read as a 1 or 0 as a


default. Suppose you have a sensor on a cable that
plugs into your device. It is possible that the user will
disconnect the cable. If the input pin is left floating, it
might sometimes read as a 1, sometimes as a 0. Your
code might interpret this as changes from a sensor
and not act the way you want.
AVR Microcontroller and Embedded
SEng4033: Real System
time andUsing © 2011
Embedded System By Mikru L. JiT-JU Pearson Higher Education,
25
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Digital input – pull up or pull down resistor
www. Micro Digital Ed. com
BIHE university

• Putting a pull up resistor will set the input voltage


near Vcc and it will read as a 1. A pull down
resistor will bring the voltage near 0V, and it will
read as a zero. Figure shows pull up and pull down
resistors. The switch, sensor, or other component that
generates the normal 1 and 0 voltages must be able
to over drive the resistor.
AVR Microcontroller and Embedded
SEng4033: Real System
time andUsing © 2011
Embedded System By Mikru L. JiT-JU Pearson Higher Education,
26
Assembly and C Upper Saddle River, NJ 07458. • All Rights
www. Micro Digital Ed. com
BIHE university

Communication:
Parallel, USB/Serial, USART, SPI,
TWI, Ethernet, Wireless

SEng4033: Real time and Embedded Systems

B.Sc(SE), 2023
By Mikru L.
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Parallel vs. Serial
• Parallel
www. Micro Digital Ed. com
BIHE university

– parallel communication the data is transmitted in a


byte (8 bit) or character on several data lines or
buses at a time-> faster, easier
• Serial
– the process of sending data sequentially over a
computer bus is called as serial communication,
which means the data will be transmitted bit by bit. -
> cheaper, ideal for long distance through phone
line (modem is needed)

AVR Microcontroller and Embedded


SEng4033: Real System
time andUsing © 2011
Embedded System By Mikru L. JiT-JU Pearson Higher Education,
28
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Direction
www. Micro Digital Ed. com
BIHE university

• Simplex: data can moves only in one directions


• Half Duplex: data can moves in two direction
but not at the same time
• Full Duplex: data can moves in two direction at
the same time

AVR Microcontroller and Embedded


SEng4033: Real System
time andUsing © 2011
Embedded System By Mikru L. JiT-JU Pearson Higher Education,
29
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Synchronous vs. asynchronous
www. Micro Digital Ed. com
BIHE university

• Synchronous
– Clock pulse should be transmitted during
data transmission.
– Only one side generates clock at the same
time.
• Asynchronous
– Clock pulse is not transmitted.
– The two sides should generate clock pulse.
– There should be a way to synchronize the
two sides.

AVR Microcontroller and Embedded


SEng4033: Real System
time andUsing © 2011
Embedded System By Mikru L. JiT-JU Pearson Higher Education,
30
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Data framing and bps
www. Micro Digital Ed. com
BIHE university

• To synchronize the two sides, framing is used:


– Each frame starts with a space (0) which is called Start bit
– A character of 7-9 bits is transmitted after start bit
– [a bit of parity can be transmitted after the character]
(optional)
– Each frame is ended by one or two mark (1) which is called
stop bit(s).
• Numbers of bit transmitted in a second is called bps

AVR Microcontroller and Embedded


SEng4033: Real System
time andUsing © 2011
Embedded System By Mikru L. JiT-JU Pearson Higher Education,
31
Assembly and C Upper Saddle River, NJ 07458. • All Rights
RS232 Standard
www. Micro Digital Ed. com
BIHE university

• RS232 was set by the Electronics Industries


Association (EIA) in 1960.
• Because the standard was set long before
the advent of the TTL logic family, its input
and output voltage levels are not TTL
compatible.
• A 1 is represented by −3 to −25 V
• A 0 is represented by +3 to +25 V

AVR Microcontroller and Embedded


SEng4033: Real System
time andUsing © 2011
Embedded System By Mikru L. JiT-JU Pearson Higher Education,
32
Assembly and C Upper Saddle River, NJ 07458. • All Rights
RS232 Pins
www. Micro Digital Ed. com
BIHE university

• DB 25 used to be the standard connector


of RS232
• Now DB9 is used instead of DB25

AVR Microcontroller and Embedded


SEng4033: Real System
time andUsing © 2011
Embedded System By Mikru L. JiT-JU Pearson Higher Education,
33
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Null modem connection
www. Micro Digital Ed. com

• DTE (Data Terminal Equipment) refers to


BIHE university

terminals and computers that send and


receive data.
• To connect two DTE, we connect TXD of
one device to RXD of the other and vise
versa. It is called Null Modem Connection

AVR Microcontroller and Embedded


SEng4033: Real System
time andUsing © 2011
Embedded System By Mikru L. JiT-JU Pearson Higher Education,
34
Assembly and C Upper Saddle River, NJ 07458. • All Rights
RS-232C Pins
1. DTR(Data Terminal Ready): This is an
www. Micro Digital Ed. com
BIHE university

output pin from DTE such as PC COM


port. when the DTE is turned on, after
going through a self-test, it sends out
signal DTR to indicate that is ready for
communication.
2. DSR(Data Set Ready): This an active-
low output signal from DCE ,such as
modem. When it is turned on and has
gone through the self-test, it asserts
DSR to indicate that it is ready to
communicate.
3. RTS(Request To Send) is an active-low
output from the DTE and an input to
the modem. When the DTE has a byte
to transmit, it asserts RTS to signal the
modem that it has a byte of data to
transmit.
AVR Microcontroller and Embedded
SEng4033: Real System
time andUsing
Embedded System By Mikru © 2011
L. JiT-JU Pearson Higher Education,
35
Assembly and C Upper Saddle River, NJ 07458. • All Rights
RS-232C Pins Contd…
www. Micro Digital Ed. com
BIHE university

4. CTS(Clear To Send): In response to


RTS, when the modem has room to
store the data it is to receive, it sends
out signal CTS to the DTE(PC) to
indicate that it can receive data now.
This is input signal to the DTE and
used by the DTE to start transmission.
5. DCD(Data Carrier Detect): The modem
asserts signal DCD to inform DTE (PC)
that a valid carrier has been detected
and that contact between it and the
other modem is established. This is an
output from the modem and an input
to the DTE.
6. RI (Ring Indicator): An output form the
modem and an input to PC indicates
that the telephone is ringing.
AVR Microcontroller and Embedded
SEng4033: Real System
time andUsing © 2011
Embedded System By Mikru L. JiT-JU Pearson Higher Education,
36
Assembly and C Upper Saddle River, NJ 07458. • All Rights
AVR Connection
www. Micro Digital Ed. com
BIHE university

AVR Microcontroller and Embedded


SEng4033: Real System
time andUsing © 2011
Embedded System By Mikru L. JiT-JU Pearson Higher Education,
37
Assembly and C Upper Saddle River, NJ 07458. • All Rights
USART Serial Data Communication in AVR Microcontroller
www. Micro Digital Ed. com
BIHE university

USART Serial Data Communication in AVR Microcontroller


The USART stands for universal synchronous and asynchronous
receiver and transmitter. It is a serial communication of two
protocols. This protocol is used for transmitting and receiving the
data bit by bit with respect to clock pulses on a single wire. The AVR
microcontroller has two pins: TXD and RXD, which are specially used
for transmitting and receiving the data serially. Any AVR
microcontroller consists of USART protocol with its own features.
The Main Features of AVR USART
•The USART protocol supports the full-duplex protocol.
•It generates high resolution baud rate.
AVR Microcontroller and Embedded
SEng4033: Real System
time andUsing © 2011
Embedded System By Mikru L. JiT-JU Pearson Higher Education,
38
Assembly and C Upper Saddle River, NJ 07458. • All Rights
USB
www. Micro Digital Ed. com
BIHE university

 USB Port: The Universal Serial Bus, or USB, is an


external port that interfaces between external
devices and a computer.
 The original IBM personal computers had an RS-232
port that connected external devices like a keyboard
or mouse.
 Today, USB ports are replacing RS-232 ports.
 One may plug most anything into a USB port.
 This includes keyboards, cameras, mice, joysticks, modems, zip

drives, floppy
AVR Microcontroller
Assembly and C
anddrives,
Embedded
SEng4033: printers,
Real System
time Embeddedand
andUsing Systemscanners . © 2011 Pearson Higher Education,
By Mikru L. JiT-JU 39
Upper Saddle River, NJ 07458. • All Rights
SPI (Serial
( Peripheral Interface )
www. Micro Digital Ed. com
BIHE university

Serial Peripheral Interface (SPI): is an interface bus commonly


used to send data between microcontrollers and small peripherals
such as shift registers, sensors, and SD cards.
It uses separate clock and data lines, along with a select line to
choose the device you wish to talk to.

SPI configuration with master and a slave

AVR Microcontroller and Embedded


SEng4033: Real System
time andUsing © 2011
Embedded System By Mikru L. JiT-JU Pearson Higher Education,
40
Assembly and C Upper Saddle River, NJ 07458. • All Rights
SPI (Serial
( Peripheral Interface )
www. Micro Digital Ed. com
BIHE university

4-wire SPI devices have four signals:


•Clock (SPI CLK, SCLK)
•Chip select (CS)
•Master out, slave in (MOSI)
•Master in, slave out (MISO)
• The chip select signal from the master is used to select
the slave device.
• MOSI and MISO are the data lines.
• MOSI transmits data from the master to the slave and.
• MISO transmits data from the slave to the master.
AVR Microcontroller and Embedded
SEng4033: Real System
time andUsing © 2011
Embedded System By Mikru L. JiT-JU Pearson Higher Education,
41
Assembly and C Upper Saddle River, NJ 07458. • All Rights
TWI:Two-wire
TWI: Serial Interface
www. Micro Digital Ed. com
BIHE university

• I2C (Inter-Integrated Circuit) is a serial bus interface


connection protocol, It is also called TWI (two-wire
interface).
• The Two-wire Serial Interface (TWI) is ideally
suited for microcontroller applications.
• The TWI protocol allows the systems designer to
interconnect up to 128 individually addressable
devices using only two bi-directional bus lines;
• one for clock (SCL) and one for data (SDA).

AVR Microcontroller and Embedded


SEng4033: Real System
time andUsing © 2011
Embedded System By Mikru L. JiT-JU Pearson Higher Education,
42
Assembly and C Upper Saddle River, NJ 07458. • All Rights
TWI:Two-wire
TWI: Serial Interface
www. Micro Digital Ed. com
BIHE university

TWI-Connection

AVR Microcontroller and Embedded


SEng4033: Real System
time andUsing © 2011
Embedded System By Mikru L. JiT-JU Pearson Higher Education,
43
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Ethernet
www. Micro Digital Ed. com
BIHE university

• Ethernet has traditionally been a quite complex


interface.
• All Ethernet chips until today had 100 pins or more,
where difficult to find in small quantities and difficult
to use from a small microcontroller with little
memory.
• Microchip has changed the world with their new
ENC28J60 Ethernet chip.

• The ENC28J60 chip has Tx/Rx, MAC and PHY address


AVR Microcontroller and Embedded
SEng4033: Real System
time andUsing © 2011
Embedded System By Mikru L. JiT-JU Pearson Higher Education,
44
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Ethernet
www. Micro Digital Ed. com
BIHE university

Ethernet Block
Diagram
AVR Microcontroller and Embedded
SEng4033: Real System
time andUsing © 2011
Embedded System By Mikru L. JiT-JU Pearson Higher Education,
45
Assembly and C Upper Saddle River, NJ 07458. • All Rights
www. Micro Digital Ed. com
BIHE university

Interrupts and Timers: ISRs, counter


management

SEng4033: Real time and Embedded Systems

B.Sc(SE), 2023
By Mikru L
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Interrupt
www. Micro Digital Ed. com
BIHE university

 Interrupts are: basically events that require immediate


attention by the microcontroller.
 When an interrupt event occurs the microcontroller pause
its current task and attend to the interrupt by executing an
Interrupt Service Routine (ISR) at the end of the ISR the
microcontroller returns to the task it had pause and
continue its normal operations.
 In order for the microcontroller to respond to an interrupt
event the interrupt feature of the microcontroller must be
enabled along with the specific interrupt.
 This is done by setting the Global Interrupt Enabled bit and
AVR Microcontroller and Embedded
SEng4033: Real System
time andUsing © 2011
Embedded System By Mikru L. JiT-JU Pearson Higher Education,
47
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Interrupts Vs Polling:
www. Micro Digital Ed. com

 A single microcontroller can serve several


BIHE university

devices. There are two methods by which


devices receive service from the
microcontroller: interrupts or polling.
 In the interrupt method, whenever any device
needs the microcontroller’s service, the device
notifies it by sending an interrupt signal.
 Upon receiving an interrupt signal, the
microcontroller stops whatever it is doing and
serves the device.
 The program associated with the interrupt is
called the interrupt service routine (ISR) or
interrupt handler.
SEng4033: Real time and Embedded
AVR Microcontroller and Embedded System Using
Assembly and C System By Mikru L. JiT-JU
© 2011
48 Pearson Higher Education,
Upper Saddle River, NJ 07458. • All Rights
Interrupts Vs Polling:
www. Micro Digital Ed. com
BIHE university

 In polling, the microcontroller continuously


monitors the status of a given device;
 when the status condition is met, it performs
the service.
 After that, it moves on to monitor the next
device until each one is serviced.
 Although polling can monitor the status of
several devices and serve each of them as
certain conditions are met, it is not an
efficient use of the microcontroller.

SEng4033: Real time and Embedded


AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
49
Assembly and C System By Mikru L. JiT-JU Upper Saddle River, NJ 07458. • All Rights
Interrupts Vs Polling:
 The advantage of interrupts is that the microcontroller can
www. Micro Digital Ed. com
BIHE university

serve many devices (not all at the same time, of course);


 each device can get the attention of the microcontroller
based on the priority assigned to it.
 The polling method cannot assign priority because it checks
all devices in a round-robin fashion.
 More importantly, in the interrupt method the
microcontroller can also ignore (mask) a device request for
service.
 This also is not possible with the polling method.
 The most important reason that the interrupt method is
preferable is that the polling method wastes much of the
microcontroller’s time by polling devices that do not need
service.
 So interrupts are used to avoid tying down the
microcontroller .
AVR Microcontroller and Embedded
SEng4033: Real time and Embedded
System Using © 2011 Pearson Higher Education,
50
Assembly and C System By Mikru L. JiT-JU Upper Saddle River, NJ 07458. • All Rights
Interrupt Service Routine:
www. Micro Digital Ed. com

 For every interrupt, there must be an


BIHE university

interrupt service routine (ISR), or interrupt


handler.
 When an interrupt is invoked, the
microcontroller runs the interrupt service
routine.
 Generally, in most microprocessors, for every
interrupt there is a fixed location in memory
that holds the address of its ISR.
 The group of memory locations set aside to
hold the addresses of ISRs is called the
interrupt vector table, as shown in Table
SEng4033: Real time and Embedded
below
AVR Microcontroller and Embedded System Using 51 © 2011 Pearson Higher Education,
Assembly and C System By Mikru L. JiT-JU Upper Saddle River, NJ 07458. • All Rights
Interrupt service routine:
www. Micro Digital Ed. com
BIHE university

• Fig: Interrupt vector


System table
By Mikru L.for
AVR Microcontroller and Embedded System Using
Assembly and C
ATmega 32
SEng4033: Real time and Embedded
JiT-JU
© 2011 Pearson Higher Education,
52
Upper Saddle River, NJ 07458. • All Rights
Steps in executing an interrupt:
www. Micro Digital Ed. com

 Upon activation of an interrupt, the microcontroller


BIHE university

goes through the following steps:


I. It finishes the instruction it is currently executing and
saves the address of the next instruction (program
counter) on the stack.
II. It jumps to a fixed location in memory called
the interrupt vector table.
 The interrupt vector table directs the
microcontroller to the address of the interrupt
service routine (ISR).
III.The microcontroller starts to execute the
interrupt service subroutine until it reaches the
last instruction of the subroutine, which is RETI
SEng4033: Real time and Embedded
(return from interrupt).
AVR Microcontroller and Embedded System Using
Assembly and CSystem By Mikru L. JiT-JU
53 © 2011 Pearson Higher Education,
Upper Saddle River, NJ 07458. • All Rights
Steps in executing an interrupt:
www. Micro Digital Ed. com
BIHE university

IV. Upon executing the RETI Instruction, the


microcontroller returns to the place where it was
interrupted.
 First, it gets the program counter (PC) address
from the stack by popping the top bytes of the
stack into the PC.
 Then it starts to execute from that address.
•Notice from Step IV. the critical role of the
stack. For this reason, we must be careful in
manipulating the stack contents in the ISR.
Specifically, in the ISR, just as in any CALL
subroutine, the number of pushes and pops must
SEng4033: Real time and Embedded
be equal. AVR Microcontroller and Embedded System Using
System By Mikru L. JiT-JU
Assembly and C
54 © 2011 Pearson Higher Education,
Upper Saddle River, NJ 07458. • All Rights
Source Of interrupt
www. Micro Digital Ed. com
BIHE university

 There are many sources of interrupts in AVR, depending on


which peripheral is incorporated into the chip. The following are
some of the most widely used sources of interrupts in the AVR.
 There are at least two interrupts set aside for each of the
timers, one for overflow and another for comparator match.
 Three interrupts are sett aside for external hardware
interrupts. Pins PD2(PORTD.2). PD3(PORTD3), and
PB2(PORTB.2), are the external hardware interrupts INT0, INT1,
and INT2 respectively
 Serial communication USART has tree interrupts, one for
receive and two interrupts for transmission
 The SPI interrupts
AVR Microcontroller and Embedded
SEng4033: Real System
time andUsing © 2011
Embedded System By Mikru L. JiT-JU Pearson Higher Education,
55
 The ADC (Analog to Digital Converter) Interrupts.
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Enabling and disabling an interrupt
www. Micro Digital Ed. com
BIHE university

 Upon reset, all interrupts are disabled


(masked). The interrupts must be enabled
(unmasked) by software in order for the
microcontroller to respond to them
 The D7 bit of the SREG (Status Register)
register is responsible for enabling and
disabling the interrupts globally.
 The I-bit makes the job of disabling all the
interrupts easy. With a single instruction
“CLI” (Clear Interrupt),
 we can make I = 0 during the operation of a
critical task. SEng4033: Real time and Embedded
AVR Microcontroller and Embedded System Using 56 © 2011 Pearson Higher Education,
Assembly and C System By Mikru L. JiT-JU Upper Saddle River, NJ 07458. • All Rights
www. Micro Digital Ed. com
BIHE university

 Fig: Status Register of ATmega 32


 Bit D7 (1) of the SREG register must be set to HIGH to
allow the interrupts to happen.
 This is done with the “SEI” (Set Interrupt) instruction.
 If I =1, each interrupt is enabled by setting to HIGH the
interrupt enable (IE) flag bit for that interrupt.
 There are some I/O registers holding the interrupt
enable bits.
 It must be noted that if I =0, no interrupt will be
responded to, even if the corresponding interrupt
enable bit is high.
SEng4033: Real time and Embedded
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
57
Assembly and C System By Mikru L. JiT-JU Upper Saddle River, NJ 07458. • All Rights
External Interrupts
www. Micro Digital Ed. com

 Three external hardware interrupts are there in ATmega


BIHE university

AVR.
 Pins PD2 (PORTD.2),
 PD3 (PORTD.3),
 PB2 (PORTB.2)
 These are for the external hardware interrupts INT0, INT1,
and INT2, respectively
PROGRAMMING EXTERNAL HARDWARE
INTERRUPTS
 The number of external hardware interrupt interrupts
varies in different AVRs.
 There are three external hardware interrupts in the
ATmega32: INTO,SEng4033:
INT1, and INT2.
Real time and Embedded
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
58
Assembly and C System By Mikru L. JiT-JU Upper Saddle River, NJ 07458. • All Rights
External Interrupts
www. Micro Digital Ed. com
BIHE university

SEng4033: Real time and Embedded


AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
59
Assembly and C System By Mikru L. JiT-JU Upper Saddle River, NJ 07458. • All Rights
External Interrupts
 The hardware interrupts must be enabled before they can take
www. Micro Digital Ed. com
BIHE university

effect.
 These interrupts are controlled by the following registers.
 GICR
 GIFR
 MCUCR
 MCUCSR
GICR:

INT0:
 When this bit is ‘1’ and global interrupt bit in SREG is
‘1’(i.e. I bit) the External Interrupt 0 is enabled.
 The ISC01 and ISC00 of MUCR register control the
interrupt when to be
Systemactivated
By Mikru L. JiT-JU(i.e.
SEng4033: Real time and Embedded
AVR Microcontroller and Embedded System Using
Assembly and C
on
© 2011
rising edge60 or
Pearson Higher Education,
Upper Saddle River, NJ 07458. • All Rights
GICR:
 The INTO is a low-level-triggered interrupt by default,
www. Micro Digital Ed. com
BIHE university

which means,
 when a low signal is applied to pin PD2 (PORTD.2), the
controller will be interrupted and jump to location
$0002 in the vector table to service the TSR.
 INT1: When this bit is ‘1’ and global interrupt bit in
SREG is ‘1’ (i.e. I bit) the External Interrupt 1 is control
the interrupt when level sensed) enabled.
 The ISC11 and ISC10 of MCUCR register to be activated
(i.e. on rising edge or falling edge or
 INT2: When this bit is ‘1’ and global interrupt bit in
SREG is ‘1’ (i.e. I bit) the External Interrupt 2 is
enabled.
 ISC2 bit of MCUCSR register control the interrupt when
SEng4033: Real time and Embedded
toAssembly
be andactivated (i.e. on Systemrising
By Mikru L.edge or falling
Saddle River, edge).
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
61
C JiT-JU Upper NJ 07458. • All Rights
MCUCR:
 There are 2 types of activation for the external hardware
www. Micro Digital Ed. com
BIHE university

interrupts.
 Level triggered
 Edge triggered
 INT0 & INT1 can be edge or level triggered, but INT2
can be edge triggered only.
 The MCUCR & MCUCSR registers decides the triggering
options of the external hardware interrupts INT0, INT1, and
INT2.
MCUCR:
 This register decides the triggering options of the external
hardware interrupts INT0 and INT1.

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
62
Assembly and C Upper Saddle River, NJ 07458. • All Rights
MCUCR:
www. Micro Digital Ed. com
BIHE university

• ISC01 & ISC00 (Interrupt Sense Control Bits):


• These bits define the level or edge on the external
INT0 pin that activate the interrupt as shown in
table below.

ISC11 &ISC10:
• These bits define the level or edge on the external INT1 pin
that activate the interrupt as shown in table below.

SEng4033: Real time and Embedded


AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
63
Assembly and C System By Mikru L. JiT-JU Upper Saddle River, NJ 07458. • All Rights
MCUSCR:
www. Micro Digital Ed. com
BIHE university

 This register decides the triggering options of the


external hardware interrupt INT2.

ISC2: This bit controls the INT2 interrupt trigger


condition.
• ISC2 = 0: the interrupt is detected on falling edge.
• ISC2 = 1: the interrupt is detected on rise edge.

SEng4033: Real time and Embedded


AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
64
Assembly and C System By Mikru L. JiT-JU Upper Saddle River, NJ 07458. • All Rights
GIFR:
 When an external interrupt is in an edge-triggered mode (falling
www. Micro Digital Ed. com
BIHE university

edge, rising edge, or change level), upon triggering an interrupt


request, the related INTFx flag becomes set.

 If the interrupt is active (the INTx bit is set and the I-bit in SREG
is one), the AVR will jump to the corresponding interrupt vector
location and the TFINx flag will be cleared automatically,
otherwise, the flag remains set. The flag can be cleared by
writing a one to it.
• In other words
• INTF1: When ‘1’ on this bit trigger INT1 Interrupt when INT1 bit
of GICR and I bit of SREG is one.
• INTF0: When ‘1’ on this bit trigger INT0 Interrupt when INT0 bit
of GICR and I bit of SREG is one.
• INTF2: When ‘1’ on this bitReal
SEng4033:
AVR Microcontroller and Embedded System Using
trigger INT2 Interrupt
time and Embedded © 2011
when INT265bit
Pearson Higher Education,
System By Mikru L. JiT-JU Upper Saddle River, NJ 07458. • All Rights
of GICR and I bit of SREG
Assembly and C
is one.
INTERRUPT PROGRAMMING IN C:
 In C language there is no instruction to manage the
www. Micro Digital Ed. com
BIHE university

interrupts.
 So, in WinAVR the following have been added to manage
the interrupts:
 Interrupt include file: We should include the interrupt
header file if we want to use interrupts in our program.
Use the following instruction:
#include <avr\ interrupt .h>
cli ( ) and sei ( ):
 In Assembly, the CLI and SEI Instructions clear and
set the I-bit of the SREG register, respectively.
 In WinAVR, the cli () and sei () macros do the same
tasks.

SEng4033: Real time and Embedded


AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
66
Assembly and C System By Mikru L. JiT-JU Upper Saddle River, NJ 07458. • All Rights
INTERRUPT PROGRAMMING IN C:
www. Micro Digital Ed. com
BIHE university

Fig: interrupt Vector Names for WinAVR


SEng4033: Real time and Embedded
AVR Microcontroller and Embedded System Using
Assembly and C System By Mikru L.
© 2011 Pearson Higher Education,
67
JiT-JU Upper Saddle River, NJ 07458. • All Rights
INTERRUPT PROGRAMMING IN C:
• Defining ISR: To write an ISR (interrupt service
www. Micro Digital Ed. com
BIHE university

routine) for an interrupt we use the following


structure:
ISR(interrupt vector name){
//our program
}
• For the interrupt vector namewe must use the
ISR names in Table shown below. For example,
the following TSR serves the Timer0 compare
match interrupt:
ISR (TIMER0_COMP_vect)
{
SEng4033: Real time and Embedded
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
68
} Assembly and C System By Mikru L. JiT-JU Upper Saddle River, NJ 07458. • All Rights
Timers in AVR
 Timer/counter are part of micro-controller internal
www. Micro Digital Ed. com
BIHE university

hardware.
 Timer is used to generate delay and counter is
for counting external events.
 There is small line between timer and counter which
differentiate them.
 Timer/Counter module has internal counter
register, which plays an important role.
 When we want to count events, we connect external
event (e.g. event : rising edge or falling edge) source
to input pin of timer/counter module.
 When external event occurs the value of counter
increments.
 Value of counter represents no. of external
SEng4033: Real time and Embedded
events.
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,69
Assembly and C System By Mikru L. JiT-JU Upper Saddle River, NJ 07458. • All Rights
Timers in AVR
www. Micro Digital Ed. com
BIHE university

• 1 to 6 timers
– 3 timers in ATmega32
• 8-bit and 16-bit timers
– two 8-bit timers and one 16-bit timer in
• So ATmega32
basically, a timer is a register! But not a normal
one.
• The value of this register increases/decreases
automatically.
• In an 8-bit timer, the register used is 8-bit wide
whereas in 16-bit timer, the register width is of 16
bits.
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
70
Assembly and C Upper
SEng4033: Real time and Embedded System By Mikru L. Saddle
JiT-JU River, NJ 07458. • All Rights
Timers in AVR
www. Micro Digital Ed. com
BIHE university

• normal operation, these three timers can be either


operated in normal mode, CTC mode or PWM mode.

Time Period = 1/Frequency


For F_CPU = 4 MHz, time period T = 1/4M = 0.00025 ms.
Thus for every transition (0 to 1, 1 to 2, etc upto 255 for
8bits), it takes only 0.00025 ms!
we need to flash an LED every 10
ms
Time Period = Required Delay -1
Clock Time Period

Substitute Required Delay = 10 ms and Clock Time Period =


0.00025 ms, and you get Timer Count = 39999. Can you
imagine that? The clock has already ticked 39999 times to give
aAVRdelay of only
Microcontroller 10 ms!
and Embedded System Using © 2011 Pearson Higher Education,
71
Assembly and C Upper
SEng4033: Real time and Embedded System By Mikru L. Saddle
JiT-JU River, NJ 07458. • All Rights
The Prescaler
www. Micro Digital Ed. com
BIHE university

 Assuming F_CPU = 4 MHz and a 16-bit timer (MAX =


65535), and substituting in the above formula, we can
get a maximum delay of 16.384 ms.
 Now what if we need a greater delay, say 20 ms? We are
stuck?!
 The question is how do we actually reduce the
frequency? This technique of frequency division is
called prescaling.
 We do not reduce the actual F_CPU. The actual F_CPU
remains the same (at 4 MHz in this case).
 So basically, we derive a frequency from it to run the
timer. Thus, while doing so, we divide the frequency and
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
72
Assembly and C Upper
SEng4033: Real time and Embedded System By Mikru L. Saddle
JiT-JU River, NJ 07458. • All Rights
The Prescaler
www. Micro Digital Ed. com
BIHE university

PSR10

Clear
clkIO 10-bit T/C Prescaler

clk/8
clk/64
clk/256

clk/1024
T0
0

CS00 0 1 2 3 4 5 6 7
CS01
CS02

Timer/Counter0 clock
source

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
73
Assembly and C Upper
SEng4033: Real time and Embedded System By Mikru L. Saddle
JiT-JU River, NJ 07458. • All Rights
The Prescaler
www. Micro Digital Ed. com
BIHE university

SEng4033: Real time and Embedded


AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
74
Assembly and C System By Mikru L. JiT-JU Upper Saddle River, NJ 07458. • All Rights
Example
www. Micro Digital Ed. com
BIHE university

SEng4033: Real time and Embedded


AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
75
Assembly and C System By Mikru L. JiT-JU Upper Saddle River, NJ 07458. • All Rights
www. Micro Digital Ed. com
BIHE university

End of the chapter

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights

You might also like