0% found this document useful (0 votes)
5 views324 pages

All Assembley Notes For Embedded System

The document is a lecture on microcontrollers and microprocessors presented by Eng George Mugala at The Copperbelt University. It covers the introduction to microcontrollers, their applications, structures, differences from microprocessors, and the principles of operation. The content emphasizes the significance of microcontrollers in embedded systems and outlines various technical aspects such as memory structure and instruction flow.

Uploaded by

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

All Assembley Notes For Embedded System

The document is a lecture on microcontrollers and microprocessors presented by Eng George Mugala at The Copperbelt University. It covers the introduction to microcontrollers, their applications, structures, differences from microprocessors, and the principles of operation. The content emphasizes the significance of microcontrollers in embedded systems and outlines various technical aspects such as memory structure and instruction flow.

Uploaded by

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

Introduction to Microcontroller/Microprocessor

Lecture 1
Embedded Systems

Eng George Mugala

THE COPPERBELT UNIVERSITY SCHOOL OF ENGINEERING

August 26, 2017

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Introduction to
SCHOOL
Microcontroller/MicroprocessorLecture
OF ENGINEERING) 1Embedded
August Systems
26, 2017 1 / 20
OUTLINE

1 Introduction to Microcontrollers

2 Examples of applications

3 What makes these devices smart

4 Structure of a microcontroller and Microprocessors

5 Instruction Sequency flow

6 Structure of a microcontroller

7 Differences between a Microprocessor and a microcontroller

8 Microcontroller and Processor End

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Introduction to
SCHOOL
Microcontroller/MicroprocessorLecture
OF ENGINEERING) 1Embedded
August Systems
26, 2017 2 / 20
OUTLINE

1 Introduction to Microcontrollers

2 Examples of applications

3 What makes these devices smart

4 Structure of a microcontroller and Microprocessors

5 Instruction Sequency flow

6 Structure of a microcontroller

7 Differences between a Microprocessor and a microcontroller

8 Microcontroller and Processor End

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Introduction to
SCHOOL
Microcontroller/MicroprocessorLecture
OF ENGINEERING) 1Embedded
August Systems
26, 2017 3 / 20
Introduction to Microcontrollers
Micro controllers have only been with us for a few decades but their
impact (direct or indirect) on our lives is profound. Usually these are
supposed to be just data processors performing exhaustive numeric
operations. But their presence is unnoticed at most of the places like;

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Introduction to
SCHOOL
Microcontroller/MicroprocessorLecture
OF ENGINEERING) 1Embedded
August Systems
26, 2017 4 / 20
Introduction to Microcontrollers
Micro controllers have only been with us for a few decades but their
impact (direct or indirect) on our lives is profound. Usually these are
supposed to be just data processors performing exhaustive numeric
operations. But their presence is unnoticed at most of the places like;

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Introduction to
SCHOOL
Microcontroller/MicroprocessorLecture
OF ENGINEERING) 1Embedded
August Systems
26, 2017 4 / 20
OUTLINE

1 Introduction to Microcontrollers

2 Examples of applications

3 What makes these devices smart

4 Structure of a microcontroller and Microprocessors

5 Instruction Sequency flow

6 Structure of a microcontroller

7 Differences between a Microprocessor and a microcontroller

8 Microcontroller and Processor End

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Introduction to
SCHOOL
Microcontroller/MicroprocessorLecture
OF ENGINEERING) 1Embedded
August Systems
26, 2017 5 / 20
Introduction to Microcontrollers

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Introduction to
SCHOOL
Microcontroller/MicroprocessorLecture
OF ENGINEERING) 1Embedded
August Systems
26, 2017 6 / 20
Introduction to Microcontrollers

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Introduction to
SCHOOL
Microcontroller/MicroprocessorLecture
OF ENGINEERING) 1Embedded
August Systems
26, 2017 6 / 20
OUTLINE

1 Introduction to Microcontrollers

2 Examples of applications

3 What makes these devices smart

4 Structure of a microcontroller and Microprocessors

5 Instruction Sequency flow

6 Structure of a microcontroller

7 Differences between a Microprocessor and a microcontroller

8 Microcontroller and Processor End

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Introduction to
SCHOOL
Microcontroller/MicroprocessorLecture
OF ENGINEERING) 1Embedded
August Systems
26, 2017 7 / 20
What inside them Makes these devices smart??

What inside them Makes these devices smart??


The answer is Microcontrollers

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Introduction to
SCHOOL
Microcontroller/MicroprocessorLecture
OF ENGINEERING) 1Embedded
August Systems
26, 2017 8 / 20
OUTLINE

1 Introduction to Microcontrollers

2 Examples of applications

3 What makes these devices smart

4 Structure of a microcontroller and Microprocessors

5 Instruction Sequency flow

6 Structure of a microcontroller

7 Differences between a Microprocessor and a microcontroller

8 Microcontroller and Processor End

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Introduction to
SCHOOL
Microcontroller/MicroprocessorLecture
OF ENGINEERING) 1Embedded
August Systems
26, 2017 9 / 20
Microprocessors and Microcontroller structures
Microprocessor structure(CPU).

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Introduction to
SCHOOL
Microcontroller/MicroprocessorLecture
OF ENGINEERING) 1Embedded
August 26,
Systems
2017 10 / 20
Microprocessor structure(CPU)- Externally Mapped

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Introduction to
SCHOOL
Microcontroller/MicroprocessorLecture
OF ENGINEERING) 1Embedded
August 26,
Systems
2017 11 / 20
OUTLINE

1 Introduction to Microcontrollers

2 Examples of applications

3 What makes these devices smart

4 Structure of a microcontroller and Microprocessors

5 Instruction Sequency flow

6 Structure of a microcontroller

7 Differences between a Microprocessor and a microcontroller

8 Microcontroller and Processor End

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Introduction to
SCHOOL
Microcontroller/MicroprocessorLecture
OF ENGINEERING) 1Embedded
August 26,
Systems
2017 12 / 20
Instruction Cycle

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Introduction to
SCHOOL
Microcontroller/MicroprocessorLecture
OF ENGINEERING) 1Embedded
August 26,
Systems
2017 13 / 20
OUTLINE

1 Introduction to Microcontrollers

2 Examples of applications

3 What makes these devices smart

4 Structure of a microcontroller and Microprocessors

5 Instruction Sequency flow

6 Structure of a microcontroller

7 Differences between a Microprocessor and a microcontroller

8 Microcontroller and Processor End

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Introduction to
SCHOOL
Microcontroller/MicroprocessorLecture
OF ENGINEERING) 1Embedded
August 26,
Systems
2017 14 / 20
Structure of a microcontroller/Microcomputer
A digital computer having microprocessor as the CPU along with I/O
devices and memory is known as microcomputer.

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Introduction to
SCHOOL
Microcontroller/MicroprocessorLecture
OF ENGINEERING) 1Embedded
August 26,
Systems
2017 15 / 20
Microcontroller as a Microcomputer

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Introduction to
SCHOOL
Microcontroller/MicroprocessorLecture
OF ENGINEERING) 1Embedded
August 26,
Systems
2017 16 / 20
OUTLINE

1 Introduction to Microcontrollers

2 Examples of applications

3 What makes these devices smart

4 Structure of a microcontroller and Microprocessors

5 Instruction Sequency flow

6 Structure of a microcontroller

7 Differences between a Microprocessor and a microcontroller

8 Microcontroller and Processor End

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Introduction to
SCHOOL
Microcontroller/MicroprocessorLecture
OF ENGINEERING) 1Embedded
August 26,
Systems
2017 17 / 20
Differences between a Microprocessor and a
microcontroller

Microprocessor is a single chip CPU,

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Introduction to
SCHOOL
Microcontroller/MicroprocessorLecture
OF ENGINEERING) 1Embedded
August 26,
Systems
2017 18 / 20
Differences between a Microprocessor and a
microcontroller

Microprocessor is a single chip CPU,


microcontroller contains, a CPU and much of the remaining
circuitry of a complete microcomputer system in a single chip

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Introduction to
SCHOOL
Microcontroller/MicroprocessorLecture
OF ENGINEERING) 1Embedded
August 26,
Systems
2017 18 / 20
Differences between a Microprocessor and a
microcontroller

Microprocessor is a single chip CPU,


microcontroller contains, a CPU and much of the remaining
circuitry of a complete microcomputer system in a single chip
Microcontroller includes RAM, ROM, serial and parallel interface,
timer, interrupt schedule circuitry (in addition to CPU) in a single
chip.

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Introduction to
SCHOOL
Microcontroller/MicroprocessorLecture
OF ENGINEERING) 1Embedded
August 26,
Systems
2017 18 / 20
Differences between a Microprocessor and a
microcontroller

Microprocessor is a single chip CPU,


microcontroller contains, a CPU and much of the remaining
circuitry of a complete microcomputer system in a single chip
Microcontroller includes RAM, ROM, serial and parallel interface,
timer, interrupt schedule circuitry (in addition to CPU) in a single
chip.
Microprocessor instruction sets are ”processing intensive”, implying
powerful addressing modes with instructions catering to large
volumes of data. Their instructions operate on nibbles, bytes, etc.
Microcontrollers have instruction sets catering to the control of
inputs and outputs. Their instructions operate also on a single bit.
E.g., a motor may be turned ON and OFF by a 1-bit output port.

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Introduction to
SCHOOL
Microcontroller/MicroprocessorLecture
OF ENGINEERING) 1Embedded
August 26,
Systems
2017 18 / 20
OUTLINE

1 Introduction to Microcontrollers

2 Examples of applications

3 What makes these devices smart

4 Structure of a microcontroller and Microprocessors

5 Instruction Sequency flow

6 Structure of a microcontroller

7 Differences between a Microprocessor and a microcontroller

8 Microcontroller and Processor End

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Introduction to
SCHOOL
Microcontroller/MicroprocessorLecture
OF ENGINEERING) 1Embedded
August 26,
Systems
2017 19 / 20
THANK YOU

THANK YOU

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Introduction to
SCHOOL
Microcontroller/MicroprocessorLecture
OF ENGINEERING) 1Embedded
August 26,
Systems
2017 20 / 20
Principle of Operation of
Microcontroller/Microprocessor
Lecturer 2
Embedded Systems

Eng George Mugala

THE COPPERBELT UNIVERSITY SCHOOL OF ENGINEERING

August 28, 2017

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Principle of Operation
SCHOOLofOF
Microcontroller/MicroprocessorLecturer
ENGINEERING) August2Embedded
28, 2017 Systems
1 / 60
OUTLINE
1 basic principle of operations
2 microprocessor and memory
3 Memory Structure
4 Bus lines
5 Types of Memory
6 Memory Structure and Organization
7 AVR Architecture Memory Layout
8 FLASH MEMORY LAYOUT
9 DATA MEMORY
10 GENERAL PURPOSE REGISTERS
11 SPECIAL FUNCTION REGISTERS
12 Memory Mapping
13 PORTS,PINS,DDR
14 REGISTERS
Eng George Mugala ( THE COPPERBELT UNIVERSITY
Principle of Operation
SCHOOLofOF
Microcontroller/MicroprocessorLecturer
ENGINEERING) August2Embedded
28, 2017 Systems
2 / 60
OUTLINE
1 basic principle of operations
2 microprocessor and memory
3 Memory Structure
4 Bus lines
5 Types of Memory
6 Memory Structure and Organization
7 AVR Architecture Memory Layout
8 FLASH MEMORY LAYOUT
9 DATA MEMORY
10 GENERAL PURPOSE REGISTERS
11 SPECIAL FUNCTION REGISTERS
12 Memory Mapping
13 PORTS,PINS,DDR
14 REGISTERS
Eng George Mugala ( THE COPPERBELT UNIVERSITY
Principle of Operation
SCHOOLofOF
Microcontroller/MicroprocessorLecturer
ENGINEERING) August2Embedded
28, 2017 Systems
3 / 60
Microprocessor/Microcontrollers and Memory

At the most primitive level, a computer consists of a microprocessor,


which executes instructions, and a memory, for storing those
instructions (as well as other data).

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Principle of Operation
SCHOOLofOF
Microcontroller/MicroprocessorLecturer
ENGINEERING) August2Embedded
28, 2017 Systems
4 / 60
OUTLINE
1 basic principle of operations
2 microprocessor and memory
3 Memory Structure
4 Bus lines
5 Types of Memory
6 Memory Structure and Organization
7 AVR Architecture Memory Layout
8 FLASH MEMORY LAYOUT
9 DATA MEMORY
10 GENERAL PURPOSE REGISTERS
11 SPECIAL FUNCTION REGISTERS
12 Memory Mapping
13 PORTS,PINS,DDR
14 REGISTERS
Eng George Mugala ( THE COPPERBELT UNIVERSITY
Principle of Operation
SCHOOLofOF
Microcontroller/MicroprocessorLecturer
ENGINEERING) August2Embedded
28, 2017 Systems
5 / 60
Microprocessor/Microcontrollers and Memory

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Principle of Operation
SCHOOLofOF
Microcontroller/MicroprocessorLecturer
ENGINEERING) August2Embedded
28, 2017 Systems
6 / 60
OUTLINE
1 basic principle of operations
2 microprocessor and memory
3 Memory Structure
4 Bus lines
5 Types of Memory
6 Memory Structure and Organization
7 AVR Architecture Memory Layout
8 FLASH MEMORY LAYOUT
9 DATA MEMORY
10 GENERAL PURPOSE REGISTERS
11 SPECIAL FUNCTION REGISTERS
12 Memory Mapping
13 PORTS,PINS,DDR
14 REGISTERS
Eng George Mugala ( THE COPPERBELT UNIVERSITY
Principle of Operation
SCHOOLofOF
Microcontroller/MicroprocessorLecturer
ENGINEERING) August2Embedded
28, 2017 Systems
7 / 60
Memory Structure
One byte is 8bits and 2byte is
Address in HEX CellSize 16 bits which is also called a
0x00 byte/2bytes(word) word
0x01 byte/2bytes(word) MemoSize =
0x02 byte/2bytes(word) NumberofCellsXCellSize
0x03 byte/2bytes(word) Units in which memory is
0x04 byte/2bytes(word) measured is bytes but when
0x05 byte/2bytes(word) I 1024bytes(210 ) = 1kilobyte
0x06 byte/2bytes(word)
I 1024Kilobyte(210 ) = 1Mbyte
I 1024Mbyte(210 ) = 1Gbyte
0x07 byte/2bytes(word) I 1024Gbytes(210 ) =
0x08 byte/2bytes(word) 1Terabyte
0x09 byte/2bytes(word) Two Operations with Memory
0x0A byte/2bytes(word)

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Principle of Operation
SCHOOLofOF
Microcontroller/MicroprocessorLecturer
ENGINEERING) August2Embedded
28, 2017 Systems
8 / 60
Memory Structure
One byte is 8bits and 2byte is
Address in HEX CellSize 16 bits which is also called a
0x00 byte/2bytes(word) word
0x01 byte/2bytes(word) MemoSize =
0x02 byte/2bytes(word) NumberofCellsXCellSize
0x03 byte/2bytes(word) Units in which memory is
0x04 byte/2bytes(word) measured is bytes but when
0x05 byte/2bytes(word) I 1024bytes(210 ) = 1kilobyte
0x06 byte/2bytes(word)
I 1024Kilobyte(210 ) = 1Mbyte
I 1024Mbyte(210 ) = 1Gbyte
0x07 byte/2bytes(word) I 1024Gbytes(210 ) =
0x08 byte/2bytes(word) 1Terabyte
0x09 byte/2bytes(word) Two Operations with Memory
0x0A byte/2bytes(word) I Write; specify Address,Value

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Principle of Operation
SCHOOLofOF
Microcontroller/MicroprocessorLecturer
ENGINEERING) August2Embedded
28, 2017 Systems
8 / 60
Memory Structure
One byte is 8bits and 2byte is
Address in HEX CellSize 16 bits which is also called a
0x00 byte/2bytes(word) word
0x01 byte/2bytes(word) MemoSize =
0x02 byte/2bytes(word) NumberofCellsXCellSize
0x03 byte/2bytes(word) Units in which memory is
0x04 byte/2bytes(word) measured is bytes but when
0x05 byte/2bytes(word) I 1024bytes(210 ) = 1kilobyte
0x06 byte/2bytes(word)
I 1024Kilobyte(210 ) = 1Mbyte
I 1024Mbyte(210 ) = 1Gbyte
0x07 byte/2bytes(word) I 1024Gbytes(210 ) =
0x08 byte/2bytes(word) 1Terabyte
0x09 byte/2bytes(word) Two Operations with Memory
0x0A byte/2bytes(word) I Write; specify Address,Value
I Read; Specify Address and
returns a value

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Principle of Operation
SCHOOLofOF
Microcontroller/MicroprocessorLecturer
ENGINEERING) August2Embedded
28, 2017 Systems
8 / 60
How to Store Data in Memory
how to store 1byte? at a
particular location
I data stored in binary. for Address in HEX CellSize(1byte)
easy reading it is displayed in
Hexadecimal
0x00 CB
I 1bytesay = 11001011 to be 0x01 16
stored at address 0x00 0x02 39
I ChangedtoHex = CB to be 0x03
stored at address 0x00 0x04
how to store 2bytes at a 0x05
particular location say address 0x06
0x01?? 0x07
lets say lets store 5689 in 0x08
memory 0x09
converted to binary 5689 = 0x0A
0001011001110001=1639 HEX

