100% found this document useful (1 vote)
389 views

Arduino

The document discusses the internal architecture of the ATmega328 microcontroller used in Arduino boards. It describes the microcontroller's features, ports, pins, and how pins are configured as inputs or outputs using data direction registers. The summary also provides an overview of the structure of an Arduino program, including the setup() and loop() functions, and how pins are accessed using digitalRead(), digitalWrite(), and pinMode() versus directly manipulating the port registers.

Uploaded by

sugadev74
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
389 views

Arduino

The document discusses the internal architecture of the ATmega328 microcontroller used in Arduino boards. It describes the microcontroller's features, ports, pins, and how pins are configured as inputs or outputs using data direction registers. The summary also provides an overview of the structure of an Arduino program, including the setup() and loop() functions, and how pins are accessed using digitalRead(), digitalWrite(), and pinMode() versus directly manipulating the port registers.

Uploaded by

sugadev74
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 22

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-Microcontrol
ler-ATmega48A-48PA-88A-88PA-168A-168PA-328-328P_datasheet.pdf
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
Flash Memory
KB 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
Pin number
name

Special
functio
n

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 C
flows 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

Inpu
t
Outp
ut
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
ATmega328
connected to pin Arduino pin 0
(PD0) (note the resistor!) Ardui
What should the data no
pin 0
direction be for pin 0 (PD0)? (PD0)
pinMode(____, ____);
Turn on the LED
digitalWrite(0,HIGH);
Turn off the LED
digitalWrite(0,LOW);
Recall the question:
Is there a way change the data direction for a set of
pins all at the same time?
All the work of MCU happens through registers
(special memory locations)
Registers on the Atmega328 are 8-bits wide
The data direction register (DDRx) handles the
data directions for pins in PORTx

Source:http://www.atmel.com/dyn/products/product_card.asp?PN=ATmega328P p. 93
Data Direction Register
If the bit is zero -> pin will be an input
Making a bit to be zero == clearing the bit
If the bit is one -> pin will be an output
Making a bit to be one == setting the bit
To change the data direction for a set of pins
belonging to PORTx at the same time:
1. Determine which bits need to be set and cleared in
DDRx
2. Store the binary number or its equivalent (in an
alternate base, such as hex) into DDRx
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 PORTx data register (read/write)
PINx
PINx PINx register (read only)
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;
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 next lecture!
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
#define DELAY_OFF 1000
loop()
void setup()
setup() {
configures pin modes and // initialize the digital pin as an output:
registers pinMode(LED_PIN, OUTPUT);
}
loop()
// loop() method runs forever,
runs the main body of the // as long as the Arduino has power
program forever
like while(1) {} void loop()
{
Where is main() ? digitalWrite(LED_PIN, HIGH); // set the LED on
delay(DELAY_ON); // wait for DELAY_ON msec
Arduino simplifies things digitalWrite(LED_PIN, LOW); // set the LED off
Does things for you delay(DELAY_OFF); // wait for DELAY_OFF msec
}
Summary
Data direction
Input is default, but okay to set explictly
Output
Arduino style: pinMode(pin_no, mode)
Alternate: Set bits in DDRx
Summary, cont.
Read digital state of a pin
Arduino style: digitalRead(pin_no)
Port-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)
Port-style: use a bit mask and bit
manipulation techniques to set or clear only
the bits of interest

You might also like