ES&RTOS-Firstppt IMODULE

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 582

Embedded System and Real time

Operating System(5131)

1
Course Objectives:
● Introduce the technologies behind embedded
computing systems.
● Provide knowledge on the working of microcontrollers
and its applications.
● Familiarize the key concepts of embedded systems
such as I/O, timers, interrupts and interaction with
peripheral devices.
● Introduce the basic concepts of Embedded Operating
Systems.

Reference Textbooks

3
• Course Outcomes :

On completion of the course, the student will be able to:


 By the end of this session, you should be
able to:

5
Content Details
What is Microprocessor ?
Central Processing Unit
(CPU): Control the
operation of the computer
and performs its data CPU
processing functions;
often simply referred to
as PROCESSPR
Microprocessor

• A Microprocessor is a general purpose digital computer having a


CPU which contains an ALU,a PC,a SP,few working registers, a
clock timing circuit and interrupt circuits.
• A MP doesnot contain any RAM,ROM,I/O ports and Timers
etc.on the chip itself .
• Hence they are called general purpose digital computers
Microprocessor – Basic concept

Addres
s
Control

Boot
CPU Instruction
Data Input/ External
ROM (program
RAM Output Comm.
Used at ) ROM
startup

Data

Microprocessor, by-itself, completely useless – must have


external peripherals to
Interact with outside world
Limitations of MP
• A microprocessor requires external memory to store the
programs.
• A microprocessor cannot directly interface with I/O devices and
requires peripheral chips.
• Glue logic (such as address decorders and buffers) is needed to
interconnect external memory and peripheral interface chip
with microprocessor.
Microcontroller – Basic concepts
• A Microcontroller is a single chip or VLSI core,which contains
everything a microprocessor contians plus a fixed amount of
RAM ,ROM,I/O ports and Timers etc. are embedded together on one
chip.

• In the case of microcntrollers system designer need not add any


external memory,I/O ports or timers etc.to it.

• The fixed amount of on chip RAM,ROM and number of I/O ports and
timers in microcontrollers makes them ideal for many applications in
which cost and size are critical.
Microcontroller – Basic concept

Addres
s
Control

Boot Progra Data Input/ Externa


m ROM Output l
CPU
ROM RAM Comm.

Dat
a

Microcontroller - put a limited amount of most commonly used resources


inside one chip
A microcontroller consists of the following components on a single chip

-CPU
-RAM
-ROM/EPROM
-I/O ports
-Timers and counters
-interrupt logic
-ADC
-DAC
• -Serial port
• -Oscillator Circuitry
• -Additional device and specific functional blocks.
Microcontroller
 Some major 8-bit microcontrollers
are :
◦ Freescale Semiconductor’s 68HC08/68HC11

◦ Intel’s 8051

◦ Atmel’s AVR

◦ Zilong’s Z8

◦ Microchip Technology’s PIC

15
16
Microprocessor vs Microcontroller

• Microprocessor: • Microcontroller:

– A chip that contains all the


– A chip that contains only components of a computer
the processor – processor, memory,
input/output, and essential
– Need other chips to peripherals.
make a working system – Less flexibility
– Less component count in
– More flexible system
– Can have very few I/O or – Processing power is the
same as long as using same
many I/O devices using processor
the same processor chip
• Need for microcontroller
-A designer using a general purpose microprocessor
such as Pentium or PowerPc must add RAM, ROM,
I/O ports to make them functional.

- Although it becomes bulkier and costlier still they


have the advantage of versatility.

- But for the microcontroller all are integrated in a


single chip, so it is sometimes called “single chip
computer”

- In many applications, the space used, the power


consumed and the price per unit is much more
critical consideration than the computing power.

• -In those applications, microcontrollers are used


Application of Microcontrollers

• Home appliances -- microwave ovens, answering machines,


thermostat, home security, washing machines, and lighting
systems;
• Office automation -- fax machines, copiers, printers, and
scanners;
• Business equipment -- cash registers, curbside check- in,
alarm systems, card readers, product scanners, and automated teller
machines;
• Automobiles -- transmission control, cruise control, fuel
injection, anti-lock brakes, and active suspension
• Consumer electronics -- cell phones, pagers, digital cameras, camcorders,
video cassette recorders, portable video games, calculators, and personal
digital assistants;
Introduction to Embedded Systems
• What is Embedded System?
• An Electronic/Electro mechanical system that is
designed to perform a specific function and is a
combination of both hardware and firmware (Software).
• E.g.
• Electronic Toys, Mobile Handsets, Washing Machines,
Air Conditioners, Automotive Control Units, Set Top Box,
DVD Player etc…
EMBEDDED SYSTEM

• Embedded systems are :

• Unique in character and behavior


• With specialized hardware and software.
Embedded Systems Vs General Computing
Systems:
Embedded Systems Vs General
Computing Systems:
Classification of Embedded Systems:
• First Generation:
• The early embedded systems built
around 8-bit microprocessors like
8085 and Z80 and 4-bit
microcontrollers
• EX. stepper motor control units,
Digital Telephone Keypads etc.
• Second Generation: Embedded Systems built around
16-bit microprocessors and 8 or
• 16-bit microcontrollers, following the first
generation embedded systems
• EX.SCADA, Data Acquisition
Systems etc.
• Third Generation: Embedded Systems built around high
performance 16/32 bit
• Microprocessors/controllers, Application Specific
Instruction set processors like Digital
• Signal Processors (DSPs), and Application Specific
Integrated Circuits (ASICs).The
• instruction set is complex and powerful
What is an Embedded System?

• a microprocessor-based system
• built to control a function or range of functions
• not designed to be programmed by the end user in the
same way that a PC is
• Usually embedded as part of a complete
device including hardware and mechanical parts
System

SOFTWARE PROGRAM
#include <16f876a.h>
#use delay (clock=20000000)
#byte P O RT B = 6
main()
{
set_tris_b(0);
portb=255; //decimal
d e la y _ m s (1 0 0 0 );
portb=0x55; //hexadecimal
d e la y _ m s (1 0 0 0 );
p o rtb = 0 b 1 0 1 0 1 0 1 0 ; //binary
delay_ms(500);
}
Embedded system

• It is defined as a control system or computer system designed to


perform a specific task.

• ES ususally have only a single task or a very small number of


related tasks that they are programmed to perform.

• Eg:-air condetioner,microwave oven,fax machine etc.


• In general ,an ES

• -is a system built to perform its duty,completetly or partially


independent of human intervention.

• Is specially designed to perform a few tasks in the most efficient way.

• Interacts with physical elements in our environment,viz.controlling and


driving a motor,sensing temperature etc.
Characteristics of Embedded Systems

• Single-functioned: An embedded system


usually executes only one program, repeatedly
• Tightly constrained: All computing systems
have constraints on design metrics, but those
on embedded systems can be especially tight
• Reactive and real-time: Many embedded
systems must continually react to changes in
the system’s environment, and must compute
certain results in real time without delay
Microcontroller for embedded system
• Microprocessors and microcontrollers are widely used
in embedded system products
• An embedded system is controlled by its own internal
microprocessor (or microcontroller) as opposed to
external controller
• Printer is an example of embedded system, it does only
one task, taking data from the PC and print it
• In a mouse, there is a microcontroller which tracks the
mouse’s position and sends the information to the PC
• In an embedded system, typically only one application
software is burned into ROM
Role of MC in embedded market

• An ES is a computing device that does a specific focused job.

• Each Embedded application will have a processor and special


hardware to meet the specific requirement of the application
along with the embedded s/w that is executed by the processor
for meeting the specific requirement.
• In case of a MC ,it contains everything

• So the designer need not add any additional digital part(to make system
functional) to complete the system and thus the system is economic and
handy.

• The additional external parts always makes the system more expensive
and bulkier.

• So MC are mostly used for embedded system design


•.
• In an ES,the microcontroller’s ROM is burned with s/w for only one
application needed for the system.
Criteria for Choosing a Microcontroller

Microcontrollers come in many flavours and verieties from


different manufactures in different bit size,instruction set and
architecture.

-so to select a suitable MC for a specific application ,many factors


are to be taken into account.
Criteria for Choosing a Microcontroller

Crieteria for choosing a microcontroller are

1.Computing need(Criteria 1 : i t must meet the task at hand


effi ciently and effecti vely).

2.s/w development tool(Criteria 2 : how easy i t is to develop


products around it.)

3.Availability(Criteria 3 : its availability in needed quanti ti es both


now and in the future.)
Criteria for Choosing a Microcontroller

1)Computing need:-
-Microcontroller should meet the task at hand efficiently and economically.
-this can be analyzed with the following features
• ---Bit size(8 bit, 16 bit, or 32 bit)

----power consumption
----speed
-------Instruction set(RISC or CISC)
-----
Criteria for Choosing a Microcontroller

porgram storage architecture(Harward Architecture or Von-


Neuman)
---total internal and external memory
--internal EPROM
--number of I/O pins
----number of timers
----cost
---ease of upgradation to higher performance or low power
consumption.
---packaging(DIP or QFP[quad flat package])
Criteria for Choosing a Microcontroller
Criteria for Choosing a Microcontroller

2.s/w development tool(Criteria 2 : how easy i t is to develop


products around it)
-next crieteria for choosing a MC is the ease for developing the s/w for
product in way to save the development effort and implimentation time.
-availability of the following development tool is important such as
-----assembler
----Debugger
-----Compiler
---Simulator
Criteria for Choosing a Microcontroller

3.Availability:-
-the next criteria in choosing the microcontroller is its availability in the
required quantity both now and future.
44
 You should now be able
to:◦ Compare and contrast microcontrollers and
microprocessors.
◦ List some names of MCs.
◦ Discuss advantages of MCs for Ess
◦ Compare MPs with MCs.
◦ Discuss criteria for considering a MC.

45
 By the end of this session, you should be
able to:

◦ Discuss a brief history of AVR microcontroller.


◦ Disti nguish between Harvard architecture and von
• Neumann architecture.
◦ List features of AVR MCs.
◦ Draw a simplifi ed architecture of AVR MC.
◦ Compare and contrast of various members of the
• AVR family.

46
 AVR stand
for?
Advanced Virtual RISC,
the founders are Alf Egil Bogen,
Vegard Wollan

47
OVERVIEW OF AVR
MICROCONTROLLER

• AVR is developed in the year 1996 by Atmel Corporation(8-bit


RISC single chip microcontroller ) .
• The AT90S8515 was the first microcontroller which was based on AVR
architecture.
• The first MC to hit the commercial market was AT90S1200 in the
year 1997.
 The AVR was one of the first
microcontroller families to use on-chip
flash memory for program storage, as
opposed to one-time
programmable ROM, EPROM, or EEPROM used
by other microcontrollers at the time.

51
• Many kinds of AVR with different properties
• Except for AVR32 which is 32bit all AVRs are 8
bit meaning that the CPU can work on only 8
bit of data.
• Data larger than 8 bit is broken in to 8 bit
pieces
• Programs written for one family is not 100%
compatible for other family
• AVRs are classified into four
• Mega
• Tiny
• Special purpose
• Classic
• Mega family is widely used as they are
powerful.
• widely available and comes in Dip
packages
 The AVR is a modified Harvard architecture
machine where program and data is stored in
separate physical memory systems that
appear in different address spaces, but
having the ability to read data items from
program memory using special instructions.

54
55
 The Harvard architecture is a computer
architecture with physically separate
storage and signal pathways for
instructions and data.

 CPU can read both an instruction and data


from memory at the same time that makes
it faster.

56
 The term originated from the Harvard
Mark I relay-based computer, which
stored instructions on punched tape (24
bits wide) and data in electro-
mechanical counters.

57
58
59
In contrast with the Harvard architecture, the
von Neumann architecture has a single
storage structure to hold both instructions
and data.
The CPU can be either reading an instruction
or reading/writing data f r om/ t o the
memory because instructions and data use
the same bus system.

60
 The phrase Von Neumann architecture
derives name of the mathematician and
early computer scientist John von
Neumann.

61
 series of 8 - b i t RISC microcontrollers from Atmel.

 All AVR microcontrollers share same instruction set


