Академический Документы
Профессиональный Документы
Культура Документы
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.
• 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 group
Flash
=128K
ATtiny44 AT90S4433
Atmel
Tiny Flash =4K Atmel Classic
Flash =4K
group 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
General Purpose I/O Ports
• Four 8 Bit I/O Ports
– Port A, Port B, Port C & Port D
– Pins identified as PAx, PBx, PCx or PDx (x=0..7)
Port x
Pull-up resistors
Pull-up resistors are used in electronic logic circuits to ensure
that inputs to logic systems settle at expected logic levels
if external devices are disconnected or high-impedance.
vcc
1 = Close
PORTx.n 0 = Open
pin n of
port x PINx.n
Port x
DDRx
0 1
PORTx
1 pull-up Out 1
The PINx register gets the reading from the input pins of the MCU
Port x
Programming
• Assign value 0x55 and 0xAA (alternatively after
specific delay) to the pin no. 33 to 40 of
Atmega32 (PDIP).
• Instruction Format
• e. g: Buzzer System
ATMega32 Architecture
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.
• Program flow is provided with conditional and unconditional jump and call
instructions, able to directly address the whole address space.
• 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.
• Since all AVR instructions are 16 or 32 bits wide, the Flash is organized as
16K × 16.
• 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:
– Because all the instructions in the AVR are either 2-byte or 4-byte, most
instruction takes no more than one or two machine cycle to execute.
– In the AVR family, the length of machine cycle depends on the frequency
of the oscillator connected to the AVR system.
– In the AVR, one machine cycle consists of one oscillator period, which
means that with each oscillator clock, one machine cycle passes.
– Therefore, to calculate the machine cycle for the AVR, we take the
inverse of the crystal frequency.
eg. : Find the size of delay of the code mention below if
Ans : 250,604 us
• Write a program to toggle all the bits of I/O
register PORTB every 1 sec. Assume that crystal
frequency is 8 MHz.
Register and Direct Addressing Mode
• The various ways of accessing data which are available in memory,
register or as an immediate value, are called addressing mode.
• eg. :
Single Register (Immediate) Addressing Mode
Cont…
Two Register Addressing Mode
• This addressing mode involves the use of two
registers to hold the data to be manipulated.
• 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.