Sharif University of
Technology
Introduction to AVR
Programming
Applied Electronics (28846)
Fall 2014
Ali Shayei
Block Diagram and
Pin Configurations
Ali Shayei
Fuse Bits
There are few parameters in the chip which are used to
configure it before it can be used for external
environment. These parameters are set with the use of
Fuse Bits.
Once the fuse bits are set for a particular configuration,
the controller can be used again and again
You dont have to set the fuse bits every time you are using
the controller till the time you want to use it under the
same configuration.
Fuse bits need to be changed only in case you want to
change the initial configuration of the controller.
Ali Shayei
System Clock and Clock Options
Ali Shayei
Device Clocking Options Select
The Clock Source is determined by CKSEL fuse bits
Ali Shayei
Input/Output Ports
Ali Shayei
I/O Ports and
The communication channels
through which information
flows into or out of the
microcontroller
Example:
Ali Shayei
PORTB
Pins PB0 PB7
May not be contiguous
Often bi-directional
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
Ali Shayei
Setting the Pin Data Direction
Each port pin consists of three register bits: DDxn, PORTxn, and PINxn. the
DDxn bits are accessed at the DDRx I/O address, the PORTxn bits at the PORTx
I/O address, and the PINxn bits at the PINx I/O address.
The DDxn bit in the DDRx Register selects the direction of this pin. If DDxn is
written logic one, Pxn is configured as an output pin. If DDxn is written logic
zero, Pxn is configured as an input pin.
If PORTxn is written logic one when the pin is configured as an input pin, the
pull-up resistor is activated. To switch the pull-up resistor off,PORTxn has to be
written logic zero or the pin has to be configured as an output pin. The port
pins are tri-stated when reset condition becomes active, even if no clocks are
running.
If PORTxn is written logic one when the pin is configured as an output pin, the
port pin is driven high (one). If PORTxn is written logic zero when the pin is
configured as an output pin, the port pin is driven low (zero).
Ali Shayei
9
Setting the Pin Data Direction
Traditional Coding
DDRB = 0x3F
PORTB = 0x2F
Arduino
pinMode(pin_no., dir)
Ex. Make Arduino pin 3 (PD3) an output
Ali Shayei
pinMode(3, OUTPUT);
Note: one pin at a time
10
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
Ali Shayei
TTL
5 V (for HIGH)
0 V (for LOW)
3.3 V CMOS
3.3 V (for HIGH)
0 V (for LOW)
11
Pin Used as an Output
Turn on an LED, which is connected to pin Arduino
pin 0 (PD0) (note the resistor!)
What
should the data direction be for pin 0 (PD0)?
pinMode(___,
______);
0
OUTPUT
Turn
on the LED
0
HIGH
digitalWrite(___,______);
Turn
off the LED
ATmega328
Arduino
pin 0
(PD0)
digitalWrite(___,______);
0
LOW
Ali Shayei
12
Pins as Inputs and Pull-up Resistors
Using a switch as a sensor
Ex. Seat belt sensor
Detect the switch state
What should the data direction be for Arduino pin 3 (PD3)?
3
INPUT
pinMode(__,
______);
What will the voltage be on PD3 when the switch is closed?
What will the voltage be on PD3 when the switch is open?
Ali Shayei
ATmega328
Arduino
pin 3
(PD3)
Indeterminate!
13
Pins as Inputs and Pull-up Resistors
Switch as a sensor, cont.
Make the voltage on the pin determinate by turning
on the pull-up resistor for PD3
ATmega328
VTG= +5V
Assuming PD3 is an input:
digitalWrite(3,HIGH);
turns on
the pull-up resistor
PD3
0
pinMode(3,INPUT_PULLUP);
What will the voltage on PD3 be when the switch is
open?
Ali Shayei
VTG
What will the voltage on PD3 be when the switch is
closed?
14
Example 1
Make Arduino pins 3, 5, and 7 (PD3, PD5, and PD7)
to be outputs
Arduino approach
pinMode(3, OUTPUT);
pinMode(5, OUTPUT);
pinMode(7, OUTPUT);
Or if me106.h is used:
pinMode(PIN_D3, OUTPUT);
pinMode(PIN_D5, OUTPUT);
pinMode(PIN_D7, OUTPUT);
Alternate approach
DDRD = 0b10101000;
or
DDRD = 0xA8;
or
DDRD | = 1<<PD7 | 1<<PD5 | 1<<PD3;
Ali Shayei
15
Example 2
Make pins Arduino pins 0 and 1 (PD0 and PD1)
inputs, and turn on pull-up resistors
Arduino approach
pinMode(0, INPUT_PULLUP);
pinMode(1, INPUT_PULLUP);
digitalWrite(0, HIGH);
digitalWrite(1, HIGH);
Or if me106.h is used:
pinMode(PIN_D0, INPUT_PULLUP);
pinMode(PIN_D1, INPUT_PULLUP);
digitalWrite(PIN_D0, HIGH);
digitalWrite(PIN_D1, HIGH);
Ali Shayei
Alternate approach
DDRD = 0; // all PORTD pins inputs
PORTD = 0b00000011;
or
PORTD = 0x03;
or better yet:
DDRD & = ~(1<<PD1 | 1<<PD0);
PORTD | = (1<<PD1 | 1<<PD0);
16
Structure of an Arduino Program
An arduino program == sketch
Must have:
setup()
loop()
setup()
configures pin modes and
registers
loop()
runs the main body of the
program forever
like while(1) {}
Where is main() ?
Arduino simplifies things
Does things for you
/* Blink - turns on an LED for DELAY_ON msec,
then off for DELAY_OFF msec, and repeats
BJ Furman rev. 1.1 Last rev: 22JAN2011
*/
#define LED_PIN 13 // LED on digital pin 13
#define DELAY_ON 1000
#define DELAY_OFF 1000
void setup()
{
// initialize the digital pin as an output:
pinMode(LED_PIN, OUTPUT);
}
// loop() method runs forever,
// as long as the Arduino has power
void loop()
{
digitalWrite(LED_PIN, HIGH); // set the LED on
delay(DELAY_ON); // wait for DELAY_ON msec
digitalWrite(LED_PIN, LOW); // set the LED off
delay(DELAY_OFF); // wait for DELAY_OFF msec
}
Analog to Digital Conversion
Ali Shayei
18
ATMEGA328 ADC Features
Ali Shayei
19
ADC Timing Diagram
Ali Shayei
20
How to use ADC in Arduino?
Use the analogRead() function to read an analog value from a given pin:
void loop() {
int sensorValue = analogRead(A0);
// Note: A0 is predefined
.....
}
A0-A5 can be used for analogRead()
Ali Shayei
21
Pulse-width modulation (PWM)
Ali Shayei
22
Pulse-width modulation (PWM)
Pulse-width modulation (PWM) is a modulation technique used
in communications systems to encode the amplitude of a signal
into the width of the pulse (duration) of another signal.
its main use is to allow the control of the power supplied to
electrical devices, especially to inertial loads such as motors.
The term duty cycle describes the proportion of 'on' time to the
regular interval or 'period' of time
The main advantage of PWM is that power loss in the switching
devices is very low. When a switch is off there is practically no
current, and when it is on and power is being transferred to the
load, there is almost no voltage drop across the switch
Ali Shayei
23
Pulse-width modulation (PWM)
Ali Shayei
24
PWM in Arduino
analogWrite() writes an analog value (PWM wave) to a pin.
A call to analogWrite() is on a scale of 0 - 255, such that
analogWrite(255) requests a 100% duty cycle (always on), and
analogWrite(127) is a 50% duty cycle (on half the time) for
example.
The frequency of the PWM signal on most pins is approximately
490 Hz. On the Uno and similar boards, pins 5 and 6 have a
frequency of approximately 980 Hz.
On most Arduino boards, this function works on pins 3, 5, 6, 9,
10, and 11.
Ali Shayei
25