I
Eng George Mugala ( THE COPPERBELT UNIVERSITY
Principle of Operation
SCHOOLofOF
Microcontroller/MicroprocessorLecturer
ENGINEERING) August2Embedded
28, 2017 Systems
9 / 60
BIG INDIAN AND LITTLE INDIAN POLICY

Storing 2bytes by first entering Most Significant byte and Least


Significant Byte 16MSB39LSB = Big Indian
Storing 2bytes by first entering Least Significant and then Most
Significant byte and 39LSB16MSB = Little Indian
Data is stored in Consective memory location e.g at 0x01 then
0x02 to store two bytes
How do you store string Hello in Memory Location??

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Principle of Operation
SCHOOLofOF
Microcontroller/MicroprocessorLecturer
ENGINEERING) August2Embedded
28, 2017 Systems
10 / 60
OUTLINE
1 basic principle of operations
2 microprocessor and memory
3 Memory Structure
4 Bus lines
5 Types of Memory
6 Memory Structure and Organization
7 AVR Architecture Memory Layout
8 FLASH MEMORY LAYOUT
9 DATA MEMORY
10 GENERAL PURPOSE REGISTERS
11 SPECIAL FUNCTION REGISTERS
12 Memory Mapping
13 PORTS,PINS,DDR
14 REGISTERS
Eng George Mugala ( THE COPPERBELT UNIVERSITY
Principle of Operation
SCHOOLofOF
Microcontroller/MicroprocessorLecturer
ENGINEERING) August2Embedded
28, 2017 Systems
11 / 60
principle of operation as seen from Diagram
Address Bus
These wires are controlled by the microprocessor to select a
particular location in memory for reading or writing. The example
uses a memory chip that has 15 address wires. Since each wire has
two states (it can be a digital one or a zero), 2 to the 15th power
locations are possible. 215 is precisely 32,768 locations; thus, the
system is said to have ”32K” of memory (1K = 1024 bytes).
Data Bus These wires are used to pass data between the
microprocessor and the memory. When data is written to the
memory, the microprocessor drives the data bus; when data is read
from the memory, memory drives the bus. In our example, there
are eight data wires (or bits). These wires can transfer one of 28 ,
or 256, different values per transaction. The data size of 8 bits is
commonly referred to as a byte. (This jargon gets worse; a data
size of 4 bits is frequently referred to as a nybble.)

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Principle of Operation
SCHOOLofOF
Microcontroller/MicroprocessorLecturer
ENGINEERING) August2Embedded
28, 2017 Systems
12 / 60
Microprocessor principle of Operations

Read/Write Control Line


This single wire is driven by the microprocessor to control the
function of the memory. If the read/write control line is asserted as
a logical one, i.e., “true”, then the memory performs a “read”
operation. If it is asserted as a logic zero, i.e., “false” then the
memory performs a “write” operation.
Memory Enable Control Line.
This wire, also called the E clock, connects to the enable circuitry
of the memory. When the memory is enabled, it performs either a
read or write operation as determined by the read/write line

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Principle of Operation
SCHOOLofOF
Microcontroller/MicroprocessorLecturer
ENGINEERING) August2Embedded
28, 2017 Systems
13 / 60
OUTLINE
1 basic principle of operations
2 microprocessor and memory
3 Memory Structure
4 Bus lines
5 Types of Memory
6 Memory Structure and Organization
7 AVR Architecture Memory Layout
8 FLASH MEMORY LAYOUT
9 DATA MEMORY
10 GENERAL PURPOSE REGISTERS
11 SPECIAL FUNCTION REGISTERS
12 Memory Mapping
13 PORTS,PINS,DDR
14 REGISTERS
Eng George Mugala ( THE COPPERBELT UNIVERSITY
Principle of Operation
SCHOOLofOF
Microcontroller/MicroprocessorLecturer
ENGINEERING) August2Embedded
28, 2017 Systems
14 / 60
Types of Memory

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Principle of Operation
SCHOOLofOF
Microcontroller/MicroprocessorLecturer
ENGINEERING) August2Embedded
28, 2017 Systems
15 / 60
Types of Memory

RAM
I SRAM- SRAM (static RAM) is random access memory (RAM) that
retains data bits in its memory as long as power is being supplied.
I DRAM -Dynamic random-access memory (DRAM) is a type of
random-access memory that stores each bit of data in a separate
capacitor within an integrated circuit. The capacitor can be either
charged or discharged; these two states are taken to represent the
two values of a bit, conventionally called 0 and 1.

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Principle of Operation
SCHOOLofOF
Microcontroller/MicroprocessorLecturer
ENGINEERING) August2Embedded
28, 2017 Systems
15 / 60
Types of Memory

RAM
I SRAM- SRAM (static RAM) is random access memory (RAM) that
retains data bits in its memory as long as power is being supplied.
I DRAM -Dynamic random-access memory (DRAM) is a type of
random-access memory that stores each bit of data in a separate
capacitor within an integrated circuit. The capacitor can be either
charged or discharged; these two states are taken to represent the
two values of a bit, conventionally called 0 and 1.
ROM
I EPROM-An EPROM (rarely EROM), or erasable programmable
read-only memory, is a type of memory chip that retains its data
when its power supply is switched off. The memory can be erased
and reused.Erasure is caused by shining an intense ultraviolet light
through a window that is designed into the memory chip

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Principle of Operation
SCHOOLofOF
Microcontroller/MicroprocessorLecturer
ENGINEERING) August2Embedded
28, 2017 Systems
15 / 60
Types of Memory

EEPROM-EEPROM (also written E2PROM and pronounced


”e-e-prom”, ”double-e-prom” or ”e-squared-prom”) stands for
electrically erasable programmable read-only memory and is a type
of non-volatile memory used in computers and other electronic
devices to store relatively small amounts of data but allowing
individual bytes to be erased and reprogrammed.

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Principle of Operation
SCHOOLofOF
Microcontroller/MicroprocessorLecturer
ENGINEERING) August2Embedded
28, 2017 Systems
16 / 60
Types of Memory

FLASH MEMORY-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.

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Principle of Operation
SCHOOLofOF
Microcontroller/MicroprocessorLecturer
ENGINEERING) August2Embedded
28, 2017 Systems
17 / 60
OUTLINE
1 basic principle of operations
2 microprocessor and memory
3 Memory Structure
4 Bus lines
5 Types of Memory
6 Memory Structure and Organization
7 AVR Architecture Memory Layout
8 FLASH MEMORY LAYOUT
9 DATA MEMORY
10 GENERAL PURPOSE REGISTERS
11 SPECIAL FUNCTION REGISTERS
12 Memory Mapping
13 PORTS,PINS,DDR
14 REGISTERS
Eng George Mugala ( THE COPPERBELT UNIVERSITY
Principle of Operation
SCHOOLofOF
Microcontroller/MicroprocessorLecturer
ENGINEERING) August2Embedded
28, 2017 Systems
18 / 60
Von Neuman Vs Harvard Architecture

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Principle of Operation
SCHOOLofOF
Microcontroller/MicroprocessorLecturer
ENGINEERING) August2Embedded
28, 2017 Systems
19 / 60
OUTLINE
1 basic principle of operations
2 microprocessor and memory
3 Memory Structure
4 Bus lines
5 Types of Memory
6 Memory Structure and Organization
7 AVR Architecture Memory Layout
8 FLASH MEMORY LAYOUT
9 DATA MEMORY
10 GENERAL PURPOSE REGISTERS
11 SPECIAL FUNCTION REGISTERS
12 Memory Mapping
13 PORTS,PINS,DDR
14 REGISTERS
Eng George Mugala ( THE COPPERBELT UNIVERSITY
Principle of Operation
SCHOOLofOF
Microcontroller/MicroprocessorLecturer
ENGINEERING) August2Embedded
28, 2017 Systems
20 / 60
AVR Architecture Memory Layout-ATMEGA 32

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Principle of Operation
SCHOOLofOF
Microcontroller/MicroprocessorLecturer
ENGINEERING) August2Embedded
28, 2017 Systems
21 / 60
OUTLINE
1 basic principle of operations
2 microprocessor and memory
3 Memory Structure
4 Bus lines
5 Types of Memory
6 Memory Structure and Organization
7 AVR Architecture Memory Layout
8 FLASH MEMORY LAYOUT
9 DATA MEMORY
10 GENERAL PURPOSE REGISTERS
11 SPECIAL FUNCTION REGISTERS
12 Memory Mapping
13 PORTS,PINS,DDR
14 REGISTERS
Eng George Mugala ( THE COPPERBELT UNIVERSITY
Principle of Operation
SCHOOLofOF
Microcontroller/MicroprocessorLecturer
ENGINEERING) August2Embedded
28, 2017 Systems
22 / 60
FLASH MEMORY LAYOUT-EXAMPLE ATMEGA-32

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Principle of Operation
SCHOOLofOF
Microcontroller/MicroprocessorLecturer
ENGINEERING) August2Embedded
28, 2017 Systems
23 / 60
OUTLINE
1 basic principle of operations
2 microprocessor and memory
3 Memory Structure
4 Bus lines
5 Types of Memory
6 Memory Structure and Organization
7 AVR Architecture Memory Layout
8 FLASH MEMORY LAYOUT
9 DATA MEMORY
10 GENERAL PURPOSE REGISTERS
11 SPECIAL FUNCTION REGISTERS
12 Memory Mapping
13 PORTS,PINS,DDR
14 REGISTERS
Eng George Mugala ( THE COPPERBELT UNIVERSITY
Principle of Operation
SCHOOLofOF
Microcontroller/MicroprocessorLecturer
ENGINEERING) August2Embedded
28, 2017 Systems
24 / 60
DATA MEMORY

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Principle of Operation
SCHOOLofOF
Microcontroller/MicroprocessorLecturer
ENGINEERING) August2Embedded
28, 2017 Systems
25 / 60
OUTLINE
1 basic principle of operations
2 microprocessor and memory
3 Memory Structure
4 Bus lines
5 Types of Memory
6 Memory Structure and Organization
7 AVR Architecture Memory Layout
8 FLASH MEMORY LAYOUT
9 DATA MEMORY
10 GENERAL PURPOSE REGISTERS
11 SPECIAL FUNCTION REGISTERS
12 Memory Mapping
13 PORTS,PINS,DDR
14 REGISTERS
Eng George Mugala ( THE COPPERBELT UNIVERSITY
Principle of Operation
SCHOOLofOF
Microcontroller/MicroprocessorLecturer
ENGINEERING) August2Embedded
28, 2017 Systems
26 / 60
GENERAL PURPOSE REGISTERS

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Principle of Operation
SCHOOLofOF
Microcontroller/MicroprocessorLecturer
ENGINEERING) August2Embedded
28, 2017 Systems
27 / 60
OUTLINE
1 basic principle of operations
2 microprocessor and memory
3 Memory Structure
4 Bus lines
5 Types of Memory
6 Memory Structure and Organization
7 AVR Architecture Memory Layout
8 FLASH MEMORY LAYOUT
9 DATA MEMORY
10 GENERAL PURPOSE REGISTERS
11 SPECIAL FUNCTION REGISTERS
12 Memory Mapping
13 PORTS,PINS,DDR
14 REGISTERS
Eng George Mugala ( THE COPPERBELT UNIVERSITY
Principle of Operation
SCHOOLofOF
Microcontroller/MicroprocessorLecturer
ENGINEERING) August2Embedded
28, 2017 Systems
28 / 60
SPECIAL PURPOSE REGISTERS

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Principle of Operation
SCHOOLofOF
Microcontroller/MicroprocessorLecturer
ENGINEERING) August2Embedded
28, 2017 Systems
29 / 60
OUTLINE
1 basic principle of operations
2 microprocessor and memory
3 Memory Structure
4 Bus lines
5 Types of Memory
6 Memory Structure and Organization
7 AVR Architecture Memory Layout
8 FLASH MEMORY LAYOUT
9 DATA MEMORY
10 GENERAL PURPOSE REGISTERS
11 SPECIAL FUNCTION REGISTERS
12 Memory Mapping
13 PORTS,PINS,DDR
14 REGISTERS
Eng George Mugala ( THE COPPERBELT UNIVERSITY
Principle of Operation
SCHOOLofOF
Microcontroller/MicroprocessorLecturer
ENGINEERING) August2Embedded
28, 2017 Systems
30 / 60
Memory Mapping-Example ATMEGA 32

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Principle of Operation
SCHOOLofOF
Microcontroller/MicroprocessorLecturer
ENGINEERING) August2Embedded
28, 2017 Systems
31 / 60
OUTLINE
1 basic principle of operations
2 microprocessor and memory
3 Memory Structure
4 Bus lines
5 Types of Memory
6 Memory Structure and Organization
7 AVR Architecture Memory Layout
8 FLASH MEMORY LAYOUT
9 DATA MEMORY
10 GENERAL PURPOSE REGISTERS
11 SPECIAL FUNCTION REGISTERS
12 Memory Mapping
13 PORTS,PINS,DDR
14 REGISTERS
Eng George Mugala ( THE COPPERBELT UNIVERSITY
Principle of Operation
SCHOOLofOF
Microcontroller/MicroprocessorLecturer
ENGINEERING) August2Embedded
28, 2017 Systems
32 / 60
PORTS,PINS,DDR

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Principle of Operation
SCHOOLofOF
Microcontroller/MicroprocessorLecturer
ENGINEERING) August2Embedded
28, 2017 Systems
33 / 60
PORTS,PINS,DATA DIRECTION REGISTERS OF
ATMEGA 32

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Principle of Operation
SCHOOLofOF
Microcontroller/MicroprocessorLecturer
ENGINEERING) August2Embedded
28, 2017 Systems
34 / 60
PORTS,PINS,DATA DIRECTION REGISTERS OF
ATMEGA 32

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Principle of Operation
SCHOOLofOF
Microcontroller/MicroprocessorLecturer
ENGINEERING) August2Embedded
28, 2017 Systems
35 / 60
PORTS,PINS,DATA DIRECTION REGISTERS OF
ATMEGA 32

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Principle of Operation
SCHOOLofOF
Microcontroller/MicroprocessorLecturer
ENGINEERING) August2Embedded
28, 2017 Systems
36 / 60
PORTS,PINS,DATA DIRECTION REGISTERS OF
ATMEGA 32

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Principle of Operation
SCHOOLofOF
Microcontroller/MicroprocessorLecturer
ENGINEERING) August2Embedded
28, 2017 Systems
37 / 60
PORTS,PINS,DATA DIRECTION REGISTERS OF
ATMEGA 32

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Principle of Operation
SCHOOLofOF
Microcontroller/MicroprocessorLecturer
ENGINEERING) August2Embedded
28, 2017 Systems
38 / 60
PORTS,PINS,DATA DIRECTION REGISTERS OF
ATMEGA 32

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Principle of Operation
SCHOOLofOF
Microcontroller/MicroprocessorLecturer
ENGINEERING) August2Embedded
28, 2017 Systems
39 / 60
PORTS,PINS,DATA DIRECTION REGISTERS OF
ATMEGA 32

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Principle of Operation
SCHOOLofOF
Microcontroller/MicroprocessorLecturer
ENGINEERING) August2Embedded
28, 2017 Systems
40 / 60
PORTS,PINS,DATA DIRECTION REGISTERS OF
ATMEGA 32

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Principle of Operation
SCHOOLofOF
Microcontroller/MicroprocessorLecturer
ENGINEERING) August2Embedded
28, 2017 Systems
41 / 60
PORTS,PINS,DATA DIRECTION REGISTERS OF
ATMEGA 32

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Principle of Operation
SCHOOLofOF
Microcontroller/MicroprocessorLecturer
ENGINEERING) August2Embedded
28, 2017 Systems
42 / 60
PORTS,PINS,DATA DIRECTION REGISTERS OF
ATMEGA 32

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Principle of Operation
SCHOOLofOF
Microcontroller/MicroprocessorLecturer
ENGINEERING) August2Embedded
28, 2017 Systems
43 / 60
PULL UP RESISTORS

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Principle of Operation
SCHOOLofOF
Microcontroller/MicroprocessorLecturer
ENGINEERING) August2Embedded
28, 2017 Systems
44 / 60
PULL UP RESISTORS

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Principle of Operation
SCHOOLofOF
Microcontroller/MicroprocessorLecturer
ENGINEERING) August2Embedded
28, 2017 Systems
45 / 60
PIN READING

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Principle of Operation
SCHOOLofOF
Microcontroller/MicroprocessorLecturer
ENGINEERING) August2Embedded
28, 2017 Systems
46 / 60
PIN READING

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Principle of Operation
SCHOOLofOF
Microcontroller/MicroprocessorLecturer
ENGINEERING) August2Embedded
28, 2017 Systems
47 / 60
OUTLINE
1 basic principle of operations
2 microprocessor and memory
3 Memory Structure
4 Bus lines
5 Types of Memory
6 Memory Structure and Organization
7 AVR Architecture Memory Layout
8 FLASH MEMORY LAYOUT
9 DATA MEMORY
10 GENERAL PURPOSE REGISTERS
11 SPECIAL FUNCTION REGISTERS
12 Memory Mapping
13 PORTS,PINS,DDR
14 REGISTERS
Eng George Mugala ( THE COPPERBELT UNIVERSITY
Principle of Operation
SCHOOLofOF
Microcontroller/MicroprocessorLecturer
ENGINEERING) August2Embedded
28, 2017 Systems
48 / 60
GENERAL PURPOSE REGISTERS

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Principle of Operation
SCHOOLofOF
Microcontroller/MicroprocessorLecturer
ENGINEERING) August2Embedded
28, 2017 Systems
49 / 60
I/O-REGISTERS/SPECIAL FUNCTION REGISTERS

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Principle of Operation
SCHOOLofOF
Microcontroller/MicroprocessorLecturer
ENGINEERING) August2Embedded
28, 2017 Systems
50 / 60
REGISTER (SP)

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Principle of Operation
SCHOOLofOF
Microcontroller/MicroprocessorLecturer
ENGINEERING) August2Embedded
28, 2017 Systems
51 / 60
REGISTER (SP)

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Principle of Operation
SCHOOLofOF
Microcontroller/MicroprocessorLecturer
ENGINEERING) August2Embedded
28, 2017 Systems
52 / 60
REGISTER (STACK POINTER)

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Principle of Operation
SCHOOLofOF
Microcontroller/MicroprocessorLecturer
ENGINEERING) August2Embedded
28, 2017 Systems
53 / 60
REGISTER (PROGRAM COUNTER)

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Principle of Operation
SCHOOLofOF
Microcontroller/MicroprocessorLecturer
ENGINEERING) August2Embedded
28, 2017 Systems
54 / 60
REGISTER (STATUS REGISTERS)

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Principle of Operation
SCHOOLofOF
Microcontroller/MicroprocessorLecturer
ENGINEERING) August2Embedded
28, 2017 Systems
55 / 60
REGISTER (STATUS REGISTERS)

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Principle of Operation
SCHOOLofOF
Microcontroller/MicroprocessorLecturer
ENGINEERING) August2Embedded
28, 2017 Systems
56 / 60
REGISTER (STATUS REGISTERS)

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Principle of Operation
SCHOOLofOF
Microcontroller/MicroprocessorLecturer
ENGINEERING) August2Embedded
28, 2017 Systems
57 / 60
REGISTER (STATUS REGISTERS)

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Principle of Operation
SCHOOLofOF
Microcontroller/MicroprocessorLecturer
ENGINEERING) August2Embedded
28, 2017 Systems
58 / 60
REGISTER (STATUS REGISTERS)

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Principle of Operation
SCHOOLofOF
Microcontroller/MicroprocessorLecturer
ENGINEERING) August2Embedded
28, 2017 Systems
59 / 60
REGISTER (STATUS REGISTERS)

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Principle of Operation
SCHOOLofOF
Microcontroller/MicroprocessorLecturer
ENGINEERING) August2Embedded
28, 2017 Systems
60 / 60
Computer Languages Classification
Lecturer 3
Embedded Systems

