1. Microcontroller Basics
1. Microcontroller Basics
Microcontroller Basics
Contents
• Difference between microprocessor and
microcontroller
• CISC Vs RISC design philosophy
• Von-Neumann vs Harvard architecture
• 8-bit and 16-bit microcontroller
• Architecture of microcontroller
• I/O ports
• Stack and use of stack pointer
• Memory structure, Data Memory, Program Memory
and execution of programs
• Different registers (SFR’s)
• Addressing modes
• Timing diagram
Reference
• AVR ATmega32 Datasheet
• “The AVR Microcontroller and embedded
system”, M. A. Mazidi, S. Naimi
Introduction to Atmel AVR
• The AVR architecture was conceived by two students Alf-Egil Bogen
and Vegard Wollan at the Norwegian Institute of Technology (NTH).
• The AVR is a modified Harvard architecture 8-bit RISC single chip
microcontroller which was developed by Atmel in 1996.
• The AVR was one of the first microcontroller families to use on-chip
flash memory for program storage, as opposed to one-time
programmable ROM, EPROM, or EEPROM used by other
microcontrollers at that time.
• The AVR is a modified Harvard architecture machine where
program and data is stored in separate physical memory systems
that appear in different address spaces, but having the ability to
read data items from program memory using special instructions.
It is ancient computer architecture based on stored It is modern computer architecture based on Harvard
program computer concept. Mark I relay based model.
Same physical memory address is used for Separate physical memory address is used for
instructions and data. instructions and data.
CPU can not access instructions and read/write at the CPU can access instructions and read/write at the
same time. same time.
It is used in personal computers and small computers. It is used in micro controllers and signal processing.
Modified Harvard Architecture
• Emphasis on software
• Application-specific AVR
– megaAVRs with special features not found on the other members of
the AVR family, such as LCD controller, USB controller, advanced
PWM, CAN, etc.
ATmega128
Atmel grou
Flash
p
=128K
ATtiny44 AT90S4433
Atmel
Tiny Flash =4K Atmel Classi
Flash =4K
group c
group
The ATmega32 provides the following features:
• 32Kbytes of In-System Programmable Flash Program memory with Read-
While-Write capabilities
• 1024bytes EEPROM
• 2Kbyte SRAM
• 32 general purpose I/O lines
• 32 general purpose working registers
• A JTAG interface for Boundary scan
• On-chip Debugging support and programming
• Three flexible Timer/Counters with compare modes
• Internal and External Interrupts
• A serial programmable USART
• A byte oriented Two-wire Serial Interface
• An 8-channel, 10-bit ADC with optional differential input stage with
programmable gain
• A programmable Watchdog Timer with Internal Oscillator
• An SPI serial port
• Six software selectable power saving modes.
• The device is manufactured using Atmel’s high
density nonvolatile memory technology.
Port B Port A
Port C
Port D
• Digital IO is the most fundamental mode of connecting a MCU to
external world. The interface is done using a Port.
• A port is the point where internal data from MCU chip comes out
or external data goes in.
• They are present is form of Pins of the IC. Most of the PINs are
dedicated to I/O function and other pins are used for power supply,
clock source etc .
• ATMega32 ports are named PortA, PortB, PortC and PortD.
Port A
Block Diagram of the AVR MCU Architecture
• In order to maximize performance and parallelism, the
AVR uses a Harvard architecture – with separate
memories and buses for program and data.
• Status Register will in many cases remove the need for using
the dedicated compare instructions, resulting in faster and
more compact code.
• eg.1
;
;
• eg.2
;
;
• eg. 3
• eg. 4
• eg. 5
Programming (Instructions)
• LDI Rd, K ; (LDI : Load Data Immediate)
– Rd represents any general purpose register between R16 to R31.
– K represents immediate 8 bit value.
– LDI instruction is not valid for R0 to R15.
• ADD Rd, Rr ; Rd = Rd + Rs
Answer :
IN Vs. LDS
Advantages
• IN instruction faster than LDS. The IN instruction lasts
1 machine cycle, whereas LDS lasts 2 machine cycle.
• IN is a 2-byte instruction whereas LDS is a 4-byte
instruction.
Disadvantage
• IN instruction are applicable to only the standard IO
memory whereas we can access all parts of data
memory using LDS instruction.
Program Flash memory & SRAM
• The AVR Stack Pointer is implemented as two 8-bit registers in the I/O
space.The number of bits actually used is implementation dependent.
• For software security, the Flash Program memory space is divided into
two sections,
– Boot Program section
– Application Program section.
• The first 96 locations address the Register File and I/O Memory,
and the next 2048 locations address the internal data SRAM.
EEPROM Data Memory
• The ATmega32 contains 1024 bytes of data EEPROM memory.
• EEPROM does not lose its data when power is OFF, whereas SRAM
does.
• So, the EEPROM is used for storing data that should rarely be
changed and should not be lost when the power is OFF ; whereas
the SRAM is used for storing data and parameters that are changed
frequently.
Stack and Stack Pointer
• The CPU needs Stack storage area because there are only limited no.
of registers.
• Two registers from I/O memory space are used to initialize stack
pointer.
Stack Pointer
• PUSH Rr;
– Rr can be any general purpose registers (R0 - R31)
• PUSH R10;
– Store R10 onto the stack and decrement SP.
Popping from the Stack
• Popping the contents of the stack back into the given register is
the opposite process of pushing.
• POP Rd;
– Rd can be any general purpose registers (R0 - R31)
• eg : POP R10;
– Increment SP, and then load top of the stack to R16.
• eg : 0x 08 After Execution :
0x 5F
CALL instruction and the role of Stack
• When the subroutine is called, the processor first
saves the address of the instruction just below the
CALL instruction (contents of PC) on the stack, and
then transfers control to that subroutine.
• Ans:
• eg. :
• ROR (Rotate Right)
• eg. :
Two Register Addressing Mode
Cont…
Direct Addressing Mode
• The entire memory can be accessed using direct
addressing mode.
• eg . :
Direct Addressing Mode
Cont…
I/O Direct Addressing Mode
• The I/O direct addressing mode can address
only the standard I/O registers.