0% found this document useful (0 votes)
138 views40 pages

Architecture of Arduino Microcontroller

The document discusses the architecture of the Arduino microcontroller. It begins by explaining the general architecture of microcontrollers and the key features of the ATmega328 microcontroller used in Arduino boards. It then describes the features and elements of the Arduino Duemilanove and Uno boards. It explains the concepts of microcontroller pins being configured as inputs or outputs and how to set the pin data direction. Finally, it provides examples of how to programmatically configure pin directions and states using both Arduino and direct register methods.

Uploaded by

Yonatan Solomon
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
138 views40 pages

Architecture of Arduino Microcontroller

The document discusses the architecture of the Arduino microcontroller. It begins by explaining the general architecture of microcontrollers and the key features of the ATmega328 microcontroller used in Arduino boards. It then describes the features and elements of the Arduino Duemilanove and Uno boards. It explains the concepts of microcontroller pins being configured as inputs or outputs and how to set the pin data direction. Finally, it provides examples of how to programmatically configure pin directions and states using both Arduino and direct register methods.

Uploaded by

Yonatan Solomon
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 40

Chapter 3

Architecture of Arduino
microcontroller
Learning Objectives
 Explain the general architecture of a microcontroller
 List the key features of the ATmega328
microcontroller
Explain the features and elements of the Arduino and
Spartronics Experimenter Shield (SES)
Explain the concepts of microcontroller pins as inputs
and outputs
Convert between binary and hexadecimal digits
What is a Microcontroller?

ANALOG
INPUTS

What is the difference between a ‘Digital Input’ and an ‘Analog Input’?


http://www.freescale.com/files/microcontrollers/doc/ref_manual/M68HC05TB.pdf
ATmega328 Internal Architecture

ATmega328 data sheet pp. 2, 5

http://www.adafruit.com/index.php?main_page=popup_image&pID=50
ATmega328 Features

ATmega328 data sheet p. 1

http://www.atmel.com/Images/Atmel-8271-8-bit-AVR-Microcontroller-
ATmega48A-48PA-88A-88PA-168A-168PA-328-328P_datasheet.pdf
Arduino Duemilanove
http://www.arduino.cc/en/Main/ArduinoBoardDuemilanove
See the handout: Arduino_ATmega328_pin_mapping_and_schematic

Pin 13 LED
Digital pins header
USB
connector Reset button

ATmega328 MCU

Barrel jack Analog pins header

Power-ground header
http://arduino.cc/en/uploads/Main/ArduinoDuemilanove.jpg
Arduino Uno R3
ATmega16u2 replaces FT232RL for USB-serial communication

http://www.adafruit.com/index.php?main_page=popup_image&pID=50

See: http://learn.adafruit.com/arduino-tips-tricks-and-techniques/arduino-uno-faq
Arduino Due Note: 3.3 V !!

Atmel SAM3X8E processor (32 bit ARM Cortex M3 architecture, 84MHz)

http://www.adafruit.com/index.php?main_page=popup_image&pID=1076

See: http://arduino.cc/en/Main/ArduinoBoardDue
Arduino Duemilanove/Uno Features
Microcontroller ATmega168/328
Operating Voltage 5V
Input Voltage (recommended) 7-12V
Input Voltage (limits) 6-20V
Digital I/O Pins 14 (of which 6 provide PWM output)
Analog Input Pins 6
DC Current per I/O Pin 40 mA
DC Current for 3.3V Pin 50 mA
16 KB (ATmega168) or 32 KB (ATmega328) of which 2 KB
Flash Memory
used by bootloader
SRAM 1 KB (ATmega168) or 2 KB (ATmega328)
EEPROM 512 bytes (ATmega168) or 1 KB (ATmega328)
Clock Speed 16 MHz

http://www.arduino.cc/en/Main/ArduinoBoardDuemilanove
http://arduino.cc/en/uploads/Main/arduino-duemilanove-schematic.pdf
ATmega328 Microcontroller
Pin number
Pin name

Special
function

Note the
limitations!
p. 316 Source:http://www.atmel.com/dyn/products/product_card.asp?PN=ATmega328P
Absolute Maximums

ATmega328 data sheet p. 316


Microcontroller Ports and Pins

 The communication channels