Eng George Mugala

THE COPPERBELT UNIVERSITY SCHOOL OF ENGINEERING

December 17, 2017

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Computer Languages
SCHOOL ClassificationLecturer
OF ENGINEERING)3Embedded Systems
December 17, 2017 1 / 43
OUTLINE
1 Computer Languages Classification
2 Explanations
3 Middle Level language
4 High Level Language
5 Summary
6 Introduction to Assembley Language
7 Avr Assembley
8 AVR-Assembley language Template format
9 PORT PROCESSING INSTRUCTIONS
10 Avr Programming Examples and Code Explanations
11 Solutions

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Computer Languages
SCHOOL ClassificationLecturer
OF ENGINEERING)3Embedded Systems
December 17, 2017 2 / 43
OUTLINE
1 Computer Languages Classification
2 Explanations
3 Middle Level language
4 High Level Language
5 Summary
6 Introduction to Assembley Language
7 Avr Assembley
8 AVR-Assembley language Template format
9 PORT PROCESSING INSTRUCTIONS
10 Avr Programming Examples and Code Explanations
11 Solutions

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Computer Languages
SCHOOL ClassificationLecturer
OF ENGINEERING)3Embedded Systems
December 17, 2017 3 / 43
Computer Languages

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Computer Languages
SCHOOL ClassificationLecturer
OF ENGINEERING)3Embedded Systems
December 17, 2017 4 / 43
OUTLINE
1 Computer Languages Classification
2 Explanations
3 Middle Level language
4 High Level Language
5 Summary
6 Introduction to Assembley Language
7 Avr Assembley
8 AVR-Assembley language Template format
9 PORT PROCESSING INSTRUCTIONS
10 Avr Programming Examples and Code Explanations
11 Solutions

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Computer Languages
SCHOOL ClassificationLecturer
OF ENGINEERING)3Embedded Systems
December 17, 2017 5 / 43
Computer Languages
.
Low level language

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Computer Languages
SCHOOL ClassificationLecturer
OF ENGINEERING)3Embedded Systems
December 17, 2017 6 / 43
Computer Languages
.
Low level language
I Only language which can be understood by a computer

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Computer Languages
SCHOOL ClassificationLecturer
OF ENGINEERING)3Embedded Systems
December 17, 2017 6 / 43
Computer Languages
.
Low level language
I Only language which can be understood by a computer
I Binary(0,1) is example of low level language

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Computer Languages
SCHOOL ClassificationLecturer
OF ENGINEERING)3Embedded Systems
December 17, 2017 6 / 43
Computer Languages
.
Low level language
I Only language which can be understood by a computer
I Binary(0,1) is example of low level language
I Computer can directly understand binary language

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Computer Languages
SCHOOL ClassificationLecturer
OF ENGINEERING)3Embedded Systems
December 17, 2017 6 / 43
Computer Languages
.
Low level language
I Only language which can be understood by a computer
I Binary(0,1) is example of low level language
I Computer can directly understand binary language
I As the CPU directly understands the binary language instructions, it
does not requires any translater

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Computer Languages
SCHOOL ClassificationLecturer
OF ENGINEERING)3Embedded Systems
December 17, 2017 6 / 43
Computer Languages
.
Low level language
I Only language which can be understood by a computer
I Binary(0,1) is example of low level language
I Computer can directly understand binary language
I As the CPU directly understands the binary language instructions, it
does not requires any translater
I CPU directly starts executing the binary language instructions, and
takes very less time to execute the instructions as it does not
requires any translation
I Advantages
F A computer can easily understand the low level language
F Low level language instructions are executed directly without any
translation
F Low level language instructions require very less time for thier
execution

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Computer Languages
SCHOOL ClassificationLecturer
OF ENGINEERING)3Embedded Systems
December 17, 2017 6 / 43
Disadvantages of Low level Languages

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Computer Languages
SCHOOL ClassificationLecturer
OF ENGINEERING)3Embedded Systems
December 17, 2017 7 / 43
Disadvantages of Low level Languages

Low level language instructions are very difficult to use and


understand

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Computer Languages
SCHOOL ClassificationLecturer
OF ENGINEERING)3Embedded Systems
December 17, 2017 7 / 43
Disadvantages of Low level Languages

Low level language instructions are very difficult to use and


understand
Low level language instructions are machine dependent, that means
a program written for a

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Computer Languages
SCHOOL ClassificationLecturer
OF ENGINEERING)3Embedded Systems
December 17, 2017 7 / 43
Disadvantages of Low level Languages

Low level language instructions are very difficult to use and


understand
Low level language instructions are machine dependent, that means
a program written for a
particular machine does not executes on other machine.

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Computer Languages
SCHOOL ClassificationLecturer
OF ENGINEERING)3Embedded Systems
December 17, 2017 7 / 43
Disadvantages of Low level Languages

Low level language instructions are very difficult to use and


understand
Low level language instructions are machine dependent, that means
a program written for a
particular machine does not executes on other machine.
In low level language, there is more chance for errors and it is very
difficult to find errors, debug and modify

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Computer Languages
SCHOOL ClassificationLecturer
OF ENGINEERING)3Embedded Systems
December 17, 2017 7 / 43
OUTLINE
1 Computer Languages Classification
2 Explanations
3 Middle Level language
4 High Level Language
5 Summary
6 Introduction to Assembley Language
7 Avr Assembley
8 AVR-Assembley language Template format
9 PORT PROCESSING INSTRUCTIONS
10 Avr Programming Examples and Code Explanations
11 Solutions

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Computer Languages
SCHOOL ClassificationLecturer
OF ENGINEERING)3Embedded Systems
December 17, 2017 8 / 43
Middle level language Introduction

Middle Level language introduction


Middle level language is a computer language in which the
instructions are created using symbols such as letters, digits and
special characters. Assembly language is an example of middle level
language. In assembly language, we use predefined words called
mnemonics. Binary code instructions in low level language are
replaced with mnemonics and operands in middle level language.
But computer can not understand mnemonics, so we use a
translator called Assembler to translate mnemonics into binary
language. Assembler is a translator which takes assembly code as
input and produces machine code as output. That means,
computer can not understand middle level language, so it needs to
be translated into low level language to make it understandable by
the computer. Assembler is used to translate middle level language
to low level language.

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Computer Languages
SCHOOL ClassificationLecturer
OF ENGINEERING)3Embedded Systems
December 17, 2017 9 / 43
Advantages
ADVANTAGES
I Writing instructions in middle level language is easier than writing
instructions in low level language.
I Middle level language is more readable compared to low level
language.
I Easy to understand, find errors and modify.
Disadvantages
I Middle level language is specific to a particular machine architecture,
that means it is machine dependent.
I Middle level language needs to be translated into low level language.
I Middle level language executes slower compared to low level
language.

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Computer Languages
SCHOOL ClassificationLecturer
OF ENGINEERING)3Embedded Systems
December 17, 2017 10 / 43
OUTLINE
1 Computer Languages Classification
2 Explanations
3 Middle Level language
4 High Level Language
5 Summary
6 Introduction to Assembley Language
7 Avr Assembley
8 AVR-Assembley language Template format
9 PORT PROCESSING INSTRUCTIONS
10 Avr Programming Examples and Code Explanations
11 Solutions

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Computer Languages
SCHOOL ClassificationLecturer
OF ENGINEERING)3Embedded Systems
December 17, 2017 11 / 43
Introduction to High level Language
High level language is a computer language which can be understood by
the users. High level language is very similar to the human languages
and have a set of grammar rules that are used to make instructions
more easily. Every high level language have a set of predefined words
known as Keywords and a set of rules known as Syntax to create
instructions. High level language is more easier to understand for the
users but the computer can not understand it. High level language
needs to be converted into low level language to make it understandable
by the computer. We use Compiler or interpretor to convert high level
language to low level language. Languages like COBOL, FORTRAN,
BASIC, C ,C++, JAVA etc., are the examples of high level languages.
All these programming languages use human understandable language
like english to write program instructions. These instructions are
converted to low level language by the compiler so that it can be
understood by the computer.

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Computer Languages
SCHOOL ClassificationLecturer
OF ENGINEERING)3Embedded Systems
December 17, 2017 12 / 43
Advantages
Advantages
I Writing instructions in high level language is more easier
I High level language is more readable and understandable
I The programs created using high level language runs on different
machines with little change or no change
I Easy to understand, create programs, find errors and modify
Disadvantages
I High level language needs to be translated to low level language.
I High level language executes slower compared to middle and low
level languages

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Computer Languages
SCHOOL ClassificationLecturer
OF ENGINEERING)3Embedded Systems
December 17, 2017 13 / 43
OUTLINE
1 Computer Languages Classification
2 Explanations
3 Middle Level language
4 High Level Language
5 Summary
6 Introduction to Assembley Language
7 Avr Assembley
8 AVR-Assembley language Template format
9 PORT PROCESSING INSTRUCTIONS
10 Avr Programming Examples and Code Explanations
11 Solutions

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Computer Languages
SCHOOL ClassificationLecturer
OF ENGINEERING)3Embedded Systems
December 17, 2017 14 / 43
Summary of Discussion

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Computer Languages
SCHOOL ClassificationLecturer
OF ENGINEERING)3Embedded Systems
December 17, 2017 15 / 43
OUTLINE
1 Computer Languages Classification
2 Explanations
3 Middle Level language
4 High Level Language
5 Summary
6 Introduction to Assembley Language
7 Avr Assembley
8 AVR-Assembley language Template format
9 PORT PROCESSING INSTRUCTIONS
10 Avr Programming Examples and Code Explanations
11 Solutions

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Computer Languages
SCHOOL ClassificationLecturer
OF ENGINEERING)3Embedded Systems
December 17, 2017 16 / 43
Introduction to Assembley Language
.

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Computer Languages
SCHOOL ClassificationLecturer
OF ENGINEERING)3Embedded Systems
December 17, 2017 17 / 43
OUTLINE
1 Computer Languages Classification
2 Explanations
3 Middle Level language
4 High Level Language
5 Summary
6 Introduction to Assembley Language
7 Avr Assembley
8 AVR-Assembley language Template format
9 PORT PROCESSING INSTRUCTIONS
10 Avr Programming Examples and Code Explanations
11 Solutions

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Computer Languages
SCHOOL ClassificationLecturer
OF ENGINEERING)3Embedded Systems
December 17, 2017 18 / 43
Avr Assembley Programing
The process of developing a program consists of the following steps

