Microcontroller part 1

113
https://www.facebook.com/groups/embedded.system.KS/ Embedded System PART 1 ENG.KEROLES SHENOUDA 1

Transcript of Microcontroller part 1

Page 1: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/

Embedded System

PART 1ENG.KEROLES SHENOUDA

1

Page 2: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

Index (1/3) Introduction to COMPUTER SYSTEM

Memory

I/O Unit

Microprocessor

The internal structure of a microprocessor.

Central Processing Unit (CPU)

Arithmetic and logic unit (ALU)

Control unit

CPU Interactions

Clocks

Cache Memory (L1 and L2) Cache

2

Page 3: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

Index (2/3) Special Purpose Registers

PC Program Counter Register

Instruction Register IR

Instruction Decoder ID

STACK Pointer SP Register

STACK Pointer SP Register

Status Register (SREG)

Status Register (SREG)

Index Register (X,Y)

Accumulator Registers (A,B)

General purpose Registers (R1,R2,R3,….)

3

Page 4: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

Index (3/3)

Microcontroller

Microcontroller Vs Microprocessor Vs SOC

von Neumann Architecture

Harvard Architecture

pipeline

CIS Vs RISC

AVR different groups

4

Page 5: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

What will get on Embedded System

AVR Microcontroller

AVR Interfacing

ARM SOC

ARM Tiva C

5

Page 6: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

GITHUP “Embedded System” Repo6

Page 7: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

Please Put your Code on the Best

Place

7

Page 8: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

Introduction to COMPUTER

SYSTEM

A computer is a programmable machine that receives input, stores and manipulates data//information, and provides output in a useful format.

8

Page 9: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

BLOCK DIAGRAM OF A BASIC

COMPUTER SYSTEM

ROM RAM I/O

interface

I/O

devicesCPU

Basic computer system consist of a Central processing unit (CPU),

memory (RAM and ROM), input/output (I/O) unit.

Block diagram of a basic computer system

Address bus

Data bus Control

bus

9

Page 10: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

Basic component of

microcomputer1. CPU - Central Processing Unit

the portion of a computer system that carries out the instructions of a computer program

the primary element carrying out the computer's functions. It is the unit that reads and executes program instructions.

The data in the instruction tells the processor what to do.

Pentium D dual core processors

10

Page 11: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

physical devices used to store data or programs (sequences of instructions) on a temporary or permanent basis for use in an electronic digital computer.

Computer main memory comes in two principal varieties: random-access memory (RAM) and read-only memory (ROM).

RAM can be read and written to anytime the CPU commands it, but ROM is pre-loaded with data and software that never changes, so the CPU can only read from it.

ROM is typically used to store the computer's initial start-up instructions.

In general, the contents of RAM are erased when the power to the computer is turned off, but ROM retains its data indefinitely.

In a PC, the ROM contains a specialized program called the BIOSthat orchestrates loading the computer's operating system from the hard disk drive into RAM whenever the computer is turned on or reset.

Memory11

Page 12: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

Input/output (I/O), refers to the communication between an information processing system (such as a computer), and the outside world possibly a human, or another information processing system.

Inputs are the signals or data received by the system, and outputsare the signals or data sent from it

Devices that provide input or output to the computer are called peripherals

On a typical personal computer, peripherals include input devices like the keyboard and mouse, and output devices such as the displayand printer. Hard disk drives, floppy disk drives and optical disc drives serve as both input and output devices. Computer networkingis another form of I/O.

I/O Unit12

Page 13: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

Microcontroller vs MicroprocessorA Microcontroller is basically a computer on a chip. It differs form normal desktop or laptop

computers in that a microcontroller is an application specific computer that usually runs

a single program performing dedicated task(s) while the the later two are general purpose

computers that can run numerous programs depending on a users needs. A microcontroller

contains on chip CPU, input/output interface, memory, clock, timer, and an assortment of of

other peripherals.

A Microprocessor on the other hand is just a CPU one

has to add externally memory, clock, input/output

