Top Banner

of 10

Lecture Programming Microcontrollers

Jun 01, 2018

Download

Documents

hectorlopez
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • 8/9/2019 Lecture Programming Microcontrollers

    1/22

    Programming Microcontrollers

    B. Furman19MAR2011

  • 8/9/2019 Lecture Programming Microcontrollers

    2/22

    Learning Objectives

    E!lain an" a!!l# best !ractices in $riting a!rogram %or a microcontroller 

    E!lain t&e structure o% a !rogram %or t&e

     Ar"uino E!lain t&e conce!t o% a 'bit mas() an" use it to

    "etermine i% a bit is clear or set *se bit+$ise logical o!erators to create bit

    mas(s an" etract bits

  • 8/9/2019 Lecture Programming Microcontrollers

    3/22

    Mec&atronics ,once!t Ma!

    ,ontroller -ar"$are / o%t$are

    #stem to

    ,ontrol

    ensor 

    ignal

    ,on"itioning

    Po$er nter%ace

     Actuator 

    *ser nter%ace

    Po$er 

    ource

    B3 Furman 223A411

    ME 105ME 167

    ME 168

    ME 196

    ME 120

    ME 298A

    ME 105

    ME 120

    ME 105

    ME 190

    ME 18

    ME 110

    ME 1:5

    ME 167

    ME 168

    ME 12

    ME 19

    ME 196

    ME 105

    ME 120

    ME 105

     INTEGRATION

  • 8/9/2019 Lecture Programming Microcontrollers

    4/22

    Reca! Last Lecture

    Binar# an" &e numbers ; use &e<

    =igital !ins can be in!uts or out!uts

    ;&at is t&e "i%%erence<

    Pins are bidirectional  %or "igital >O ;&ic& Ar"uino %unction "o #ou use<

    ==R - ? B@ ,@ or = %or Amega:2 register

    "etermines "irection +bit register 

    a '1) in ==R means<

    a '0) in ==R means<

    012:7658

  • 8/9/2019 Lecture Programming Microcontrollers

    5/22

    est Cour ,om!re&ension

    ;rite co"e to ma(e all !ins o% POR= to be

    out!uts -Ar"uino an" alternate ==R= ?

    ==R= ? 0b11111111; ==R= ? 266D

    ;rite co"e to ma(e !ins 6@ :@ an" 1 o%

    POR= to be out!uts@ an" t&e rest in!uts ==R= ? 0b00101010D

    ==R= ? 02AD ==R= ? -16 -1: -11D

    0xFF;!inMo"e-0@ O*P*D

     

    !inMo"e-8@ O*P*D

     Ar"uino st#le

    !inMo"e-1@ O*P*D

    !inMo"e-:@ O*P*D

    !inMo"e-6@ O*P*D

     Ar"uino st#le

  • 8/9/2019 Lecture Programming Microcontrollers

    6/22

    tructure o% an Ar"uino Program

     An ar"uino !rogram ?? 'sketch)

    Must &aveG setu!-

    loo!-

    setu!- con%igures !in mo"es an"

    registers

    loo!- runs t&e main bo"# o% t&e

    !rogram %orever  li(e $&ile-1 HI

    ;&ere is main- <  Ar"uino sim!li%ies t&ings

    =oes t&ings %or #ou

    >J Blink  + turns on an LE= %or =ELACKO4 msec@

    t&en o%% %or =ELACKOFF msec@ an" re!eatsB3 Furman rev. 1.1 Last revG 223A42011

    J>

    "e%ine LE=KP4? 1:D >> LE= on "igital !in 1:

    "e%ine =ELACKO4 ? 1000D

    "e%ine =ELACKOFF ? 1000D

    voi" setu!-

    H

    >> initialie t&e "igital !in as an out!utG

      !inMo"e-LE=KP4@ O*P*D

    I

    >> loo!- met&o" runs %orever@

    >> as long as t&e Ar"uino &as !o$er 

    voi" loo!-

    H

      "igital;rite-LE=KP4@ ND >> set t&e LE= on

      "ela#-=ELACKO4D >> $ait %or =ELACKO4 msec

      "igital;rite-LE=KP4@ LO;D >> set t&e LE= o%% 

      "ela#-=ELACKOFFD >> $ait %or =ELACKOFF msec

    I

  • 8/9/2019 Lecture Programming Microcontrollers

    7/22

    Best Practices an" Patterns +1

    Programmer)s bloc(  At a minimumG

    Program name

    =escri!tion o% $&at

    t&e !rogram "oes  Aut&or 

    Revision number 

    Revision "ate>time

    Even betterG ,reation "ate

    n!uts

    Out!uts

    Met&o">algorit&m

    >J Blink  + turns on an LE= %or =ELACKO4 msec@ t&en

    o%% %or =ELACKOFF msec@ an" re!eats

    B3 Furman rev. 1.1 Last revG 223A42011

    J>

  • 8/9/2019 Lecture Programming Microcontrollers

    8/22

    Best Practices an" Patterns +2

     Avoi" '&ar" co"ing)constants *se "e%ine an"

    s#mbolic names

    instea" ;<

    #mbolic names are

    usuall# !ut in all

    ca!s to "i%%erentiate%rom variables

    ee me106.h

    "e%ine LE=KP4 ? 1:D >> LE= on "igital !in 1:

    "e%ine =ELACKO4 ? 1000D

    "e%ine =ELACKOFF ? 1000D

  • 8/9/2019 Lecture Programming Microcontrollers

    9/22

    o$ to $i""le Bits

    Recall t&e eam!le o% t&e seat beltin"icator s#stem , co"e sni!!et -not %ull !rogram

    ATmega328

    D0, D1

    VTG= +5V

    0

    1

    D2

    D3

    "e%ine LA,E= 0

    "e%ine E4NANE= 0

    !inMo"e-0@ 4P*D >> (e# s$itc&!inMo"e-1@ 4P*D >> belt latc& s$itc&

    !inMo"e-2@ O*P*D >> lam!

    !inMo"e-:@ O*P*D >> buer 

    (e#Kstate?"igitalRea"-0D

    beltKstate?"igitalRea"1D

    i%-(e#Kstate??E4NANE=  i%-beltKstate??LA,E=

      "igital;rite-:@ LO;D

      "igital;rite-2@ LO;D

      else

      "igital;rite-2@ ND

      "igital;rite-:@ ND

    else  D

  • 8/9/2019 Lecture Programming Microcontrollers

    10/22

    Bit Mani!ulation Practice

    ee t&e &an"out on Bit Mani!ulation etting bits

    ,learing bits

    oggling bits

    Ma(e bits 6 an" : o% PORB &ig& an" t&e rest lo$

    ChallengeG

  • 8/9/2019 Lecture Programming Microcontrollers

    11/22

    ummar# o% Bit Mani!ulation

    etting a bit -ma(ing it a '1) Bit$ise OR t&e POR register $it& t&e

    corres!on"ing bit mas( E. PORTB | = B!"#$;

    ,learing a bit -ma(ing it a '0) Bit$ise A4= t&e POR register $it& t&e

    corres!on"ing complemented  bit mas( E. PORTB % = &" B!"#$ $;

    oggling a bit -ma(ing it %li! Bit$ise OR t&e POR register $it& t&e

    corres!on"ing bit mas( E. PORTB ' = B!"#$;

  • 8/9/2019 Lecture Programming Microcontrollers

    12/22

    Bit $i""ling Practice

    Ma(e Ar"uino !ins 11 1: to be out!uts an"

    !ins 10 to be in!uts

    1. *se t&e Ar"uino met&o"

    2. *se t&e 'all+at+once) -general met&o"

    ,&ec( i% !in 9 is &ig& % !in 9 is &ig&@ ma(e !in 1: &ig& an" !in 11 lo$

    Else bot& !ins 1: s&oul" be lo$

    1. *se t&e Ar"uino met&o"

    2. *se t&e general !ort+st#le met&o"

  • 8/9/2019 Lecture Programming Microcontrollers

    13/22

    Pull+u! Resistors

    Pins con%igure" as 4P* can be '!ulle"u!) to QN ; is t&is use%ul<

    Puts an in!ut !in in a (no$n state -logic &ig& i% noeternal in%luence &as !ulle" it "o$n -to logic lo$ Eam!le o% a s$itc& connecte" bet$een a !in an"

    groun"

    o$ is it "one

  • 8/9/2019 Lecture Programming Microcontrollers

    14/22

    Re"o eat Belt ensor #stem

    *se !ort+st#le !rogrammingATmega328

    D0, D1

    VTG= +5V

    0

    1

    D2

    D3

    "e%ine LA,E= 0

    "e%ine E4NANE= 0

    ==R= ? KBQ-2 KBQ-:D >> =2 an" =: are O*P*s

    POR= ? KBQ-0 KBQ-1D >> turn on !ull+u!s %or =0 an" =1currentKstate ? P4=D >> invert %or active+lo$ s$itc&es

    (e#Kstate?currentKstate / - KBQ-0

    beltKstate?currentKstate / - KBQ-1

    i%-(e#Kstate??E4NANE=

      i%-beltKstate??LA,E=

      POR= / ? - KBQ-2 KBQ-: D >> buer an" lam! o%%   else

      POR= ? - KBQ-2 KBQ-: D >> buer an" lam! on

    else

      POR= / ? - KBQ-2 KBQ-: D >> buer an" lam! o%% 

    Key on D0Belt on D1

  • 8/9/2019 Lecture Programming Microcontrollers

    15/22

    Reca! Amega =igital >O

    Pins are bi+"irectional. ,an con%igure asG In)*ts  KKKKKKK "etermines t&e !in voltage

    O*t)*ts  KKKKKK "etermines t&e !in voltage

    =irection "etermine" b# bits in ++R register  ;&ere is B@ ,@ = %or t&e Amega:2 -an" ==R

    corres!on"s to all   !ins associate" $it& t&e !ort

    % con%igure" as output G Program can s!eci%# a !in to be &ig& -QN or lo$ -N4= b#

    $riting a corres!on"ing 1 or 0 -res!ectivel# to POR

    register  E. o ma(e Port = !ins 8@ :@ an" 7 lo$@ an" t&e rest &ig&

    POR=?KKKKKKKKKKKD -$rite in binar#@ t&en in &e

  • 8/9/2019 Lecture Programming Microcontrollers

    16/22

    Reca! Amega =igital >O@ cont.

    % !ins con%igure" as input @ t&is meansG Eternal "evice can !ull !in voltage &ig& or lo$

    i.e. ta(e u! to QN or ta(e "o$n to N4=

    Cou can "etermine t&e state o% t&e !ort

    !ins b# rea"ing t&e P4 register  Nrabs all eig&t logic levels at t&e same time

    E. POR= con%igure" as in!uts

    P=0

    P=1

    P=2

    P=:

    P=7

    P=6

    P=5

    P=8

    QN

    uint8_t a_pins;

    a_pins=PINDD

    ;&at is t&e content o% aK!insG

    binar#GKKKKKKKKKK 

    &eGKKKKK 

  • 8/9/2019 Lecture Programming Microcontrollers

    17/22

    Reca! Amega =igital >O@ cont.

    % !ins con%igure" as input @ cont.G ,an turn !ull+u! resistors on or o%% b#

    $riting a 1 or 0 to corres!on"ing !insin POR A !ull+u! resistor internall# connects a

    !in to QN to give it a "e%ine" state-logic &ig&@ i.e.@ 1

    E. ;rite t&e co"e t&at $illG Ma(e Port = !ins inputs urn on !ull+u! resistors Rea" t&e voltages on t&e !ins an" store

    t&em in a variable@ test=

    ;&at is t&e value o% test= in binar#an" &e< P=0

    P=1

    P=2

    P=:P=7

    P=6

    P=5

    P=8 QN

  • 8/9/2019 Lecture Programming Microcontrollers

    18/22

    Rea"ing POR= Pins Eam!le

    unsigne" c&ar test=D

    ==R=?0D

    test=?P4=D

    ;&at is t&e content o% test=<

    binar#G 11111001

    &eG F9 P=0

    P=1

    P=2

    P=:

    P=7

    P=6

    P=5

    P=8 QN

  • 8/9/2019 Lecture Programming Microcontrollers

    19/22

     Amega:2 Features

     Amega:2 "ata s&eet !. 1

    &tt!G>>$$$.atmel.com>"#n>resources>!ro"K"ocuments>"oc281.!"% 

  • 8/9/2019 Lecture Programming Microcontrollers

    20/22

     Amega:2 nternal Arc&itecture

     Amega:2 "ata s&eet !!. 2@ 6

  • 8/9/2019 Lecture Programming Microcontrollers

    21/22

    POR Pin

    c&ematics

     Amega:2 "atas&eet@ !!. 85+88

  • 8/9/2019 Lecture Programming Microcontrollers

    22/22

     Amega:2 Port Pin =etails

    ee t&e Amega:2 "ata s&eet@ !!. 85+97 Port !in %unctionalit# is controlle" b# t&ree

    register  -s!ecial memor# location bitsG

    ==R =ata =irection bit in ==R register -rea">$rite

    PORn PORn bit in POR "ata register -rea">$rite

    P4n P4n bit in P4 register -rea" onl#