1 Select a particular AVR chip and construct the program flow chart
2 Write the program(Using Note pad,Avr studio or any development
software)
3 Assemble the Program
4 Simulate or Emulate the program to see whether or not it works
5 Program the AVr(Feeds what you have written into the actual chip)

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Computer Languages
SCHOOL ClassificationLecturer
OF ENGINEERING)3Embedded Systems
December 17, 2017 19 / 43
OUTLINE
1 Computer Languages Classification
2 Explanations
3 Middle Level language
4 High Level Language
5 Summary
6 Introduction to Assembley Language
7 Avr Assembley
8 AVR-Assembley language Template format
9 PORT PROCESSING INSTRUCTIONS
10 Avr Programming Examples and Code Explanations
11 Solutions

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Computer Languages
SCHOOL ClassificationLecturer
OF ENGINEERING)3Embedded Systems
December 17, 2017 20 / 43
AVR-Assembley language Template format

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Computer Languages
SCHOOL ClassificationLecturer
OF ENGINEERING)3Embedded Systems
December 17, 2017 21 / 43
OUTLINE
1 Computer Languages Classification
2 Explanations
3 Middle Level language
4 High Level Language
5 Summary
6 Introduction to Assembley Language
7 Avr Assembley
8 AVR-Assembley language Template format
9 PORT PROCESSING INSTRUCTIONS
10 Avr Programming Examples and Code Explanations
11 Solutions

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Computer Languages
SCHOOL ClassificationLecturer
OF ENGINEERING)3Embedded Systems
December 17, 2017 22 / 43
SOME USEFUL PORT PROCESSING INSTRUCTIONS
Sbi ioregister,bit; this instruction sets bit mentioned high
I Sbi PORTB,0 set portb0 high
Cbi ioregister,bit;this instruction clears bit mentioned and it
becomes low
I cbi PORTB,0 set portb0 low
Sbic ioregister,bit; this tests a bit in an I/O register and skips the
next instruction if the bit is cleared
I Sbic PIND,0 ; skip the next instruction if the bit in PORTD 0 is
cleared
Sbis ioregister,bit; this tests a bit in an I/O register and skips the
next instruction if the bit is set
I Sbis PIND,0 ; skip the next instruction if the bit in PORTD 0 is set
ldi Register,number ; this loads the immediate number into the
register;note this for register R16-R31

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Computer Languages
SCHOOL ClassificationLecturer
OF ENGINEERING)3Embedded Systems
December 17, 2017 23 / 43
PORT PROCESSING INSTRUCTIONS
CONTINUATION
clr Register; this clears the contents of Register. it moves zero into
it
ser Register; this sets the contents of Register. it moves ones into
it.this works with Register R16-R31
out ioreg,Register; this moves the number out from a register and
moves it into IOregister.
in Register,ioreg; this copies the number from I/O Register into a
working register

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Computer Languages
SCHOOL ClassificationLecturer
OF ENGINEERING)3Embedded Systems
December 17, 2017 24 / 43
OUTLINE
1 Computer Languages Classification
2 Explanations
3 Middle Level language
4 High Level Language
5 Summary
6 Introduction to Assembley Language
7 Avr Assembley
8 AVR-Assembley language Template format
9 PORT PROCESSING INSTRUCTIONS
10 Avr Programming Examples and Code Explanations
11 Solutions

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Computer Languages
SCHOOL ClassificationLecturer
OF ENGINEERING)3Embedded Systems
December 17, 2017 25 / 43
Avr Programming Examples and Explanations
1 Example 1
Write a Simple program in AVR STUDIO to Program an ATMEGA32
to constantly Switch on an LED connected to PORTB 0.
I Develop the flow chart
I Write the code in avr studio 6
I Simulate in Avr Studio
I Load the Generated HEX file in proteus to test functionality

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Computer Languages
SCHOOL ClassificationLecturer
OF ENGINEERING)3Embedded Systems
December 17, 2017 26 / 43
OUTLINE
1 Computer Languages Classification
2 Explanations
3 Middle Level language
4 High Level Language
5 Summary
6 Introduction to Assembley Language
7 Avr Assembley
8 AVR-Assembley language Template format
9 PORT PROCESSING INSTRUCTIONS
10 Avr Programming Examples and Code Explanations
11 Solutions

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Computer Languages
SCHOOL ClassificationLecturer
OF ENGINEERING)3Embedded Systems
December 17, 2017 27 / 43
Solution to Question One

flow chart

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Computer Languages
SCHOOL ClassificationLecturer
OF ENGINEERING)3Embedded Systems
December 17, 2017 28 / 43
Eng George Mugala ( THE COPPERBELT UNIVERSITY
Computer Languages
SCHOOL ClassificationLecturer
OF ENGINEERING)3Embedded Systems
December 17, 2017 29 / 43
PUSH BUTTON
Testing Inputs
Controlling Outputs

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Computer Languages
SCHOOL ClassificationLecturer
OF ENGINEERING)3Embedded Systems
December 17, 2017 30 / 43
Testing Inputs Controlling SevenSegmentDisplay Using
Indirect Addressing
1 Write a simple program in AVR studio 6.0 in which a Button
connected PC0 when pressed causes a Zero to be displayed on the
Seven Segment display connected to PORTD

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Computer Languages
SCHOOL ClassificationLecturer
OF ENGINEERING)3Embedded Systems
December 17, 2017 31 / 43
Parametor Initialization

R20 = 0b00111111 contains code for Zero

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Computer Languages
SCHOOL ClassificationLecturer
OF ENGINEERING)3Embedded Systems
December 17, 2017 32 / 43
Main Code

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Computer Languages
SCHOOL ClassificationLecturer
OF ENGINEERING)3Embedded Systems
December 17, 2017 33 / 43
Proteus Simulation

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Computer Languages
SCHOOL ClassificationLecturer
OF ENGINEERING)3Embedded Systems
December 17, 2017 34 / 43
Modified Code

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Computer Languages
SCHOOL ClassificationLecturer
OF ENGINEERING)3Embedded Systems
December 17, 2017 35 / 43
Modified code

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Computer Languages
SCHOOL ClassificationLecturer
OF ENGINEERING)3Embedded Systems
December 17, 2017 36 / 43
Modified programe to display One

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Computer Languages
SCHOOL ClassificationLecturer
OF ENGINEERING)3Embedded Systems
December 17, 2017 37 / 43
TIMERS AND COUNTERS
Timing without a timer
Write a program to blink on and off an LED connected to PORTDO
every one second

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Computer Languages
SCHOOL ClassificationLecturer
OF ENGINEERING)3Embedded Systems
December 17, 2017 38 / 43
solution

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Computer Languages
SCHOOL ClassificationLecturer
OF ENGINEERING)3Embedded Systems
December 17, 2017 39 / 43
Continuation

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Computer Languages
SCHOOL ClassificationLecturer
OF ENGINEERING)3Embedded Systems
December 17, 2017 40 / 43
Continuation

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Computer Languages
SCHOOL ClassificationLecturer
OF ENGINEERING)3Embedded Systems
December 17, 2017 41 / 43
Explanation
Since the Atmega32 has an internal clock frequency of 1MHz, 1MHz
clock means 1000000 cycles per second. So 1 cycle equals 1/1000000
seconds or 1us. To get 1 second delay, you need 1000000 cycles of 1us,
so it means that you have to create an algorithm of 1000000 cycles.
Building on the example, a 1 sec delay @ 1MHz clock would be as
shown above in the written code

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Computer Languages
SCHOOL ClassificationLecturer
OF ENGINEERING)3Embedded Systems
December 17, 2017 42 / 43
Code Explanation
In this case there is the internal loop Delay3 that is 4 cycles long
because DEC=1, NOP=1 and BRNE=2 when jumping to Delay3.
So, 4 cycles repeated 250 times (the value of dly3) are 1000 cycles
or 1000us = 1ms.
Then the loop Delay2 repeats the Delay3 125 times (the value of
dly2). So the accumulated delay in this case is 125ms.
And finally, the loop Delay1 repeats the Delay2 8 times (the value
of dly1). So the accumulated delay in this case is 1000ms or 1
second

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Computer Languages
SCHOOL ClassificationLecturer
OF ENGINEERING)3Embedded Systems
December 17, 2017 43 / 43
Introduction to Instruction Set Architecture
Lecture 3
Embedded Systems

Eng George Mugala

THE COPPERBELT UNIVERSITY SCHOOL OF ENGINEERING

October 9, 2017

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Introduction to
SCHOOL
Instruction
OF Set
ENGINEERING)
ArchitectureLecture 3 Embedded
October
Systems
9, 2017 1 / 27
OUTLINE

1 Introduction to ISA

2 kinds of instruction

3 Addressing Modes

4 Data Indirect with Displacement LDD/STD

5 Data Indirect LD/ST

6 Data Indirect LD/ST

7 Data Indirect with Pre-decrement

8 Data Indirect with Post-increment

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Introduction to
SCHOOL
Instruction
OF Set
ENGINEERING)
ArchitectureLecture 3 Embedded
October
Systems
9, 2017 2 / 27
Introduction to Instruction Set Architecture

Instruction Set Architecture- Description of the computer from


view of the programmer/compiler
There are 2 types of Instruction Set Design
CISC
I Complex Instruction Set Computer
I A single instruction performs complex operation involving several
action
I A single instruction performs a complex operation involving several
actions
RISC
I Reduced Instruction Set Computer
I Each Instruction performs only a single operation

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Introduction to
SCHOOL
Instruction
OF Set
ENGINEERING)
ArchitectureLecture 3 Embedded
October
Systems
9, 2017 3 / 27
OUTLINE

1 Introduction to ISA

2 kinds of instruction

3 Addressing Modes

4 Data Indirect with Displacement LDD/STD

5 Data Indirect LD/ST

6 Data Indirect LD/ST

7 Data Indirect with Pre-decrement

8 Data Indirect with Post-increment

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Introduction to
SCHOOL
Instruction
OF Set
ENGINEERING)
ArchitectureLecture 3 Embedded
October
Systems
9, 2017 4 / 27
Instruction Set of a RISC

Instruction Set Architecture


Each instruction Set Architecture has the instruction Manual
The ISA Manual Includes specification of

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Introduction to
SCHOOL
Instruction
OF Set
ENGINEERING)
ArchitectureLecture 3 Embedded
October
Systems
9, 2017 5 / 27
Instruction Set of a RISC

Instruction Set Architecture


Each instruction Set Architecture has the instruction Manual
The ISA Manual Includes specification of
1 Different types of Instructions available(Instruction Set)

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Introduction to
SCHOOL
Instruction
OF Set
ENGINEERING)
ArchitectureLecture 3 Embedded
October
Systems
9, 2017 5 / 27
Instruction Set of a RISC

Instruction Set Architecture


Each instruction Set Architecture has the instruction Manual
The ISA Manual Includes specification of
1 Different types of Instructions available(Instruction Set)
2 How operands are specified(Addressing Modes)

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Introduction to
SCHOOL
Instruction
OF Set
ENGINEERING)
ArchitectureLecture 3 Embedded
October
Systems
9, 2017 5 / 27
Instruction Set of a RISC

Instruction Set Architecture


Each instruction Set Architecture has the instruction Manual
The ISA Manual Includes specification of
1 Different types of Instructions available(Instruction Set)
2 How operands are specified(Addressing Modes)
3 What each instruction looks like(Instruction format)

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Introduction to
SCHOOL
Instruction
OF Set
ENGINEERING)
ArchitectureLecture 3 Embedded
October
Systems
9, 2017 5 / 27
OUTLINE

1 Introduction to ISA

2 kinds of instruction

3 Addressing Modes

4 Data Indirect with Displacement LDD/STD

5 Data Indirect LD/ST

6 Data Indirect LD/ST

7 Data Indirect with Pre-decrement

8 Data Indirect with Post-increment

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Introduction to
SCHOOL
Instruction
OF Set
ENGINEERING)
ArchitectureLecture 3 Embedded
October
Systems
9, 2017 6 / 27
Kinds of Instructions

1 Arithmetic/logic instruction
I Add,subract,Multiply,Divide,Compare
I OR,AND,NOT, XOR
I Shift(Left/right,arithmetic/logical),rotate

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Introduction to
SCHOOL
Instruction
OF Set
ENGINEERING)
ArchitectureLecture 3 Embedded
October
Systems
9, 2017 7 / 27
Kinds of Instructions

1 Arithmetic/logic instruction
I Add,subract,Multiply,Divide,Compare
I OR,AND,NOT, XOR
I Shift(Left/right,arithmetic/logical),rotate
2 Data transfer Instruction
I Load(Copy data to a Register from Memory)
I Store(Copy Data to a Memory location from a Register)
I Move
3 Control transfer functions
I Jump,Conditional Branch,function call,Return
4 Other type of instructions
I e.g halt instruction

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Introduction to
SCHOOL
Instruction
OF Set
ENGINEERING)
ArchitectureLecture 3 Embedded
October
Systems
9, 2017 7 / 27
ADD,SUBTRACT,MULTIPLY,DIVIDE,COMPARE
EXAMPLES IN AVR STUDIO 6
Add,Multiply,Subtract,Divide numbers found in Registers and place
results in R16
I R16 = 5, R17 = 10

I
Eng George Mugala ( THE COPPERBELT UNIVERSITY
Introduction to
SCHOOL
Instruction
OF Set
ENGINEERING)
ArchitectureLecture 3 Embedded
October
Systems
9, 2017 8 / 27
CONTENTS OF THE REGISTER AFTER
COMPILATION

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Introduction to
SCHOOL
Instruction
OF Set
ENGINEERING)
ArchitectureLecture 3 Embedded
October
Systems
9, 2017 9 / 27
RESULTS OF R16 AFTER ADDITION

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Introduction to
SCHOOL
Instruction
OF Set
ENGINEERING)
ArchitectureLecture 3 Embedded
October
Systems
9, 2017 10 / 27
OR,AND,NOT, XOR IN AVR STUDIO 6
Question, Using the And Operator show the effect of the And
Operation between PORTB = 0b11100011 and
Registervalue = 0b00000000 Show the status of the ports after the
and Operation in Avr studio.

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Introduction to
SCHOOL
Instruction
OF Set
ENGINEERING)
ArchitectureLecture 3 Embedded
October
Systems
9, 2017 11 / 27
Shift Left/right,arithmetic/logical),rotate
Question, Using the And Operator show the effect of the And
Operation between PORTB = 0b11100011 and
Registervalue = 0b00000000 Show the status of the ports after the
and Operation in Avr studio.

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Introduction to
SCHOOL
Instruction
OF Set
ENGINEERING)
ArchitectureLecture 3 Embedded
October
Systems
9, 2017 12 / 27
OUTLINE

1 Introduction to ISA

2 kinds of instruction

3 Addressing Modes

4 Data Indirect with Displacement LDD/STD

5 Data Indirect LD/ST

6 Data Indirect LD/ST

7 Data Indirect with Pre-decrement

8 Data Indirect with Post-increment

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Introduction to
SCHOOL
Instruction
OF Set
ENGINEERING)
ArchitectureLecture 3 Embedded
October
Systems
9, 2017 13 / 27
Addressing Modes
Operands to an instruction
I Source: Input value to an instruction
I Destination: Where results go
Addressing Modes
I How the source of operand to an instruction is specified
An operand can be either in
I in a Memory Location
I in a Register

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Introduction to
SCHOOL
Instruction
OF Set
ENGINEERING)
ArchitectureLecture 3 Embedded
October
Systems
9, 2017 14 / 27
Operand in Registers

Types of Addressing Modes


1 Register Direct Addressing Mode
Operand is specified in the specified general purpose Registers
Example: if we take R0,R1,R2 e.t.c as general purpose registers
then example of an Instruction
ADD R1,R2,R3 / R1 = R3+R2

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Introduction to
SCHOOL
Instruction
OF Set
ENGINEERING)
ArchitectureLecture 3 Embedded
October
Systems
9, 2017 15 / 27
Operand in Registers

Types of Addressing Modes


1 Register Direct Addressing Mode
Operand is specified in the specified general purpose Registers
Example: if we take R0,R1,R2 e.t.c as general purpose registers
then example of an Instruction
ADD R1,R2,R3 / R1 = R3+R2
2 Immediate Addressing Modes
Operand value is included within the instruction
ADD R1 R2,7 /R1 = R2+7

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Introduction to
SCHOOL
Instruction
OF Set
ENGINEERING)
ArchitectureLecture 3 Embedded
October
Systems
9, 2017 15 / 27
Addressing Mode: Operands in Memory
1 Data Direct LDS/STS
The LDS instruction loads a single byte of data from the data space
to a register and depending on whether it uses an 8 or 16 bit address
the Opcode size is either 16-bit or 32-bit respectively.To transfer a
single byte of Data from Register to Memory STS is used

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Introduction to
SCHOOL
Instruction
OF Set
ENGINEERING)
ArchitectureLecture 3 Embedded
October
Systems
9, 2017 16 / 27
Example of LDS and STS

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Introduction to
SCHOOL
Instruction
OF Set
ENGINEERING)
ArchitectureLecture 3 Embedded
October
Systems
9, 2017 17 / 27
OUTLINE