interfaces, timer and all other needed peripheral. This is

the reason a microprocessor has so many pins.

13

Page 14: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

Microprocessor There are five important components in a microprocessor. They are Arithmetic

and Logic Unit (ALU), Control Unit, Registers, Instruction Decoder and Data Bus but the first three are considered significant components. The block diagram of a microprocessor with these basic components is shown below.

14

Page 15: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

Microprocessor

The internal structure

of a microprocessor.

15

Page 16: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

Central Processing Unit (CPU)

Arithmetic / Logic Unit

Registers

Control Unit

Small, fast

storage areas for

instructions and

data

Performs calculations

and decisions

Coordinates

processing steps

16

Page 17: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/

The Little Man Computer17

Page 18: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/

The Processor

The processor is the "brain" of the computer system.

Main processor is called the Central Processing Unit (CPU).

A particular computer will have a particular type of processor, such as a Pentium

or a SPARC chip.

Co-processors assist the CPU with some of the processing functions. Examples:

Math co-processors handle heavy duty math processing

Graphics coprocessors speed up the display of graphics onto the monitor

18

Page 19: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

Internal structure and basic

operation of microprocessor

ALU Register

Section

Control and timing

section

Address bus

Data bus

Control bus

Block diagram of a microprocessor

19

Page 20: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

20

Page 21: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

Arithmetic and logic unit (ALU)

The component that performs the arithmetic and logical operations

the most important components in a microprocessor, and is typically the part of the processor that is designed first.

able to perform the basic logical operations (AND, OR), including the addition operation.

The inclusion of inverters on the inputs enables the same ALU hardware to perform the subtraction operation (adding an inverted operand), and the operations NAND and NOR.

21

Page 22: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

Internal structure of ALU

2 bits of ALU 4 bits of ALU

22

Page 23: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

Control unit

The circuitry that controls the flow of information through the processor, and coordinates the activities of the other units within it.

In a way, it is the "brain within the brain", as it controls what happens inside the processor, which in turn controls the rest of the PC.

On a regular processor, the control unit performs the tasks of fetching, decoding, managing execution and then storing results.

23

Page 24: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

Internal structure of control unit24

Page 25: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

Memory

Computer Memory - millions/billions of on/off charges

Divided into:

Bits 0 or 1

Bytes Groups of 8 bits A byte is the smallest unit of storage.(Can hold one text character)

WordsGroups of bits/bytes (8, 16, 32, 64-bits)

25

Page 26: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

Memory

Storage is usually too large to be expressed in bytes or

words. Instead we use:

Kilobyte (KB) = 1024 bytes(210 bytes)

Megabyte (MB) = 1024 x 1024 bytes or one

million bytes (220 bytes)

Gigabyte (GB) = 1024 x 1024 x 1024 bytes or

one trillion bytes (230 bytes)

Terabyte (TB) = 1024 x 1024 x 1024 x 1024 bytes

one quadrillion bytes (240 bytes)

26

Page 27: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

Main Memory

Each computer has a specific word size

Word sizes vary from computer to computer.

Word size is an even multiple of a bytes.

Each word within memory can hold either

data or

program instructions

27

Page 28: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

Main Memory

5248

5249

5250

5251

5252

5253

5254

5255

5256

A word is stored in

consecutive

memory bytes.

10011010

Each memory cell stores a

set number of bits (some

computers use 8 bits/one

byte, others use words)

Each memory cell

has a numeric

address, which

uniquely identifies

its location

28

Page 29: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

CPU and Memory

CPU can interact with main memory in two ways:

It can write a byte/word to a given memory location.

The previous bits that were in that location are destroyed

The new bits are saved for future use.

It can read a byte/word from a given memory location.

The CPU copies the bits stored at that location and stores them in a CPU register

The contents of the memory location are NOT changed.

29

Page 30: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

Main Memory Characteristics

Very closely connected to the CPU.

Contents are quickly and easily changed.

Holds the programs and data that the

processor is actively working with.

Interacts with the processor millions of times

per second.

Nothing permanent is kept in main memory.