and a basic CPU (Harvard) architecture.

 It has 32 8-Bit general purpose registers.


 Mostly instruction Execute in Single clock cycle.
Which makes it faster among 8 bit
microcontrollers.

 AVR was designed for efficient execution of compiled


C code.

62
 AVR is a family of 8 - b i t microcontrollers with
a large range of variants differing in:

- size of program-memory (flash)


- size of data RAM
- size of EEPROM memory
- number of I/O pins, Timers
-number of on-chip features such as
UART and ADC, USB etc

63
64
65
 All of the product numbers start with AT,
which stands for Atmet.
 Now, look at the product number, from left to
right, and
 find the biggest number that is power of 2.
 This number most probably
shows the amount of microcontroller’s ROM.

66
Eg:-
Atmega 1280,the biggest power of 2 is 128.
-so it has 128 Kbytes of ROM.
-ATtiny 44,the amount of memory is 4K.
-ATmega169 ,the amount of ROM is 16K.
 Classic AVR (AT90Sxxxx)
 Mega AVR (ATmegaxxxx)
 Tiny AVR (ATtinyxxxx)
 Special purpose AVR

* x x x x is the product
number

69
71
73
75
Special capabilities are:- LCD
controller. USB controller,
Ethernet controller etc
77
AVR with other Microcontrollers

Features 8051 family PIC family AVR family


Program ROM 8K 32K 32K
DATA RAM 256 BYTES 2K 2K
EEPROM NIL 256 BYTES 1K
TIMERS 3 4 3
I/O PINS 32 35 32
ARCHITECTURE CISC RISC RISC
PWM CHANNEL NIL INBUILT INBUILT
ADC NIL INBUILT INBUILT
79
80
 You should now able
to:◦ Discuss a brief history of AVR microcontroller.
◦ Disti nguish between Harvard architecture and von
Neumann architecture.

◦ List features of AVR MCs.

◦ Draw a simplifi ed architecture of AVR MC.

◦ Compare and contrast of various members of the AVR


family.

◦ Compare AVR with other MCs.


81
Flash memory(Not in Syllabus)

• Flash memory is a non-volatile memory chip used


for storage and for transfering data between a
personal computer (PC) and digital devices. It has
the ability to be electronically reprogrammed and
erased.
• It is often found in USB flash drives, MP3 players,
digital cameras and solid-state drives.
Flash Memory(Not specified in Syllabus)

• Flash memory is a type of electronically erasable


programmable read only memory (EEPROM), but
may also be a standalone memory storage device
such as a USB drive. EEPROM is a type of data
memory device using an electronic device to erase
or write digital data. Flash memory is a distinct
type of EEPROM, which is programmed and erased
in large blocks.
Flash memory &&EEPROM(Not specified in
Syllabus)

• Flash is just one type of EEPROM.


• Flash uses NAND-type memory, while EEPROM uses
NOR type.
• Flash is block-wise erasable, while EEPROM is byte-wise
erasable.
• Flash is constantly rewritten, while other EEPROMs are
seldom rewritten.
• Flash is used when large amounts are needed, while
EEPROM is used when only small amounts are needed.
 By the end of this session, you should be
able to:Basic Architecture of AVR
◦ List the registers of AVR MCs.
◦ Examine the data memory of AVR MC.
◦ Perform simple operations, such as ADD and load,
and access internal RAM in the AVR MC.
◦ Explain the purpose of the status registers.

85
Basics About AVR microcontroller.
-AVR microcontroller is an 8 bit , RISC single chip mc with Harvard
architecture that come with some std features
---on-chip ROM
----data RAM
---data EEPROM
---timers and I/O ports.
-it consists of 32,8 bit general purpose working registers.
AVR does not have registers like accumulator .
-so the operation can be performed b/w any of the registers and can be
stored in either of them.
-it follows Harvard architecture format .
-the processor equipped with separate memories and buses for pgm and
data .
-so prefetching is possible.
-AVR can execute a million instructions per second if cycle frequency is
1MHz.
-higher is the frequency,higher will be the processing speed.
Basic AVR architecture

Atmega has four 8-bit i/o ports.


PORTA
PORTB
PORTC
PORTD
AVR-Basic Architecture

Oscillator:-
-calibrated with an internal oscillator for driving its clock.
-By default it is set to operate with a frequency of 1 MHz.
-maximum frequency is 8MHz.
-it also set with an external crystal oscillator with a maximum frequency of
16 MHz.
AVR-Basic Architecture

ADC interface:-
-it is equipped with an 8 channel ADC with resolution of 10 bits.
-ADC reads the analog i/p from a sensor or from any other sources and
converts it into digital information which is understandable by the
microcontroller.
AVR-Basic Architecture

Timers/Counters:
It consists of 2 8-bit and 1 16-bit imer/counter.
-timers are useful for creating time delays b/w two operations.
WDT:-
-it is present with internal oscillator.
-WDT continously monitors and reset the controller if the code gets stuck
at any execution action for more than a defined interval.
AVR-Basic Architecture

Interrupt:-
-it consists of 21 interrupt sources out of which 4 are external.
-the remaining are internal which support the peripherals like
USART,ADC,and timers etc.
USART:-
-USART interface is available for interfacing with external device
capable of communicating serially(data transmission bit by bit).
 Registers: Two types of registers
◦ GERNEL purpose & SPECIAL purpose registers

 GERNERAL purpose(GPRs)
◦ 32 general purpose registers having storage capacity of 8-Bits
◦ Named as R0,R1,R2 to R31 coupled directly with ALU.
◦ Register 0 to 15 & 16 to 31 are different.
◦ Can store both Data & Addresses.

 SPECIAL purpose: Three registers


◦ Program counter
◦ Stack Pointer
◦ Status Register
◦ etc

94
 There are 32 GPR
 All are of size 8 bit
 D0 is LSB
 D7 is MSB
 R0-R31 are located in the lowest
memory location of mem addr.

95
AVR-GPRS

• AVR contains 32 general purpose registers which are coupled directly


with ALU of CPU.
• AVR have many registers for arithmetic and
logic operation
• CPU uses many registers to store data
temporarily
• That information can be byte of data to be
processed or address pointing to the data to
be fetched
• AVR registers are 8 bit registers
• With an 8 bit data type , any data
larger than 8 bits must be broken into 8 bit chunks before it is processed
• In AVR there are 32 general purpose registers
• They are R0-R31and are located in the lowest
location of memory address($00-$1F)
• GPRs in AVR are the same as accumulator in
other microprocessor
• GPRs are used by all arithmetic and logic instruction.
ALU Instructions Involving GPRs
LDI Rd, K ; load Rd with
immediate value K
; d must be between 16 and
31

99
LDI R20, 0x25 ; load R20 with 0x25 (R20 = 0x25)

LDI R20, $25 ; load R20 with 0x25 (R20 = 0x25)

LDI R20, 0b00101001; load R20 with 0x25 (R20 =


0x25)

LDI R20, 37; load R20 with 0x25 (R20 = 0x25)

100
LDI R5, 0x25 ; why?

LDI R20, $fff ; why?

LDI R20, 0b001010011 ; why?

LDI R20, 270 ; why?

101
ADD Rd, Rr ; ADD Rr to Rd and store
the result in Rd.

102
LDI R16, 0x25 ; load 0x25 into
R16 LDI R17, 0x34 ; load 0x34
into R17 ADD R16, R17
Executing the above lines results R16=0x59.

103
/*
* adding.asm
* Created: 1 2 / 2 / 2 0 1 7 9:35:52
PM

*/

.INCLUDE "M32DEF.INC"
.ORG 00
LDI R16,0X25 ; load 0x25 into R16
LDI ; load 0x32 into R17
ADD R17,0X35 ; add value R17 to R16
R16, R17
NEXT: JMP
NEXT

104
105
106
DATA MEMORY

-AVR contains two kinds of memory space:-


------code memory
-----data memory
Code memory:-store user programs
Data memory:-store data
Data memory composed of three parts:-
----GPRS
-----I/O memory
----internal data SRAM

GPRs
◦ GPRs use 32 bytes of data memory space.
◦ They always take the address location $00 - $1F in
the data memory space, regardless of the AVR
chip number.

109
I/O MEMORY(SFRs)
-functions of I/O memory is fixed by the CPU designer at the time of
design.
-it is used for control of the microcontroller or peripheral..
-the AVR I/O memory is made of 8-bit registres.
-it is used for specific functions such as
----status register
----temers
----serial communication
-I/O ports
---ADC
-all the AVR ha at least 64bytes of I/O memory locations
-this 64 byte is called standard I/O memory.
-AVR with more than 32 pins(Atmega64,Atmega128etc),there are an
extended I/O memory .
-extended I/O memory is used for controlling the extra ports and
peripherals.
-I/O memory is called SFRs since each one is dedicated to a specific
function.
112
Internal data
SRAM
◦ Internal data is widely used for storing data and
parameters by AVR programmers and C compilers.
Generally that is called scratch pad.
◦ Each location of SRAM can be accessed by its
address.
◦ Each location is 8 bits wide.
◦ Size of data SRAM can vary form chip to chip

113
SRAM vs EEPROM
• AVR as an EEPROM memory that is used for storing
data.
• EEPROM does not lose it’s data when power id off
where as SRAM lose its data when power is on
• EEPROM is used for storing data that should rarely be
changed whereas the SRAM is used for storing data
and parameters that are changed frequently.
• In AVR data sheet EEPROM refers to EEPROM’s size
and SRAM is the internal SRAM’s size.
115
USING INSTRUCTIONS WITH DATA MEMORY

1.LDS(LoaD direct from data Space)


2.STS(STore direct to data Space)
3.IN
4.IN Vs LDS
5.OUT
6.MOV
LDS Rd,
; load Rd with the content of locaton K
K ;0≤d≤31
;K is an addr b / w $0000 to $FFFF

117
LDS:-
This instruction instruct CPU to copy one byte from a location in the data
memory to one of the GPRs.

LDS Rd,k ;load Rd with content of location k


;k is an address between $0000 to $FFFF

LDS R6,0x240 ;load R6 with content of location $240


LDS R0, 0x300 ; R0 = content of 0x0300
LDS R1, 0x302 ; R0 = content of 0x0302
ADD R1, R2 ; R1 = R1 + R2

119
120
STS K, Rr ; Store Rr into locaton K

; K is an addr b / w $0000 to
$FFFF

121
STS(Store direct To data space)

- this instruction tells the CPU to store the content of the GPR to an
address in the data memory.
Eg:-
STS 0x230,R25 ;store R25 to data space location 0x230
To add the content of location 0x300 to content of location 0x302 and
store the result in 0x303
LDS R16,0x300
LDS R17,0X302
ADD R16,R17
STS 0X303,R16
STP:JMP STP
SAMPLE PROGRAMS

Load the value 0x92 into location from 0x200 to 0x203 of the SRAM region
in the data memory.

LDI R20,0X99
STS 0X200,R20
STS 0X201,R20
STS 0X202,R20
STS 0X203,R20
/*
* STA.asm

*/
.INCLUDE "M32DEF.INC"
.ORG 00
LDI R16,0X99
0X212,R16
ST R16,0X85
S 0X213,R16
LDI R16,0X3F
0X214,R16
ST R16,0X63
S 0X215,R16
LDI NEXT