1 Introduction to ISA

2 kinds of instruction

3 Addressing Modes

4 Data Indirect with Displacement LDD/STD

5 Data Indirect LD/ST

6 Data Indirect LD/ST

7 Data Indirect with Pre-decrement

8 Data Indirect with Post-increment

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Introduction to
SCHOOL
Instruction
OF Set
ENGINEERING)
ArchitectureLecture 3 Embedded
October
Systems
9, 2017 18 / 27
Data Indirect with Displacement LDD/STD
Loads or stores a single byte of data to or from data memory and a
register.

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Introduction to
SCHOOL
Instruction
OF Set
ENGINEERING)
ArchitectureLecture 3 Embedded
October
Systems
9, 2017 19 / 27
OUTLINE

1 Introduction to ISA

2 kinds of instruction

3 Addressing Modes

4 Data Indirect with Displacement LDD/STD

5 Data Indirect LD/ST

6 Data Indirect LD/ST

7 Data Indirect with Pre-decrement

8 Data Indirect with Post-increment

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Introduction to
SCHOOL
Instruction
OF Set
ENGINEERING)
ArchitectureLecture 3 Embedded
October
Systems
9, 2017 20 / 27
Data Indirect LD/ST
This instruction is similar to the Data Indirect with Displacement except
it doesn’t use a displacement but instead loads indirectly using the X, Y
or Z registers

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Introduction to
SCHOOL
Instruction
OF Set
ENGINEERING)
ArchitectureLecture 3 Embedded
October
Systems
9, 2017 21 / 27
OUTLINE

1 Introduction to ISA

2 kinds of instruction

3 Addressing Modes

4 Data Indirect with Displacement LDD/STD

5 Data Indirect LD/ST

6 Data Indirect LD/ST

7 Data Indirect with Pre-decrement

8 Data Indirect with Post-increment

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Introduction to
SCHOOL
Instruction
OF Set
ENGINEERING)
ArchitectureLecture 3 Embedded
October
Systems
9, 2017 22 / 27
SAMPLE CODE

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Introduction to
SCHOOL
Instruction
OF Set
ENGINEERING)
ArchitectureLecture 3 Embedded
October
Systems
9, 2017 23 / 27
OUTLINE

1 Introduction to ISA

2 kinds of instruction

3 Addressing Modes

4 Data Indirect with Displacement LDD/STD

5 Data Indirect LD/ST

6 Data Indirect LD/ST

7 Data Indirect with Pre-decrement

8 Data Indirect with Post-increment

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Introduction to
SCHOOL
Instruction
OF Set
ENGINEERING)
ArchitectureLecture 3 Embedded
October
Systems
9, 2017 24 / 27
Data Indirect with Pre-decrement
Similar to the Data Indirect instruction this instruction decrements the
X, Y or Z register before the data is accessed and like the Data Indirect
instruction it allows the registers to be used.

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Introduction to
SCHOOL
Instruction
OF Set
ENGINEERING)
ArchitectureLecture 3 Embedded
October
Systems
9, 2017 25 / 27
OUTLINE

1 Introduction to ISA

2 kinds of instruction

3 Addressing Modes

4 Data Indirect with Displacement LDD/STD

5 Data Indirect LD/ST

6 Data Indirect LD/ST

7 Data Indirect with Pre-decrement

8 Data Indirect with Post-increment

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Introduction to
SCHOOL
Instruction
OF Set
ENGINEERING)
ArchitectureLecture 3 Embedded
October
Systems
9, 2017 26 / 27
Data Indirect with Post-increment

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Introduction to
SCHOOL
Instruction
OF Set
ENGINEERING)
ArchitectureLecture 3 Embedded
October
Systems
9, 2017 27 / 27
TIMERS AND COUNTERS
Lecturer 4
Embedded Systems

Eng George Mugala

THE COPPERBELT UNIVERSITY SCHOOL OF ENGINEERING

February 25, 2018

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TIMERS ANDSCHOOL
COUNTERSLecturer
OF ENGINEERING)
4Embedded Systems February 25, 2018 1 / 80
OUTLINE

1 TIMING WITHOUT A COUNTER

2 TIMING WITH TIMERS/COUNTER

3 TIMER/COUNTER 0

4 EXAMPLES WITH TIMER/COUNTER 0

5 TIMERS/COUNTER PROGRAMING STEPS

6 PROGRAMING EXAMPLES

7 CLEAR TIMER ON COMPARE MATCH(CTC)

8 TIMER/COUNTER1

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TIMERS ANDSCHOOL
COUNTERSLecturer
OF ENGINEERING)
4Embedded Systems February 25, 2018 2 / 80
OUTLINE

1 TIMING WITHOUT A COUNTER

2 TIMING WITH TIMERS/COUNTER

3 TIMER/COUNTER 0

4 EXAMPLES WITH TIMER/COUNTER 0

5 TIMERS/COUNTER PROGRAMING STEPS

6 PROGRAMING EXAMPLES

7 CLEAR TIMER ON COMPARE MATCH(CTC)

8 TIMER/COUNTER1

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TIMERS ANDSCHOOL
COUNTERSLecturer
OF ENGINEERING)
4Embedded Systems February 25, 2018 3 / 80
TIMING WITHOUT A COUNTER
Question
Write a program to blink on and off an LED connected to PORTDO
every one second

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TIMERS ANDSCHOOL
COUNTERSLecturer
OF ENGINEERING)
4Embedded Systems February 25, 2018 4 / 80
solution

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TIMERS ANDSCHOOL
COUNTERSLecturer
OF ENGINEERING)
4Embedded Systems February 25, 2018 5 / 80
Continuation

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TIMERS ANDSCHOOL
COUNTERSLecturer
OF ENGINEERING)
4Embedded Systems February 25, 2018 6 / 80
Continuation

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TIMERS ANDSCHOOL
COUNTERSLecturer
OF ENGINEERING)
4Embedded Systems February 25, 2018 7 / 80
Explanation
Since the Atmega32 has an internal clock frequency of 1MHz, 1MHz
clock means 1000000 cycles per second. So 1 cycle equals 1/1000000
seconds or 1us. To get 1 second delay, you need 1000000 cycles of 1us,
so it means that you have to create an algorithm of 1000000 cycles.
Building on the example, a 1 sec delay @ 1MHz clock would be as
shown above in the written code

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TIMERS ANDSCHOOL
COUNTERSLecturer
OF ENGINEERING)
4Embedded Systems February 25, 2018 8 / 80
Code Explanation
In this case there is the internal loop Delay3 that is 4 cycles long
because DEC=1, NOP=1 and BRNE=2 when jumping to Delay3.
So, 4 cycles repeated 250 times (the value of dly3) are 1000 cycles
or 1000us = 1ms.
Then the loop Delay2 repeats the Delay3 125 times (the value of
dly2). So the accumulated delay in this case is 125ms.
And finally, the loop Delay1 repeats the Delay2 8 times (the value
of dly1). So the accumulated delay in this case is 1000ms or 1
second

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TIMERS ANDSCHOOL
COUNTERSLecturer
OF ENGINEERING)
4Embedded Systems February 25, 2018 9 / 80
SOLUTION

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TIMERS ANDSCHOOL
COUNTERSLecturer
OF ENGINEERING)
4Embedded Systems February 25, 2018 10 / 80
OUTLINE

1 TIMING WITHOUT A COUNTER

2 TIMING WITH TIMERS/COUNTER

3 TIMER/COUNTER 0

4 EXAMPLES WITH TIMER/COUNTER 0

5 TIMERS/COUNTER PROGRAMING STEPS

6 PROGRAMING EXAMPLES

7 CLEAR TIMER ON COMPARE MATCH(CTC)

8 TIMER/COUNTER1

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TIMERS ANDSCHOOL
COUNTERSLecturer
OF ENGINEERING)
4Embedded Systems February 25, 2018 11 / 80
TIMING WITH TIMERS/COUNTER-WAIT FOR
TIMER-NORMAL MODE
A computer timer/counter is used to perform a task at a set interval. A
timer that increments a counter once every second could be used to
make a realtime clock by keeping track of the hours, minutes and
seconds that have elapsed.

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TIMERS ANDSCHOOL
COUNTERSLecturer
OF ENGINEERING)
4Embedded Systems February 25, 2018 12 / 80
ATMEGA32 TIMERS/COUNTERS
General View: A counter Register that counts events on the clock
input
I Timer:Count System Clock(Constant Cycle Time)
I Counter:Count external events
I Can be checked and cleared by software

I
I flag is set when the counter overflows
F can be checked and cleared by software
I Atmega32 has 3 timers
F TIMER 0,TIMER1 and TIMER2

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TIMERS ANDSCHOOL
COUNTERSLecturer
OF ENGINEERING)
4Embedded Systems February 25, 2018 13 / 80
ATMEGA32 TIMERS/COUNTERS

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TIMERS ANDSCHOOL
COUNTERSLecturer
OF ENGINEERING)
4Embedded Systems February 25, 2018 14 / 80
OUTLINE

1 TIMING WITHOUT A COUNTER

2 TIMING WITH TIMERS/COUNTER

3 TIMER/COUNTER 0

4 EXAMPLES WITH TIMER/COUNTER 0

5 TIMERS/COUNTER PROGRAMING STEPS

6 PROGRAMING EXAMPLES

7 CLEAR TIMER ON COMPARE MATCH(CTC)

8 TIMER/COUNTER1

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TIMERS ANDSCHOOL
COUNTERSLecturer
OF ENGINEERING)
4Embedded Systems February 25, 2018 15 / 80
TIMER/COUNTER 0

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TIMERS ANDSCHOOL
COUNTERSLecturer
OF ENGINEERING)
4Embedded Systems February 25, 2018 16 / 80
TIMER COUNTER CONTROL REGISTER 0(TCCR0)

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TIMERS ANDSCHOOL
COUNTERSLecturer
OF ENGINEERING)
4Embedded Systems February 25, 2018 17 / 80
TIMER/COUNTER INTERRUPT FLAG
REGISTER(TIFR)

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TIMERS ANDSCHOOL
COUNTERSLecturer
OF ENGINEERING)
4Embedded Systems February 25, 2018 18 / 80
OUTLINE

1 TIMING WITHOUT A COUNTER

2 TIMING WITH TIMERS/COUNTER

3 TIMER/COUNTER 0

4 EXAMPLES WITH TIMER/COUNTER 0

5 TIMERS/COUNTER PROGRAMING STEPS

6 PROGRAMING EXAMPLES

7 CLEAR TIMER ON COMPARE MATCH(CTC)

8 TIMER/COUNTER1

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TIMERS ANDSCHOOL
COUNTERSLecturer
OF ENGINEERING)
4Embedded Systems February 25, 2018 19 / 80
EXAMPLES WITH TIMER/COUNTER 0

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TIMERS ANDSCHOOL
COUNTERSLecturer
OF ENGINEERING)
4Embedded Systems February 25, 2018 20 / 80
OUTLINE

1 TIMING WITHOUT A COUNTER

2 TIMING WITH TIMERS/COUNTER

3 TIMER/COUNTER 0

4 EXAMPLES WITH TIMER/COUNTER 0

5 TIMERS/COUNTER PROGRAMING STEPS

6 PROGRAMING EXAMPLES

7 CLEAR TIMER ON COMPARE MATCH(CTC)

8 TIMER/COUNTER1

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TIMERS ANDSCHOOL
COUNTERSLecturer
OF ENGINEERING)
4Embedded Systems February 25, 2018 21 / 80
TIMERS/COUNTER PROGRAMING STEPS

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TIMERS ANDSCHOOL
COUNTERSLecturer
OF ENGINEERING)
4Embedded Systems February 25, 2018 22 / 80
OUTLINE

1 TIMING WITHOUT A COUNTER

2 TIMING WITH TIMERS/COUNTER

3 TIMER/COUNTER 0

4 EXAMPLES WITH TIMER/COUNTER 0

5 TIMERS/COUNTER PROGRAMING STEPS

6 PROGRAMING EXAMPLES

7 CLEAR TIMER ON COMPARE MATCH(CTC)

8 TIMER/COUNTER1

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TIMERS ANDSCHOOL
COUNTERSLecturer
OF ENGINEERING)
4Embedded Systems February 25, 2018 23 / 80
PROGRAMING EXAMPLES
3 Question-Using Timers/Counter 0 Write a programe to switch on/off
an LED connected to PortC3 with a delay of 1.75µs. Use the internal
clock frequency of 8MHZ .

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TIMERS ANDSCHOOL
COUNTERSLecturer
OF ENGINEERING)
4Embedded Systems February 25, 2018 24 / 80
First Solution Step is to find the Timer Value
1 Calculate the period of Timer Clock T = 1/FTimerClock
2 divide desired time delay D by T (n = D/T )
3 perform dd = 256 − n
4 convert the results to hex(XX)
5 set TCNT0 =XX

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TIMERS ANDSCHOOL
COUNTERSLecturer
OF ENGINEERING)
4Embedded Systems February 25, 2018 25 / 80
Following steps
Solution
Since the microcontroller is using 8MHZ of Clock frequence and there is
no prescalar.
1
Tclkstep = 8000000 = 0.125µs meaning counter counts up per clock time
with no prescalar
TclkstepFinal = Tclkstep ∗ prescalar = 0.125µs
1.75µs
We need to wait 0.125µs = 14clocks
dd = 256 − 14
Results in hex =0xF2
Therefore TCNT0 =0xF2

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TIMERS ANDSCHOOL
COUNTERSLecturer
OF ENGINEERING)
4Embedded Systems February 25, 2018 26 / 80
Written Code

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TIMERS ANDSCHOOL
COUNTERSLecturer
OF ENGINEERING)
4Embedded Systems February 25, 2018 27 / 80
Proteus Simulation

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TIMERS ANDSCHOOL
COUNTERSLecturer
OF ENGINEERING)
4Embedded Systems February 25, 2018 28 / 80
Example
Question
Write a program to generate a square wave with a period of 12.5µs on
PORTC3 in normal mode
solution
Since the period is 12.5µs then Delay = 1/2 ∗ Period = 6.25µs
knowing our desired time delay lets calculate as following the steps
n = 6.25/0.125 = 50
dd = 256 − 50 = 206
ddhex = 0xCE

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TIMERS ANDSCHOOL
COUNTERSLecturer
OF ENGINEERING)
4Embedded Systems February 25, 2018 29 / 80
Written Code

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TIMERS ANDSCHOOL
COUNTERSLecturer
OF ENGINEERING)
4Embedded Systems February 25, 2018 30 / 80
Eng George Mugala ( THE COPPERBELT UNIVERSITY
TIMERS ANDSCHOOL
COUNTERSLecturer
OF ENGINEERING)
4Embedded Systems February 25, 2018 31 / 80
Proteus Simulation

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TIMERS ANDSCHOOL
COUNTERSLecturer
OF ENGINEERING)
4Embedded Systems February 25, 2018 32 / 80
Example
Question
Modify the previous Example to generate a square wave form of 16Khz
solution
T = 0.125µs
Desiredperiod = 1/16000 = 62.5µs
DesiredTimeDelay = Desiredperiod/2 = 31.25µs
n = 31.25/0.125= 250
dd =256-250 =6
ddHex =0x6 to be loaded into TCNT0
Write the program Yourselvs and simulate in proteus!!

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TIMERS ANDSCHOOL
COUNTERSLecturer
OF ENGINEERING)
4Embedded Systems February 25, 2018 33 / 80
MORE EXAMPLES
Question
Assume XTAL = 8MHZ (a) Find the clock period fed into Timer0 if a
prescalar option of 1024 is chosen
(b)Show what is the largest time delay we can get using this prescalar
option
a) 8*1/1024 =7812.5Hz and Period T = 1/7812.5 = 128µs
b) To get the largest delay we make TCNT0 zero.Making TCNT0=0
means that the counter will count from 0 to 0xFF, and then roll over to
raise the TOV0 flag. As a result it goes through a total states of 256.
Were delay = (256 − 0) ∗ 128µs
This is equal =0,032768seconds

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TIMERS ANDSCHOOL
COUNTERSLecturer
OF ENGINEERING)
4Embedded Systems February 25, 2018 34 / 80
Examples
Question
Write a program to generate a square wave of 125Hz Use Timer Zero,in
normal mode and prescalar of 256
Look at the following steps
T =1/125 =8ms the period of the square waveform
half of the high and low portion T/2 =4ms as the delay
n = 4ms/(0.125x256) = 125
dd =256-125=131 in decimal
ddHx =0x83
TCNT0=0x83

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TIMERS ANDSCHOOL
COUNTERSLecturer
OF ENGINEERING)
4Embedded Systems February 25, 2018 35 / 80
Written Code

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TIMERS ANDSCHOOL
COUNTERSLecturer
OF ENGINEERING)
4Embedded Systems February 25, 2018 36 / 80
Continuation

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TIMERS ANDSCHOOL
COUNTERSLecturer
OF ENGINEERING)
4Embedded Systems February 25, 2018 37 / 80
Proteus Simulation

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TIMERS ANDSCHOOL
COUNTERSLecturer
OF ENGINEERING)
4Embedded Systems February 25, 2018 38 / 80
OUTLINE