30

Page 31: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

Secondary Storage Characteristics

Connected to main memory through a bus and a

device controller.

Contents are easily changed, but access is very

slow compared to main memory.

Only occasionally interacts with CPU.

Used for long-term storage of programs and data.

Much larger than main memory (GBs vs. MBs).

31

Page 32: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

Program Instructions

Programs instructions are stored in secondary storage

(hard disks, CD-ROM, DVD).

To process data, the CPU requires a working area

Uses Main Memory

Also called: RAM (random access memory),

primary storage, and internal memory.

Before a program is run, instructions must first be copied

from the slow secondary storage into fast main memory

Provides the CPU with fast access to instructions to

execute.

32

Page 33: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

Instructions

An instruction is a sequence of bits.

A simple instruction format may consist of an

operation code (op code) and an address or

operands.

Op Code Operands / Address

Instructions tell the computer’s CPU what to do.

33

Page 34: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

Instructions

The operation code specifies the operation the computer is to carry out (add, compare, etc)

The operand/address area can store an operand or an address

An operand is a specific value or a register number

An address allows the instruction to refer to a location in main memory

The CPU runs each instruction in the program, starting with instruction 0, using the fetch-decode-execute cycle.

34

Page 35: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

Fetch-Decode-Execute Cycle

During the fetch part, the CPU fetches the next instruction from the address contained in the Program Counter and places the instruction in the Instruction Register.

When a program starts, the program counter contains 0, so the instruction at address 0 is fetched.

As soon as an instruction is fetched, the CPU adds 1 word to the contents of the Program Counter, so that it will contain the address of the next sequential instruction.

35

Page 36: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

Fetch-Decode-Execute Diagram

Program Counter (PC)

3023

Instruction Register

General Purpose Registers

Fetch

Get instruction and

increment PC

CPU

3020

3021

3022

3023

3024

3025

3026

3027

3028

Main Memory

add r3, r1, r2

33

45

add r3, r1, r2

r1

r2

r378

3024

Execute

In this case add r1 and

r2 and put result in r3.

Decode

Determine what the

instruction is (add)

Then begin again by Fetching the instruction in 3024….

36

Page 37: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

Clocks

Computer systems have an internal clock, which is used to synchronize their

activities.

Processors run at a particular clock speed.

Clock speed is measured in Hertz

One Hertz is one clock tick per second.

MHz means mega Hertz

One MHz is one million clock ticks per second.

The clock speed determines how fast instructions can be run

37

Page 38: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/

Access to Instructions

The hard disk is too slow to provide instructions to the CPU.

So programs are first loaded into main memory, which is much faster.

The CPU can then access the instructions more quickly.

38

Page 39: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

Cache Memory

But as CPU speeds became faster, the main memory

couldn’t provide the CPU with the instructions at a fast

enough rate.

So even faster memory ( cache memory) is now placed

between the CPU and main memory to provide the

instructions at an quicker rate to the CPU.

39

Page 40: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/

Cache Memory

When an instruction or data is accessed from main memory, it is placed in

the cache. Second and subsequent use of the same instruction/data will

then be faster, since it is accessed directly from the cache.

40

Page 41: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

Primary and Secondary

Cache Memory Most modern CPUs now have a cache memory (L1), on the

same silicon wafer as the CPU, to provide the CPU with

instructions at the same clock speed as the CPU.

An additional off-the-chip secondary cache (L2) may also

interact with the CPU at a slower speed.

41

Page 42: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/

L1 and L2 Cache Memory42

Page 43: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/

Memory 43

Page 44: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/

Random Access Memory

Its a type of memory that can be accessed randomly

- that is, a memory location can be accessed without touching other locations.

RAM is also called Read-Write Memory, as you can perform both read and

write operations on this memory device.

RAM are of two types

•Static RAM (SRAM) :1) Storage cells are made of flip-flops and therefore don’t require refreshing in order to keep their data 2) The problem of flip-flops is that each cell requires at least 6 transistors to build and this cell hold only 1 bit 3) The use of 4-transistor cells plus the use of CMOS technology give birth to a high-capacity SRAM but its capacity is so far below that of DRAM