ST
S
LDI 125
IN instruction(IN from I/O location

IN
IN Rd,A ;load an I/O location to GPR
0<=d<=31
0<=A<=63
IN instruction tells the CPU to load one byte from an I/O register to GPR.
After the execution,GPR will have the same value as I/O register.
In IN instruction,the I/O registers are referred by their I/O address.($00 to
$3F)
IN Instruction

• -each location in the data memory has two addresses.


• - I/O address and data memory address
• -each location in the data memory has a unique address called data
memory address
• -each I/O register has a relative address in comparison to the
beginning of the I/O memory called I/O address.
128
IN Rd, A ; load an I/O location to the GPR
;0≤d≤31
;A is an I/O addr b / w $00 to $3F
;(0≤A≤63)

129
IN R19, 0x10 ; load R19 with location $10

IN R19, PIND ; load R19 with PIND

Which does the same as follows :


LDS R19, 0x30

130
IN R1, PIND ; load R1 with PIND
IN R2, PINB ; load R2 with PINB
ADD R1, ;R1=R1+R2
R2 ;Store R1 to $0300
STS 0x300,
R1

131
1. The CPU executes the IN instruction faster than LDS.
The IN lasts 1 machine cycle, where LDS lasts 2
machine cycles.

2. The IN is 2 byte instruction, where LDS is a 4 byte


instruction.

3. When we use the IN instruction we can use the names


of the I/O registers instead of their addresses.

4. The IN instruction is available in all of the AVRs,


where LDS is not implemented in some of the AVRs.

132
OUT A, Rr ; store register to I/O location
;0≤d≤31
;A is an I/O addr b / w $00 to $3F
;(0≤A≤63)

133
OUT (Out to I/O location)
OUT instruction tells the CPU to store GPR to the I/O register.

OUT POTRD,R10 ;copy the content of R10 into PORTD.


Eg:-

Pgm segment to copy PINC to PORTB.


IN R0,PINC
OUT PORTB,R0
LDI R20, 0xE6 ; load R20 with location $E6
OUT SPL, R20 ; out R19 to SPL

IN R0, PIND ; load R20 from PIND


OUT PORTA, R0 ; out R20 from R0

135
MOV Rd,
; Rd = Rr (copy Rr to Rd)
Rr ; Rd and Rr can be any of
GPRS

MOV R10, R20

136
MOV
-This instruction is used to copy the data among the GPR register to R0-
R31
MOV Rd,Rr
MOV R10,R20

Eg:-
Add the immediate value 0x19 with the content of memory location
0x220 and the result has to be stored in to 0x225
LDI R20,0x19
LDS R21,0x220
ADD R20,R21
STS 0X225,R20
INC
; increment the content of Rd by one
Rd ; 0≤d≤31

INC R2 ; R2 = R2 + 1

139
DEC
; decrement the content of Rd by one
Rd ; 0≤d≤31

DEC R2 ; R2 = R2 - 1

140
SUB Rd,
; Rd = Rd - Rr
Rr ; Rd and Rr can be any of
GPRS

141
 Complements (inverts) the content of Rd
and place the result back into the Rd
register.

142
AVR Status register
• Status register is an 8 bit register also known a
flag register.
• The bits C,Z,N,V,S, and H are called conditional
flag meaning that they indicate some
condition that result after an instruction is
executed.
144
Carry Flag C
• This flag is set whenever there is a carry out
from the D7 bit.
• This flag bit is affected after an 8 bit addition
or subtraction.
ZERO flag Z
• This flag reflect the result of an arithmetic or
logic operation .
• If the result is 0 then Z=1
• If the result is not = 0 then Z=0
Negative flag N
• Binary representation of signed numbers uses
D7 as the sign bit.
• Negative flag reflect the result of an
arithmetic operation .
• If the D7 bit of the result is 0, then N=0 and
the result is positive.
• If the D7 bit of the result is 1, then N=1 and
the result is negative.
Overflow Bit V
• This flag is set whenever the result of an
signed number operation is too large .
• Overflow flag is used to detect errors in signed
arithmetic operation .
Sign Bit S

• This flag is the result of exclusive ORing of N


&V flags.
Half carry flag H

• If there is a carry from D3 to D4 during ADD or


SUB operation ,this bit is set, otherwise it is
cleared.
• This bit is used by instruction that performs
BCD operation
• Another term(Auxiliary carry)
Bit copy storage

• Used with BLD (bit load) and BST(bit store)instructions for loading
and storing bit from one register to another.
Global Interrupt Enable

• Setting this bit enabled all the interrupt.


• Resetting this disables all interrupts
153
154
155
156
157
158
159
160
161
163
164
165
 Now you should able
to:◦ List the registers of AVR MCs.
◦ Examine the data memory of AVR MC.
◦ Perform simple operations, such as ADD and load,
and access internal RAM in the AVR MC.
◦ Explain the purpose of the status registers.

166
Thank
ou

167
What is an Embedded System?

• a microprocessor-based system
• built to control a function or range of functions
• not designed to be programmed by the end user in the
same way that a PC is
• Usually embedded as part of a complete
device including hardware and mechanical parts
System

SOFTWARE PROGRAM
#include <16f876a.h>
#use delay (clock=20000000)
#byte P O RT B = 6
main()
{
set_tris_b(0);
portb=255; //decimal
d e la y _ m s (1 0 0 0 );
portb=0x55; //hexadecimal
d e la y _ m s (1 0 0 0 );
p o rtb = 0 b 1 0 1 0 1 0 1 0 ; //binary
delay_ms(500);
}
Embedded system

• It is defined as a control system or computer system designed to


perform a specific task.

• ES ususally have only a single task or a very small number of


related tasks that they are programmed to perform.

• Eg:-air condetioner,microwave oven,fax machine etc.


• In general ,an ES

• -is a system built to perform its duty,completetly or partially


independent of human intervention.

• Is specially designed to perform a few tasks in the most efficient way.

• Interacts with physical elements in our environment,viz.controlling and


driving a motor,sensing temperature etc.
Characteristics of Embedded Systems

• Single-functioned: An embedded system


usually executes only one program, repeatedly
• Tightly constrained: All computing systems
have constraints on design metrics, but those
on embedded systems can be especially tight
• Reactive and real-time: Many embedded
systems must continually react to changes in
the system’s environment, and must compute
certain results in real time without delay
Microcontroller for embedded system
• Microprocessors and microcontrollers are widely used
in embedded system products
• An embedded system is controlled by its own internal
microprocessor (or microcontroller) as opposed to
external controller
• Printer is an example of embedded system, it does only
one task, taking data from the PC and print it
• In a mouse, there is a microcontroller which tracks the
mouse’s position and sends the information to the PC
• In an embedded system, typically only one application
software is burned into ROM
Role of MC in embedded market

• An ES is a computing device that does a specific focused job.

• Each Embedded application will have a processor and special


hardware to meet the specific requirement of the application
along with the embedded s/w that is executed by the processor
for meeting the specific requirement.
• In case of a MC ,it contains everything

• So the designer need not add any additional digital part(to make system
functional) to complete the system and thus the system is economic and
handy.

• The additional external parts always makes the system more expensive
and bulkier.

• So MC are mostly used for embedded system design


•.
• In an ES,the microcontroller’s ROM is burned with s/w for only one
application needed for the system.
Criteria for Choosing a Microcontroller

Microcontrollers come in many flavours and verieties from


different manufactures in different bit size,instruction set and
architecture.

-so to select a suitable MC for a specific application ,many factors


are to be taken into account.
Criteria for Choosing a Microcontroller

Crieteria for choosing a microcontroller are

1.Computing need(Criteria 1 : i t must meet the task at hand


effi ciently and effecti vely).

2.s/w development tool(Criteria 2 : how easy i t is to develop


products around it.)

3.Availability(Criteria 3 : its availability in needed quanti ti es both


now and in the future.)
Criteria for Choosing a Microcontroller

1)Computing need:-
-Microcontroller should meet the task at hand efficiently and economically.
-this can be analyzed with the following features
• ---Bit size(8 bit, 16 bit, or 32 bit)

----power consumption
----speed
-------Instruction set(RISC or CISC)
-----
Criteria for Choosing a Microcontroller

porgram storage architecture(Harward Architecture or Von-


Neuman)
---total internal and external memory
--internal EPROM
--number of I/O pins
----number of timers
----cost
---ease of upgradation to higher performance or low power
consumption.
---packaging(DIP or QFP[quad flat package])
Criteria for Choosing a Microcontroller
Criteria for Choosing a Microcontroller

2.s/w development tool(Criteria 2 : how easy i t is to develop


products around it)
-next crieteria for choosing a MC is the ease for developing the s/w for
product in way to save the development effort and implimentation time.
-availability of the following development tool is important such as
-----assembler
----Debugger
-----Compiler
---Simulator
Criteria for Choosing a Microcontroller

3.Availability:-
-the next criteria in choosing the microcontroller is its availability in the
required quantity both now and future.
183
 You should now be able
to:◦ Compare and contrast microcontrollers and
microprocessors.
◦ List some names of MCs.
◦ Discuss advantages of MCs for Ess
◦ Compare MPs with MCs.
◦ Discuss criteria for considering a MC.

184
 By the end of this session, you should be
able to:

◦ Discuss a brief history of AVR microcontroller.


◦ Disti nguish between Harvard architecture and von
• Neumann architecture.
◦ List features of AVR MCs.
◦ Draw a simplifi ed architecture of AVR MC.
◦ Compare and contrast of various members of the
• AVR family.

185
 AVR stand
for?
Advanced Virtual RISC,
the founders are Alf Egil Bogen Vegard
Wollan RISC

186
OVERVIEW OF AVR
MICROCONTROLLER

• AVR is developed in the year 1996 by Atmel Corporation(8-bit


RISC single chip microcontroller ) .
• The AT90S8515 was the first microcontroller which was based on AVR
architecture.
• The first MC to hit the commercial market was AT90S1200 in the
year 1997.
 The AVR was one of the first
microcontroller families to use on-chip
flash memory for program storage, as
opposed to one-time
programmable ROM, EPROM, or EEPROM used
by other microcontrollers at the time.

190
• Many kinds of AVR with different properties
• Except for AVR32 which is 32bit all AVRs are 8
bit meaning that the CPU can work on only 8
bit of data.
• Data larger than 8 bit is broken in to 8 bit
pieces
• Programs written for one family is not 100%
compatible for other family
• AVRs are classified into four
• Mega
• Tiny
• Special purpose
• Classic
• Mega family is widely used as they are
powerful.
• widely available and comes in Dip
packages
 The AVR is a modified Harvard architecture
machine where program and data is stored in
separate physical memory systems that
appear in different address spaces, but
having the ability to read data items from
program memory using special instructions.

193
194
 The Harvard architecture is a computer
architecture with physically separate
storage and signal pathways for
instructions and data.

 CPU can read both an instruction and data


from memory at the same time that makes
it faster.

195
 The term originated from the Harvard
Mark I relay-based computer, which
stored instructions on punched tape (24
bits wide) and data in electro-
mechanical counters.

196
197
198
In contrast with the Harvard architecture, the
von Neumann architecture has a single
storage structure to hold both instructions
and data.
The CPU can be either reading an instruction
or reading/writing data f r om/ t o the
memory because instructions and data use
the same bus system.

199
 The phrase Von Neumann architecture
derives name of the mathematician and
early computer scientist John von
Neumann.

200
 series of 8 - b i t RISC microcontrollers from Atmel.

 All AVR microcontrollers share same instruction set


and a basic CPU (Harvard) architecture.

 It has 32 8-Bit general purpose registers.


 Mostly instruction Execute in Single clock cycle.
Which makes it faster among 8 bit
microcontrollers.

 AVR was designed for efficient execution of compiled


C code.

201
 AVR is a family of 8 - b i t microcontrollers with
a large range of variants differing in:

- size of program-memory (flash)


- size of data RAM
- size of EEPROM memory
- number of I/O pins, Timers
-number of on-chip features such as
UART and ADC, USB etc

202
203
204
 All of the product numbers start with AT,
which stands for Atmet.
 Now, look at the product number, from left to
right, and
 find the biggest number that is power of 2.
 This number most probably
shows the amount of microcontroller’s ROM.

205
Eg:-
Atmega 1280,the biggest power of 2 is 128.
-so it has 128 Kbytes of ROM.
-ATtiny 44,the amount of memory is 4K.
-ATmega169 ,the amount of ROM is 16K.
 Classic AVR (AT90Sxxxx)
 Mega AVR (ATmegaxxxx)
 Tiny AVR (ATtinyxxxx)
 Special purpose AVR

* x x x x is the product
number

208
210
212
214
Special capabilities are:- LCD
controller. USB controller,
Ethernet controller etc
216
AVR with other Microcontrollers

Features 8051 family PIC family AVR family


