ICT 6641: Advanced Embedded Systemteacher.buet.ac.bd/lutfulkabir/ICT6641_April2011/Lecture_1.pdf ·...

25
1 Lecture 1 Introduction to the course Computer system AVR microcontroller: history and features AVR architecture & Assembly language programming Prof. S. M. Lutful Kabir IICT, BUET Session: April, 2011 ICT 6641: Advanced Embedded System ICT 6641: Advanced Embedded System IICT, BUET Prerequisite knowledge for the Prerequisite knowledge for the course course • Digital Electronics • Analogue Electronics • Basic power circuits • Experience of making electronics boards • Programming in C language

Transcript of ICT 6641: Advanced Embedded Systemteacher.buet.ac.bd/lutfulkabir/ICT6641_April2011/Lecture_1.pdf ·...

1

Lecture 1

Introduction to the course

Computer system

AVR microcontroller: history and features

AVR architecture &

Assembly language programming

Prof. S. M. Lutful Kabir

IICT, BUET

Session: April, 2011

ICT 6641: Advanced Embedded SystemICT 6641: Advanced Embedded System

IICT, BUET

Prerequisite knowledge for the Prerequisite knowledge for the

coursecourse

• Digital Electronics

• Analogue Electronics

• Basic power circuits

• Experience of making electronics boards

• Programming in C language

2

IICT, BUET

Lecture PlanLecture Plan

• Lecture # 1: AVR microcontroller: History and Features, AVR Architecture and

assembly language programming

• Lecture # 2: Branch, Call and Delay Loops, AVR I/O port programming

• Lecture # 3: Arithmetic, Logic Instructions, AVR Advanced assembly language

programming,

• Lecture # 4: Assigning projects

• Lecture # 5: MID-TERM-I Examination, AVR programming in C, AVR Hardware

Connection, Hex file and flash loader

• Lecture # 6: AVR programming in C (continued)

• Lecture # 7: AVR Timer programming

• Lecture # 8: AVR Interrupt programming

• Lecture # 9: MID-TERM-II Examination, AVR serial port programming

• Lecture #10: ADC, DAC and sensor interfacing

• Lecture #11: SPI And I2C protocol

• Lecture #12: LCD and Keyboard Interfacing

• Lecture #13: More Interfacing concepts

• Lecture #14: Presentation on Projects

IICT, BUET

AssessmentAssessment

• Mid term I Exam – 25%

• Mid term II Exam – 25%

• Final Exam – 25 %

• Project Paper & Viva – 25%

3

IICT, BUET

ReferencesReferences

• The AVR microcontroller and embedded system:

using assembly and C by Muhammad Ali Mazidi,

Sarmad Naimi and Sepehr Naimi

• C Programming for microcontrollers, featuring

ATMEL’s AVR Butterfly and the free WinAVR

compiler by Joe Pardue

• The reference books, lecture notes and other

materials related to this course can be downloaded

using the following link

http://teacher.buet.ac.bd/lutfulkabir/ICT6641_April2011/

IICT, BUET

Internal Organization of a ComputerInternal Organization of a Computer

• Internal working of every computer can be broken

down in three parts:

– CPU (Central Processing Unit)

– Memory and

– I/O (Input/Output) Devices

Address Bus

Data Bus

Control Bus (MEMR, MEMW, IORD, IOWR)

CPURAM ROM PRINTER DISK MONITOR KEYBOARD

4

IICT, BUET

The function of CPUThe function of CPU

• To execute (process) information stored in memory

• The CPU is connected to memory and I/O devices

through strips of wires called BUS

• Three types of BUSES: Data, Address and Control Bus

• Each device has an unique address

• Address is placed on the address bus, the data to be

written or to be read is placed on the data bus and

the proper signal of read or write is placed on the

control bus

IICT, BUET

Instruction executionInstruction execution

We can think of a microprocessor’s execution of instructions as consisting of several basic stages:

• Fetch instruction: the task of reading the next instruction from memory into the instruction register

• Decode instruction: the task of determining what operation the instruction in the instruction register represents (e.g., add, move, etc.)