44

Page 45: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/

•Dynamic RAM :1) It’s lower than SRAM in price power consumption 2) Using a capacitor to store data cuts down the number of transistors to build the cells 3) The capacitors require constant refreshing due to charge leakage 4) While DRAM is being refreshed , its data cannot be accessed in contrast to SRAM whose data can be accessed any time as it doesn’t need refreshing

Random Access Memory45

Page 46: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/

Read Only Memory•It is a memory device from which the processor can read data but to which it

cannot write data. Like CD. The programs and data that needs to be permanently

stored are kept in ROM devices. ROM Retains its contents even if power is switched

off; therefore it is used to store program codes and any permanent data

retained to initialize and operate embedded systems. A verity of ROM devices

are available, each with different capabilities.

•PROM(Programmable ROM): It can be programmed only once.

1) The kind of ROM that the user can burn information into 2) For every bit , there is a fuse 3) PROM is programmed by blowing the fuses 4) The information can be burned into PROM only once , so it’s called OTP(one-time programmable)

46

Page 47: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/

Read Only Memory

•EPROM (Erasable ROM): It can be programmed many times.An

EPROM programmer is required to program the EPROM chip.

Can be programed and erased thousands of time 2) A widely used (EPROM) is called (UV-EPROM) 3) UV-EPROM has a window through which the programmer can shine ultra-violet radiation to erase its contents 4) To burn code into the UV-EPROM , the ROM burner uses 12.5 volts or higher 5) The major disadvantage with UV-EPROM is that it cannot be erased while it’s in the system board

47

Page 48: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/

Read Only Memory

Mask ROM

1) It’s not a user programmable ROM

2) It’s contents are only programmed by the IC-Manufacturer

3) Cheaper than other kinds of ROMs and is one-time programmable

(OTP)

48

Page 49: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/

Hybrid Memory

•Electrically Erasable Programmable ROM (EEPROM) : It

is similar to EPROM, but it can be erased electrically by

applying an electrical signal to one of the pins.

Its method of erasure is electrical and therefore instant as opposed to 20-minute erasure time required for UV-EPROM 2) In EEPROM , you can select the byte to be erased in contrast to UV-EPROM whose contents can be only entirely erased 3) It can be programmed while it’s in the system board without the need of an external erasure or programming device 4) It’s cost is higher than UV-EEPROM

49

Page 50: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/

Hybrid Memory

•Non-Volatile RAM (NVRAM) : It is a SRAM with battery backup so that the

contents are not erased even if power is switched off. Its is very expensive but data

access through it is fast.

1) It allows the CPU to read and write to it and when the power is off , the contents are not lost 2) It can retain its contents up to 10 years after turning off the power 3) It uses extremely power-efficient SRAM cells built out of CMOS 4) It uses internal lithium battery as a backup energy source 5) If the power to the Vcc pin falls bellow out of tolerance condition , the control circuitry switches automatically to its internal power source (the lithium battery) 6) Another type of the NVARM combines SRAM and EEPROM so that its content is written into the EEPROM when power is off and is read back from the EEPROM when power is restored.

50

Page 51: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/

Hybrid Memory

•Flash Memory : it is also a non - volatile memory, fast EEPROM. The main attraction

of flash is that it can be erased one block at a time and programmed one bit at a time.

Flash memory devices are high density, low cost, fast (to read but not to write), and

electrically programmable. It is being extensively used for embedded systems that

contain embedded OS and the application program such as handheld computers.

The erasure method is electrical so , it’s referred to as flash EEPROM 2) In old flash EEPROM you can only erase the entire contents (not just specific byte) 3) In recent decades flash memory contents are divided into blocks where erasing is done block by block 4) It can be programmed while it’s in the system board

51

Page 52: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/

User view of Computer Systems

Software that

controls the

hardware devices

Operating System –

- the user interface

Applications

e.g. Word, Netscape, etc