Program ROM 8K 32K 32K
DATA RAM 256 BYTES 2K 2K
EEPROM NIL 256 BYTES 1K
TIMERS 3 4 3
I/O PINS 32 35 32
ARCHITECTURE CISC RISC RISC
PWM CHANNEL NIL INBUILT INBUILT
ADC NIL INBUILT INBUILT
218
219
 You should now able
to:◦ Discuss a brief history of AVR microcontroller.
◦ Disti nguish between Harvard architecture and von
Neumann architecture.

◦ List features of AVR MCs.

◦ Draw a simplifi ed architecture of AVR MC.

◦ Compare and contrast of various members of the AVR


family.

◦ Compare AVR with other MCs.


220
Flash memory(Not in Syllabus)

• Flash memory is a non-volatile memory chip used


for storage and for transfering data between a
personal computer (PC) and digital devices. It has
the ability to be electronically reprogrammed and
erased.
• It is often found in USB flash drives, MP3 players,
digital cameras and solid-state drives.
Flash Memory(Not specified in Syllabus)

• Flash memory is a type of electronically erasable


programmable read only memory (EEPROM), but
may also be a standalone memory storage device
such as a USB drive. EEPROM is a type of data
memory device using an electronic device to erase
or write digital data. Flash memory is a distinct
type of EEPROM, which is programmed and erased
in large blocks.
Flash memory &&EEPROM(Not specified in
Syllabus)

• Flash is just one type of EEPROM.


• Flash uses NAND-type memory, while EEPROM uses
NOR type.
• Flash is block-wise erasable, while EEPROM is byte-wise
erasable.
• Flash is constantly rewritten, while other EEPROMs are
seldom rewritten.
• Flash is used when large amounts are needed, while
EEPROM is used when only small amounts are needed.
 By the end of this session, you should be
able to:Basic Architecture of AVR
◦ List the registers of AVR MCs.
◦ Examine the data memory of AVR MC.
◦ Perform simple operations, such as ADD and load,
and access internal RAM in the AVR MC.
◦ Explain the purpose of the status registers.

224
Basics About AVR microcontroller.
-AVR microcontroller is an 8 bit , RISC single chip mc with Harvard
architecture that come with some std features
---on-chip ROM
----data RAM
---data EEPROM
---timers and I/O ports.
-it consists of 32,8 bit general purpose working registers.
AVR does not have registers like accumulator .
-so the operation can be performed b/w any of the registers and can be
stored in either of them.
-it follows Harvard architecture format .
-the processor equipped with saparate memories and buses for pgm and
data .
-so prefetching is possible.
-AVR can execute a million instructions per second if cycle frequency is
1MHz.
-higher is the frequency,higher will be the processing speed.
Basic AVR architecture

Atmega has four 8-bit i/o ports.


PORTA
PORTB
PORTC
PORTD
AVR-Basic Architecture

Oscillator:-
-calibrated with an internal oscillator for driving its clock.
-By default it is set to operate with a frequency of 1 MHz.
-maximum frequency is 8MHz.
-it also set with an external crystal oscillator with a maximum frequency of
16 MHz.
AVR-Basic Architecture

ADC interface:-
-it is equipped with an 8 channel ADC with resolution of 10 bits.
-ADC reads the analog i/p from a sensor or from any other sources and
converts it into digital information which is understandable by the
microcontroller.
AVR-Basic Architecture

Timers/Counters:
It consists of 2 8-bit and 1 16-bit imer/counter.
-timers are useful for creating time delays b/w two operations.
WDT:-
-it is present with internal oscillator.
-WDT continously monitors and reset the controller if the code gets stuck
at any execution action for more than a defined interval.
AVR-Basic Architecture

Interrupt:-
-it consists of 21 interrupt sources out of which 4 are external.
-the remaining are internal which support the peripherals like
USART,ADC,and timers etc.
USART:-
-USART interface is available for interfacing with external device
capable of communicating serially(data transmission bit by bit).
 Registers: Two types of registers
◦ GERNEL purpose & SPECIAL purpose registers

 GERNERAL purpose(GPRs)
◦ 32 general purpose registers having storage capacity of 8-Bits
◦ Named as R0,R1,R2 to R31 coupled directly with ALU.
◦ Register 0 to 15 & 16 to 31 are different.
◦ Can store both Data & Addresses.

 SPECIAL purpose: Three registers


◦ Program counter
◦ Stack Pointer
◦ Status Register
◦ etc

233
 There are 32 GPR
 All are of size 8 bit
 D0 is LSB
 D7 is MSB
 R0-R31 are located in the lowest
memory location of mem addr.

234
AVR-GPRS

• AVR contains 32 general purpose registers which are coupled directly


with ALU of CPU.
• AVR have many registers for arithmetic and
logic operation
• CPU uses many registers to store data
temporarily
• That information can be byte of data to be
processed or address pointing to the data to
be fetched
• AVR registers are 8 bit registers
• With an 8 bit data type , any data
larger than 8 bits must be broken into 8 bit chunks before it is processed
• In AVR there are 32 general purpose registers
• They are R0-R31and are located in the lowest
location of memory address($00-$1F)
• GPRs in AVR are the same as accumulator in
other microprocessor
• GPRs are used by all arithmetic and logic instruction.
ALU Instructions Involving GPRs
LDI Rd, K ; load Rd with
immediate value K
; d must be between 16 and
31

238
LDI R20, 0x25 ; load R20 with 0x25 (R20 = 0x25)

LDI R20, $25 ; load R20 with 0x25 (R20 = 0x25)

LDI R20, 0b00101001; load R20 with 0x25 (R20 =


0x25)

LDI R20, 37; load R20 with 0x25 (R20 = 0x25)

239
LDI R5, 0x25 ; why?

LDI R20, $fff ; why?

LDI R20, 0b001010011 ; why?

LDI R20, 270 ; why?

240
ADD Rd, Rr ; ADD Rr to Rd and store
the result in Rd.

241
LDI R16, 0x25 ; load 0x25 into
R16 LDI R17, 0x34 ; load 0x34
into R17 ADD R16, R17
Executing the above lines results R16=0x59.

242
/*
* adding.asm
* Created: 1 2 / 2 / 2 0 1 7 9:35:52
PM

*/

.INCLUDE "M32DEF.INC"
.ORG 00
LDI R16,0X25 ; load 0x25 into R16
LDI ; load 0x32 into R17
ADD R17,0X35 ; add value R17 to R16
R16, R17
NEXT: JMP
NEXT

243
244
245
DATA MEMORY

-AVR contains two kinds of memory space:-


------code memory
-----data memory
Code memory:-store user programs
Data memory:-store data
Data memory composed of three parts:-
----GPRS
-----I/O memory
----internal data SRAM

GPRs
◦ GPRs use 32 bytes of data memory space.
◦ They always take the address location $00 - $1F in
the data memory space, regardless of the AVR
chip number.

248
I/O MEMORY(SFRs)
-functions of I/O memory is fixed by the CPU designer at the time of
design.
-it is used for control of the microcontroller or peripheral..
-the AVR I/O memory is made of 8-bit registres.
-it is used for specific functions such as
----status register
----temers
----serial communication
-I/O ports
---ADC
-all the AVR ha at least 64bytes of I/O memory locations
-this 64 byte is called standard I/O memory.
-AVR with more than 32 pins(Atmega64,Atmega128etc),there are an
extended I/O memory .
-extended I/O memory is used for controlling the extra ports and
peripherals.
-I/O memory is called SFRs since each one is dedicated to a specific
function.
251
Internal data
SRAM
◦ Internal data is widely used for storing data and
parameters by AVR programmers and C compilers.
Generally that is called scratch pad.
◦ Each location of SRAM can be accessed by its
address.
◦ Each location is 8 bits wide.
◦ Size of data SRAM can vary form chip to chip

252
SRAM vs EEPROM
• AVR as an EEPROM memory that is used for storing
data.
• EEPROM does not lose it’s data when power id off
where as SRAM lose its data when power is on
• EEPROM is used for storing data that should rarely be
changed whereas the SRAM is used for storing data
and parameters that are changed frequently.
• In AVR data sheet EEPROM refers to EEPROM’s size
and SRAM is the internal SRAM’s size.
254
USING INSTRUCTIONS WITH DATA MEMORY

1.LDS(LoaD direct from data Space)


2.STS(STore direct to data Space)
3.IN
4.IN Vs LDS
5.OUT
6.MOV
LDS Rd,
; load Rd with the content of locaton K
K ;0≤d≤31
;K is an addr b / w $0000 to $FFFF

256
LDS:-
This instruction instruct CPU to copy one byte from a location in the data
memory to one of the GPRs.

LDS Rd,k ;load Rd with content of location k


;k is an address between $0000 to $FFFF

LDS R6,0x240 ;load R6 with content of location $240


LDS R0, 0x300 ; R0 = content of 0x0300
LDS R1, 0x302 ; R0 = content of 0x0302
ADD R1, R2 ; R1 = R1 + R2

258
259
STS K, Rr ; Store Rr into locaton K

; K is an addr b / w $0000 to
$FFFF

260
STS(Store direct To data space)

- this instruction tells the CPU to store the content of the GPR to an
address in the data memory.
Eg:-
STS 0x230,R25 ;store R25 to data space location 0x230
To add the content of location 0x300 to content of location 0x302 and
store the result in 0x303
LDS R16,0x300
LDS R17,0X302
ADD R16,R17
STS 0X303,R16
STP:JMP STP
SAMPLE PROGRAMS

Load the value 0x92 into location from 0x200 to 0x203 of the SRAM region
in the data memory.

LDI R20,0X99
STS 0X200,R20
STS 0X201,R20
STS 0X202,R20
STS 0X203,R20
/*
* STA.asm

*/
.INCLUDE "M32DEF.INC"
.ORG 00
LDI R16,0X99
0X212,R16
ST R16,0X85
S 0X213,R16
LDI R16,0X3F
0X214,R16
ST R16,0X63
S 0X215,R16
LDI NEXT

