Lecture 3 Architecture
Lecture 3 Architecture
EMBEDDED SYSTEMS
PROGRAMMING
LECTURE 3 ATMEGA328 ARCHITECTURE
Arduino Uno
•It is a microcontroller based on the ATmega328
•14 digital input/output (6 for PWM)
•6 Analog inputs
•CPU (8 bits) clock: 16 MHz
•Programs size can be max 30Kbytes
•SRAM 2KB, EEPROM 1KB
•UART, SPI and I2C protocol are enabled
Atmega 328 Pinout
AVR Architecture
•Clocks and Power
• Beyond scope of this course
•CPU
• Harvard architecture
•Flash – program memory
• 32K
•SRAM – data memory
• 2K
•EEPROM
• For long-term data
• On I/O data bus
Memory
•Flash (32K) (15-bit addresses)
• Program memory – read only
• Non-volatile
• Allocate data to Flash using PROGMEM keyword
• see documentation
•SRAM (2K)
• Temporary values, stack, etc.
• Volatile
• Limited space!
•EEPROM (1K)
• Long-term data
• see documentation on EEPROM library
AVR CPU
•Instruction Fetch and Decode
•ALU Instructions
•I/O and special functions
AVR Memory Organization
•Program Memory (ROM) •Data Memory
Data Memory
•Rx space
• 32 8-bit GP registers
•I/O Memory
• Special Function Registers (SFRs)
•To change multiple pins simultaneously, directly read/write the pin registers
• DDR{B/C/D}
• PORT{B/C/D}
• PIN{B/C/D}
Pin Circuitry
PORTx
PINx
DDRx = 0
Pin Input
off
PORTx
PINx
Synchronization Timing
•Note: Takes a clock cycle for data output to be reflected on the input
DDRx = 1
Pin Output
on
PORTx
PINx
Arduino C/C++ Programs
•Arduino calls these “sketches”
• Basically C/C++ with libraries
•Program structure
• Header: declarations, includes, etc.
• setup()
• loop()
•setup()
• executes once when program starts
•loop() as superloop
• continuously re-executed when the end is reached
The Arduino C/C++ Main Program
int main(void)
{
setup();
for (;;)
loop();
return 0;
}
Blink Program
int ledPin = 13; // LED connected to digital pin 13
void setup() {
void loop()
{
digitalWrite(ledPin, HIGH); // set the LED on
delay(1000); // wait for a second
digitalWrite(ledPin, LOW); // set the LED off
delay(1000); // wait for a second