52

Page 53: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/

Watchdog Timer

Most of the embedded systems have no provision of resetting the processor in

such cases a watchdog timer is used.

It is set to a large value and is counted down.

When value reaches zero, the processor resets.

If things are fine and there is no need to reset the processor, the

processor resets the watchdog timer to that large value again.

53

Page 54: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/

54

Page 55: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

Register sets

The register section/array consists completely of circuitry used to temporarily store data or program codes until they are sent to the ALU or to the control section or to memory.

The number of registers are different for any particular CPU and the more register a CPU have will result in easier programming tasks.

Registers are normally measured by the number of bits they can hold, for example, an "8-bit register" or a "32-bit register".

55

Page 56: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

Registers

Use of Registers

Scratchpad for currently executing program

Holds data needed quickly or frequently

Stores information about status of CPU and currently executing program

Address of next program instruction

Signals from external devices

General Purpose Registers

User-visible registers

Hold intermediate results or data values, e.g., loop counters

Equivalent to LMC’s calculator

Typically several dozen in current CPUs

56

Page 57: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

57

Page 58: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

PC Program Counter Register58

Page 59: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

Instruction Register IR59

Page 60: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

Instruction Decoder ID60

Page 61: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

STACK Pointer SP Register61

Page 62: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

STACK Pointer SP Register62

Page 63: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

Status Register (SREG) 63

Page 64: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

Status Register (SREG) 64

Page 65: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

ATMega32 Programmer Model: Registers (SR)

CPUPC

ALU

registers

R1

R0

R15

R2

R16

R17

R30

R31Instruction Register

Instruction decoder

SREG: I T H S V N CZ

$0000

$0001

$0020

General

purpose

RAM

(SRAM)

$001F

$005F

TWBR

TWSR

SPH

SREG

...

General

Purpose

Registers

Standard IO

Registers

$00

$01

$3E

$3F

$0060

...

...

Data Address

Space

IO Address

...

$FFFF

SREG:

CarryZero

Negative

oVerflow

SignN+VHalf carry

Temporary

Interrupt

H S V N CZTI

65

Page 66: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

ATMega32 Programmer Model: Registers (SR)

CPUPC

ALU

registers

R1

R0

R15

R2

R16

R17

R30

R31Instruction Register

Instruction decoder

SREG: I T H S V N CZ

$0000

$0001

$0020

General

purpose

RAM

(SRAM)

$001F

$005F

TWBR

TWSR

SPH

SREG

...

General

Purpose

Registers

Standard IO

Registers

$00

$01

$3E

$3F

$0060

...

...

Data Address

Space

IO Address

...

$FFFF

SREG:

CarryZero

Negative

oVerflow

SignN+VHalf carry

Temporary

Interrupt

H S V N CZTI

Example: Show the status of the C, H, and Z flags after the addition of 0x38

and 0x2F in the following instructions:

LDI R16, 0x38 ;R16 = 0x38

LDI R17, 0x2F ;R17 = 0x2F

ADD R16, R17 ;add R17 to R16

Solution: 1

$38 0011 1000

+ $2F 0010 1111

$67 0110 0111 R16 = 0x67

C = 0 because there is no carry beyond the D7 bit.

H = 1 because there is a carry from the D3 to the D4 bit.

Z = 0 because the R16 (the result) has a value other than 0 after the addition.

66

Page 67: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

ATMega32 Programmer Model: Registers (SR)

CPUPC

ALU

registers

R1

R0

R15

R2

R16

R17

R30

R31Instruction Register

Instruction decoder

SREG: I T H S V N CZ

$0000

$0001

$0020

General

purpose

RAM

(SRAM)

$001F

$005F

TWBR

TWSR

SPH

SREG

...

General

Purpose

Registers

Standard IO

Registers

$00

$01

$3E

$3F

$0060

...

...

Data Address

Space

IO Address

...

$FFFF

SREG:

CarryZero

Negative

oVerflow

SignN+VHalf carry

Temporary

Interrupt