through which information flows C
into or out of the
microcontroller
 Ex. PORTB
 Pins PB0 – PB7
 May not be contiguous
 Often bi-directional See next slides!
Port Pin Data Directionality

• Input
• When you want to take information from the external
world (sensors) into the MCU
• Output
• When you want to change the state of something outside
the MCU (turn a motor on or off, etc.)
• Pins default to input direction on power-up or reset
• Your program can set or change the directionality of
a pin at any time
ATmega328
Block Diagram

Input

Output
M68HC11 microcontroller
Setting the Pin Data Direction
• Arduino
• pinMode(pin_no., dir)
• Ex. Make Arduino pin 3 (PD3) an output
• pinMode(3, OUTPUT);
• pinMode(PIN_D3, OUTPUT); // with
me106.h
• Note: one pin at a time
• Suppose you wanted Arduino pins 3, 5, and 7 (PD3, PD5,
and PD7) to be outputs?
• Is there a way to make them all outputs at the same time?
• Yes! Answer coming later…
Pin Voltages

• Microcontrollers are fundamentally digital devices. For


digital IO pins:
• Information is ‘coded’ in two discrete states:
• HIGH or LOW (logic: 1 or 0)
• Voltages
• TTL
• 5 V (for HIGH)
• 0 V (for LOW)
• 3.3 V CMOS
• 3.3 V (for HIGH)
• 0 V (for LOW)
Pin Used as an Output
• Turn on an LED, which is connected
to pin Arduino pin 0 (PD0) (note the ATmega328
resistor!)
• What should the data direction be for pin 0 (PD0)? Arduino
• pinMode(____, ____); pin 0
• Turn on the LED (PD0)
• digitalWrite(PIN_LED,HIGH);
• Turn off the LED
• digitalWrite(PIN_LED,LOW);
Pins as Inputs and Pull-up Resistors - 1
• Using a switch as a sensor
• Ex. Seat belt sensor ATmega328
• Detect the switch state
• What should the data direction be for
Arduino
Arduino pin 3 (PD3)?
pin 3
• pinMode(____, ____); (PD3)
• What will the voltage be on PD3 when the
switch is closed?
• What will the voltage be on PD3 when the
switch is open?
• Indeterminate!
SPST

momentary
Pins as Inputs and Pull-up Resistors - 2
• Switch as a sensor, cont.
• Make the voltage on the pin ATmega328
determinate by turning on the pull-up VTG= +5V
resistor for PD3
• Assuming PD3 is an input:
• digitalWrite(PIN_SWITCH,HIGH); 1
PD3
turns on the “pull-up” resistor
0
• pinMode(PIN_SWITCH,INPUT_PULLUP);
• What will the voltage on PD3 be when the
switch is open?
• VTG
• What will the voltage on PD3 be when the
switch is closed?
Pins as Inputs and Pull-up Resistors - 3
• Switch as a sensor, cont.
• To turn off the pull-up resistor ATmega328
• Assuming PD3 is an input: VTG= +5V
digitalWrite(PIN_SWITCH,
LOW); turns the “pull-up” resistor 1
off PD3
0
Pins as Inputs and Pull-up Resistors - 4
• Possibility of ‘weak drive’ when pull-up
resistor is turned on ATmega328
• Pin set as an input with a pull-up VTG= +5V
resistor turned on can source a small
current iweak
1
• Remember this! PD3
0
ATmega328 Registers of Interest

• See the ATmega328 data sheet, pp. 76-94


• For digital IO, the important registers are:
• DDRx
• Data Direction bit in DDRx register (read/write)
• PORTx
• PORTx data register (read/write)
• PINx
• PINx register (read only)
PORT Pin and
register details
ATmega328 datasheet, pp. 76-94

Jump to bits
Example 1
 Make Arduino pins 3, 5, and 7 (PD3, PD5, and
PD7) to be outputs
• Arduino approach • Alternate approach

pinMode(3, OUTPUT); DDRD = 0b10101000;


pinMode(5, OUTPUT);
pinMode(7, OUTPUT); or
Or if me106.h is used:
DDRD = 0xA8;
pinMode(PIN_D3, OUTPUT);
pinMode(PIN_D5, OUTPUT); or
pinMode(PIN_D7, OUTPUT);
DDRD | = 1<<PD7 | 1<<PD5 | 1<<PD3;

More on this coming soon!