• Fetch operands: the task of moving the instruction’s operand data into appropriate registers

• Execute operation: the task of feeding the appropriate registers through the ALU and back into an appropriate register

• Store results: the task of writing a register into memory. If each stage takes one clock cycle, then we can see that a single instruction may take several cycles to complete

5

IICT, BUET

Types of memoryTypes of memory

• ROM

– Does not lose its contents when the power is turned off

– Called non-volatile memory

– Different types : PROM, EPROM, EEPROM, Flash EPROM, mask ROM

• PROM

– Programmable, once burned can not be changed

– So, it is also called One Time Programmable (OTP)

• EPROM

– Erasable, can be erased by UV light, takes long time (20m)

– So, it is also called UV-EPROM

IICT, BUET

Types of memory (continued)Types of memory (continued)

• EEPROM

– Electrically erasable

– Takes less time

– A portion (even a byte) can be erased

– Can be erased while it is on-board

• Flash EPROM

– Same as EEPROM but a byte can not be erased

– Can be erased block by block

6

IICT, BUET

Types of memory (continued)Types of memory (continued)

• Mask ROM

– It refers to the ROM in which the contents are

programmed during IC fabrication stage

– Costly, so done when volume is high

• RAM

– Called volatile memory, data losses with power being cut-

off

– Three types of RAM

• SRAM

• NV-RAM

• DRAM

IICT, BUET

Types of memory (continued)Types of memory (continued)

• SRAM (static RAM)

– Storage cells are made of flip-flops

– Requires number of transistor to keep one bit of data

– Use of CMOS technology allows producing high capacity

SRAM, but still far below than DRAM

• NV-RAM

– Like other RAM, CPU can read/write on NV-RAM but unlike

them, data does not loss on Power-cut

– Use an internal lithium battery to store the energy

– Uses an intelligence control circuit

7

IICT, BUET

Types of memory (continued)Types of memory (continued)

• DRAM

– Uses capacitor to store bits of data

– Requires continuous refreshing due to leakage

– Smaller in size, so high density

IICT, BUET

Microcontroller versus Microcontroller versus

MicroprocessorMicroprocessor

• When one talk about microprocessor it is meant for general

purpose processor

• It does not have and built-in RAM, ROM, I/O ports etc.

• On the other hand, microcontroller contains all those built-in

CPU

CPU RAM ROM

I/O

ADC

Timer

Serial

COM

port

ROMRAMI/O

PortTimer

Serial

COM

port

Data Bus

Address Bus

a) General Purpose Microprocessor System b) Microcontroller

8

IICT, BUET

General purpose microprocessor General purpose microprocessor

system and microcontroller systemsystem and microcontroller system

• A designer using a general purpose microprocessor such

as Pentium or PowerPc must add RAM, ROM, I/O ports

to make them functional

• Although it becomes bulkier and costlier still they have

the advantage of versatility

• But for the microcontroller all are integrated in a single

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

• In many applications, the space used, the power

consumed and the price per unit is much more critical

consideration than the computing power

• In those applications, microcontrollers are used

IICT, BUET

Application of MicrocontrollersApplication of Microcontrollers

• Consumer electronics -- cell phones, pagers, digital cameras, camcorders, video cassette recorders, portable video games, calculators, and personal digital assistants;

• Home appliances -- microwave ovens, answering machines, thermostat, home security, washing machines, and lighting systems;

• Office automation -- fax machines, copiers, printers, and scanners;

• Business equipment -- cash registers, curbside check-in, alarm systems, card readers, product scanners, and automated teller machines;

• Automobiles -- transmission control, cruise control, fuel injection, anti-lock brakes, and active suspension.

9

IICT, BUET

Characteristics of Embedded SystemsCharacteristics of Embedded Systems

• Single-functioned: An embedded system usually executes only one program, repeatedly

• Tightly constrained: All computing systems have constraints on design metrics, but those on embedded systems can be especially tight

• Reactive and real-time: Many embedded systems must continually react to changes in the system’s environment, and must compute certain results in real time without delay