1 TIMING WITHOUT A COUNTER

2 TIMING WITH TIMERS/COUNTER

3 TIMER/COUNTER 0

4 EXAMPLES WITH TIMER/COUNTER 0

5 TIMERS/COUNTER PROGRAMING STEPS

6 PROGRAMING EXAMPLES

7 CLEAR TIMER ON COMPARE MATCH(CTC)

8 TIMER/COUNTER1

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TIMERS ANDSCHOOL
COUNTERSLecturer
OF ENGINEERING)
4Embedded Systems February 25, 2018 39 / 80
CLEAR TIMER ON COMPARE MATCH(CTC)

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TIMERS ANDSCHOOL
COUNTERSLecturer
OF ENGINEERING)
4Embedded Systems February 25, 2018 40 / 80
Question:
Create a square waveform on portB3 of 50% percent duty cycle.PortB3
should switch on and off every 640µs. Timer Zero should be used to
generate this.Write the code in Avr studion and Show the simulation
with proteus. Take clock frequency of 1MHZ with a prescalar of 64

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TIMERS ANDSCHOOL
COUNTERSLecturer
OF ENGINEERING)
4Embedded Systems February 25, 2018 41 / 80
Written Code

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TIMERS ANDSCHOOL
COUNTERSLecturer
OF ENGINEERING)
4Embedded Systems February 25, 2018 42 / 80
Written Code continuation

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TIMERS ANDSCHOOL
COUNTERSLecturer
OF ENGINEERING)
4Embedded Systems February 25, 2018 43 / 80
Calculation of Generated Delay and Explanation
Since the microcontroller is using 1MHZ of Clock frequence and a
prescalar of 64.
1
Tclkstep = 1000000 = 1µs
with prescalar of 64 clock step will change to
TclkstepFinal = 64 ∗ Tcklstep = 64µs
But Since the OCR0 is loaded with 9. It means TCNT0 overflows and
OCF0 is set after 10 Clock cycles.
TotalT imeDelay = 10 ∗ Tclkstepfinal = 640µs
The PORTB3 toggles therefore every 640µs.
due to the command
EOR temp2,temp
OUT PORTB,temp2
plse Prove this!!

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TIMERS ANDSCHOOL
COUNTERSLecturer
OF ENGINEERING)
4Embedded Systems February 25, 2018 44 / 80
Graph For Delay Explanation

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TIMERS ANDSCHOOL
COUNTERSLecturer
OF ENGINEERING)
4Embedded Systems February 25, 2018 45 / 80
Proteus Simulation

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TIMERS ANDSCHOOL
COUNTERSLecturer
OF ENGINEERING)
4Embedded Systems February 25, 2018 46 / 80
MORE QUESTIONS
Assuming XTAL = 8MHZ ,write a program to Generate a delay of 25.6ms
on PORTC5.Use Timer0,CTC mode with prescalar =1024. Write the
code in Avr studio and simulate the results in proteus.

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TIMERS ANDSCHOOL
COUNTERSLecturer
OF ENGINEERING)
4Embedded Systems February 25, 2018 47 / 80
SOLUTION
Since the microcontroller is using 8MHZ of Clock frequence and a
prescalar of 1024.
1
Tclkstep = 8000000 = 0.125µs
with prescalar of 1024 clock step will change to
TclkstepFinal = 64 ∗ Tcklstep = 128µs
Thus in order to generate a delay of 25.6ms
We need to wait 25.6ms
128µs = 200clocks
Therefore the OCR0 should be loaded with 200-1 =199

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TIMERS ANDSCHOOL
COUNTERSLecturer
OF ENGINEERING)
4Embedded Systems February 25, 2018 48 / 80
Program Code

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TIMERS ANDSCHOOL
COUNTERSLecturer
OF ENGINEERING)
4Embedded Systems February 25, 2018 49 / 80
Program Code

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TIMERS ANDSCHOOL
COUNTERSLecturer
OF ENGINEERING)
4Embedded Systems February 25, 2018 50 / 80
Proteus Simulation

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TIMERS ANDSCHOOL
COUNTERSLecturer
OF ENGINEERING)
4Embedded Systems February 25, 2018 51 / 80
MORE EXAMPLES
Assuming XTAL = 8MHZ Write a program to generate a delay of 1ms.
Use Timer0 with a prescalar of 64. Since the microcontroller is using
8MHZ of Clock frequence and a prescalar of 64.
8
Tclkstep = 1000000 = 0.125µs
with prescalar of 64 clock step will change to
TclkstepFinal = 64 ∗ Tcklstep = 8µs
Thus in order to generate a delay of 1ms
We need to wait 1ms
8µs = 125clocks
We need to load OCR0 with 125-1 = 124

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TIMERS ANDSCHOOL
COUNTERSLecturer
OF ENGINEERING)
4Embedded Systems February 25, 2018 52 / 80
Written Code

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TIMERS ANDSCHOOL
COUNTERSLecturer
OF ENGINEERING)
4Embedded Systems February 25, 2018 53 / 80
CONTINUATION

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TIMERS ANDSCHOOL
COUNTERSLecturer
OF ENGINEERING)
4Embedded Systems February 25, 2018 54 / 80
Proteus Simulation

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TIMERS ANDSCHOOL
COUNTERSLecturer
OF ENGINEERING)
4Embedded Systems February 25, 2018 55 / 80
OUTLINE

1 TIMING WITHOUT A COUNTER

2 TIMING WITH TIMERS/COUNTER

3 TIMER/COUNTER 0

4 EXAMPLES WITH TIMER/COUNTER 0

5 TIMERS/COUNTER PROGRAMING STEPS

6 PROGRAMING EXAMPLES

7 CLEAR TIMER ON COMPARE MATCH(CTC)

8 TIMER/COUNTER1

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TIMERS ANDSCHOOL
COUNTERSLecturer
OF ENGINEERING)
4Embedded Systems February 25, 2018 56 / 80
TIMER/COUNTER1

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TIMERS ANDSCHOOL
COUNTERSLecturer
OF ENGINEERING)
4Embedded Systems February 25, 2018 57 / 80
TIMER/COUNTER DESCRIPTIONS

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TIMERS ANDSCHOOL
COUNTERSLecturer
OF ENGINEERING)
4Embedded Systems February 25, 2018 58 / 80
TIMER/COUNTER INTERRUPT REGISTER

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TIMERS ANDSCHOOL
COUNTERSLecturer
OF ENGINEERING)
4Embedded Systems February 25, 2018 59 / 80
TCCR1A-TIMER/COUNTER CONTROL REGISTER
FOR CHANNEL A

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TIMERS ANDSCHOOL
COUNTERSLecturer
OF ENGINEERING)
4Embedded Systems February 25, 2018 60 / 80
TCCR1B-TIMER/COUNTER CONTROL REGISTER
FOR CHANNEL B

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TIMERS ANDSCHOOL
COUNTERSLecturer
OF ENGINEERING)
4Embedded Systems February 25, 2018 61 / 80
TOVI and OCIA

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TIMERS ANDSCHOOL
COUNTERSLecturer
OF ENGINEERING)
4Embedded Systems February 25, 2018 62 / 80
Examples on Timer 1

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TIMERS ANDSCHOOL
COUNTERSLecturer
OF ENGINEERING)
4Embedded Systems February 25, 2018 63 / 80
Example

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TIMERS ANDSCHOOL
COUNTERSLecturer
OF ENGINEERING)
4Embedded Systems February 25, 2018 64 / 80
Writing a Program
Use Timer1,Write a program to switch on and off an LED connected to
PC3 every 1ms.Use XTAL = 8MHZ ,CTC Mode,no prescalar
Solution
Since the microcontroller is using 8MHZ of Clock frequence and no
prescalar.
8
Tclkstep = 1000000 = 0.125µs
with no prescalar
TclkstepFinal = 0.125µs
Thus in order to generate a delay of 1ms
1ms
We need to wait 0.125µs = 8000clocks
We need to load OCR1AL with 8000-1 = 7999

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TIMERS ANDSCHOOL
COUNTERSLecturer
OF ENGINEERING)
4Embedded Systems February 25, 2018 65 / 80
Written Code

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TIMERS ANDSCHOOL
COUNTERSLecturer
OF ENGINEERING)
4Embedded Systems February 25, 2018 66 / 80
Written Code

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TIMERS ANDSCHOOL
COUNTERSLecturer
OF ENGINEERING)
4Embedded Systems February 25, 2018 67 / 80
Proteus Simulation

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TIMERS ANDSCHOOL
COUNTERSLecturer
OF ENGINEERING)
4Embedded Systems February 25, 2018 68 / 80
Example
Use Timer1,Write a program to switch on and off an LED connected to
PC3 every 1ms.Use XTAL = 8MHZ ,Normal Mode,no prescalar
Solution
Since the microcontroller is using 8MHZ of Clock frequence and no
prescalar.
1
Tclkstep = 8000000 = 0.125µs
with no prescalar
TclkstepFinal = 0.125µs
Thus in order to generate a delay of 1ms
1ms
We need to wait 0.125µs = 8000clocks
We need to load TCNT1 with 65536-8000

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TIMERS ANDSCHOOL
COUNTERSLecturer
OF ENGINEERING)
4Embedded Systems February 25, 2018 69 / 80
Written Code

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TIMERS ANDSCHOOL
COUNTERSLecturer
OF ENGINEERING)
4Embedded Systems February 25, 2018 70 / 80
Written Code

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TIMERS ANDSCHOOL
COUNTERSLecturer
OF ENGINEERING)
4Embedded Systems February 25, 2018 71 / 80
Proteus Simulation

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TIMERS ANDSCHOOL
COUNTERSLecturer
OF ENGINEERING)
4Embedded Systems February 25, 2018 72 / 80
Example

Use Timer1,Write a program to switch on and off an LED connected to


PC3 every 1s.Use XTAL = 8MHZ ,CTC Mode,Choose a prescalar of 256
Solution
Since the microcontroller is using 8MHZ of Clock frequence and has
prescalar of 256.
1
Tclkstep = 8000000 = 0.125µs
with no prescalar
TclkstepFinal = Tclkstep ∗ 256 = 32µs
Thus in order to generate a delay of 1s
1s
We need to wait 32µs = 31250clocks
We need to load OCR1AL with (31250-1)

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TIMERS ANDSCHOOL
COUNTERSLecturer
OF ENGINEERING)
4Embedded Systems February 25, 2018 73 / 80
Written code

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TIMERS ANDSCHOOL
COUNTERSLecturer
OF ENGINEERING)
4Embedded Systems February 25, 2018 74 / 80
Written code

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TIMERS ANDSCHOOL
COUNTERSLecturer
OF ENGINEERING)
4Embedded Systems February 25, 2018 75 / 80
Proteus Simulation

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TIMERS ANDSCHOOL
COUNTERSLecturer
OF ENGINEERING)
4Embedded Systems February 25, 2018 76 / 80
Example

Use Timer1,Write a program to switch on and off an LED connected to


PC3 at a frequency 1Hz .Use XTAL = 8MHZ ,CTC Mode,Choose a
prescalar of 64
Solution
Since the microcontroller is using 8MHZ of Clock frequence and has
prescalar of 64.
1
Tclkstep = 8000000 = 0.125µs
with no prescalar
1Hz = 1second = 1/2delay
TclkstepFinal = Tclkstep ∗ 64 = 8µs
Thus in order to generate a delay of 0.5s
We need to wait 0.5s
8µs = 62500clocks
We need to load OCR1AL with (62500-1)

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TIMERS ANDSCHOOL
COUNTERSLecturer
OF ENGINEERING)
4Embedded Systems February 25, 2018 77 / 80
Written Code

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TIMERS ANDSCHOOL
COUNTERSLecturer
OF ENGINEERING)
4Embedded Systems February 25, 2018 78 / 80
Written Code

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TIMERS ANDSCHOOL
COUNTERSLecturer
OF ENGINEERING)
4Embedded Systems February 25, 2018 79 / 80
Proteus Simulation

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TIMERS ANDSCHOOL
COUNTERSLecturer
OF ENGINEERING)
4Embedded Systems February 25, 2018 80 / 80
Assembley Programmes and Examples
Lecturer 5
Prime Width Modulation(PWM)

Eng George Mugala

THE COPPERBELT UNIVERSITY SCHOOL OF ENGINEERING

March 27, 2018

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Assembley Programmes
SCHOOL OFandENGINEERING)
ExamplesLecturer 5Prime Width March
Modulation(PWM)
27, 2018 1 / 29
OUTLINE

1 INTRODUCTION TO PWM WITH ATMEGA32

2 Types of PWM generated with ATMEGA32

3 Data sheet Explanations TIMER0

4 Examples of PWM with Timer0

5 Phase correct PWM

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Assembley Programmes
SCHOOL OFandENGINEERING)
ExamplesLecturer 5Prime Width March
Modulation(PWM)
27, 2018 2 / 29
OUTLINE

1 INTRODUCTION TO PWM WITH ATMEGA32

2 Types of PWM generated with ATMEGA32

3 Data sheet Explanations TIMER0

4 Examples of PWM with Timer0

5 Phase correct PWM

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Assembley Programmes
SCHOOL OFandENGINEERING)
ExamplesLecturer 5Prime Width March
Modulation(PWM)
27, 2018 3 / 29
INTRODUCTION TO PWM WITH ATMEGA32

Take a look at the above circuit and PWM graph


if the switch in above figure is closed continuously over a period of
time then the bulb will continuously on during that time

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Assembley Programmes
SCHOOL OFandENGINEERING)
ExamplesLecturer 5Prime Width March
Modulation(PWM)
27, 2018 4 / 29
INTRODUCTION TO PWM WITH ATMEGA32
If the switch is closed for 8ms and opened for 2ms over a cycle of
10ms, then the bulb will be ON only in the 8ms time

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Assembley Programmes
SCHOOL OFandENGINEERING)
ExamplesLecturer 5Prime Width March
Modulation(PWM)
27, 2018 5 / 29
INTRODUCTION TO PWM WITH ATMEGA32
If the switch is closed for 8ms and opened for 2ms over a cycle of
10ms, then the bulb will be ON only in the 8ms time
Now the average terminal over across the over a period of
Ton 8
10ms = (Ton+Toff ) , this is called duty cycle and is of 80%, (8+2) , so
the average output voltage will be 80% of the battery voltage.

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Assembley Programmes
SCHOOL OFandENGINEERING)
ExamplesLecturer 5Prime Width March
Modulation(PWM)
27, 2018 5 / 29
INTRODUCTION TO PWM WITH ATMEGA32
If the switch is closed for 8ms and opened for 2ms over a cycle of
10ms, then the bulb will be ON only in the 8ms time
Now the average terminal over across the over a period of
Ton 8
10ms = (Ton+Toff ) , this is called duty cycle and is of 80%, (8+2) , so
the average output voltage will be 80% of the battery voltage.
In the second case, the switch is closed for 5ms and opened for 5ms
over a period of 10ms, so the average terminal voltage at the
output will be 50% of the battery voltage. Say if the battery
voltage is 5V and the duty cycle is 50% and so the average
terminal voltage will be 2.5V

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Assembley Programmes
SCHOOL OFandENGINEERING)
ExamplesLecturer 5Prime Width March
Modulation(PWM)
27, 2018 5 / 29
INTRODUCTION TO PWM WITH ATMEGA32
If the switch is closed for 8ms and opened for 2ms over a cycle of
10ms, then the bulb will be ON only in the 8ms time
Now the average terminal over across the over a period of
Ton 8
10ms = (Ton+Toff ) , this is called duty cycle and is of 80%, (8+2) , so
the average output voltage will be 80% of the battery voltage.
In the second case, the switch is closed for 5ms and opened for 5ms
over a period of 10ms, so the average terminal voltage at the
output will be 50% of the battery voltage. Say if the battery
voltage is 5V and the duty cycle is 50% and so the average
terminal voltage will be 2.5V
In the third case the duty cycle is 20% and the average terminal
voltage is 20% of the battery voltage.
In ATMEGA32 we have four PWM channels, namely OC0, OC1A,
OC1B, and OC2. Here we are going to use OC0 PWM channel to vary
the brightness of the LED
Eng George Mugala ( THE COPPERBELT UNIVERSITY
Assembley Programmes
SCHOOL OFandENGINEERING)
ExamplesLecturer 5Prime Width March
Modulation(PWM)
27, 2018 5 / 29
OUTLINE

