444_ATmega328-2(1)

download 444_ATmega328-2(1)

of 10

Transcript of 444_ATmega328-2(1)

  • 8/13/2019 444_ATmega328-2(1)

    1/10

    r u no an me u o

    Suree Pumrin, Ph.D.

    Semester 2/2556

    1

    Lecture material taken from:

    Lecture 6 Introduction to the ATmega328 and Arduino, CSE

    P567, University of Washington,

    http://courses.cs.washington.edu/courses/csep567/10wi/lectures/lect

    ures.html.

    Intro to Arduino Linz Crai Nick Poole Prashanta Ar al Theo

    Simpson, Tai Johnson, and Eli Santistevan, Sparkfun Electronics.

    Beginning C for Arduino, Jack Purdum, Ph.D., Technology in Action,

    .

    2

    Objectives

    Programming the Arduino Uno Rev3 (cont.)

    Creating project on Atmel Studio

    3

    Arduino input/output (cont.)

    Programming Atmel studio

    4

  • 8/13/2019 444_ATmega328-2(1)

    2/10

    Arduino Uno Rev3

    5

    PWR INUSB

    (to Computer)

    RESET

    (I2C Bus)

    POWER

    5V / 3.3V / GND

    Analog

    g aPWM(3, 5, 6, 9, 10, 11)

    INPUTS6

    a ng n an a ng u

    Analo or Di ital?

    A few ins on the Arduino allow for us to

    modify the output to mimic an analog

    .

    This is done by a technique called:

    Concepts: Analog vs. Digital

    To create an analog signal, the microcontroller

    uses a techni ue called PWM. B var in the dut

    cycle, we can mimic an average analog voltage.

  • 8/13/2019 444_ATmega328-2(1)

    3/10

    Introducing a new command

    analogWrite( pi n, val ) ;

    , ,

    6, 9, 10, 11

    val 8 bit value (0 255).

    => =>

    ove one o your p ns over

    to Pin 9In Arduino, open up:

    File Exam les 01.Basics Fade

    Fade - Code Review Fade - Code Review

  • 8/13/2019 444_ATmega328-2(1)

    4/10

    Example-- Fading

    Challen e a Chan e the rate of the

    fading in and out. There are at least two

    them out?

    that one fades in as the other one fades

    .

    Current Loads

    NPN Transistor (Common Emitter Amplifier

    Circuit

    to Digitaln

    Digital Input

    Connect digital input to your Arduino using Pins # 0 13

    (Although pins # 0 & 1 are also used for programming)

    Digital Input needs a pi nMode command:

    inMode inNumber INPUT

    Make sure to use ALL CAPS for INPUT

    To get a digital reading:

    int buttonState = digitalRead

    (pinNumber);

    Digital Sensors

    Digital sensors are more straight forward than Analog

    No matter what the sensor there are only two settings:

    Signal is always either HIGH (On) or LOW (Off)

    Voltage signal for HIGH will be a little less than 5V on

    Voltage signal for LOW will be 0V on most systems

  • 8/13/2019 444_ATmega328-2(1)

    5/10

    http://opensourcehardwarejunkies.com/tutorial-03-

    Description( ) == ( ) is equal?

    ( ) != ( ) is not equal?

    ( ) > ( ) greater than

    ( ) < ( ) less than

    ( )

  • 8/13/2019 444_ATmega328-2(1)

    6/10

    Using Serial Communication

    Method used to transfer data between two devices.

    Data passes between the computer and Arduino

    through the USB cable. Data is transmitted as zeros

    an ones sequen a y.

    Arduino dedicates Digital I/O pin # 0 to

    receiving and Digital I/O pin #1 to transmit.

    Serial Monitor & analogRead()

    Initializes the Serial Communication

    9600 baud data rate

    prints data to serial bus

    Serial Monitor & analogRead()

    Opens up a

    Serial

    TerminalWindow

    Analo Sensors

    2 Pin Analog Sensors = var.

    res s orTake two sensors -- Use

    the Serial Monitor and findthe range of input values

    .

    ax na og ea = _________

    = _________

  • 8/13/2019 444_ATmega328-2(1)

    7/10

    Analog Sensors

    Examples:Sensors Variables

    Mic soundVolume

    Photoresistor lightLevel

    Potentiometer dialPosition

    Temp Sensor temperature

    Flex Sensor bendAccelerometer tilt/acceleration

    Sending a Message

    {

    Ser i al . pr i nt ( Hands on ) ;

    er a . pr n ear n ng ;

    Ser i al . pr i nt l n( i s Fun! ! ! ) ;

    Serial Debugging

    voi d loo ( )

    {

    i nt xVar = 10;

    Ser i al . pr i nt ( Var i abl e xVar i s ) ;

    Ser i al . pr i nt l n ( xVar ) ;

  • 8/13/2019 444_ATmega328-2(1)

    8/10

    Serial Troubleshooting

    voi d l oo ( )

    {

    Ser i al . pr i nt ( Di gi tal pi n 9: ) ;

    Ser i al . pr i nt l n ( di gi t al Read( 9) ) ;

    }

    eve opmen oo s

    Atmel Studio 6 is the integrated development

    environment (IDE) for developing and debuggingembedded applications based on Atmel AVR and

    ARM Cortex-M microcontrollers (MCUs) in

    .

    The IDE brings together an editor with assisted

    code writin a wizard for uickl creatin new

    projects, the Atmel Software Framework with free

    source code library, a GNU C/C++ Compiler, apowerful simulator, and the front-end for all Atmel

    programmers and in-circuit debuggers.

    302102444 Introduction toEmbedded Systems

    Starting Atmel Studio (1)

    Atmel Studio is started by clicking on the Atmel Studio 6.1

    shortcut in the Start-Up menu.

    , .

    you can create new projects and reopen recently used projects,

    as well as browse through articles providing tutorials, help and

    .

    The Start page can also be accessed from ViewStart Page,orAl t+V+G.

    312102444 Introduction toEmbedded Systems

    32Figure 1. The Project related section of the Start Page

  • 8/13/2019 444_ATmega328-2(1)

    9/10

    Starting Atmel Studio (2)

    The left section of the start page contains project-related

    items: New project - Use this to create a new project. If you are very

    new to the concept of software development with Atmel Studio,

    - - .

    available options are described in detail in Project Management

    New example project - To take a step-by-step tour of the

    ava a e me p a orms unc ona es us ng e o ware

    Framework, click this button .

    O en ro ect - Load an existin ro ect, not mentioned onthe Recent projects pane

    332102444 Introduction toEmbedded Systems

    Creating a project (1)

    On the Start Page, click the New Project option.

    Figure 2. Project options

    Creating a project (2)

    1. In the New Project dialog box, select Installed Templates. This lists

    e ava a e pro ec ypes.2. For this example, create an GCC C Executable Project.

    . , .

    4. In the Location box, select a save location.

    5. Atmel Studio will suggest a name in the Solution name box. You

    can override this name if wanted.

    6. Leave the Create directory for solution checkbox checked.

    . .

    35

    oos ng a arge ev ce

    When a new project is created, the Device Selection dialog is

    displayed and you will be prompted to select the project targetdevice..

    36

  • 8/13/2019 444_ATmega328-2(1)

    10/10

    Writing and Compiling Code

    372102444 Introduction toEmbedded Systems

    #include

    int main(void)

    DDRB = 0xff;

    while (1)

    z = PINB;

    PORTB = z+1;}

    }

    382102444 Introduction toEmbedded Systems

    nc u e

    #include

    int main(void)

    DDRB = 0xff;

    while (1)

    {

    *or n = ; = ; =

    {

    PORTB = i;

    _delay_loop_2(30000);

    for (int i = 128; i> 1; i -= i/2)

    {

    = ;

    _delay_loop_2(30000);

    }

    }

    392102444 Introduction toEmbedded Systems

    Exercise

    1. Code Atmel Studio 6.1 Tool Chain Arduino Setup Video2. Atmel studio

    odevoid setup() {

    DDRB = B00011111;}void loop() {

    PORTB = 1;}3. Datasheet Atmel PORT

    ON/OFF LED PORT

    40