IICT, BUET

Design challengesDesign challenges

Main challenges

• Non-Recurring Engineering Cost

• Unit cost

• Size

• Performance

• Power

• Flexibility

• Time-to-market

• Time-to-prototype

• Correctness

• Safety

10

IICT, BUET

Microcontroller for embedded Microcontroller for embedded

systemsystem

• Microprocessors and microcontrollers are widely used in

embedded system products

• An embedded system is controlled by its own internal

microprocessor (or microcontroller) as opposed to

external controller

• Printer is an example of embedded system, it does only

one task, taking data from the PC and print it

• In a mouse, there is a microcontroller which tracks the

mouse’s position and sends the information to the PC

• In an embedded system, typically only one application

software is burned into ROM

IICT, BUET

High end embedded systemHigh end embedded system

• Sometimes microcontroller is inadequate for a

certain embedded system

• So, some manufacturers like Intel, AMD, Freescale

targeted their microprocessors for the high end

embedded market

• For example, PowerPC microprocessor

manufactured jointly by IBM and Freescale is

targeted for that market

• Another chip called ARM (Advanced Risk Machine) is

used for high end embedded system

11

IICT, BUET

Choosing a microcontroller Choosing a microcontroller

• Efficiency and cost effectiveness

– Speed

– Packaging

– Power consumption

– The amount of ROM and RAM

– The number of I/O pins and timers

– Ease of upgrade to higher performance and lower

power consumption

– Cost per unit

IICT, BUET

Choosing a microcontroller Choosing a microcontroller

(continued)(continued)

• Easy to develop product around it. Availability of

– Assembler

– Debugger

– Code efficient C language compiler

– An emulator

– Technical support

– Expertise

• Ready availability now and in near future. In recent

years, companies have begun to sell Field-Programmable

Gate Array (FPGA) and Application Specific Integrated

Circuit (ASIC) libraries for the different microcontrollers

12

IICT, BUET

A brief history of AVR A brief history of AVR

microcontrollermicrocontroller

• Two students of Norwegian Institute of Technology – Alf

Bogan and Vegard Wollan designed the basic

architecture of AVR

• How did the name come? Atmel says it is nothing but a

product name. Guess works – Advanced Virtual Risk, Alf

Vegard RISC

• Almost all of AVR microcontrollers are 8-bit

• They are not 100% compatible when moved from one

family to other

• Recompilation and changing of some code requires

• Four types: Mega, Tiny, Special Purpose and Classic

IICT, BUET

AVR family overviewAVR family overview

• Classic AVR (AT90Sxxxx)

– This is the original AVR Chip

• Mega AVR (ATmegaxxxx)

– Powerful, with more than 120 instructions and lots of peripheral

capabilities

• Tiny AVR (ATtinyxxxx)

– Have less number of instructions and smaller package

• Special Purpose AVR

– Considered as a subset of other groups

– Special capabilities are made for designing specific application

– USB controller, CAN controller, LCD controller, Zigbee, Ethernet

controller, FPGA, Advanced PWM etc

13

IICT, BUET

AVR featuresAVR features

• RISC Architecture

• Harvard Architecture

• On chip program ROM

• Data RAM

• Data EEPROM

• Timers

• I/O ports

• Most of the AVRs have ADC, PWM, USART

• Some of them have SPI, I2C (TWI), CAN and USB interface

IICT, BUET

AVR Microcontroller program ROMAVR Microcontroller program ROM

• The ROM is used for storing the program and hence

is called program ROM

• AVR has the capabilities of 8M ROM space but

program ROM size varies from 1K to 256K

• AVR is one of the first microcontrollers to use on-

chip Flash memory for program storage

14

IICT, BUET

AVR Microcontroller peripheralsAVR Microcontroller peripherals

• AVR can have 3 to 86 pins for I/O

• Most of the AVR comes with ADC, timer and USART

as standard peripherals

• ADC is 10-bit and number of channel can be upto 16

IICT, BUET

