avr programming

25
Introduction to AVR Programming Applied Electronics (28846) Fall 2014 Ali Shayei 1 Sharif University of Technology

Upload: ali-darijani

Post on 14-Sep-2015

65 views

Category:

Documents


5 download

DESCRIPTION

it is a brief introduction to micro controller programming specially arduino

TRANSCRIPT

  • Introduction to AVR

    ProgrammingApplied Electronics (28846)

    Fall 2014

    Ali Shayei 1

    Sharif University of

    Technology

  • Block Diagram and

    Pin Configurations

    Ali Shayei 2

  • Fuse Bits

    There are few parameters in the chip which are used toconfigure it before it can be used for externalenvironment. These parameters are set with the use ofFuse 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 usingthe controller till the time you want to use it under thesame configuration.

    Fuse bits need to be changed only in case you want tochange the initial configuration of the controller.

    Ali Shayei 3

  • System Clock and Clock Options

    Ali Shayei 4

  • Device Clocking Options Select

    Ali Shayei

    The Clock Source is determined by CKSEL fuse bits

    5

  • Input/Output Ports

    Ali Shayei

  • I/O Ports and

    The communication channels

    through which information

    flows into or out of the

    microcontroller

    Example: PORTB

    Pins PB0 PB7

    May not be contiguous

    Often bi-directional

    C

    Ali Shayei 7

  • 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 8

  • 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

    pinMode(3, OUTPUT);

    Note: one pin at a time

    Ali Shayei 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

    TTL

    5 V (for HIGH)

    0 V (for LOW)

    3.3 V CMOS

    3.3 V (for HIGH)

    0 V (for LOW)

    Ali Shayei 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(___, ______);

    Turn on the LED

    digitalWrite(___,______);

    Turn off the LED

    digitalWrite(___,______);

    ATmega328

    Arduinopin 0(PD0)

    0 OUTPUT

    0 HIGH

    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)?

    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?

    Indeterminate!

    ATmega328

    Arduinopin 3(PD3)

    3 INPUT

    Ali Shayei 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

    Assuming PD3 is an input:

    digitalWrite(3,HIGH); turns on

    the pull-up resistor

    pinMode(3,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?

    ATmega328

    PD3

    VTG= +5V

    0

    1

    Ali Shayei 14

  • Example 1

    Arduino approach

    Ali Shayei 15

    Alternate approach

    Make Arduino pins 3, 5, and 7 (PD3, PD5, and PD7)

    to be outputs

    pinMode(3, OUTPUT);

    pinMode(5, OUTPUT);

    pinMode(7, OUTPUT);

    DDRD = 0b10101000;

    or

    DDRD = 0xA8;

    or

    DDRD | = 1

  • Example 2

    Arduino approach

    Ali Shayei 16

    Alternate approach

    Make pins Arduino pins 0 and 1 (PD0 and PD1)

    inputs, and turn on pull-up resistors

    pinMode(0, INPUT_PULLUP);

    pinMode(1, INPUT_PULLUP);

    digitalWrite(0, HIGH);

    digitalWrite(1, HIGH);

    DDRD = 0; // all PORTD pins inputs

    PORTD = 0b00000011;

    or

    PORTD = 0x03;

    or better yet:

    DDRD & = ~(1

  • 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