Microprocessor, Interfacing and System Design
Lecture 8
Architecture of ATmega328P Microcontroller
Md Shaikh Abrar Kabir, Lecturer, EEE 1
Use of Microcontroller
An MCU can be used to build the following measuring instruments:
Figure-8.1 (a): Taxi Meter (b) Prepaid Electrical Energy Meter (c) Digital Weighing Machine
Architecture of ATmega328P Microcontroller 2
Introduction to Microprocessor and Microcontroller
Architecture of ATmega328P Microcontroller 3
Physical Pin Diagram of 328P MCU
Figure-8.2: Physical Pin Diagram of ATmega328P MCU
Architecture of ATmega328P Microcontroller 4
Basic Tasks of a Microcontroller
● Taking command (+, _, *, and /) from the user via
keyboard/switch (input device).
● Taking data from the user via keyboard (input device).
● Modifying data in a way the user wants (say, adding two
numbers)
● Delivering result to the user via 7-segment or LCD display
unit (output device).
Figure-8.2: Physical Pin Diagram of 328P MCU
Architecture of ATmega328P Microcontroller 5
Brief External Architecture
(1) This is an 8-bit MCU which means that the MCU can add
or subtract or multiply or divide two 8-bit data at one time. If
the addition generates a carry bit, it is saved in Carry Flag of
MCU.
(2) There are 28 pins for the MCU of which Pin-7 takes 5V
supply for internal digital electronics and Pin-20 takes 5V
supply for internal analog circuits. There are two GND pins.
(3) The RESET/ signal for the MCU can be supplied from
external Reset-Circuit via Pin-1 or can be generated internally
during power up sequence. Figure-8.2: Physical Pin Diagram of 328P MCU
Architecture of ATmega328P Microcontroller 6
Brief External Architecture
(4) There are three groups of digital IO (input and output)
lines and these are: Port-B (PB0 – PB7), Port-C (PC0 – PC6),
and Port-D (PD0 – PD7).
(5) A pin is attached with one or more signals/functions of
which the default signals/functions are shown outside
parentheses. For example: default function of Pin-14 is: IO line
named PB0.
(6) The alternate functions of a pin are shown within
parentheses, and these are activated through software
initialization. For example: PC0 – PC5 lines can be used as
Figure-8.2: Physical Pin Diagram of 328P MCU
analog channels (ADC0/Ch0 – ADC5/Ch5) for the internal
10-bit uni-polar ADC (analog-to-digital converter) by
executing this code for Ch0: int y = analogRead(A0).
Architecture of ATmega328P Microcontroller 7
Port Structured Diagram for the 28 Pins of ATmega328P MCU
Figure-8.3: Port Structured Diagram for the 28 Pins of ATmega328P MCU
Architecture of ATmega328P Microcontroller 8
Figure-8.4: Port Structured Diagram for the 28 Pins of ATmega328P MCU
Architecture of ATmega328P Microcontroller 9
Internal Architecture of ATMEGA328P
MCU
Figure-8.5: Internal Architecture of ATMEGA328P MCU
Architecture of ATmega328P Microcontroller 10
Internal Architecture of AT328P MCU
(a) Clock Circuit
The base frequency of the MCU is determined by “external 16 MHz crystal controlled oscillator ” or
by “internal 8 MHz oscillator”. The selection is done with the help of fuse bits of the MCU. The base
frequency passes through a “clock prescaler with division factor 1” to generate actual operating
frequency which is 16 MHz for the ATmega328P MCU of the Arduino UNO Board. For other MCUs
other than the Arduino UNO Board, the operating frequency is 1 MHz which has been obtained
from internal 8 MHz oscillator being divided by 8 by the clock prescaler.
Architecture of ATmega328P Microcontroller 11
Internal Architecture of AT328P MCU
Figure-8.6: Internal Architecture of
ATmega328P MCU
Architecture of ATmega328P Microcontroller 12
Internal Architecture of AT328P MCU
The ATmega328P MCU contains the following
hardware modules (as depicted in Fig-8.6):
(a) Clock Circuit
M1: External 16 MHz Clock Oscillator
M2: System Clock Prescaler
M3: Internal 8 MHz Clock Oscillator
(b) Serial Data Communication Network
M4: UART Port Based Serial Data Communication
M19: SPI Port Based Serial Data Communication
M21: I2C Bus Based Serial Data Communication
Figure-8.6: Internal Architecture of AT328P MCU
Architecture of ATmega328P Microcontroller 13
Internal Architecture of AT328P MCU
(c) Memory System
M14: Flash Memory/Code Memory/Program
Memory
M15: EEPROM Data Memory (Electrically
Erasable Programmable Read Only Memory)
M16: SRAM/PORTS
(d) Digital IO Port
M9: Port-B IO Register
M10: Port-C IO Register
M11: Port-D IO Register
Figure-8.6: Internal Architecture of AT328P MCU
Architecture of ATmega328P Microcontroller 14
Internal Architecture of AT328P MCU
(e) Analog-to-Digital Converter
M8: 6-Channel 10-bit Uni-polar Analog-to-Digital
Converter
(f) Registers
M17: 32 General Purpose Register (R0 –R31)
64 Standard IO Port Registers
160 Extended IO Port Registers
Architecture of ATmega328P Microcontroller 15
Internal Architecture of AT328P MCU
(g) Timer Modules
M5: Two 8-bit and one 16-bit Timer
(h) Counter Modules
M6: One 8-bit Counter and one 16-bit Counter
(i) Other Modules
M7: External Interrupt Handler
M12: Watchdog Timer
M13: AVR (Advanced Virtual RISC
Microcontroller) Machine
M16: Sequence Generator
M18: In System Programming Interface
M20: Analog Comparator
M22: 6-Channel Pulse Width Modulator Figure-8.6: Internal Architecture of AT328P MCU
Architecture of ATmega328P Microcontroller 16
Flash Memory Organization
(a) Capacity: 2x16x1024 Bytes
(b) Location Organization: 16-bit
(c) Lock Bit (when programmed) does not allow
others to read the program codes of the flash
memory. So, the program is secured.
Figure-8.7: Flash Memory Organization
Architecture of ATmega328P Microcontroller 17
EEPROM Data Memory Organization RAM and Stack Space Organization
Capacity: 1024 Byte Capacity: 2048 Byte
Figure-8.8: EEPROM Data Memory Organization Figure-8.9: RAM and Stack Space Organization
Architecture of ATmega328P Microcontroller 18
Flash Memory Programming:
const PROGMEM byte y = 0x35;
//declaration stores data in flash by virtue of PROGMEM keyword
byte z; //z will hold data after reading back from flash
void setup()
{
Serial.begin(9600);
z = pgm_read_byte(&y);
//read 1-byte data from program memory
Serial.println(z, HEX); //Serial Monitor shows: 35
}
void loop()
{}
Architecture of ATmega328P Microcontroller 19
Flash Memory Programming:
const PROGMEM unsigned int y = 0x1235;
unsigned int z;
void setup()
{
Serial.begin(9600);
z = pgm_read_word(&y);
//read 2-byte data from program memory
Serial.println(z, HEX); //Serial Monitor shows: 1235
}
void loop(){}
Architecture of ATmega328P Microcontroller 20
Flash Memory Programming:
void setup() When an instruction like:
{ Serial.print("Forum"); is used, the string to be
Serial.begin(9600); printed is saved first in RAM memory. If your
Serial.println("Hello"); sketch prints a lot of stuff on the Serial Monitor,
Serial.println(F("Hello")); you can easily and quickly fill the RAM. You can
} easily indicate that the string must be saved in
void loop(){} flash memory using the syntax:
Serial.print(F("Forum"));
Architecture of ATmega328P Microcontroller 21
EEPROM Programming:
Programming refers to the process of storing data into EEPROM and read it back.
(1) Writing/Reading 1-byte data in an EEPROM location using write()/read()
method Procedures:
(a) Include the following Libray in the IDE and Sketch:
#include<EEPROM.h>
(b) Execute the following methods to write a data bytes into the target location.
EEPROM.write(locAddress, dataByte); //location takes about 5 ms time to absorb the data
(c) Execute the following methods to read a data bytes from a target location.
EEPROM.read(locAddress); //location takes about 5 ms time to absorb the data
Architecture of ATmega328P Microcontroller 22
EEPROM Programming:
(d) Example: Write codes to store 0x23 into location 0x0010 of EEPROM
#include<EEPROM.h>
void setup() {
#define locAddrs 0x0010
// Read back the data and show
#define dataByte 0x23 // on Serial Monitor:
Serial.begin(9600); byte rdData =
// Write 0x23 in 0x0010 address of EPROM:
EEPROM.read(locAddrs);
EEPROM.write(locAddrs, dataByte);
Serial.println(rdData, HEX);
}
void loop() {}
Architecture of ATmega328P Microcontroller 23
EEPROM Programming:
(2) Home work
(a) Write codes to store data item 0x1234 into locations 0x0234 and 0x0235 of EEPROM. Note
that lower byte of data will enter into low-order memory location. This is known as little
endianness.
(b) Write codes to store “AUST” into EEPROM starting at location 0x0124.
(c) Write codes to store
“HELLO”
“AUST”
into EEPROM starting at location 0x5124.
Architecture of ATmega328P Microcontroller 24
Solution (a):
#include<EEPROM.h>
void setup() {
#define locAddrs 0x0234
#define myLowByte lowByte(0x1234)
#define myHighByte highByte(0x1234) Serial.println(rdLowByte, HEX);
Serial.begin(9600); Serial.println(rdHighByte, HEX);
EEPROM.write(locAddrs, myLowByte); }
EEPROM.write(locAddrs+1, myHighByte); void loop() {}
byte rdLowByte = EEPROM.read(locAddrs);
byte rdHighByte = EEPROM.read(locAddrs+1);
Architecture of ATmega328P Microcontroller 25
Solution (b):
#include<EEPROM.h>
void setup() {
#define locAddrs 0x0124
char myCharArray[] = "AUST";
Serial.begin(9600);
for (byte i = 0; i<4; i++){
EEPROM.write(locAddrs+i, myCharArray[i]);
}
for (byte i = 0; i<4; i++)
{ char myChar = EEPROM.read(locAddrs+i);
Serial.print(myChar); } } void loop() {}
Architecture of ATmega328P Microcontroller 26
Solution (c):
#include<EEPROM.h>
void setup() {
#define locAddrs 0x5124
char myCharArray[] = "HELLO\nAUST";
byte len = sizeof(myCharArray);
Serial.begin(9600);
for (byte i = 0; i<len-1; i++)
{ EEPROM.write(locAddrs+i, myCharArray[i]); }
for (byte i = 0; i<len-1; i++)
{ char myChar = EEPROM.read(locAddrs+i);
Serial.print(myChar); } }
void loop() {}
Architecture of ATmega328P Microcontroller 27
Solution (c) (Alternative)
#include<EEPROM.h> strcpy(wrData.a,"Hello");
struct myData{ strcpy(wrData.b,"AUST");
char a[6]; Serial.begin(9600);
char b[5]; EEPROM.put(locAddrs,wrData);
int c; EEPROM.get(locAddrs,rdData);
float d; Serial.println(rdData.a);
}; Serial.println(rdData.b);
void setup() { Serial.println(rdData.c);
#define locAddrs 0x5124 Serial.println(rdData.d);
myData wrData; }
myData rdData; void loop() {}
Architecture of ATmega328P Microcontroller 28
(1) Assembly Code:
ldi r16, 0x35 ; load immediate
ldi => load data (0x35) to register (r16)
lds r16, 0x0120 ; load from data space address (16 bits address)
lds => load from ram space (0x0120) to register (r16)
ldi r27, 0x01
ldi r26, 0x20 ; <r27, r26> register pair now works as X-pointer register
ld r16, x
ld => load indirectly from ram space (0x0120) which is in X register to register (r16)
st x, r16 ; content of r16 reg goes to a location whose address is in x-pointer reg.
st => store indirectly from register (r16) to ram space (0x0120) which is in X register
sts 0x0120, r16 ; content of r16 reg goes to a location whose address is in 0x0120.
sts => store from register (r16) to ram space (0x0120)
Architecture of ATmega328P Microcontroller 29
(1) Assembly Code:
ldi r16, 0x35 ; load immediate //
lds r16, 0x0120 ; load from data space address (16 bits address)
ldi r27, 0x01
ldi r26, 0x20 ; <r27, r26> register pair now works as X-pointer register
ld r16, x
st x, r16 ; content of r16 reg goes to a location whose address is in x-pointer reg.
sts 0x0120, r16 ; content of r16 reg goes to a location whose address is in 0x0120.
(2) Arithmetic Instructions
C++ Code:
byte x1 = 0x12, x2 = 0x45;
byte sum = x1 + x2;
Assembly Code:
ldi r16, 0x12
ldi r17, 0x45
add r16, r17
Architecture of ATmega328P Microcontroller 30
(3) Logical Instructions
C++ Code:
byte x1 = 0x65;
byte x2 = 0x73;
byte y = x1 | x2;
Assembly Code:
ldi r16, 0x65 (ldi can be used only for r16 - r31)
ldi r17, 0x73
or r16, r17
mov r0, r16 (mov can be used for r0 - r31)
Find more assembly instructions here.
Architecture of ATmega328P Microcontroller 31
(3) Branch Instructions
C++ Code:
byte x1 = 0x65;
byte x2 = 0x45;
if( x1 != x2) //Skip (do not execute) next instruction if they are equal
{ L1: add with carry }
else
{ L2: add without carry }
Architecture of ATmega328P Microcontroller 32
Probable Assembly Code: (Wrong)
ldi r18, 0x65
ldi r19, 0x45
cpse r18, r19
; compare two regs and skip execution of next instruction if regs are equal
L1: adc r18, r19
L2: addr18, r19 ; add without carry
Architecture of ATmega328P Microcontroller 33
(3) Branch Instructions
C++ Code:
byte x1 = 0x65;
byte x2 = 0x45;
if( x1 != x2) //Skip (do not execute) next instruction if they are equal
{ L1: add with carry }
else
{ L2: add without carry }
Assembly Code:
ldi r16, 0x65
ldi r17, 0x45
cpse r16, r17 ; compare two regs and skip execution of next instruction if regs are
equal
jmp L1
L2: add r16, r17 ; add without carry
jmp DONE
L1: adc r16, r17 ; add with carry
DONE:
Architecture of ATmega328P Microcontroller 34
(3) Branch Instructions
C++ Code:
byte x1 = 0x65;
byte x2 = 0x45;
if( x1 != x2) //Skip (do not execute) next instruction if they are equal
{ L1: add with carry }
else
{ L2: add without carry }
Assembly Code: (Alternative)
ldi r16, 0x65
ldi r17, 0x45
cp r16, r17 ; compare two regs
BREQ L2 ; branch if equal
L1: adc r16, r17
jmp DONE
L2: add r16, r17 ; add without carry
DONE:
Architecture of ATmega328P Microcontroller 35
Any Questions?
Architecture of ATmega328P Microcontroller 36