ST
S
LDI 264
IN instruction(IN from I/O location

IN
IN Rd,A ;load an I/O location to GPR
0<=d<=31
0<=A<=63
IN instruction tells the CPU to load one byte from an I/O register to GPR.
After the execution,GPR will have the same value as I/O register.
In IN instruction,the I/O registers are referred by their I/O address.($00 to
$3F)
IN Instruction

• -each location in the data memory has two addresses.


• - I/O address and data memory address
• -each location in the data memory has a unique address called data
memory address
• -each I/O register has a relative address in comparison to the
beginning of the I/O memory called I/O address.
267
IN Rd, A ; load an I/O location to the GPR
;0≤d≤31
;A is an I/O addr b / w $00 to $3F
;(0≤A≤63)

268
IN R19, 0x10 ; load R19 with location $10

IN R19, PIND ; load R19 with PIND

Which does the same as follows :


LDS R19, 0x30

269
IN R1, PIND ; load R1 with PIND
IN R2, PINB ; load R2 with PINB
ADD R1, ;R1=R1+R2
R2 ;Store R1 to $0300
STS 0x300,
R1

270
1. The CPU executes the IN instruction faster than LDS.
The IN lasts 1 machine cycle, where LDS lasts 2
machine cycles.

2. The IN is 2 byte instruction, where LDS is a 4 byte


instruction.

3. When we use the IN instruction we can use the names


of the I/O registers instead of their addresses.

4. The IN instruction is available in all of the AVRs,


where LDS is not implemented in some of the AVRs.

271
OUT A, Rr ; store register to I/O location
;0≤d≤31
;A is an I/O addr b / w $00 to $3F
;(0≤A≤63)

272
OUT (Out to I/O location)
OUT instruction tells the CPU to store GPR to the I/O register.

OUT POTRD,R10 ;copy the content of R10 into PORTD.


Eg:-

Pgm segment to copy PINC to PORTB.


IN R0,PINC
OUT PORTB,R0
LDI R20, 0xE6 ; load R20 with location $E6
OUT SPL, R20 ; out R19 to SPL

IN R0, PIND ; load R20 from PIND


OUT PORTA, R0 ; out R20 from R0

274
MOV Rd,
; Rd = Rr (copy Rr to Rd)
Rr ; Rd and Rr can be any of
GPRS

MOV R10, R20

275
MOV
-This instruction is used to copy the data among the GPR register to R0-
R31
MOV Rd,Rr
MOV R10,R20

Eg:-
Add the immediate value 0x19 with the content of memory location
0x220 and the result has to be stored in to 0x225
LDI R20,0x19
LDS R21,0x220
ADD R20,R21
STS 0X225,R20
INC
; increment the content of Rd by one
Rd ; 0≤d≤31

INC R2 ; R2 = R2 + 1

278
DEC
; decrement the content of Rd by one
Rd ; 0≤d≤31

DEC R2 ; R2 = R2 - 1

279
SUB Rd,
; Rd = Rd - Rr
Rr ; Rd and Rr can be any of
GPRS

280
 Complements (inverts) the content of Rd
and place the result back into the Rd
register.

281
AVR Status register
• Status register is an 8 bit register also known a
flag register.
• The bits C,Z,N,V,S, and H are called conditional
flag meaning that they indicate some
condition that result after an instruction is
executed.
283
Carry Flag C
• This flag is set whenever there is a carry out
from the D7 bit.
• This flag bit is affected after an 8 bit addition
or subtraction.
ZERO flag Z
• This flag reflect the result of an arithmetic or
logic operation .
• If the result is 0 then Z=1
• If the result is not = 0 then Z=0
Negative flag N
• Binary representation of signed numbers uses
D7 as the sign bit.
• Negative flag reflect the result of an
arithmetic operation .
• If the D7 bit of the result is 0, then N=0 and
the result is positive.
• If the D7 bit of the result is 1, then N=1 and
the result is negative.
Overflow Bit V
• This flag is set whenever the result of an
signed number operation is too large .
• Overflow flag is used to detect errors in signed
arithmetic operation .
Sign Bit S

• This flag is the result of exclusive ORing of N


&V flags.
Half carry flag H

• If there is a carry from D3 to D4 during ADD or


SUB operation ,this bit is set, otherwise it is
cleared.
• This bit is used by instruction that performs
BCD operation
• Another term(Auxiliary carry)
Bit copy storage

• Used with BLD (bit load) and BST(bit store)instructions for loading
and storing bit from one register to another.
Global Interrupt Enable

• Setting this bit enabled all the interrupt.


• Resetting this disables all interrupts
292
293
294
295
296
297
298
299
300
302
303
304
 Now you should able
to:◦ List the registers of AVR MCs.
◦ Examine the data memory of AVR MC.
◦ Perform simple operations, such as ADD and load,
and access internal RAM in the AVR MC.
◦ Explain the purpose of the status registers.

305
Thank
ou

306
What is an Embedded System?

• a microprocessor-based system
• built to control a function or range of functions
• not designed to be programmed by the end user in the
same way that a PC is
• Usually embedded as part of a complete
device including hardware and mechanical parts
System

SOFTWARE PROGRAM
#include <16f876a.h>
#use delay (clock=20000000)
#byte P O RT B = 6
main()
{
set_tris_b(0);
portb=255; //decimal
d e la y _ m s (1 0 0 0 );
portb=0x55; //hexadecimal
d e la y _ m s (1 0 0 0 );
p o rtb = 0 b 1 0 1 0 1 0 1 0 ; //binary
delay_ms(500);
}
Embedded system

• It is defined as a control system or computer system designed to


perform a specific task.

• ES ususally have only a single task or a very small number of


related tasks that they are programmed to perform.

• Eg:-air condetioner,microwave oven,fax machine etc.


• In general ,an ES

• -is a system built to perform its duty,completetly or partially


independent of human intervention.

• Is specially designed to perform a few tasks in the most efficient way.

• Interacts with physical elements in our environment,viz.controlling and


driving a motor,sensing temperature etc.
Characteristics of Embedded Systems

• Single-functioned: An embedded system


usually executes only one program, repeatedly
• Tightly constrained: All computing systems
have constraints on design metrics, but those
on embedded systems can be especially tight
• Reactive and real-time: Many embedded
systems must continually react to changes in
the system’s environment, and must compute
certain results in real time without delay
Microcontroller for embedded system
• Microprocessors and microcontrollers are widely used
in embedded system products
• An embedded system is controlled by its own internal
microprocessor (or microcontroller) as opposed to
external controller
• Printer is an example of embedded system, it does only
one task, taking data from the PC and print it
• In a mouse, there is a microcontroller which tracks the
mouse’s position and sends the information to the PC
• In an embedded system, typically only one application
software is burned into ROM
Role of MC in embedded market

• An ES is a computing device that does a specific focused job.

• Each Embedded application will have a processor and special


hardware to meet the specific requirement of the application
along with the embedded s/w that is executed by the processor
for meeting the specific requirement.
• In case of a MC ,it contains everything

• So the designer need not add any additional digital part(to make system
functional) to complete the system and thus the system is economic and
handy.

• The additional external parts always makes the system more expensive
and bulkier.

• So MC are mostly used for embedded system design


•.
• In an ES,the microcontroller’s ROM is burned with s/w for only one
application needed for the system.
Criteria for Choosing a Microcontroller

Microcontrollers come in many flavours and verieties from


different manufactures in different bit size,instruction set and
architecture.

-so to select a suitable MC for a specific application ,many factors


are to be taken into account.
Criteria for Choosing a Microcontroller

Crieteria for choosing a microcontroller are

1.Computing need(Criteria 1 : i t must meet the task at hand


effi ciently and effecti vely).

2.s/w development tool(Criteria 2 : how easy i t is to develop


products around it.)

3.Availability(Criteria 3 : its availability in needed quanti ti es both


now and in the future.)
Criteria for Choosing a Microcontroller

1)Computing need:-
-Microcontroller should meet the task at hand efficiently and economically.
-this can be analyzed with the following features
• ---Bit size(8 bit, 16 bit, or 32 bit)

----power consumption
----speed
-------Instruction set(RISC or CISC)
-----
Criteria for Choosing a Microcontroller

porgram storage architecture(Harward Architecture or Von-


Neuman)
---total internal and external memory
--internal EPROM
--number of I/O pins
----number of timers
----cost
---ease of upgradation to higher performance or low power
consumption.
---packaging(DIP or QFP[quad flat package])
Criteria for Choosing a Microcontroller
Criteria for Choosing a Microcontroller

2.s/w development tool(Criteria 2 : how easy i t is to develop


products around it)
-next crieteria for choosing a MC is the ease for developing the s/w for
product in way to save the development effort and implimentation time.
-availability of the following development tool is important such as
-----assembler
----Debugger
-----Compiler
---Simulator
Criteria for Choosing a Microcontroller

3.Availability:-
-the next criteria in choosing the microcontroller is its availability in the
required quantity both now and future.
321
 You should now be able
to:◦ Compare and contrast microcontrollers and
microprocessors.
◦ List some names of MCs.
◦ Discuss advantages of MCs for Ess
◦ Compare MPs with MCs.
◦ Discuss criteria for considering a MC.

322
 By the end of this session, you should be
able to:

◦ Discuss a brief history of AVR microcontroller.


◦ Disti nguish between Harvard architecture and von
• Neumann architecture.
◦ List features of AVR MCs.
◦ Draw a simplifi ed architecture of AVR MC.
◦ Compare and contrast of various members of the
• AVR family.

323
 AVR stand
for?
Advanced Virtual RISC,
the founders are Alf Egil Bogen Vegard
Wollan RISC

324
OVERVIEW OF AVR
MICROCONTROLLER

• AVR is developed in the year 1996 by Atmel Corporation(8-bit


RISC single chip microcontroller ) .
• The AT90S8515 was the first microcontroller which was based on AVR
architecture.
• The first MC to hit the commercial market was AT90S1200 in the
year 1997.
 The AVR was one of the first
microcontroller families to use on-chip
flash memory for program storage, as
opposed to one-time
programmable ROM, EPROM, or EEPROM used
by other microcontrollers at the time.

328
• Many kinds of AVR with different properties
• Except for AVR32 which is 32bit all AVRs are 8
bit meaning that the CPU can work on only 8
bit of data.
• Data larger than 8 bit is broken in to 8 bit
pieces
• Programs written for one family is not 100%
compatible for other family
• AVRs are classified into four
• Mega
• Tiny
• Special purpose
• Classic
• Mega family is widely used as they are
powerful.
• widely available and comes in Dip
packages
 The AVR is a modified Harvard architecture
machine where program and data is stored in
separate physical memory systems that
appear in different address spaces, but
having the ability to read data items from
program memory using special instructions.

331
332
 The Harvard architecture is a computer
architecture with physically separate
storage and signal pathways for
instructions and data.

 CPU can read both an instruction and data


from memory at the same time that makes
it faster.

333
 The term originated from the Harvard
Mark I relay-based computer, which
stored instructions on punched tape (24
bits wide) and data in electro-
mechanical counters.

334
335
336
In contrast with the Harvard architecture, the
von Neumann architecture has a single
storage structure to hold both instructions
and data.
The CPU can be either reading an instruction
or reading/writing data f r om/ t o the
memory because instructions and data use
the same bus system.

337
 The phrase Von Neumann architecture
derives name of the mathematician and
early computer scientist John von
Neumann.

338
 series of 8 - b i t RISC microcontrollers from Atmel.

 All AVR microcontrollers share same instruction set


and a basic CPU (Harvard) architecture.

 It has 32 8-Bit general purpose registers.


 Mostly instruction Execute in Single clock cycle.
Which makes it faster among 8 bit
microcontrollers.

 AVR was designed for efficient execution of compiled


C code.

339
 AVR is a family of 8 - b i t microcontrollers with
a large range of variants differing in:

- size of program-memory (flash)


- size of data RAM
- size of EEPROM memory
- number of I/O pins, Timers
-number of on-chip features such as
UART and ADC, USB etc

340
341
342
 All of the product numbers start with AT,
which stands for Atmet.
 Now, look at the product number, from left to
right, and
 find the biggest number that is power of 2.
 This number most probably
shows the amount of microcontroller’s ROM.

343
Eg:-
Atmega 1280,the biggest power of 2 is 128.
-so it has 128 Kbytes of ROM.
-ATtiny 44,the amount of memory is 4K.
-ATmega169 ,the amount of ROM is 16K.
 Classic AVR (AT90Sxxxx)
 Mega AVR (ATmegaxxxx)
 Tiny AVR (ATtinyxxxx)
 Special purpose AVR

* x x x x is the product
number

346
348
350
352
Special capabilities are:- LCD
controller. USB controller,
Ethernet controller etc
354
AVR with other Microcontrollers

Features 8051 family PIC family AVR family


Program ROM 8K 32K 32K
DATA RAM 256 BYTES 2K 2K
EEPROM NIL 256 BYTES 1K
TIMERS 3 4 3
I/O PINS 32 35 32
ARCHITECTURE CISC RISC RISC
PWM CHANNEL NIL INBUILT INBUILT
ADC NIL INBUILT INBUILT
356
357
 You should now able
to:◦ Discuss a brief history of AVR microcontroller.
◦ Disti nguish between Harvard architecture and von
Neumann architecture.

◦ List features of AVR MCs.

◦ Draw a simplifi ed architecture of AVR MC.

◦ Compare and contrast of various members of the AVR


family.

◦ Compare AVR with other MCs.


358
Flash memory(Not in Syllabus)

• Flash memory is a non-volatile memory chip used


for storage and for transfering data between a
personal computer (PC) and digital devices. It has
the ability to be electronically reprogrammed and
erased.
• It is often found in USB flash drives, MP3 players,
digital cameras and solid-state drives.
Flash Memory(Not specified in Syllabus)

• Flash memory is a type of electronically erasable


programmable read only memory (EEPROM), but
may also be a standalone memory storage device
such as a USB drive. EEPROM is a type of data
memory device using an electronic device to erase
or write digital data. Flash memory is a distinct
type of EEPROM, which is programmed and erased
in large blocks.
Flash memory &&EEPROM(Not specified in
Syllabus)