H S V N CZTI

Example: Show the status of the C, H, and Z flags after the addition of

0x9C and 0x64 in the following instructions:

LDI R20, 0x9C

LDI R21, 0x64

ADD R20, R21 ;add R21 to R20

Solution: 1

$9C 1001 1100

+ $64 0110 0100

$100 1 0000 0000 R20 = 00C = 1 because there is a carry beyond the D7 bit.

H = 1 because there is a carry from the D3 to the D4 bit.

Z = 1 because the R20 (the result) has a value 0 in it after the addition.

67

Page 68: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

Index Register (X,Y)68

Page 69: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

Accumulator Registers (A,B)69

Page 70: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

General purpose Registers

(R1,R2,R3,….) Most instructions have direct access to all registers ▫ LD, MUL

Some only operate on R16-R31

▫ ANDI, CPI, SUBI, MULS

A few operate only on R16:R23

▫ Special multiply operations

Double word operations operate on register pairs (R24-31)

Most single-byte register or register+immediate operations are single cycle instructions

70

Page 71: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/

Instruction Set Architecture ISA 71

Page 72: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/

AVR Assembler Statement

• The Assembler works on source files containing instruction mnemonics, labels anddirectives. The instruction mnemonics and the directives often take operands.

• Code lines should be limited to 120 characters.

• Every input line can be preceded by a label, which is an alphanumeric string terminated by acolon. Labels are used as targets for jump and branch instructions and as variable names inProgram memory and RAM.

• An input line may take one of the five following forms:

<label> <opcode> <operand> <;comments>

<label> <opcode> <operands> <;comments>

<label> <directive> <operands> <;comments>

Comments

Empty line

test: rjmp test ; Infinite loop (Instruction)

main: ldi R16, 9 ; store value 9 into register R16label: .EQU var1=100 ; Set var1 to 100 (Directive)

.EQU var2=200 ; Set var2 to 200; Pure comment line

72

Page 73: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/

AVR Assembler Output Files

EDITOR

PROGRAM

ASSEMBLER

PROGRAM

DOWNLOAD TO

AVR ’s FLASH

DOWNLOAD TO

AVR’s EEPROM

myfile.asm

myfile.objmyfile.eep myfile.hex myfile.map myfile.lst

73

Page 74: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/

AVR Assembler: Example

; Start on the flash ram's address 0.org 0

000000 e009 main: ldi R16, 9000001 e016 ldi R17, 6000002 0f10 add R17, R16000003 ef2f ldi R18, 255000004 bb27 out DDRB, R18000005 bb18 out PORTB, R17000006 cff9 rjmp main

LCMachine

code

Assembly

code

Assembly complete, 0 errors, 0 warnings

74

Page 75: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/

Instruction Set Architecture ISA 75

Page 76: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/

Instruction Set Architecture ISA 76

Page 77: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/

Instruction Life Cycle 77

Page 78: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/

Microprocessor revision (funny way) 78

Page 79: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/

Microprocessor revision (funny way) 79

Page 80: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/

Microprocessor revision (funny way) 80

Page 81: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/

Microprocessor revision (funny way) 81

Page 82: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/

Microprocessor revision (funny way) 82

Page 83: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

Microcontroller Microcontrollers are basically used in embedded systems. Computerized or

digital control of devices is made plausible with the development of microcontrollers. The development process of microcontroller is similar to that of a microprocessor.

Microcontrollers can be classified based on bus width, memory structure and instruction set. Bus width indicates the size of the data bus. Microcontrollers can be classified as 8 – bit, 16 – bit or 32 – bit based on the bus width. Higher bus widths often result in better performance.

83

Page 84: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

84

Page 85: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

What is the difference between microprocessor,

microcontroller and microcomputer...?

85

Page 86: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

MICROPROCESSOR MICROCONTROLLER

Microprocessor assimilates the function of a central processing unit (CPU) on to a single integrated circuit (IC).

Microcontroller can be considered as a small computer which has a processor and some other components in order to make it a computer.

