Programación C++
Programación C++
Lecture 1
Introduction to the course
Computer system
AVR microcontroller: history and features
AVR architecture &
Assembly language programming
IICT, BUET
1
Lecture 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
Assessment
• Mid term I Exam – 25%
• Mid term II Exam – 25%
• Final Exam – 25 %
• Project Paper & Viva – 25%
IICT, BUET
2
References
• 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
Data Bus
IICT, BUET
3
The 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 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
IICT, BUET
4
Types 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
IICT, BUET
5
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
IICT, BUET
6
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
Microprocessor
Data Bus
CPU RAM ROM
Serial
ROM I/O Timer
CPU RAM COM Serial
Port
port I/O
Timer COM
ADC port
Address Bus
a) General Purpose Microprocessor System b) Microcontroller
IICT, BUET
7
General purpose microprocessor
system 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 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.
IICT, BUET
8
Characteristics of Embedded Systems
IICT, BUET
Design challenges
Main challenges
• Non-Recurring Engineering Cost
• Unit cost
• Size
• Performance
• Power
• Flexibility
• Time-to-market
• Time-to-prototype
• Correctness
• Safety
IICT, BUET
9
Microcontroller for embedded
system
• Microprocessors and microcontrollers are widely used in
embedded system products
• An embedded system is controlled by its own internal
microprocessor (or microcontroller) as opposed to
external controller
• Printer is an example of embedded system, it does only
one task, taking data from the PC and print it
• In a mouse, there is a microcontroller which tracks the
mouse’s position and sends the information to the PC
• In an embedded system, typically only one application
software is burned into ROM
IICT, BUET
10
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
(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
IICT, BUET
11
A brief history of AVR
microcontroller
• 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
IICT, BUET
12
AVR 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
IICT, BUET
13
AVR 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 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
IICT, BUET
14
The general purpose registers in the
AVR
• 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 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
IICT, BUET
15
ADD instruction
16
Different 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
IICT, BUET
17
Using Instructions with Data
Memory
• 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
IICT, BUET
18
Using Instructions with Data
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
IICT, BUET
19
Using Instructions with Data
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
I T H S V N Z C
IICT, BUET
20
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 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
IICT, BUET
21
Introduction to AVR assembly language
programming
• 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
IICT, BUET
22
The 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
IICT, BUET
23
AVR’s wakeup 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
D
D D
24
RISC 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
IICT, BUET
25