Example 2
 Make pins Arduino pins 0 and 1 (PD0 and PD1)
inputs, and turn on pull-up resistors
• Arduino approach • Alternate approach

pinMode(0, INPUT); DDRD = 0; // all PORTD pins inputs


pinMode(1, INPUT); PORTD = 0b00000011;
digitalWrite(0, HIGH); or
digitalWrite(1, HIGH); PORTD = 0x03;
Or if me106.h is used:
or better yet:
pinMode(PIN_D0, INPUT); DDRD & = ~(1<<PD1 | 1<<PD0);
pinMode(PIN_D1, INPUT); PORTD | = (1<<PD1 | 1<<PD0);
digitalWrite(PIN_D0, HIGH);
digitalWrite(PIN_D1, HIGH); More on this coming soon!
Structure of an Arduino Program
/* Blink - turns on an LED for DELAY_ON msec,
then off for DELAY_OFF msec, and repeats
• An arduino program == ‘sketch’ BJ Furman rev. 1.1 Last rev: 22JAN2011
• Must have: */
#define LED_PIN 13 // LED on digital pin 13
• setup() #define DELAY_ON 1000
• loop() #define DELAY_OFF 1000
• setup() void setup()
• configures pin modes and {
registers // initialize the digital pin as an output:
pinMode(LED_PIN, OUTPUT);
• loop() }
• runs the main body of the
program forever // loop() method runs forever,
// as long as the Arduino has power
• like while(1) {…}
• Where is main() ? void loop()
• Arduino simplifies things {
digitalWrite(LED_PIN, HIGH); // set the LED on
• Does things for you delay(DELAY_ON); // wait for DELAY_ON msec
digitalWrite(LED_PIN, LOW); // set the LED off
delay(DELAY_OFF); // wait for DELAY_OFF msec
}
Digital IO – Practice 1
• ‘Reading a pin’
• Write some lines of C code for the ATmega328
Arduino to determine a course of action
if the seat belt has been latched (switch
closed).
• If latched, the ignition should be enabled PD3
through a call to a function ig_enable().
• If not latched, the ignition should be disabled
through a call to a function ig_disable()
• Write pseudocode first
Digital IO – Practice 1 Pseudocode
• ‘Reading a pin’
• Pseudocode: ATmega328
Set up PD3 as an input
VTG= +5V
Turn on PD3 pull-up resistor
Read voltage on Arduino pin 3 (PIN_D3)
IF PIN_D3 voltage is LOW (latched), THEN 1
call function ig_enable() PD3
ELSE 0
call function ig_disable()
Digital IO – Practice 1 Code
• ‘Reading a pin’ ATmega328
• Pseudocode:
VTG= +5V
Set up PD3 as an input
Turn on PD3 pull-up resistor
Read voltage on Arduino pin 3 (PIN_D3)
1
IF PIN_D3 voltage is LOW (latched), THEN PD3
call function ig_enable() 0
ELSE
call function ig_disable()

One way  #define PIN_SWITCH 3