Other MicrocontrollersOther Microcontrollers

• Microchip produces PIC family

• Freescale (formerly Motorola) makes HCS08 and

many of its variations

• Zilog produces the Z8 and its variations

• Intel and number of other companies make 8051

microcontrollers

15

IICT, BUET

The general purpose registers in the The general purpose registers in the

AVRAVR

• CPU uses many registers to store information temporarily

• This information could be a byte of data to be processed or an address pointing to the data to be fetched

• In AVR, there are 32 general purpose registers, they are R0-R31 and are located in the lowest location of memory address

IICT, BUET

LDI InstructionLDI Instruction

• LDI Rd, K ; load Rd (destination) with immediate

value K) and 16<=d<=31

• “I” in LDI instruction stands for immediate

• Among the two operands of LDI instruction

destination comes first

• Moving a value larger that 255 will cause an error

16

IICT, BUET

ADD instructionADD instruction

• The ADD instruction has the following

format:

ADD Rd, Rr ; ADD Rr ro Rd and store the

result in Rd

• To add two numbers such as 0x25 and

0x34, one can do the following:

LDI R16, 0x25 ; Load 0x25 into R16

LDI R16, 0x34 ; Load 0x34 into R17

ADD R16, R17 ; Add value R17 to R16

IICT, BUET

The AVR Data MemoryThe AVR Data Memory

• Two kinds of memory space:

– Code memory

– Data memory

• Data memory has three parts:

– GPRs

– I/O memory

– Internal data SRAM

General

Purpose

Registers

I/O

Registers

(SFRs)

General

purpose

RAM

(SRAM)

17

IICT, BUET

Different types of data memoryDifferent types of data memory

• GPRs

– Regardless of AVR chip, GPRs occupies 0x00-0xFF

addresses

• I/O memory

– Dedicated to specific functions such as status register,

timers, serial communication, I/O ports, ADC and so on.

– Space varies from chip to chip

– Standard 64 bytes of I/O memory

– Extended I/O memory > if more than 64 bytes

IICT, BUET

Different types of data memory Different types of data memory

(continued)(continued)

• Internal data SRAM

– Scratch pad

– Used by program to store data temporarily

– Size varies from chip to chip

– May be standard (512 byte) and extended

• EEPROM

– A small amount of EEPROM is available

– Size varies from chip to chip

18

IICT, BUET

Using Instructions with Data Using Instructions with Data

MemoryMemory

• Other than Immediate value or data at the GPRs, AVR allows direct access to other locations in the data memory

• LDS instruction

– LDS Rd, K ; load Rd with the contents of location K, K is the address between 0x0000 to 0xFFFF

• The following program adds the contents of 0x300 to location 0x302

– LDS R0, 0x300 ; R0 = the contents of location 0x300

– LDS R1, 0x302 ; R1 = the contents of location 0x302

– ADD R1, R0 ; add R0 to R1 and store in R1

IICT, BUET

Using Instructions with Data Using Instructions with Data

Memory (continued)Memory (continued)

• STS Instruction (STore direct to data Space)

STS K, Rr ; Store the content of register Rr into the memory

location having address K

• The following program first loads the content of R16

register with value 0x55, then moves this value to a

memory location having address 0x38

LDI R16, 0x55 ; R16 = 55 (in hex)

STS 0x38, R16 ; Copy R16 to 0x38

19

IICT, BUET

Using Instructions with Data Using Instructions with Data

Memory (continued)Memory (continued)

• IN instruction (IN from I/O location)

– IN Rd, A ; load an I/O location to one GPR

• This instruction is only valid for I/O locations (see

page 64 of the book)

• Here we have to use relative address of I/O

locations

• Similar to LDS, but IN has several advantages

– IN > 1 m/c; LDS > 2 m/c

– For IN we can use name of the I/O registers

– IN is available in all AVR, but LDS is not available in all

IICT, BUET

Using Instructions with Data Using Instructions with Data

Memory (continued)Memory (continued)

• OUT instruction (OUT to I/O location)

– OUT A, Rr ; store to I/O location