Microprocessors are mainly used in designing general purpose systems from small to large and complex systems like super computers.

Microcontrollers are used in automatically controlled devices.

86

Page 87: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

MICROPROCESSOR MICROCONTROLLER

Microprocessors are basic components of personal computers.

Microcontrollers are generally used in embedded systems

Computational capacity of microprocessor is very high. Hence can perform complex tasks.

Less computational capacity when compared to microprocessors. Usually used for simpler tasks.

A microprocessor based system can perform numerous tasks.

A microcontroller based system can perform single or very few tasks.

87

Page 88: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

MICROPROCESSOR MICROCONTROLLER

Microprocessors have integrated Math Coprocessor. Complex mathematical calculations which involve floating point can be performed with great ease.

Microcontrollers do not have math coprocessors. They use software to perform floating point calculations which slows down the device.

The main task of microprocessor is to perform the instruction cycle repeatedly. This includes fetch, decode and execute.

In addition to performing the tasks of fetch, decode and execute, a microcontroller also controls its environment based on the output of the instruction cycle.

In order to build or design a system (computer), a microprocessor has to be connected externally to some other components like Memory (RAM and ROM) and Input / Output ports.

The IC of a microcontroller has memory (both RAM and ROM) integrated on it along with some other components like I / O devices and timers.

88

Page 89: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

MICROPROCESSOR MICROCONTROLLER

The overall cost of a system built using a microprocessor is high. This is because of the requirement of external components.

Cost of a system built using a microcontroller is less as all the components are readily available.

Generally power consumption and dissipation is high because of the external devices. Hence it requires external cooling system.

Power consumption is less.

The clock frequency is very high usually in the order of Giga Hertz.

Clock frequency is less usually in the order of Mega Hertz.

89

Page 90: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

MICROPROCESSOR MICROCONTROLLER

Instruction throughput is given higher priority than interrupt latency.

In contrast, microcontrollers are designed to optimize interrupt latency.

Have few bit manipulation instructions Bit manipulation is powerful and widely used feature in microcontrollers. They have numerous bit manipulation instructions.

Generally microprocessors are not used in real time systems as they are severely dependent on several other components.

Microcontrollers are used to handle real time tasks as they are single programmed, self sufficient and task oriented devices.

90

Page 91: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

Microcontroller Vs Microprocessor

https://www.youtube.com/watch?v=bJY78VIaPCE

91

Page 92: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

Embedded System Classifications

There are two main families of embedded system platforms:

MICROCONTROLLER Family

High Performance (System On Chip) SOC Platform Family

92

Page 93: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

93

Page 94: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

94

Page 95: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

SOCs Examples95

Page 96: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

microcontrollers and

SoC(System on Chip). https://www.youtube.com/watch?v=yTKg8LZoIVE

96

Page 97: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

Conclusion

SOC is the full system, often the computer on a chip, or at least that is the

goal. They are very powerful compared to mere micro controllers. SOC may have large amount of memory, peripheral interfaces, radio/wireless connection.May be multicore.

Microcontrollers are processor chips with inbuilt peripheral

components, ADC,DAC and some memory. Designing a system is easier with these than with a raw microprocessor.

Microprocessors are raw processors with minimal ALU+ registers

without any peripheral components. You need to connect other chips to make it useful.

97

Page 98: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

Von Neumann Vs Harvard

Earlier microprocessors made use of Von Neumann

architecture where the data and instructions

(programs) are stored in the same memory. Even

though this architecture is simple, there are many draw

backs. One of the major drawbacks is that instruction and

data cannot be accessed at the same time as they share

a single data bus.

This often degrades the overall performance of the

device. Later, Harvard architecture is introduced which

makes use of separate program and data memories

with separate buses so that both data and

instructions can be accessed at the same time. Later

Modified Harvard Architecture is developed in which the

program memory is accessed as if it were data memory

98

Page 99: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

von Neumann

• Same memory holds data, instructions. • A single set of address/data buses between CPU and memory

99

Page 100: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

Harvard Architecture