1 INTRODUCTION TO PWM WITH ATMEGA32

2 Types of PWM generated with ATMEGA32

3 Data sheet Explanations TIMER0

4 Examples of PWM with Timer0

5 Phase correct PWM

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Assembley Programmes
SCHOOL OFandENGINEERING)
ExamplesLecturer 5Prime Width March
Modulation(PWM)
27, 2018 6 / 29
Types of PWM generated with ATMEGA32
In ATMEGA32 there are a couple of ways to generate PWM
Phase correct PWM

In here the pulses occurs in the middle

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Assembley Programmes
SCHOOL OFandENGINEERING)
ExamplesLecturer 5Prime Width March
Modulation(PWM)
27, 2018 7 / 29
FAST PWM

Fast PWM

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Assembley Programmes
SCHOOL OFandENGINEERING)
ExamplesLecturer 5Prime Width March
Modulation(PWM)
27, 2018 8 / 29
FAST PWM ON (OCO-PB3)

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Assembley Programmes
SCHOOL OFandENGINEERING)
ExamplesLecturer 5Prime Width March
Modulation(PWM)
27, 2018 9 / 29
OUTLINE

1 INTRODUCTION TO PWM WITH ATMEGA32

2 Types of PWM generated with ATMEGA32

3 Data sheet Explanations TIMER0

4 Examples of PWM with Timer0

5 Phase correct PWM

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Assembley Programmes
SCHOOL OFandENGINEERING)
ExamplesLecturer 5Prime WidthMarch
Modulation(PWM)
27, 2018 10 / 29
Data sheet Explanations

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Assembley Programmes
SCHOOL OFandENGINEERING)
ExamplesLecturer 5Prime WidthMarch
Modulation(PWM)
27, 2018 11 / 29
TCCR0 Register
Now to get the FAST PWM of 50Hz clock out of the ATMEGA, we
need to enable the appropriate bits in TCCR0 register. This is the only
register we need to bother, for getting 8bit FAST PWM

1 CS00, CS01, CS02 (YELLOW)-select the appropriate


prescalar(divider) for choosing the counter clock.
Below is the table showing different prescalars

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Assembley Programmes
SCHOOL OFandENGINEERING)
ExamplesLecturer 5Prime WidthMarch
Modulation(PWM)
27, 2018 12 / 29
TABLE OF PRESCALARS

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Assembley Programmes
SCHOOL OFandENGINEERING)
ExamplesLecturer 5Prime WidthMarch
Modulation(PWM)
27, 2018 13 / 29
WGM01,WGM00
WGM01,WGM00 are altered to change waveform generation mode
for Fast PWM we have WGM01=WGM00=1

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Assembley Programmes
SCHOOL OFandENGINEERING)
ExamplesLecturer 5Prime WidthMarch
Modulation(PWM)
27, 2018 14 / 29
COM01,COM00

1 Now we know that PWM is a signal with different duty ratio or


different turn ON turn OFF times. Until now we have chosen
frequency and type of PWM. For getting different duty ratio, we
are going to choose a value between 0 and 255 because of 8 bit.
Say we choose a value 180, as the counter start counting from 0
and reaches the value 180, the output response may be triggered.
This trigger may be inverting or non inverting. That is the output
can be told to pulled up on reaching the count, or it can be told to
pulled down on reaching the count.
This selection of pulling up or down is chosen by CM00 and CM01
bits.

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Assembley Programmes
SCHOOL OFandENGINEERING)
ExamplesLecturer 5Prime WidthMarch
Modulation(PWM)
27, 2018 15 / 29
COM01,COM00 Table

As shown in table, for the output to go high on compare and the output
will stay high until max value (as shown in figure at bottom). We have
to choose inverting mode to do that, so COM00=1; COM01=1.

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Assembley Programmes
SCHOOL OFandENGINEERING)
ExamplesLecturer 5Prime WidthMarch
Modulation(PWM)
27, 2018 16 / 29
Further Explanation
As shown in below figure, OCR0 (Output Compare Register 0) is the
byte which stores the user chosen value. So if we change OCR0=180,
the controller triggers the change (high) when counter reaches 180 from
0

Now for varying the brightness of LED we have to change the DUTY
RATIO of PWM signal. In order to change duty ratio, we need to
change the OCR0 value. When we change this value of OCR0, the
counter takes different time, to reach the OCR0. So the controller pulls
the output high at different times.

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Assembley Programmes
SCHOOL OFandENGINEERING)
ExamplesLecturer 5Prime WidthMarch
Modulation(PWM)
27, 2018 17 / 29
OUTLINE

1 INTRODUCTION TO PWM WITH ATMEGA32

2 Types of PWM generated with ATMEGA32

3 Data sheet Explanations TIMER0

4 Examples of PWM with Timer0

5 Phase correct PWM

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Assembley Programmes
SCHOOL OFandENGINEERING)
ExamplesLecturer 5Prime WidthMarch
Modulation(PWM)
27, 2018 18 / 29
Registers to configure TIMER0
Here we are going to keep everything simple, So we are going to use
FAST PWM method to generate the PWM signal
Question
Generate a PWM Signal on PB3 with 50percent duty cycle from a
clock frequency of 1MHZ . Connect two buttons to PC0,PC1 and to
PB3 connect the motor such that when a button on PC0 is
pressed, Motor runs without PWM, and when button on PC1 is
pressed motor runs with PWM.

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Assembley Programmes
SCHOOL OFandENGINEERING)
ExamplesLecturer 5Prime WidthMarch
Modulation(PWM)
27, 2018 19 / 29
CIRCUIT SET UP AND SOLUTION

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Assembley Programmes
SCHOOL OFandENGINEERING)
ExamplesLecturer 5Prime WidthMarch
Modulation(PWM)
27, 2018 20 / 29
Written code for fast PWM

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Assembley Programmes
SCHOOL OFandENGINEERING)
ExamplesLecturer 5Prime WidthMarch
Modulation(PWM)
27, 2018 21 / 29
Written code continuation

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Assembley Programmes
SCHOOL OFandENGINEERING)
ExamplesLecturer 5Prime WidthMarch
Modulation(PWM)
27, 2018 22 / 29
SIMULATION WITHOUT PWM ON PB3

notice simulation of the signal on PB3 without PWM in yellow is just


high and not a PWM signal.

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Assembley Programmes
SCHOOL OFandENGINEERING)
ExamplesLecturer 5Prime WidthMarch
Modulation(PWM)
27, 2018 23 / 29
SIMULATION WITH PWM ON PB3

notice simulation of the signal on PB3 with PWM in yellow is


alternating between high and low. Very importantly this is a set on
compare match. because the signal is set up and clears on low as
programed
Eng George Mugala ( THE COPPERBELT UNIVERSITY
Assembley Programmes
SCHOOL OFandENGINEERING)
ExamplesLecturer 5Prime WidthMarch
Modulation(PWM)
27, 2018 24 / 29
OUTLINE

1 INTRODUCTION TO PWM WITH ATMEGA32

2 Types of PWM generated with ATMEGA32

3 Data sheet Explanations TIMER0

4 Examples of PWM with Timer0

5 Phase correct PWM

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Assembley Programmes
SCHOOL OFandENGINEERING)
ExamplesLecturer 5Prime WidthMarch
Modulation(PWM)
27, 2018 25 / 29
PULSES OCCUR IN THE MIDDLE

The center of the wave is normally out sync in fast PWM for different
duty cycles, however in phase correct phase is maintained. This for
some applications such as server motors, it may be very important to
run the motors in phase correct and not Fast PWM

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Assembley Programmes
SCHOOL OFandENGINEERING)
ExamplesLecturer 5Prime WidthMarch
Modulation(PWM)
27, 2018 26 / 29
Examples
Question
Generate a PWM Signal on PB3 with 50percent duty cycle from a
clock frequency of 1MHZ . Connect two buttons to PC0,PC1 and to
PB3 connect the motor such that when a button on PC0 is
pressed, Motor runs without PWM, and when button on PC1 is
pressed motor runs with PWM but in phase Correct Mode. Show
your simulations in Proteus

to change into phase correct mode,we need to to set WGM00 and


clear WGM01 in TCCR0. Other settings may remain as per
requirement.

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Assembley Programmes
SCHOOL OFandENGINEERING)
ExamplesLecturer 5Prime WidthMarch
Modulation(PWM)
27, 2018 27 / 29
t
SOLUTION

TCCR0 = 0b01110001
this sets WGM00 to 1 which is bit 6 and clears WGM01 which is
bit bit 3, bit 0 is 1 to mean no prescalar and normal mode

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Assembley Programmes
SCHOOL OFandENGINEERING)
ExamplesLecturer 5Prime WidthMarch
Modulation(PWM)
27, 2018 28 / 29
Written Code

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Assembley Programmes
SCHOOL OFandENGINEERING)
ExamplesLecturer 5Prime WidthMarch
Modulation(PWM)
27, 2018 29 / 29
Results with phase correct

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Assembley Programmes
SCHOOL OFandENGINEERING)
ExamplesLecturer 5Prime WidthMarch
Modulation(PWM)
27, 2018 30 / 29
Analog to Digital Convension with Microcontrollers
Lecturer 5
Embedded Systems

Eng George Mugala

THE COPPERBELT UNIVERSITY SCHOOL OF ENGINEERING

