Microprocessor, Interfacing and System Design
Lecture 8
Architecture of ATmega328P Microcontroller
Md Shaikh Abrar Kabir, Lecturer, EEE 1
Physical Pin Diagram of 328P MCU
Figure-8.1: Physical Pin Diagram of 328P MCU
Architecture of ATmega328P Microcontroller 2
Physical Pin Diagram of 328P MCU
● There are 28 pins.
● A pin has a serial number. For example: Pin-27.
● A pin is attached with one or more signals. For example:
Pin-27 is attached with four signals which are:
PC4 (ADC4/SDA/PCINT12).
● The signal PC4 which is outside the pair of parentheses ()
is the default function. The other signals within the pair of
parentheses () are the alternate functions. We will study the Figure-8.1: Physical Pin Diagram of 328P MCU
alternate functions of the IO lines in the relevant Chapters.
Architecture of ATmega328P Microcontroller 3
Physical Pin Diagram of 328P MCU
● A signal has a meaning. For example:
PC4 stands for Bit-4 IO line of Port-C Register.
● A pin/signal has logic level and current handling
capability. For example:
(i) Output HIGH Level (VOH) : 4.2 V – 5.0 V (minimum)
(ii) Output LOW Level (VOL) : 0.0 V – 0.9 V (maximum)
(iii) Minimum input voltage that will be recognized as
HIGH (VIH) : 3.0 V
(iv) Maximum input voltage that will be recognized as
LOW (VIL) : 1.0 V Figure-8.1: Physical Pin Diagram of 328P MCU
Architecture of ATmega328P Microcontroller 4
Port Structured Diagram for the 28 Pins of ATmega328P MCU
Figure-8.2: Port Structured Diagram for the 28 Pins of ATmega328P MCU
Architecture of ATmega328P Microcontroller 5
Port Structured Diagram for the 28 Pins of ATmega328P MCU
● In Fig-8.2, we observe that there are 20 IO (input and
output) lines of the MCU and are grouped as: Port-B,
Port-C, and Port-D.
● The naming styles for the ports are: Port-B, Port-C,
and Port-D when the directions of the IO lines are
not yet determined. The corresponding port lines are
named as: pb0, pb1, …, pb5.
● PORTB, PORTC, and PORTD, when the IO lines
work as output. The corresponding port lines are
named as: PB, PB1, …, PB5.
Figure-8.2: Port Structured Diagram of ATmega328P MCU
Architecture of ATmega328P Microcontroller 6
Port Structured Diagram for the 28 Pins of ATmega328P MCU
● PINB, PINC, and PIND when the IO lines work as
input. The corresponding port lines are named as:
PINC0, PINC1, …, PINC5.
Figure-8.2: Port Structured Diagram of ATmega328P MCU
Architecture of ATmega328P Microcontroller 7
Internal Architecture of AT328P MCU
Figure-8.3: Internal Architecture of AT328P MCU
Architecture of ATmega328P Microcontroller 8
Internal Architecture of AT328P MCU
The ATmega328P MCU contains the following
hardware modules (as depicted in Fig-8.3):
(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.3: Internal Architecture of AT328P MCU
Architecture of ATmega328P Microcontroller 9
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.3: Internal Architecture of AT328P MCU
Architecture of ATmega328P Microcontroller 10
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 11
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.3: Internal Architecture of AT328P MCU
Architecture of ATmega328P Microcontroller 12
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.3: Internal Architecture of AT328P MCU
Architecture of ATmega328P Microcontroller 13
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.4: Flash Memory Organization
Architecture of ATmega328P Microcontroller 14
EEPROM Data Memory Organization RAM and Stack Space Organization
Capacity: 1024 Byte Capacity: 2048 Byte
Architecture of ATmega328P Microcontroller 15
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 Lobray 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 16
EEPROM Programming:
(d) Example: Write codes to store 0x23 into location 0x0010 of EEPROM
#include<EEPROM.h>
void setup() {
#define locAddrs 0x0010
#define dataByte 0x23
Serial.begin(9600);
//--------------------------------------------
// Write 0x23 in 0x0010 address of EPROM:
EEPROM.write(locAddrs, dataByte);
//--------------------------------------------
// Read back the data and show on Serial Monitor:
byte rdData = EEPROM.read(locAddrs);
Serial.println(rdData, HEX);
}
void loop() {}
Architecture of ATmega328P Microcontroller 17
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 18
Solution (a):
#include<EEPROM.h>
void setup() {
#define locAddrs 0x0234
#define myLowByte lowByte(0x1234)
#define myHighByte highByte(0x1234)
Serial.begin(9600);
EEPROM.write(locAddrs, myLowByte);
EEPROM.write(locAddrs+1, myHighByte);
byte rdLowByte = EEPROM.read(locAddrs);
byte rdHighByte = EEPROM.read(locAddrs+1);
Serial.println(rdLowByte, HEX);
Serial.println(rdHighByte, HEX);
}
void loop() {}
Architecture of ATmega328P Microcontroller 19
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 20
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 21
Solution (c): (Alternative)
#include<EEPROM.h>
struct myData{
char a[6]; //L1:
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() {}
wrData.c = 12;
wrData.d = 5.34;
strcpy(wrData.a,"Hello");
strcpy(wrData.b,"AUST");
Serial.begin(9600);
// L1:
Architecture of ATmega328P Microcontroller 22
Solution (c): (Alternative but not recommended as memory is not efficiently used)
#include<EEPROM.h>
struct myData{
String a;
String b;
};
void setup() {
#define locAddrs 0x5124
myData wrData;
myData rdData;
wrData.a = "Hello";
wrData.b = "AUST";
Serial.begin(9600);
EEPROM.put(locAddrs,wrData);
EEPROM.get(locAddrs,rdData);
Serial.println(rdData.a);
Serial.println(rdData.b);
}
void loop() {}
Architecture of ATmega328P Microcontroller 23
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
ldi r16, 0x35
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 24
(3) Logical Instructions
C++ Code:
byte x1 = x65;
byte x2 = 0x73;
byte y = x1 | x2;
Assembly Code:
ldi r16, 0x65
ldi r17, 0x73
or r16, r17
Find more assembly instructions here.
Architecture of ATmega328P Microcontroller 25
(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: Execute this line of code }
else
{ L2: Execute this line of code }
Probable Assembly Code:
ldi r16, 0x65
ldi r17, 0x45
cpse r16, r17 ; compare two regs and skip execution of next instruction if regs are equal
L1: adc r18, r19
L2: add r18, r19 ; add without carry
Architecture of ATmega328P Microcontroller 26
(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: Execute this line of code }
else
{ L2: Execute this line of code }
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 r18, r19 ; add without carry
jmp DONE
L1: adc r18, r19 ; add with carry
DONE:
Architecture of ATmega328P Microcontroller 27
(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: Execute this line of code }
else
{ L2: Execute this line of code }
Assembly Code: (Alternative)
ldi r16, 0x65
ldi r17, 0x45
cp r16, r17 ; compare two regs
BREQ L2 ; branch if equal
L1: adc r18, r19
jmp DONE
L2: add r18, r19 ; add without carry
DONE:
Architecture of ATmega328P Microcontroller 28
Any Questions?
Architecture of ATmega328P Microcontroller 29