• Flash is just one type of EEPROM.


• Flash uses NAND-type memory, while EEPROM uses
NOR type.
• Flash is block-wise erasable, while EEPROM is byte-wise
erasable.
• Flash is constantly rewritten, while other EEPROMs are
seldom rewritten.
• Flash is used when large amounts are needed, while
EEPROM is used when only small amounts are needed.
 By the end of this session, you should be
able to:Basic Architecture of AVR
◦ List the registers of AVR MCs.
◦ Examine the data memory of AVR MC.
◦ Perform simple operations, such as ADD and load,
and access internal RAM in the AVR MC.
◦ Explain the purpose of the status registers.

362
Basics About AVR microcontroller.
-AVR microcontroller is an 8 bit , RISC single chip mc with Harvard
architecture that come with some std features
---on-chip ROM
----data RAM
---data EEPROM
---timers and I/O ports.
-it consists of 32,8 bit general purpose working registers.
AVR does not have registers like accumulator .
-so the operation can be performed b/w any of the registers and can be
stored in either of them.
-it follows Harvard architecture format .
-the processor equipped with saparate memories and buses for pgm and
data .
-so prefetching is possible.
-AVR can execute a million instructions per second if cycle frequency is
1MHz.
-higher is the frequency,higher will be the processing speed.
Basic AVR architecture

Atmega has four 8-bit i/o ports.


PORTA
PORTB
PORTC
PORTD
AVR-Basic Architecture

Oscillator:-
-calibrated with an internal oscillator for driving its clock.
-By default it is set to operate with a frequency of 1 MHz.
-maximum frequency is 8MHz.
-it also set with an external crystal oscillator with a maximum frequency of
16 MHz.
AVR-Basic Architecture

ADC interface:-
-it is equipped with an 8 channel ADC with resolution of 10 bits.
-ADC reads the analog i/p from a sensor or from any other sources and
converts it into digital information which is understandable by the
microcontroller.
AVR-Basic Architecture

Timers/Counters:
It consists of 2 8-bit and 1 16-bit imer/counter.
-timers are useful for creating time delays b/w two operations.
WDT:-
-it is present with internal oscillator.
-WDT continously monitors and reset the controller if the code gets stuck
at any execution action for more than a defined interval.
AVR-Basic Architecture

Interrupt:-
-it consists of 21 interrupt sources out of which 4 are external.
-the remaining are internal which support the peripherals like
USART,ADC,and timers etc.
USART:-
-USART interface is available for interfacing with external device
capable of communicating serially(data transmission bit by bit).
 Registers: Two types of registers
◦ GERNEL purpose & SPECIAL purpose registers

 GERNERAL purpose(GPRs)
◦ 32 general purpose registers having storage capacity of 8-Bits
◦ Named as R0,R1,R2 to R31 coupled directly with ALU.
◦ Register 0 to 15 & 16 to 31 are different.
◦ Can store both Data & Addresses.

 SPECIAL purpose: Three registers


◦ Program counter
◦ Stack Pointer
◦ Status Register
◦ etc

371
 There are 32 GPR
 All are of size 8 bit
 D0 is LSB
 D7 is MSB
 R0-R31 are located in the lowest
memory location of mem addr.

372
AVR-GPRS

• AVR contains 32 general purpose registers which are coupled directly


with ALU of CPU.
• AVR have many registers for arithmetic and
logic operation
• CPU uses many registers to store data
temporarily
• That information can be byte of data to be
processed or address pointing to the data to
be fetched
• AVR registers are 8 bit registers
• With an 8 bit data type , any data
larger than 8 bits must be broken into 8 bit chunks before it is processed
• In AVR there are 32 general purpose registers
• They are R0-R31and are located in the lowest
location of memory address($00-$1F)
• GPRs in AVR are the same as accumulator in
other microprocessor
• GPRs are used by all arithmetic and logic instruction.
ALU Instructions Involving GPRs
LDI Rd, K ; load Rd with
immediate value K
; d must be between 16 and
31

376
LDI R20, 0x25 ; load R20 with 0x25 (R20 = 0x25)

LDI R20, $25 ; load R20 with 0x25 (R20 = 0x25)

LDI R20, 0b00101001; load R20 with 0x25 (R20 =


0x25)

LDI R20, 37; load R20 with 0x25 (R20 = 0x25)

377
LDI R5, 0x25 ; why?

LDI R20, $fff ; why?

LDI R20, 0b001010011 ; why?

LDI R20, 270 ; why?

378
ADD Rd, Rr ; ADD Rr to Rd and store
the result in Rd.

379
LDI R16, 0x25 ; load 0x25 into
R16 LDI R17, 0x34 ; load 0x34
into R17 ADD R16, R17
Executing the above lines results R16=0x59.

380
/*
* adding.asm
* Created: 1 2 / 2 / 2 0 1 7 9:35:52
PM

*/

.INCLUDE "M32DEF.INC"
.ORG 00
LDI R16,0X25 ; load 0x25 into R16
LDI ; load 0x32 into R17
ADD R17,0X35 ; add value R17 to R16
R16, R17
NEXT: JMP
NEXT

381
382
383
DATA MEMORY

-AVR contains two kinds of memory space:-


------code memory
-----data memory
Code memory:-store user programs
Data memory:-store data
Data memory composed of three parts:-
----GPRS
-----I/O memory
----internal data SRAM

GPRs
◦ GPRs use 32 bytes of data memory space.
◦ They always take the address location $00 - $1F in
the data memory space, regardless of the AVR
chip number.

386
I/O MEMORY(SFRs)
-functions of I/O memory is fixed by the CPU designer at the time of
design.
-it is used for control of the microcontroller or peripheral..
-the AVR I/O memory is made of 8-bit registres.
-it is used for specific functions such as
----status register
----temers
----serial communication
-I/O ports
---ADC
-all the AVR ha at least 64bytes of I/O memory locations
-this 64 byte is called standard I/O memory.
-AVR with more than 32 pins(Atmega64,Atmega128etc),there are an
extended I/O memory .
-extended I/O memory is used for controlling the extra ports and
peripherals.
-I/O memory is called SFRs since each one is dedicated to a specific
function.
389
Internal data
SRAM
◦ Internal data is widely used for storing data and
parameters by AVR programmers and C compilers.
Generally that is called scratch pad.
◦ Each location of SRAM can be accessed by its
address.
◦ Each location is 8 bits wide.
◦ Size of data SRAM can vary form chip to chip

390
SRAM vs EEPROM
• AVR as an EEPROM memory that is used for storing
data.
• EEPROM does not lose it’s data when power id off
where as SRAM lose its data when power is on
• EEPROM is used for storing data that should rarely be
changed whereas the SRAM is used for storing data
and parameters that are changed frequently.
• In AVR data sheet EEPROM refers to EEPROM’s size
and SRAM is the internal SRAM’s size.
392
USING INSTRUCTIONS WITH DATA MEMORY

1.LDS(LoaD direct from data Space)


2.STS(STore direct to data Space)
3.IN
4.IN Vs LDS
5.OUT
6.MOV
LDS Rd,
; load Rd with the content of locaton K
K ;0≤d≤31
;K is an addr b / w $0000 to $FFFF

394
LDS:-
This instruction instruct CPU to copy one byte from a location in the data
memory to one of the GPRs.

LDS Rd,k ;load Rd with content of location k


;k is an address between $0000 to $FFFF

LDS R6,0x240 ;load R6 with content of location $240


LDS R0, 0x300 ; R0 = content of 0x0300
LDS R1, 0x302 ; R0 = content of 0x0302
ADD R1, R2 ; R1 = R1 + R2

396
397
STS K, Rr ; Store Rr into locaton K

; K is an addr b / w $0000 to
$FFFF

398
STS(Store direct To data space)

- this instruction tells the CPU to store the content of the GPR to an
address in the data memory.
Eg:-
STS 0x230,R25 ;store R25 to data space location 0x230
To add the content of location 0x300 to content of location 0x302 and
store the result in 0x303
LDS R16,0x300
LDS R17,0X302
ADD R16,R17
STS 0X303,R16
STP:JMP STP
SAMPLE PROGRAMS

Load the value 0x92 into location from 0x200 to 0x203 of the SRAM region
in the data memory.

LDI R20,0X99
STS 0X200,R20
STS 0X201,R20
STS 0X202,R20
STS 0X203,R20
/*
* STA.asm

*/
.INCLUDE "M32DEF.INC"
.ORG 00
LDI R16,0X99
0X212,R16
ST R16,0X85
S 0X213,R16
LDI R16,0X3F
0X214,R16
ST R16,0X63
S 0X215,R16
LDI NEXT