• To store the content of R10 to the I/O register called

PORTD, one should write

– OUT PORTD, R10

20

IICT, BUET

Using Instructions with Data Using Instructions with Data

Memory (continued)Memory (continued)

• MOV instruction

– The MOV instruction is used to copy data among the GPR

registers, it has the following format

– MOV Rd, Rr

• INC Rd, DEC Rd, COM Rd

– Increment, decrement and complements the content of

the register

IICT, BUET

AVR Status RegisterAVR Status Register

• It is an 8-bit register

• Also called flag register

I T H S V N Z C

C - Carry flag S - Sign flag

Z - Zero flag H - Half carry

N - Negative flag T - Bit copy storage

V - Overflow flag I - Global Interrupt Enable

21

IICT, BUET

AVR Status Register (continued)AVR Status Register (continued)

• C, the carry flag – carry out from D7

• Z, the zero flag – if the result of an arithmetic or logic operation zero

• N, the negative flag – used for signed number

• V, the overflow flag – used for signed number

• S, the sign flag – used for signed number

• H, the half carry flag – when there is a carry from D3 to D4

• T, Bit copy storage – will be discussed later

• I, Global Interrupt enable – will be discussed later

IICT, BUET

Assembler DirectivesAssembler Directives

• Instructions – direction to the CPU

• Directives – direction to the assembler

• .EQU, .SET – same, but .SET directive may be

reassigned

• .ORG – beginning of the address

• .INCLUDE – to add the content of a file

• Label – to reference a point in the program; label

name must be unique; consists of a-z, A-Z, 0-9, ?, . ,

@, _ ; not a number, not reserved word

22

IICT, BUET

Introduction to AVR assembly language Introduction to AVR assembly language

programmingprogramming

• CPU can work only in binary

• Programming in binary, called machine language, is tedious

• Eventually assembly language was developed

• This use shorter form of binary codes called mnemonics

• A program called assembler converts mnemonics into machine language

• Assembly language is referred to as low level language

• But now a days high level language such as C is used to program microcontroller

IICT, BUET

Structure of Assembly LanguageStructure of Assembly Language

• Contains instructions called mnemonics optionally

followed by one or two operands

• In general, there are four fields –

[label] mnemonics [operands] [;comment]

• Bracket indicates that the fields are optional

• Comment field starts with ; like // in C

• Assembler ignores the comments

23

IICT, BUET

The program counterThe program counter

• The most important register in the AVR microcontroller is the program counter (PC)

• It is used by the CPU to point to the address of the next instruction to be executed

• As CPU fetches opcode from the program ROM, the program counter is incremented automatically to point to the next instruction

• The wider the program counter the more memory locations a CPU can access; a 14 bit program counter can access 214 = 16K byte of program memory

IICT, BUET

Program ROM spaceProgram ROM space

• Some family members have only a few kilobytes of

on-chip ROM and some, such as the ATmega128,

have 128K of ROM

• No member can access more than 4M of ROM

because the program counter can not be more than

22 bits

24

IICT, BUET

AVRAVR’’ss wakeup locationwakeup location

• In case of AVR, it wakes up at 0x0000 location when

it is powered up

• That means that it expects the first opcode to be

stored in 0x0000 location

• We achieve this by using .ORG statement

IICT, BUET

Harvard Architecture in AVRHarvard Architecture in AVR

• AVR uses Harvard Architecture, it means that it uses

separate buses for the code and the data memory

• Program bus provides access to the program flash

whereas data bus is used for bringing data to the CPU

CODE

MEMORY

DATA

MEMORY

CODE

MEMORY

DATA

MEMORYCPU

CPU

A

D

A A

D D

Von Neumann Harvard

25

IICT, BUET

RISC Architecture in the AVRRISC Architecture in the AVR

• More or less fixed instruction size

• Large number of registers

• Less number of instruction set

• More than 95% of instructions are executed in one

clock cycle

• Harvard Architecture

• No microinstruction circuitry

• Load/ Store architecture

IICT, BUET

End of Lecture 1