Separate memories for data and instructions.

Two sets of address/data buses between CPU and memory

100

Page 101: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

Harvard architecture

Address bus

Data bus

Control bus

Address bus

Data bus

Control bus

CPU Address bus

Data bus

Control bus

DataMemory

CodeMemory

CPUCodeMemory

DataMemory

Von Neumann architecture

Von Neumann vs. Harvard architecture 101

Page 102: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

pipeline102

Page 103: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

Computer SystemsNot All Processors Are Created Equal

Newer machines use specialized processor chips that have been developed to speed up the processing of data.

CISC - Complex instruction set computer

Large instruction set, many formats

RISC - Reduced instruction set computer

Small instruction set, single or small variations informat

MPP - Massive parallel processing

Many CPUs working in parallel

103

Page 104: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

CIS Vs RISC104

Page 105: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

CIS Vs RISC105

Page 106: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/

At mega 32

Atmel family Flash size

40 pins (4 port * 8)Vref>>>ADC

106

Page 107: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

AVR different groups Classic AVR

e.g. AT90S2313, AT90S4433 Mega

e.g. ATmega8, ATmega32, ATmega128 Tiny

e.g. ATtiny13, ATtiny25 Special Purpose AVR

e.g. AT90PWM216,AT90USB1287

107

Page 108: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

ATmega128

ATtiny44

Atmel groupFlash =128K

AtmelFlash =4K

AT90S4433

Atmel Classic group

Flash =4KTiny group

AVR part numbers108

Page 109: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

ATMega32 Programmer Model: Memory1. 2KB SRAM

– For temporary data storage– Memory is lost when power is

shut off (volatile)– Fast read and write

2. 1KB EEPROM– For persistent data storage– Memory contents are retained

when power is off (non-volatile)– Fast read; slow write– Can write individual bytes

3. 32KB Flash Program Memory– Used to store program code– Memory contents retained when

power is off (non-volatile)– Fast to read; slow to write– Can only write entire “blocks” of

memory at a time– organized in 16-bit words

(16KWords)

109

Page 110: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

ATMega32 Programmer Model: Memory

Type Flash RAM EEPROM

F_END Size, kB RAMEND Size, kB E_END Size, kB

Atmega8 $0FFF 8 $045F 1 $1FF 0.5

Atmega32 $3FFF 32 $085F 2 $3FF 1

Atmega64 $7FFF 64 $10FF 4 $7FF 2

Atmega128 $FFFF 128 $10FF 4 $FFF 4

• AVR microcontrollers are Harvardarchitecture. This means, that in thisarchitecture are separate memory types(program memory and data memory)connected with distinct buses. Such memoryarchitecture allows processor to accessprogram memory and data memory at thesame time. This increases performance ofMCU comparing to CISC architecture, whereCPU uses same bus for accessing programmemory and data memory.

• Each memory type has its own addressspace:

110

Page 111: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

ATMega32 Programmer Model: Data Memory

• GPRs (general purposeregisters),

• Special FunctionRegisters (SFRs), and

• Internal data SRAM.

The data memory iscomposed of three parts:

111

Page 112: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

References

https://docs.google.com/viewer?a=v&pid=sites&srcid=ZmtlLnV0bS5teXxyaWR6dWFuLXMtd2Vic2l0ZXxneDo2ODU0NzlkM2JkOTg4MjRk

http://www.avrprojects.net/index.php/avr-projects/sensors/38-humidity-and-temperature-sensor-dht11?showall=&start=1

http://www.cse.wustl.edu/~lu/cse467s/slides/dsp.pdf

http://www.avr-tutorials.com/

Microprocessor: ATmega32 (SEE3223-10) http://ridzuan.fke.utm.my/microprocessor-atmega32-see3223-10

http://circuitdigest.com/article/what-is-the-difference-between-microprocessor-and-microcontroller

http://cs4hs.cs.pub.ro/wiki/roboticsisfun/chapter2/ch2_7_programming_a_microcontroller

112

Page 113: Microcontroller part 1

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

113