ST
S
LDI 402
IN instruction(IN from I/O location

IN
IN Rd,A ;load an I/O location to GPR
0<=d<=31
0<=A<=63
IN instruction tells the CPU to load one byte from an I/O register to GPR.
After the execution,GPR will have the same value as I/O register.
In IN instruction,the I/O registers are referred by their I/O address.($00 to
$3F)
IN Instruction

• -each location in the data memory has two addresses.


• - I/O address and data memory address
• -each location in the data memory has a unique address called data
memory address
• -each I/O register has a relative address in comparison to the
beginning of the I/O memory called I/O address.
405
IN Rd, A ; load an I/O location to the GPR
;0≤d≤31
;A is an I/O addr b / w $00 to $3F
;(0≤A≤63)

406
IN R19, 0x10 ; load R19 with location $10

IN R19, PIND ; load R19 with PIND

Which does the same as follows :


LDS R19, 0x30

407
IN R1, PIND ; load R1 with PIND
IN R2, PINB ; load R2 with PINB
ADD R1, ;R1=R1+R2
R2 ;Store R1 to $0300
STS 0x300,
R1

408
1. The CPU executes the IN instruction faster than LDS.
The IN lasts 1 machine cycle, where LDS lasts 2
machine cycles.

2. The IN is 2 byte instruction, where LDS is a 4 byte


instruction.

3. When we use the IN instruction we can use the names


of the I/O registers instead of their addresses.

4. The IN instruction is available in all of the AVRs,


where LDS is not implemented in some of the AVRs.

409
OUT A, Rr ; store register to I/O location
;0≤d≤31
;A is an I/O addr b / w $00 to $3F
;(0≤A≤63)

410
OUT (Out to I/O location)
OUT instruction tells the CPU to store GPR to the I/O register.

OUT POTRD,R10 ;copy the content of R10 into PORTD.


Eg:-

Pgm segment to copy PINC to PORTB.


IN R0,PINC
OUT PORTB,R0
LDI R20, 0xE6 ; load R20 with location $E6
OUT SPL, R20 ; out R19 to SPL

IN R0, PIND ; load R20 from PIND


OUT PORTA, R0 ; out R20 from R0

412
MOV Rd,
; Rd = Rr (copy Rr to Rd)
Rr ; Rd and Rr can be any of
GPRS

MOV R10, R20

413
MOV
-This instruction is used to copy the data among the GPR register to R0-
R31
MOV Rd,Rr
MOV R10,R20

Eg:-
Add the immediate value 0x19 with the content of memory location
0x220 and the result has to be stored in to 0x225
LDI R20,0x19
LDS R21,0x220
ADD R20,R21
STS 0X225,R20
INC
; increment the content of Rd by one
Rd ; 0≤d≤31

INC R2 ; R2 = R2 + 1

416
DEC
; decrement the content of Rd by one
Rd ; 0≤d≤31

DEC R2 ; R2 = R2 - 1

417
SUB Rd,
; Rd = Rd - Rr
Rr ; Rd and Rr can be any of
GPRS

418
 Complements (inverts) the content of Rd
and place the result back into the Rd
register.

419
AVR Status register
• Status register is an 8 bit register also known a
flag register.
• The bits C,Z,N,V,S, and H are called conditional
flag meaning that they indicate some
condition that result after an instruction is
executed.
421
Carry Flag C
• This flag is set whenever there is a carry out
from the D7 bit.
• This flag bit is affected after an 8 bit addition
or subtraction.
ZERO flag Z
• This flag reflect the result of an arithmetic or
logic operation .
• If the result is 0 then Z=1
• If the result is not = 0 then Z=0
Negative flag N
• Binary representation of signed numbers uses
D7 as the sign bit.
• Negative flag reflect the result of an
arithmetic operation .
• If the D7 bit of the result is 0, then N=0 and
the result is positive.
• If the D7 bit of the result is 1, then N=1 and
the result is negative.
Overflow Bit V
• This flag is set whenever the result of an
signed number operation is too large .
• Overflow flag is used to detect errors in signed
arithmetic operation .
Sign Bit S

• This flag is the result of exclusive ORing of N


&V flags.
Half carry flag H

• If there is a carry from D3 to D4 during ADD or


SUB operation ,this bit is set, otherwise it is
cleared.
• This bit is used by instruction that performs
BCD operation
• Another term(Auxiliary carry)
Bit copy storage

• Used with BLD (bit load) and BST(bit store)instructions for loading
and storing bit from one register to another.
Global Interrupt Enable

• Setting this bit enabled all the interrupt.


• Resetting this disables all interrupts
430
431
432
433
434
435
436
437
438
440
441
442
 Now you should able
to:◦ List the registers of AVR MCs.
◦ Examine the data memory of AVR MC.
◦ Perform simple operations, such as ADD and load,
and access internal RAM in the AVR MC.
◦ Explain the purpose of the status registers.

443
Thank
ou

444
What is an Embedded System?

• a microprocessor-based system
• built to control a function or range of functions
• not designed to be programmed by the end user in the
same way that a PC is
• Usually embedded as part of a complete
device including hardware and mechanical parts
System

SOFTWARE PROGRAM
#include <16f876a.h>
#use delay (clock=20000000)
#byte P O RT B = 6
main()
{
set_tris_b(0);
portb=255; //decimal
d e la y _ m s (1 0 0 0 );
portb=0x55; //hexadecimal
d e la y _ m s (1 0 0 0 );
p o rtb = 0 b 1 0 1 0 1 0 1 0 ; //binary
delay_ms(500);
}
Embedded system

• It is defined as a control system or computer system designed to


perform a specific task.

• ES ususally have only a single task or a very small number of


related tasks that they are programmed to perform.

• Eg:-air condetioner,microwave oven,fax machine etc.


• In general ,an ES

• -is a system built to perform its duty,completetly or partially


independent of human intervention.

• Is specially designed to perform a few tasks in the most efficient way.

• Interacts with physical elements in our environment,viz.controlling and


driving a motor,sensing temperature etc.
Characteristics of Embedded Systems

• Single-functioned: An embedded system


usually executes only one program, repeatedly
• Tightly constrained: All computing systems
have constraints on design metrics, but those
on embedded systems can be especially tight
• Reactive and real-time: Many embedded
systems must continually react to changes in
the system’s environment, and must compute
certain results in real time without delay
Microcontroller for embedded system
• Microprocessors and microcontrollers are widely used
in embedded system products
• An embedded system is controlled by its own internal
microprocessor (or microcontroller) as opposed to
external controller
• Printer is an example of embedded system, it does only
one task, taking data from the PC and print it
• In a mouse, there is a microcontroller which tracks the
mouse’s position and sends the information to the PC
• In an embedded system, typically only one application
software is burned into ROM
Role of MC in embedded market

• An ES is a computing device that does a specific focused job.

• Each Embedded application will have a processor and special


hardware to meet the specific requirement of the application
along with the embedded s/w that is executed by the processor
for meeting the specific requirement.
• In case of a MC ,it contains everything

• So the designer need not add any additional digital part(to make system
functional) to complete the system and thus the system is economic and
handy.

• The additional external parts always makes the system more expensive
and bulkier.

• So MC are mostly used for embedded system design


•.
• In an ES,the microcontroller’s ROM is burned with s/w for only one
application needed for the system.
Criteria for Choosing a Microcontroller

Microcontrollers come in many flavours and verieties from


different manufactures in different bit size,instruction set and
architecture.

-so to select a suitable MC for a specific application ,many factors


are to be taken into account.
Criteria for Choosing a Microcontroller

Crieteria for choosing a microcontroller are

1.Computing need(Criteria 1 : i t must meet the task at hand


effi ciently and effecti vely).

2.s/w development tool(Criteria 2 : how easy i t is to develop


products around it.)

3.Availability(Criteria 3 : its availability in needed quanti ti es both


now and in the future.)
Criteria for Choosing a Microcontroller

1)Computing need:-
-Microcontroller should meet the task at hand efficiently and economically.
-this can be analyzed with the following features
• ---Bit size(8 bit, 16 bit, or 32 bit)

----power consumption
----speed
-------Instruction set(RISC or CISC)
-----
Criteria for Choosing a Microcontroller

porgram storage architecture(Harward Architecture or Von-


Neuman)
---total internal and external memory
--internal EPROM
--number of I/O pins
----number of timers
----cost
---ease of upgradation to higher performance or low power
consumption.
---packaging(DIP or QFP[quad flat package])
Criteria for Choosing a Microcontroller
Criteria for Choosing a Microcontroller

2.s/w development tool(Criteria 2 : how easy i t is to develop


products around it)
-next crieteria for choosing a MC is the ease for developing the s/w for
product in way to save the development effort and implimentation time.
-availability of the following development tool is important such as
-----assembler
----Debugger
-----Compiler
---Simulator
Criteria for Choosing a Microcontroller

3.Availability:-
-the next criteria in choosing the microcontroller is its availability in the
required quantity both now and future.
459
 You should now be able
to:◦ Compare and contrast microcontrollers and
microprocessors.
◦ List some names of MCs.
◦ Discuss advantages of MCs for Ess
◦ Compare MPs with MCs.
◦ Discuss criteria for considering a MC.

460
 By the end of this session, you should be
able to:

◦ Discuss a brief history of AVR microcontroller.


◦ Disti nguish between Harvard architecture and von
• Neumann architecture.
◦ List features of AVR MCs.
◦ Draw a simplifi ed architecture of AVR MC.
◦ Compare and contrast of various members of the
• AVR family.

461
 AVR stand
for?
Advanced Virtual RISC,
the founders are Alf Egil Bogen Vegard
Wollan RISC

462
OVERVIEW OF AVR
MICROCONTROLLER

• AVR is developed in the year 1996 by Atmel Corporation(8-bit


RISC single chip microcontroller ) .
• The AT90S8515 was the first microcontroller which was based on AVR
architecture.
• The first MC to hit the commercial market was AT90S1200 in the
year 1997.
 The AVR was one of the first
microcontroller families to use on-chip
flash memory for program storage, as
opposed to one-time
programmable ROM, EPROM, or EEPROM used
by other microcontrollers at the time.

466
• Many kinds of AVR with different properties
• Except for AVR32 which is 32bit all AVRs are 8
bit meaning that the CPU can work on only 8
bit of data.
• Data larger than 8 bit is broken in to 8 bit
pieces
• Programs written for one family is not 100%
compatible for other family
• AVRs are classified into four
• Mega
• Tiny
• Special purpose
• Classic
• Mega family is widely used as they are
powerful.
• widely available and comes in Dip
packages
 The AVR is a modified Harvard architecture
machine where program and data is stored in
separate physical memory systems that
appear in different address spaces, but
having the ability to read data items from
program memory using special instructions.

469
470
 The Harvard architecture is a computer
architecture with physically separate
storage and signal pathways for
instructions and data.

 CPU can read both an instruction and data


from memory at the same time that makes
it faster.

471
 The term originated from the Harvard
Mark I relay-based computer, which
stored instructions on punched tape (24
bits wide) and data in electro-
mechanical counters.

472
473
474
In contrast with the Harvard architecture, the
von Neumann architecture has a single
storage structure to hold both instructions
and data.
The CPU can be either reading an instruction
or reading/writing data f r om/ t o the
memory because instructions and data use
the same bus system.

475
 The phrase Von Neumann architecture
derives name of the mathematician and
early computer scientist John von
Neumann.

476
 series of 8 - b i t RISC microcontrollers from Atmel.

 All AVR microcontrollers share same instruction set


and a basic CPU (Harvard) architecture.

 It has 32 8-Bit general purpose registers.


 Mostly instruction Execute in Single clock cycle.
Which makes it faster among 8 bit
microcontrollers.

 AVR was designed for efficient execution of compiled


C code.

477
 AVR is a family of 8 - b i t microcontrollers with
a large range of variants differing in:

- size of program-memory (flash)


- size of data RAM
- size of EEPROM memory
- number of I/O pins, Timers
-number of on-chip features such as
UART and ADC, USB etc

478
479
480
 All of the product numbers start with AT,
which stands for Atmet.
 Now, look at the product number, from left to
right, and
 find the biggest number that is power of 2.
 This number most probably
shows the amount of microcontroller’s ROM.

481
Eg:-
Atmega 1280,the biggest power of 2 is 128.
-so it has 128 Kbytes of ROM.
-ATtiny 44,the amount of memory is 4K.
-ATmega169 ,the amount of ROM is 16K.
 Classic AVR (AT90Sxxxx)
 Mega AVR (ATmegaxxxx)
 Tiny AVR (ATtinyxxxx)
 Special purpose AVR

* x x x x is the product
number

484
486
488
490
Special capabilities are:- LCD
controller. USB controller,
Ethernet controller etc
492
AVR with other Microcontrollers

Features 8051 family PIC family AVR family


Program ROM 8K 32K 32K
DATA RAM 256 BYTES 2K 2K
EEPROM NIL 256 BYTES 1K
TIMERS 3 4 3
I/O PINS 32 35 32
ARCHITECTURE CISC RISC RISC
PWM CHANNEL NIL INBUILT INBUILT
ADC NIL INBUILT INBUILT
494
495
 You should now able
to:◦ Discuss a brief history of AVR microcontroller.
◦ Disti nguish between Harvard architecture and von
Neumann architecture.

◦ List features of AVR MCs.

◦ Draw a simplifi ed architecture of AVR MC.

◦ Compare and contrast of various members of the AVR


family.

◦ Compare AVR with other MCs.


496
Flash memory(Not in Syllabus)

• Flash memory is a non-volatile memory chip used


for storage and for transfering data between a
personal computer (PC) and digital devices. It has
the ability to be electronically reprogrammed and
erased.
• It is often found in USB flash drives, MP3 players,
digital cameras and solid-state drives.
Flash Memory(Not specified in Syllabus)

• Flash memory is a type of electronically erasable


programmable read only memory (EEPROM), but
may also be a standalone memory storage device
such as a USB drive. EEPROM is a type of data
memory device using an electronic device to erase
or write digital data. Flash memory is a distinct
type of EEPROM, which is programmed and erased
in large blocks.
Flash memory &&EEPROM(Not specified in
Syllabus)

• Flash is just one type of EEPROM.


• Flash uses NAND-type memory, while EEPROM uses
NOR type.
• Flash is block-wise erasable, while EEPROM is byte-wise
erasable.
• Flash is constantly rewritten, while other EEPROMs are
seldom rewritten.
• Flash is used when large amounts are needed, while
EEPROM is used when only small amounts are needed.
 By the end of this session, you should be
able to:Basic Architecture of AVR
◦ List the registers of AVR MCs.
◦ Examine the data memory of AVR MC.
◦ Perform simple operations, such as ADD and load,
and access internal RAM in the AVR MC.
◦ Explain the purpose of the status registers.

500
Basics About AVR microcontroller.
-AVR microcontroller is an 8 bit , RISC single chip mc with Harvard
architecture that come with some std features
---on-chip ROM
----data RAM
---data EEPROM
---timers and I/O ports.
-it consists of 32,8 bit general purpose working registers.
AVR does not have registers like accumulator .
-so the operation can be performed b/w any of the registers and can be
stored in either of them.
-it follows Harvard architecture format .
-the processor equipped with saparate memories and buses for pgm and
data .
-so prefetching is possible.
-AVR can execute a million instructions per second if cycle frequency is
1MHz.
-higher is the frequency,higher will be the processing speed.
Basic AVR architecture

Atmega has four 8-bit i/o ports.


