microcontroller slide
microcontroller slide
Group-14
A microcontroller is a VLSI
Integrated Circuit housing a CPU,
memory , I/O ports, and other
components, designed for specific
task control within electronic
systems.
Note : VLSI(Very Large-Scale
Integration)
Common Types of Microcontroller
PIC (Peripheral ARM (Advanced RISC 8051: AVR (Alf and Vegard's MSP (Mixed Signal
Interface Controller): Machine): RISC Processor): Processor):
Microchip's PIC excels ARM microcontrollers, Intel's 8051, an 8-bit AVR microcontrollers, Part of Texas
with separate code and balancing cost powerhouse since 1981, with a modified Instruments, MSP
data storage, sensitivity and high features a versatile 40- Harvard architecture, microcontrollers
enhancing efficiency in performance, dominate pin DIP form factor, distinguish themselves feature a 16-bit CPU
diverse electronic the digital embedded 4KB ROM, and 128 by storing program designed for low cost
systems. system landscape in bytes of RAM, making and data separately, and power-efficient
applications like it suitable for a range offering flexibility embedded systems,
industrial control and of applications. across diverse streamlining
wireless networking. applications. programming for
efficient performance.
Microcontroller vs
Microprocessor
The basic architecture of AVR was designed by Alf-Egil Bogen and Vegard Wollan, students of Norwegian
Institute of Technology (NTH). Later in 1996, Atmel acquired and further enhanced this architecture.
According to Atmel, AVR is simply a product name; however, some suggest it could represent
"Advanced Virtual RISC" or potentially it could be named using their acronym "Alf and Vegard
RISC.”
Note: CISC - Complex Instruction Set Computer (CISC architecture has a lot of complex instruction
set built-in into the CPU to handle various tasks)
RISC - Reduced Instruction Set Computer (RISC architecture has simpler instruction set built-in into
the CPU to improve efficiency and speed)
AVR features
Microcontroller Peripherals:
AVRs typically feature special functions like ADC (analog-to-digital
converter), timers, and USART (Universal Synchronous Asynchronous
Receiver Transmitter) as standard peripherals. These enhance the
microcontroller's capabilities for diverse applications.
General Architecture of a
AVR microcontroller:
Chapter 2 -
AVR ARCHITECTURE AND ASSEMBLY LANGUAGE PROGRAMMING
General Purpose
Microcontroller Microcontroller
Registers in
Data Memory Status Register
Microcontroller
MSB LSB
GPRs
Structure:
LDI Rd, K ; load Rd(destination) with Immediate value of K
The LDI loads the right operand to the left operand. Hence the destination
comes first, then comes the source.
Example:
LDI Rd20,0x27 ; load Rd20 with 0x27 (Rd20 = 0x27)
LDI Instruction (Load Immediate)
1. d must be between 16 and 31. We cannot load values into registers R0 - R15 using the LDI
instruction. It would display "invalid instruction".
To present a number in hex, a dollar sign ($) or a 0x can be put in front of it.
3. If values 0 to F are moved into an 8-bit register, the rest of the bits are assumed to be all
zeros.
4. Moving a value larger than 255 (FF in hex) into the GPRs will cause an error.
ADD Instruction
1. Write instruction to move the value 0x34 into the R29 register.
Answer: LDI R29, 0x34
2. Write instructions to add the values 0x16 and 0xCD. Place the result in
the R19 register.
Answer: LDI R20, 0x16
LDI R19, 0xCD
LDI R19, R20
Microcontroller Data
Memory
Chapter – 2.2
Microcontroller Data Memory
In AVR microcontrollers, there are two The data memory is again composed of
kinds of memory space: three parts-
Code Memory Space: Saves the code GPRs (General Purpose Registers)
I/O memory (Special Function
Data Memory Space: Stores the data Registers , SFRs)
Internal data SRAM
GPRs:The GPRs use 32 bytes of data memory space. They always take the address location $00–$1F in the
data memory space, regardless of the AVR chip number.
SFRs: The AVR I/O memory is also a 8-bit register. This are dedicated to specific functions such as status
register, serial communication, I/O ports, ADC(Analog to Digital Converter), and so on. The function of
each I/O memory location is fixed by the CPU designer. The number of I/O memory depends on the pin
numbers and peripheral functions supported by that chip and the number varies from chip to chip.
However, all of the AVRs have at least 64 bytes of I/O memory locations. This 64-byte section is called
standard I/O memory.
SRAMs: Internal data SRAM is widely used for storing data and parameters by AVR programmers. Each
location is 8 bits wide. Generally, this is called scratch pad. Again, the size of SRAM can vary from chip to
chip.
Memory Space
EEPROM SRAM
SRAM is a volatile memory
The AVR has an
used for temporary data
EEPROM memory that is
storage
used for storing data. SRAM loses its data when
EEPROM does not lose its
power is off.
data when power is off. The EEPROM is used for
The EEPROM is used for
storing data that should
storing data that should
frequently be changed.
rarely be changed.
Some AVR CHIPS
Recap
The I/O registers are used as an interface to the I/O devices and peripherals on board the
microcontroller.
2. The GPRs together with I/O registers and SRAM are called Data Memory.
MOV instruction
The MOV instruction is used to copy data among the GPR registers of R0–R31.
Format:
MOV Rd, Rr ;Rd = Rr (copy Rr to Rd)
;Rd and Rr can be any of the GPRs
Example:
MOV R10, R20 ;R10 = R20
Instructions USING GPR
AVR Status Register
Chapter – 2.4
AVR Status Register
Importance :
Efficient Programming: Flags aid in optimizing code by providing feedback.
Interrupt Control: The Global Interrupt Flag is pivotal for managing interrupt-driven tasks.
Debugging: By monitoring these flags, developers can better understand the outcome of operations.
Components of AVR Register
5. Sign Flag(S): 6. Half carry Flag(H): 7. Bit copy Storage (T): 8. Global Interrupt Flag (I):
S = N ⊕V; Indicates if the Indicates a carry between bit 3 Temporary storage for bit I= 1; Global interrupts
result is negative based on two's and bit 4 during arithmetic manipulations. enabled.
compliment operations. I = 0; Global interrupts
disabled.
ADD Instruction and Status
Register- Example1
Used to define a constant Used to define a Tells the AVR assembler Used to indicate the
value or a fixed address. constant value or a fixed to add the contents of a beginning of an address
Example: address. file to our program (like
.EQU COUNT = 0x25 Example: the #include directive in C
LDI R21, COUNT .SET COUNT = 0x25 language).
LDI R21, COUNT Example:
.INCLUDE “M32DEF.INC”
.SET vs .EQU: The only difference is that the value assigned by the .SET directive may be reassigned later.
Rules For Labels
The label field allows the program to refer to a line of code by name
Mnemonic (instruction) and operand(s) fields together performs a task
The comment field begins with a semicolon and is ignored by the assembler
** Brackets indicate that a field is optional
Structure of
Assembly Language
A sample program
would look like that.
Assembling a
program
Use a text editor to type in a program
which can produce an ASCII file (such
as AVRStudio IDE, Notepad) and save
the source file with “asm” extension.
Feed the “asm” extension source file to
an AVR Assembler and it will produce
an object file, a hex file, an eeprom file,
a list file and a map file.
The hex file is burned into the AVR’s
program rom.
Assembling a program
The map file shows the labels defined in the program together with their
values. Whereas the list (lst) file is optional, and it shows the binary and
source code. It can be used to know:
Which instructions are used
The amount of memory the program uses
Assembling a
program
ATmega32 has a 14-bit program counter. The ATmega64 has a 15-bit program
That means it can access a maximum counter, so it has 32K locations (2^15 =
of2^14 = 16K of program memory 32K). As each location containing 2
locations. In AVR microcontrollers each bytes of memory the maximum capacity
memory location is 2 bytes wide. So the is 64K bytes. The addresses range from
memory can hold maximum 32K bytes $0000 to $7FFF.
of code. The addresses range from $0000
to $3FFF.
Program
Counter
The program counter in the AVR
family can be up to 22 bits wide.
This means that the AVR family can
access program addresses $000000 to
$3FFFFF, a total of 4M
locations. Because each location is 2
bytes wide, the AVR can have a
maximum of 8M bytes of code.
AVR On-Chip Program
ROM Address Range
When the AVR is powered up, the PC (program
counter) has the value of 00000 in it.
Let’s see a code and examine the PC: .
ORG 0
LDI R16, 0x25
LDI R17, 0x34
The machine code for LDI R16, 0x25 is “E205” where E is the opcode and 205 is the operands. Similarly, the machine code
for LDI R17, 0x34 is “E314”. E205 is 2 bytes in length. So, it occupies only one memory address from the ROM space. As it’s
the first instruction the code E205 is stored in location $00000 and code E314 is stored in $00001.
When the AVR is powered up, the PC (program counter) has 00000 and starts to fetch the first instruction from location
00000 of the program ROM. In the case of the above program the first code is E205, which is the code for moving operand
0x25 to R16. Upon executing the code, the CPU places the value of 25 in R16. Now one instruction is finished. Then the
program counter is incremented to point to 00001 (PC = 00001), which contains code E314, the machine code for the
instruction “LDI R17,0x34”.
Program ROM Width for the
AVR
Branch instructions and looping,
call instructions and stack
Chapter- 3.1, 3.2
Importance of Loops in Programming:
Branch Loops are fundamental structures in programming.
Enable the repetition of a sequence of instructions or operations.
LDI R21, 0
LDI R20, 10
Again: ADD R21, R20
DEC R20
BRNE Again
Question: What is the maximum number
Nested Loop of times the loop in the Example can be
repeated?
Location R20 is an 8-bit register, it can hold
maximum of 0xFF (255 in decimal). Therefore,
the loop can be repeated a maximum of 255
times.
LDI R16, 0x55
LDI R22, 10
LOP_2: LDI R21, 100
LOP_1: COM R16 Question: What if I want the loop to run
DEC R21 more than 255 times?
BRNE LOP_1
DEC R22 Loop inside a loop. To do that, we use a loop
BRNE LOP_2 inside a loop, which is called a nested loop. Two
This loop will run total 10*100 = 1000 times.
registers give us a maximum value of 255 × 255
= 65,025 so a nested 2 loop can run more than
65k times. We can use three registers to go more
than 16 million (255 x 255 x 255) iterations
Conditional Jump
10 bits for opcode, 22 bits for the target Only 12 bits of the 2 bytes are used for the
subroutine address. address.
Because the Z register is 16 bits wide, the ICALL The Z register is loaded into the lower 16 bits of the
instruction can call subroutines that are within the Program Counter (PC), and the EIND register is
lowest 64K words of the program memory. loaded into the upper 6 bits of the PC.
The Z register plays a crucial role in the ICALL Similar to ICALL, it jumps to the address pointed by
instruction. It specifies the target address of the the Z register. However, it also takes into account the
EIND register for extended memory. This allows the
subroutine to be called. EICALL instruction to access a larger memory space
compared to the ICALL instruction.
The stack is a section of RAM used by the CPU to store
Stack information temporarily. This information could be data or
an address. The CPU needs this storage area because there
are only a limited number of registers.
When data is pushed onto the stack, it gets stored at When data is popped from the stack, the Stack
the location pointed by the Stack Pointer (SP). Pointer (SP) is first incremented to point to the
most recently pushed item.
The PUSH instruction is used to push data onto the The POP instruction is used to pop data from the
stack. stack.
For example, PUSH R10 stores the contents of For instance, POP R16 increments the SP and then
register R10 onto the stack and decrements the SP. loads the top of the stack into register R16. This
This means that the value in R10 is saved onto the means that the most recently pushed value is
stack for later use, and the SP now points to the removed from the stack and stored in R16 for use in
next available location on the stack. the program.
Time Delay
Chapter – 3.3
Instruction cycle time for the AVR
The following shows the frequency for four different AVR-based systems.
Find the period of the instruction cycle in each case -
(a) 8 MHz
(b) 16 MHz
(c) 10 MHz
(d) 1 MHz
Solution:
For an AVR system of 1 MHz, find how long it takes to execute each of the
following instructions:
(a) LDI (b) DEC (c) LD
(d) ADD (e) JMP (f) CALL
(g) BRNE (h) .DEF
Solution
Here, LDI, DEC, LD, ADD are 1 byte instruction so it takes only one cycle to execute
this instructions. Whereas, JMP, CALL are 3 and 4 byte instructions so they take 3 and
4 clock cycles respectively. BRNE is a branch instruction if the condition is meet and
it need to branch then it takes 2 clock cycle otherwise 1. .DEF is a directive so it has
no machine code so the cpu does not need to execute it.
Example:
If the time take by a single cycle is 1μs the calculate the time delay for the
program below:
Solution:
Arithmetic , Logic and
compare Instructions
Chapter – 5.1, 5.2
Overview
Indicates if there was a carry-out from Set to 1 if there is a carry- In the addition of 0xF5 and 0x0B, if
Carry Flag (C)
the most significant bit. out; 0 otherwise. there's a carry from the MSB, C is set to 1.
Indicates whether the result of the Set to 1 if the result is zero; 0 If the result of adding 0xF5 and 0x0B is
Zero Flag (Z)
addition is zero. otherwise. 0x00, Z is set to 1.
Half Carry Indicates if there's a carry-out from Set to 1 if there's a carry-out If there's a carry from the 4th bit in the
Flag (H) bit 3 to bit 4 (for 8-bit addition). from D3 to D4; 0 otherwise. addition, H is set to 1.
Indicates whether the result is
Negative Flag Set to 1 if the result is If the MSB of the result is 1, N is set to 1,
negative (interpreted as a signed
(N) negative; 0 otherwise. indicating a negative result.
number).
Similar to the Negative Flag, indicates If the MSB of the result is 1, S is set to 1,
Sign Flag (S) Set to the MSB of the result.
the sign of the result. indicating a negative result.
Subtraction of unsigned number
SBC SBC Rd, Rr Subtract Rr and Carry from Rd SBC R16, R18 1, 1, 0, 0, 1
Signed byte operands are a type of data in assembly language where the most
significant bit (D7) represents the sign (positive or negative) and the
remaining bits (D0 to D6) represent the magnitude of the number.
Addition Subtraction
LDI R20, 0X60 ; LDI R20, 0x80 ;
R20 = 0110 0000 (+70) R20 = 1000 0000 (-128)
LDI R21, 0x46 ; LDI R21, 0xFE ;
R21 = 1111 1110 (-2)
R21 = 0100 0110 (+96) SUB R20, R21 ;
ADD R20, R21 ; R20 = (-128) - (-2) = 1000 0000 + 0000 0010 = 1000
R20 = 0110 0000 + 0100 0110 = 1010 0110 0010
Overflow Problem in Signed Number
Addition and Subtraction
An overflow error occurs when the result of an addition or Example:
subtraction operation exceeds the maximum value that can Consider the operation of adding +70 and +96. The
be stored in a byte (255 for unsigned numbers, 127 for correct result should be +166. However, because this
signed numbers).
result cannot be represented with a signed byte, an
overflow error occurs.
In the context of signed numbers, an overflow error can
lead to incorrect results because the sign bit (the most
significant bit) can be inadvertently changed. The microcontroller incorrectly gives the result as -90
(represented by the two’s complement of +166 in an 8-
The AVR microcontroller sets the V flag (Overflow flag) to bit register) and sets the V flag to 1 to indicate the
1 to indicate an overflow error. overflow error.
Multiplication of Signed and Unsigned
numbers
Unsigned Number Signed Number
MUL instruction: MUL is a byte-by-byte multiply MULS instruction: MULS is a signed number multiplication instruction.
instruction. After multiplication, the 16-bit unsigned After multiplication, the 16-bit signed product is placed in R1 (high byte)
product is placed in R1 (high byte) and R0 (low byte). and R0 (low byte).
Example: Example:
LDI R23, 0x25
LDI R23, 0x25
LDI R24, 0x65
LDI R24, 0x65
MUL R23, R24 ;
MULS R23, R24 ;
25H * 65H = E99 where ;R1 = 0EH and R0 = 99H
25H * 65H = E99 where ;R1 = 0EH and R0 = 99H
Instruction Operation Example Result
Various Logic
AND Bitwise AND AND R3, R5 R3 = R3 & R5
Operators
OR Bitwise OR OR R6, R2 R6 = R6
Rotate: Shift:
ROR LSL
ROL LSR
Rotate Instructions-ROR