(snippet, not full program) #define LATCHED LOW
pinMode(PIN_SWITCH,INPUT_PULLUP);
belt_state = digitalRead(PIN_SWITCH);
if (belt_state == LATCHED)
{ ig_enable(); }
else
{ ig_disabled(); }
Digital IO – Practice 2
• ‘Reading from and writing to a pin’ ATmega328
• Write some lines of C code for the PD3
Arduino to turn on a lamp (PD2) and
PD2
buzzer (PD3) if the key is in the
ignition (PD0 closed), but seat belt is
not latched (PD1 open)
• (diagram shows only one of the two
switches, but both are similar) PD0, PD1
• Pseudocode first
Digital IO – Practice 2 Pseudocode
• Pseudocode:
Set up data direction of pins ATmega328
Make PD0 and PD1 inputs
Turn on pull up resistors for PD0 and PD1 PD3
Make PD2 and PD3 outputs
Loop forever PD2
IF key is in ignition THEN
IF belt is latched, THEN
Turn off buzzer
VTG= +5V
Turn off lamp
ELSE
Turn on lamp
Turn on buzzer
1
ELSE PD0, PD1
Turn off buzzer
Turn off lamp 0
Digital IO – Practice 2 (Arduino style code)
#define PIN_IGNITION 0
#define PIN_SEATBELT 1
#define PIN_LED 2 ATmega328
#define PIN_BUZZER 3
#define SEATBELT_LATCHED LOW
PD3
#define KEY_IN_IGNITION LOW
#define LED_ON HIGH PD2
#define LED_OFF LOW
#define BUZZER_ON HIGH VTG= +5V
#define BUZZER_OFF LOW
void setup()
{
pinMode(PIN_IGNITION, INPUT_PULLUP); // key switch 1
PD0, PD1
pinMode(PIN_SEATBELT, INPUT_PULLUP); // belt latch switch
pinMode(PIN_LED, OUTPUT); // lamp 0
pinMode(PIN_BUZZER, OUTPUT); // buzzer
}
void loop()
{ /* see next page for code */}
Digital IO – Practice 2 (Arduino style code)
/* see previous page for code before loop() */
void loop()
{ ATmega328
int key_state = digitalRead(PIN_IGNITION);
int belt_state = digitalRead(PIN_SEATBELT);
PD3
if (key_state == KEY_IN_IGNITION)
{ PD2
if (belt_state == SEATBELT_LATCHED)
{ VTG= +5V
digitalWrite(PIN_BUZZER, BUZZER_OFF);
digitalWrite(PIN_LED, LED_OFF);
}
else // key is in ignition, but seatbelt NOT latched 1
PD0, PD1
{
digitalWrite(PIN_BUZZER, BUZZER_ON); 0
digitalWrite(PIN_LED, LED_ON);
}
else // key is NOT in ignition
{
digitalWrite(PIN_BUZZER, BUZZER_OFF);
digitalWrite(PIN_LED, LED_OFF);
}
}
}
Digital IO – Practice 3 (Register style code)
/* NOTE: #defines use predefined PORT pin numbers for ATmega328 */
#define PIN_IGNITION PD0 ATmega328
#define PIN_SEATBELT PD1 PD3
#define PIN_LED PD2
#define PIN_BUZZER PD3
#define SEATBELT_LATCHED LOW
PD2
#define KEY_IN_IGNITION LOW
#define LED_ON HIGH VTG= +5V
#define LED_OFF LOW
#define BUZZER_ON HIGH
#define BUZZER_OFF LOW 1
PD0, PD1
#define _BIT_MASK( bit ) ( 1 << (bit) ) // same as _BV( bit)
0
void setup()
{
PORTD = 0; // all PORTD pullups off
DDRD = _BIT_MASK(PIN_LED) | _BIT_MASK(PIN_BUZZER); // LED and buzzer
PORTD | = _BV(PIN_IGNITION) | _BV(PIN_SEATBELT); // pullups for switches
}

/* See next page for loop() code */


Digital IO – Practice 3 (Register style code)
/* see previous page for setup() code */
void loop()
{ ATmega328
uint8_t current_PORTD_state, key_state, belt_state;
current_PORTD_state = PIND; // snapshot of PORTD pins
PD3
key_state = current_PORTD_state & _BV(PIN_IGNITION);
belt_state = current_PORTD_state & _BV(PIN_SEATBELT); PD2
if (key_state == KEY_IN_IGNITION)
{ VTG= +5V
if (belt_state == SEATBELT_LATCHED)
{
PORTD & = ~( _BV(PIN_LED) | _BV(PIN_BUZZER) ); 1
}
PD0, PD1
else 0
{
PORTD | = ( _BV(PIN_LED) | _BV(PIN_BUZZER) );
}
}
else
{
PORTD & = ~( _BV(PIN_LED) | _BV(PIN_BUZZER) );
}
}
Summary
• Data direction
• Input is default, but okay to set explictly
• Output
• Arduino style: pinMode(pin_no, mode)
• Alternate: Set bits in DDRx
• Pull-up resistors
• Pin must be an input
• Arduino style: digitalWrite(pin_no, state)
• Alternate style: Set bits in PORTx
Summary, cont.
• Read digital state of a pin
• Arduino style: digitalRead(pin_no)
• ‘Register-style’: need to form a bit mask and use it to
‘single-out’ the bit of interest
• Write to a pin (assuming it is an output)
• Arduino style: digitalWrite(pin_no, state)
• ‘Register-style’: use a bit mask and bit manipulation
techniques to set or clear only the bits of interest
Digital Input

You might also like