PORTA
PORTB
PORTC
PORTD
AVR-Basic Architecture

Oscillator:-
-calibrated with an internal oscillator for driving its clock.
-By default it is set to operate with a frequency of 1 MHz.
-maximum frequency is 8MHz.
-it also set with an external crystal oscillator with a maximum frequency of
16 MHz.
AVR-Basic Architecture

ADC interface:-
-it is equipped with an 8 channel ADC with resolution of 10 bits.
-ADC reads the analog i/p from a sensor or from any other sources and
converts it into digital information which is understandable by the
microcontroller.
AVR-Basic Architecture

Timers/Counters:
It consists of 2 8-bit and 1 16-bit imer/counter.
-timers are useful for creating time delays b/w two operations.
WDT:-
-it is present with internal oscillator.
-WDT continously monitors and reset the controller if the code gets stuck
at any execution action for more than a defined interval.
AVR-Basic Architecture

Interrupt:-
-it consists of 21 interrupt sources out of which 4 are external.
-the remaining are internal which support the peripherals like
USART,ADC,and timers etc.
USART:-
-USART interface is available for interfacing with external device
capable of communicating serially(data transmission bit by bit).
 Registers: Two types of registers
◦ GERNEL purpose & SPECIAL purpose registers

 GERNERAL purpose(GPRs)
◦ 32 general purpose registers having storage capacity of 8-Bits
◦ Named as R0,R1,R2 to R31 coupled directly with ALU.
◦ Register 0 to 15 & 16 to 31 are different.
◦ Can store both Data & Addresses.

 SPECIAL purpose: Three registers


◦ Program counter
◦ Stack Pointer
◦ Status Register
◦ etc

509
 There are 32 GPR
 All are of size 8 bit
 D0 is LSB
 D7 is MSB
 R0-R31 are located in the lowest
memory location of mem addr.

510
AVR-GPRS

• AVR contains 32 general purpose registers which are coupled directly


with ALU of CPU.
• AVR have many registers for arithmetic and
logic operation
• CPU uses many registers to store data
temporarily
• That information can be byte of data to be
processed or address pointing to the data to
be fetched
• AVR registers are 8 bit registers
• With an 8 bit data type , any data
larger than 8 bits must be broken into 8 bit chunks before it is processed
• In AVR there are 32 general purpose registers
• They are R0-R31and are located in the lowest
location of memory address($00-$1F)
• GPRs in AVR are the same as accumulator in
other microprocessor
• GPRs are used by all arithmetic and logic instruction.
ALU Instructions Involving GPRs
LDI Rd, K ; load Rd with
immediate value K
; d must be between 16 and
31

514
LDI R20, 0x25 ; load R20 with 0x25 (R20 = 0x25)

LDI R20, $25 ; load R20 with 0x25 (R20 = 0x25)

LDI R20, 0b00101001; load R20 with 0x25 (R20 =


0x25)

LDI R20, 37; load R20 with 0x25 (R20 = 0x25)

515
LDI R5, 0x25 ; why?

LDI R20, $fff ; why?

LDI R20, 0b001010011 ; why?

LDI R20, 270 ; why?

516
ADD Rd, Rr ; ADD Rr to Rd and store
the result in Rd.

517
LDI R16, 0x25 ; load 0x25 into
R16 LDI R17, 0x34 ; load 0x34
into R17 ADD R16, R17
Executing the above lines results R16=0x59.

518
/*
* adding.asm
* Created: 1 2 / 2 / 2 0 1 7 9:35:52
PM

*/

.INCLUDE "M32DEF.INC"
.ORG 00
LDI R16,0X25 ; load 0x25 into R16
LDI ; load 0x32 into R17
ADD R17,0X35 ; add value R17 to R16
R16, R17
NEXT: JMP
NEXT

519
520
521
DATA MEMORY

-AVR contains two kinds of memory space:-


------code memory
-----data memory
Code memory:-store user programs
Data memory:-store data
Data memory composed of three parts:-
----GPRS
-----I/O memory
----internal data SRAM

GPRs
◦ GPRs use 32 bytes of data memory space.
◦ They always take the address location $00 - $1F in
the data memory space, regardless of the AVR
chip number.

524
I/O MEMORY(SFRs)
-functions of I/O memory is fixed by the CPU designer at the time of
design.
-it is used for control of the microcontroller or peripheral..
-the AVR I/O memory is made of 8-bit registres.
-it is used for specific functions such as
----status register
----temers
----serial communication
-I/O ports
---ADC
-all the AVR ha at least 64bytes of I/O memory locations
-this 64 byte is called standard I/O memory.
-AVR with more than 32 pins(Atmega64,Atmega128etc),there are an
extended I/O memory .
-extended I/O memory is used for controlling the extra ports and
peripherals.
-I/O memory is called SFRs since each one is dedicated to a specific
function.
527
Internal data
SRAM
◦ Internal data is widely used for storing data and
parameters by AVR programmers and C compilers.
Generally that is called scratch pad.
◦ Each location of SRAM can be accessed by its
address.
◦ Each location is 8 bits wide.
◦ Size of data SRAM can vary form chip to chip

528
SRAM vs EEPROM
• AVR as an EEPROM memory that is used for storing
data.
• EEPROM does not lose it’s data when power id off
where as SRAM lose its data when power is on
• EEPROM is used for storing data that should rarely be
changed whereas the SRAM is used for storing data
and parameters that are changed frequently.
• In AVR data sheet EEPROM refers to EEPROM’s size
and SRAM is the internal SRAM’s size.
530
USING INSTRUCTIONS WITH DATA MEMORY

1.LDS(LoaD direct from data Space)


2.STS(STore direct to data Space)
3.IN
4.IN Vs LDS
5.OUT
6.MOV
LDS Rd,
; load Rd with the content of locaton K
K ;0≤d≤31
;K is an addr b / w $0000 to $FFFF

532
LDS:-
This instruction instruct CPU to copy one byte from a location in the data
memory to one of the GPRs.

LDS Rd,k ;load Rd with content of location k


;k is an address between $0000 to $FFFF

LDS R6,0x240 ;load R6 with content of location $240


LDS R0, 0x300 ; R0 = content of 0x0300
LDS R1, 0x302 ; R0 = content of 0x0302
ADD R1, R2 ; R1 = R1 + R2

534
535
STS K, Rr ; Store Rr into locaton K

; K is an addr b / w $0000 to
$FFFF

536
STS(Store direct To data space)

- this instruction tells the CPU to store the content of the GPR to an
address in the data memory.
Eg:-
STS 0x230,R25 ;store R25 to data space location 0x230
To add the content of location 0x300 to content of location 0x302 and
store the result in 0x303
LDS R16,0x300
LDS R17,0X302
ADD R16,R17
STS 0X303,R16
STP:JMP STP
SAMPLE PROGRAMS

Load the value 0x92 into location from 0x200 to 0x203 of the SRAM region
in the data memory.

LDI R20,0X99
STS 0X200,R20
STS 0X201,R20
STS 0X202,R20
STS 0X203,R20
/*
* STA.asm

*/
.INCLUDE "M32DEF.INC"
.ORG 00
LDI R16,0X99
0X212,R16
ST R16,0X85
S 0X213,R16
LDI R16,0X3F
0X214,R16
ST R16,0X63
S 0X215,R16
LDI NEXT

ST
S
LDI 540
IN instruction(IN from I/O location

IN
IN Rd,A ;load an I/O location to GPR
0<=d<=31
0<=A<=63
IN instruction tells the CPU to load one byte from an I/O register to GPR.
After the execution,GPR will have the same value as I/O register.
In IN instruction,the I/O registers are referred by their I/O address.($00 to
$3F)
IN Instruction

• -each location in the data memory has two addresses.


• - I/O address and data memory address
• -each location in the data memory has a unique address called data
memory address
• -each I/O register has a relative address in comparison to the
beginning of the I/O memory called I/O address.
543
IN Rd, A ; load an I/O location to the GPR
;0≤d≤31
;A is an I/O addr b / w $00 to $3F
;(0≤A≤63)

544
IN R19, 0x10 ; load R19 with location $10

IN R19, PIND ; load R19 with PIND

Which does the same as follows :


LDS R19, 0x30

545
IN R1, PIND ; load R1 with PIND
IN R2, PINB ; load R2 with PINB
ADD R1, ;R1=R1+R2
R2 ;Store R1 to $0300
STS 0x300,
R1

546
1. The CPU executes the IN instruction faster than LDS.
The IN lasts 1 machine cycle, where LDS lasts 2
machine cycles.

2. The IN is 2 byte instruction, where LDS is a 4 byte


instruction.

3. When we use the IN instruction we can use the names


of the I/O registers instead of their addresses.

4. The IN instruction is available in all of the AVRs,


where LDS is not implemented in some of the AVRs.

547
OUT A, Rr ; store register to I/O location
;0≤d≤31
;A is an I/O addr b / w $00 to $3F
;(0≤A≤63)

548
OUT (Out to I/O location)
OUT instruction tells the CPU to store GPR to the I/O register.

OUT POTRD,R10 ;copy the content of R10 into PORTD.


Eg:-

Pgm segment to copy PINC to PORTB.


IN R0,PINC
OUT PORTB,R0
LDI R20, 0xE6 ; load R20 with location $E6
OUT SPL, R20 ; out R19 to SPL

IN R0, PIND ; load R20 from PIND


OUT PORTA, R0 ; out R20 from R0

550
MOV Rd,
; Rd = Rr (copy Rr to Rd)
Rr ; Rd and Rr can be any of
GPRS

MOV R10, R20

551
MOV
-This instruction is used to copy the data among the GPR register to R0-
R31
MOV Rd,Rr
MOV R10,R20

Eg:-
Add the immediate value 0x19 with the content of memory location
0x220 and the result has to be stored in to 0x225
LDI R20,0x19
LDS R21,0x220
ADD R20,R21
STS 0X225,R20
INC
; increment the content of Rd by one
Rd ; 0≤d≤31

INC R2 ; R2 = R2 + 1

554
DEC
; decrement the content of Rd by one
Rd ; 0≤d≤31

DEC R2 ; R2 = R2 - 1

555
SUB Rd,
; Rd = Rd - Rr
Rr ; Rd and Rr can be any of
GPRS

556
 Complements (inverts) the content of Rd
and place the result back into the Rd
register.

557
AVR Status register
• Status register is an 8 bit register also known a
flag register.
• The bits C,Z,N,V,S, and H are called conditional
flag meaning that they indicate some
condition that result after an instruction is
executed.
559
Carry Flag C
• This flag is set whenever there is a carry out
from the D7 bit.
• This flag bit is affected after an 8 bit addition
or subtraction.
ZERO flag Z
• This flag reflect the result of an arithmetic or
logic operation .
• If the result is 0 then Z=1
• If the result is not = 0 then Z=0
Negative flag N
• Binary representation of signed numbers uses
D7 as the sign bit.
• Negative flag reflect the result of an
arithmetic operation .
• If the D7 bit of the result is 0, then N=0 and
the result is positive.
• If the D7 bit of the result is 1, then N=1 and
the result is negative.
Overflow Bit V
• This flag is set whenever the result of an
signed number operation is too large .
• Overflow flag is used to detect errors in signed
arithmetic operation .
Sign Bit S

• This flag is the result of exclusive ORing of N


&V flags.
Half carry flag H

• If there is a carry from D3 to D4 during ADD or


SUB operation ,this bit is set, otherwise it is
cleared.
• This bit is used by instruction that performs
BCD operation
• Another term(Auxiliary carry)
Bit copy storage

• Used with BLD (bit load) and BST(bit store)instructions for loading
and storing bit from one register to another.
Global Interrupt Enable

• Setting this bit enabled all the interrupt.


• Resetting this disables all interrupts
568
569
570
571
572
573
574
575
576
578
579
580
 Now you should able
to:◦ List the registers of AVR MCs.
◦ Examine the data memory of AVR MC.
◦ Perform simple operations, such as ADD and load,
and access internal RAM in the AVR MC.
◦ Explain the purpose of the status registers.

581
Thank
ou

582

You might also like