March 25, 2018

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Analog to Digital
SCHOOL
Convension
OF ENGINEERING)
with MicrocontrollersLecturer 5Embedded
March 25, 2018
Systems 1 / 38
OUTLINE
1 INTRODUCTION FOR ADC
2 ANALOG-DIGITAL TERMINOLOGIES
3 ADC BASIC PRINCIPLES
4 QUANTIZATION
5 ANALOG TO DIGITAL CONVERTER STEPS
6 ANALOG TO DIGITAL CONVERTER STEPS EXAMPLE
7 ANALOG TO DIGITAL CONVERTER STEPS
EXAMPLE(ENCODING)
8 ATMEGA32 CIRCUIT DESCRIPTION FOR ADC
9 PRINCIPLE OF CIRCUIT OPERATIONS
10 Starting a Conversion
11 Prescaling and Conversion Timing
12 ADC PROGRAMMING EXAMPLES
Eng George Mugala ( THE COPPERBELT UNIVERSITY
Analog to Digital
SCHOOL
Convension
OF ENGINEERING)
with MicrocontrollersLecturer 5Embedded
March 25, 2018
Systems 2 / 38
OUTLINE
1 INTRODUCTION FOR ADC
2 ANALOG-DIGITAL TERMINOLOGIES
3 ADC BASIC PRINCIPLES
4 QUANTIZATION
5 ANALOG TO DIGITAL CONVERTER STEPS
6 ANALOG TO DIGITAL CONVERTER STEPS EXAMPLE
7 ANALOG TO DIGITAL CONVERTER STEPS
EXAMPLE(ENCODING)
8 ATMEGA32 CIRCUIT DESCRIPTION FOR ADC
9 PRINCIPLE OF CIRCUIT OPERATIONS
10 Starting a Conversion
11 Prescaling and Conversion Timing
12 ADC PROGRAMMING EXAMPLES
Eng George Mugala ( THE COPPERBELT UNIVERSITY
Analog to Digital
SCHOOL
Convension
OF ENGINEERING)
with MicrocontrollersLecturer 5Embedded
March 25, 2018
Systems 3 / 38
INTRODUCTION FOR ADC
.
ADC- Analog to digital conversion
I Conversion of an Analog signal to a Digital Signal

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Analog to Digital
SCHOOL
Convension
OF ENGINEERING)
with MicrocontrollersLecturer 5Embedded
March 25, 2018
Systems 4 / 38
OUTLINE
1 INTRODUCTION FOR ADC
2 ANALOG-DIGITAL TERMINOLOGIES
3 ADC BASIC PRINCIPLES
4 QUANTIZATION
5 ANALOG TO DIGITAL CONVERTER STEPS
6 ANALOG TO DIGITAL CONVERTER STEPS EXAMPLE
7 ANALOG TO DIGITAL CONVERTER STEPS
EXAMPLE(ENCODING)
8 ATMEGA32 CIRCUIT DESCRIPTION FOR ADC
9 PRINCIPLE OF CIRCUIT OPERATIONS
10 Starting a Conversion
11 Prescaling and Conversion Timing
12 ADC PROGRAMMING EXAMPLES
Eng George Mugala ( THE COPPERBELT UNIVERSITY
Analog to Digital
SCHOOL
Convension
OF ENGINEERING)
with MicrocontrollersLecturer 5Embedded
March 25, 2018
Systems 5 / 38
Analog to digital conversion terminologies
Analog:Continuiosly valued signal such as temperature,speed,with
infinit possible values in the middle.
Digital:discretely valued signal, such as integers, encoded in binary
ADC-CONVERTER:ADC, A/D, A2D;converts an analog signal to
a digital signal

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Analog to Digital
SCHOOL
Convension
OF ENGINEERING)
with MicrocontrollersLecturer 5Embedded
March 25, 2018
Systems 6 / 38
OUTLINE
1 INTRODUCTION FOR ADC
2 ANALOG-DIGITAL TERMINOLOGIES
3 ADC BASIC PRINCIPLES
4 QUANTIZATION
5 ANALOG TO DIGITAL CONVERTER STEPS
6 ANALOG TO DIGITAL CONVERTER STEPS EXAMPLE
7 ANALOG TO DIGITAL CONVERTER STEPS
EXAMPLE(ENCODING)
8 ATMEGA32 CIRCUIT DESCRIPTION FOR ADC
9 PRINCIPLE OF CIRCUIT OPERATIONS
10 Starting a Conversion
11 Prescaling and Conversion Timing
12 ADC PROGRAMMING EXAMPLES
Eng George Mugala ( THE COPPERBELT UNIVERSITY
Analog to Digital
SCHOOL
Convension
OF ENGINEERING)
with MicrocontrollersLecturer 5Embedded
March 25, 2018
Systems 7 / 38
ADC BASIC PRINCIPLES
Basic principle:Use a Comparator principle to determine whether or
not to turn on a particular bit of the binary number output
It is typical for an ADC to use a digital-to analog converter (DAC)
to determine one of the inputs to the comparator

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Analog to Digital
SCHOOL
Convension
OF ENGINEERING)
with MicrocontrollersLecturer 5Embedded
March 25, 2018
Systems 8 / 38
OUTLINE
1 INTRODUCTION FOR ADC
2 ANALOG-DIGITAL TERMINOLOGIES
3 ADC BASIC PRINCIPLES
4 QUANTIZATION
5 ANALOG TO DIGITAL CONVERTER STEPS
6 ANALOG TO DIGITAL CONVERTER STEPS EXAMPLE
7 ANALOG TO DIGITAL CONVERTER STEPS
EXAMPLE(ENCODING)
8 ATMEGA32 CIRCUIT DESCRIPTION FOR ADC
9 PRINCIPLE OF CIRCUIT OPERATIONS
10 Starting a Conversion
11 Prescaling and Conversion Timing
12 ADC PROGRAMMING EXAMPLES
Eng George Mugala ( THE COPPERBELT UNIVERSITY
Analog to Digital
SCHOOL
Convension
OF ENGINEERING)
with MicrocontrollersLecturer 5Embedded
March 25, 2018
Systems 9 / 38
Quantization
Quantization
I Quantization is the process of converting the sampled
continuous-Valued signals into discrete-valued data

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Analog to Digital
SCHOOL
Convension
OF ENGINEERING)
with MicrocontrollersLecturer March
5Embedded
25, 2018
Systems 10 / 38
Quantizing
The number of possible states that the converter can output is:
N = 2n
where n is the number of bits in the AD converter
Example: For a 3 bit A/D converter, N = 23 = 8.
(Vmax −Vmin ) (10−0)
Analog quantization size:Q = N = 8 = 1.25V

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Analog to Digital
SCHOOL
Convension
OF ENGINEERING)
with MicrocontrollersLecturer March
5Embedded
25, 2018
Systems 11 / 38
OUTLINE
1 INTRODUCTION FOR ADC
2 ANALOG-DIGITAL TERMINOLOGIES
3 ADC BASIC PRINCIPLES
4 QUANTIZATION
5 ANALOG TO DIGITAL CONVERTER STEPS
6 ANALOG TO DIGITAL CONVERTER STEPS EXAMPLE
7 ANALOG TO DIGITAL CONVERTER STEPS
EXAMPLE(ENCODING)
8 ATMEGA32 CIRCUIT DESCRIPTION FOR ADC
9 PRINCIPLE OF CIRCUIT OPERATIONS
10 Starting a Conversion
11 Prescaling and Conversion Timing
12 ADC PROGRAMMING EXAMPLES
Eng George Mugala ( THE COPPERBELT UNIVERSITY
Analog to Digital
SCHOOL
Convension
OF ENGINEERING)
with MicrocontrollersLecturer March
5Embedded
25, 2018
Systems 12 / 38
ANALOG TO DIGITAL CONVERTER STEPS
Quantizing:breaking down analog value to a set of finit states
Encoding:assigning a digital word or number to each state and
matching it to the input signal

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Analog to Digital
SCHOOL
Convension
OF ENGINEERING)
with MicrocontrollersLecturer March
5Embedded
25, 2018
Systems 13 / 38
OUTLINE
1 INTRODUCTION FOR ADC
2 ANALOG-DIGITAL TERMINOLOGIES
3 ADC BASIC PRINCIPLES
4 QUANTIZATION
5 ANALOG TO DIGITAL CONVERTER STEPS
6 ANALOG TO DIGITAL CONVERTER STEPS EXAMPLE
7 ANALOG TO DIGITAL CONVERTER STEPS
EXAMPLE(ENCODING)
8 ATMEGA32 CIRCUIT DESCRIPTION FOR ADC
9 PRINCIPLE OF CIRCUIT OPERATIONS
10 Starting a Conversion
11 Prescaling and Conversion Timing
12 ADC PROGRAMMING EXAMPLES
Eng George Mugala ( THE COPPERBELT UNIVERSITY
Analog to Digital
SCHOOL
Convension
OF ENGINEERING)
with MicrocontrollersLecturer March
5Embedded
25, 2018
Systems 14 / 38
ANALOG TO DIGITAL CONVERTER STEPS
EXAMPLE(QUANTIZATION)

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Analog to Digital
SCHOOL
Convension
OF ENGINEERING)
with MicrocontrollersLecturer March
5Embedded
25, 2018
Systems 15 / 38
OUTLINE
1 INTRODUCTION FOR ADC
2 ANALOG-DIGITAL TERMINOLOGIES
3 ADC BASIC PRINCIPLES
4 QUANTIZATION
5 ANALOG TO DIGITAL CONVERTER STEPS
6 ANALOG TO DIGITAL CONVERTER STEPS EXAMPLE
7 ANALOG TO DIGITAL CONVERTER STEPS
EXAMPLE(ENCODING)
8 ATMEGA32 CIRCUIT DESCRIPTION FOR ADC
9 PRINCIPLE OF CIRCUIT OPERATIONS
10 Starting a Conversion
11 Prescaling and Conversion Timing
12 ADC PROGRAMMING EXAMPLES
Eng George Mugala ( THE COPPERBELT UNIVERSITY
Analog to Digital
SCHOOL
Convension
OF ENGINEERING)
with MicrocontrollersLecturer March
5Embedded
25, 2018
Systems 16 / 38
ANALOG TO DIGITAL CONVERTER STEPS
EXAMPLE(ENCODING)

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Analog to Digital
SCHOOL
Convension
OF ENGINEERING)
with MicrocontrollersLecturer March
5Embedded
25, 2018
Systems 17 / 38
OUTLINE
1 INTRODUCTION FOR ADC
2 ANALOG-DIGITAL TERMINOLOGIES
3 ADC BASIC PRINCIPLES
4 QUANTIZATION
5 ANALOG TO DIGITAL CONVERTER STEPS
6 ANALOG TO DIGITAL CONVERTER STEPS EXAMPLE
7 ANALOG TO DIGITAL CONVERTER STEPS
EXAMPLE(ENCODING)
8 ATMEGA32 CIRCUIT DESCRIPTION FOR ADC
9 PRINCIPLE OF CIRCUIT OPERATIONS
10 Starting a Conversion
11 Prescaling and Conversion Timing
12 ADC PROGRAMMING EXAMPLES
Eng George Mugala ( THE COPPERBELT UNIVERSITY
Analog to Digital
SCHOOL
Convension
OF ENGINEERING)
with MicrocontrollersLecturer March
5Embedded
25, 2018
Systems 18 / 38
ATMEGA32 CIRCUIT DESCRIPTION FOR ADC

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Analog to Digital
SCHOOL
Convension
OF ENGINEERING)
with MicrocontrollersLecturer March
5Embedded
25, 2018
Systems 19 / 38
OUTLINE
1 INTRODUCTION FOR ADC
2 ANALOG-DIGITAL TERMINOLOGIES
3 ADC BASIC PRINCIPLES
4 QUANTIZATION
5 ANALOG TO DIGITAL CONVERTER STEPS
6 ANALOG TO DIGITAL CONVERTER STEPS EXAMPLE
7 ANALOG TO DIGITAL CONVERTER STEPS
EXAMPLE(ENCODING)
8 ATMEGA32 CIRCUIT DESCRIPTION FOR ADC
9 PRINCIPLE OF CIRCUIT OPERATIONS
10 Starting a Conversion
11 Prescaling and Conversion Timing
12 ADC PROGRAMMING EXAMPLES
Eng George Mugala ( THE COPPERBELT UNIVERSITY
Analog to Digital
SCHOOL
Convension
OF ENGINEERING)
with MicrocontrollersLecturer March
5Embedded
25, 2018
Systems 20 / 38
PRINCIPLE OF CIRCUIT OPERATIONS
Atmega32 features 10-bit successful approximation ADC.
The ADC is connected to and 8-channel Analog Multiplexer which
allows 8 single-ended voltage inputs constructed from the pins of
Port A. The single-ended voltage inputs refer to 0V (GND)
The minimum value represents GND and the maximum value
represents the voltage on the AREF pin minus 1 LSB. Optionally,
AVCC or an internal 2.56V reference voltage may be connected to
the AREF pin by writing to the REFSn bits in the ADMUX Register
The analog input channel and differential gain are selected by
writing to the MUX bits in ADMUX
Any of the ADC input pins, as well as GND and a fixed bandgap
voltage reference, can be selected as single ended inputs to the
ADC
A selection of ADC input pins can be selected as positive and
negative inputs to the differential gain amplifier
Eng George Mugala ( THE COPPERBELT UNIVERSITY
Analog to Digital
SCHOOL
Convension
OF ENGINEERING)
with MicrocontrollersLecturer March
5Embedded
25, 2018
Systems 21 / 38
ADC:The ADC generates a 10-bit result which is presented in the
ADC Data Registers, ADCH and ADCL
By default, the result is presented right adjusted, but can optionally
be presented left adjusted by setting the ADLAR bit in ADMUX
ADCL is read first then ADCH
The ADC has its own interrupt which can be triggered when a
conversion completes. When ADC access to the Data Registers is
prohibited between reading of ADCH and ADCL, the interrupt will
trigger even if the result is lost.

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Analog to Digital
SCHOOL
Convension
OF ENGINEERING)
with MicrocontrollersLecturer March
5Embedded
25, 2018
Systems 22 / 38
OUTLINE
1 INTRODUCTION FOR ADC
2 ANALOG-DIGITAL TERMINOLOGIES
3 ADC BASIC PRINCIPLES
4 QUANTIZATION
5 ANALOG TO DIGITAL CONVERTER STEPS
6 ANALOG TO DIGITAL CONVERTER STEPS EXAMPLE
7 ANALOG TO DIGITAL CONVERTER STEPS
EXAMPLE(ENCODING)
8 ATMEGA32 CIRCUIT DESCRIPTION FOR ADC
9 PRINCIPLE OF CIRCUIT OPERATIONS
10 Starting a Conversion
11 Prescaling and Conversion Timing
12 ADC PROGRAMMING EXAMPLES
Eng George Mugala ( THE COPPERBELT UNIVERSITY
Analog to Digital
SCHOOL
Convension
OF ENGINEERING)
with MicrocontrollersLecturer March
5Embedded
25, 2018
Systems 23 / 38
Starting a Conversion
Conversion:Single conversion started by wrting 1 to the ADC Start
Conversion bit, ADSC.
ADSC stays high for as long as conversion is in progress.Will be
cleared by hardware when conversion is complete
If a different data channel is selected while a conversion is in
progress, the ADC will finish the current conversion before
performing the channel change
Auto Triggering is enabled by setting the ADC Auto Trigger Enable
bit, ADATE in ADCSRA
The trigger source is selected by setting the ADC Trigger Select
bits, ADTS in SFIOR

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Analog to Digital
SCHOOL
Convension
OF ENGINEERING)
with MicrocontrollersLecturer March
5Embedded
25, 2018
Systems 24 / 38
ADC Timing Diagram, First Conversion (Single
Conversion Mode)

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Analog to Digital
SCHOOL
Convension
OF ENGINEERING)
with MicrocontrollersLecturer March
5Embedded
25, 2018
Systems 25 / 38
ADC Timing Diagram, First Conversion (Single
Conversion Mode Second time)

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Analog to Digital
SCHOOL
Convension
OF ENGINEERING)
with MicrocontrollersLecturer March
5Embedded
25, 2018
Systems 26 / 38
OUTLINE
1 INTRODUCTION FOR ADC
2 ANALOG-DIGITAL TERMINOLOGIES
3 ADC BASIC PRINCIPLES
4 QUANTIZATION
5 ANALOG TO DIGITAL CONVERTER STEPS
6 ANALOG TO DIGITAL CONVERTER STEPS EXAMPLE
7 ANALOG TO DIGITAL CONVERTER STEPS
EXAMPLE(ENCODING)
8 ATMEGA32 CIRCUIT DESCRIPTION FOR ADC
9 PRINCIPLE OF CIRCUIT OPERATIONS
10 Starting a Conversion
11 Prescaling and Conversion Timing
12 ADC PROGRAMMING EXAMPLES
Eng George Mugala ( THE COPPERBELT UNIVERSITY
Analog to Digital
SCHOOL
Convension
OF ENGINEERING)
with MicrocontrollersLecturer March
5Embedded
25, 2018
Systems 27 / 38
Prescaling and Conversion Timing

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Analog to Digital
SCHOOL
Convension
OF ENGINEERING)
with MicrocontrollersLecturer March
5Embedded
25, 2018
Systems 28 / 38
Prescaling and Conversion Timing

By default, the successive approximation circuitry requires an input


clock frequency between 50kHz and 200kHz to get maximum
resolution
The ADC module contains a prescaler, which generates an
acceptable ADC clock frequency from any CPU frequency above
100kHz. The prescaling is set by the ADPS bits in ADCSRA.
The prescaler starts counting from the moment the ADC is
switched on by setting the ADEN bit in ADCSRA

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Analog to Digital
SCHOOL
Convension
OF ENGINEERING)
with MicrocontrollersLecturer March
5Embedded
25, 2018
Systems 29 / 38
Voltage Reference Selections for ADC

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Analog to Digital
SCHOOL
Convension
OF ENGINEERING)
with MicrocontrollersLecturer March
5Embedded
25, 2018
Systems 30 / 38
OUTLINE
1 INTRODUCTION FOR ADC
2 ANALOG-DIGITAL TERMINOLOGIES
3 ADC BASIC PRINCIPLES
4 QUANTIZATION
5 ANALOG TO DIGITAL CONVERTER STEPS
6 ANALOG TO DIGITAL CONVERTER STEPS EXAMPLE
7 ANALOG TO DIGITAL CONVERTER STEPS
EXAMPLE(ENCODING)
8 ATMEGA32 CIRCUIT DESCRIPTION FOR ADC
9 PRINCIPLE OF CIRCUIT OPERATIONS
10 Starting a Conversion
11 Prescaling and Conversion Timing
12 ADC PROGRAMMING EXAMPLES
Eng George Mugala ( THE COPPERBELT UNIVERSITY
Analog to Digital
SCHOOL
Convension
OF ENGINEERING)
with MicrocontrollersLecturer March
5Embedded
25, 2018
Systems 31 / 38
ADC PROGRAMMING EXAMPLES
Question:Write a program to read an analog signal from channel 0
and display the digital signal results on portc Use the internal
reference voltage of 2.56V. The set up of the circuit is shown below

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Analog to Digital
SCHOOL
Convension
OF ENGINEERING)
with MicrocontrollersLecturer March
5Embedded
25, 2018
Systems 32 / 38
SOLUTION
1 First initialise ADMUX Register for relevant bits

I
I Since we are using internal reference of 2.56V, REFS1 and REFS0 =1
I Since we are using channel 0, MUX1 to MUX4 = 0
I ADLAR We shall wrt 0 since we want the result to be right justified.
I Binary number to be loaded:0b11000000

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Analog to Digital
SCHOOL
Convension
OF ENGINEERING)
with MicrocontrollersLecturer March
5Embedded
25, 2018
Systems 33 / 38
2 Initialize the ADCSRA Register
I

I We need to enable ADC,therefore ADEN =1


I set ADSC =0 since we are only initializing
I Set ADATE=0 since we dont have automatic trigger
I Set ADIF =0 since the interrupt will only happen automatically
I set ADIE =0; since we are not using in automatic mode
I set ADPS0 to ADPS2 =1; to assign the prescalar of 128
I Binary number to be loaded:0b10000111

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Analog to Digital
SCHOOL
Convension
OF ENGINEERING)
with MicrocontrollersLecturer March
5Embedded
25, 2018
Systems 34 / 38
PRESCALER SELECTION TABLE

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Analog to Digital
SCHOOL
Convension
OF ENGINEERING)
with MicrocontrollersLecturer March
5Embedded
25, 2018
Systems 35 / 38
Written Code

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Analog to Digital
SCHOOL
Convension
OF ENGINEERING)
with MicrocontrollersLecturer March
5Embedded
25, 2018
Systems 36 / 38
Proteus Simulation Results

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Analog to Digital
SCHOOL
Convension
OF ENGINEERING)
with MicrocontrollersLecturer March
5Embedded
25, 2018
Systems 37 / 38
ADC Conversion Results
For single Ended Conversion according to the datasheet
ADC = VIN∗1024
Vref wher in this case Vref = 2.56 and VIN is the
voltage appearing at the analog input pin
Going by our proteus results lets prove the conversion
ADC = 0.105∗1024
2.56 = 42
this decimal result in binary is 101010
but because the first two upper bits are zero,the results will now be
00101010. which is exactely what our proteus shows
Testing the results at 0.5V the binary equivalent gave 11001000
which is true with proteus simulation!! prove this

Eng George Mugala ( THE COPPERBELT UNIVERSITY


Analog to Digital
SCHOOL
Convension
OF ENGINEERING)
with MicrocontrollersLecturer March
5Embedded
25, 2018
Systems 38 / 38

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy