ARDUINO MICROCONTROLLER.ppt

download ARDUINO MICROCONTROLLER.ppt

of 55

Transcript of ARDUINO MICROCONTROLLER.ppt

  • 8/18/2019 ARDUINO MICROCONTROLLER.ppt

    1/55

    Microcontroller

    Fundamentals

    B. Furman13FEB2014

  • 8/18/2019 ARDUINO MICROCONTROLLER.ppt

    2/55

    Learning Objectives

    Exlain t!e general arc!itecture o" amicrocontroller 

    List t!e #e$ "eatures o" t!e %&mega32'

    microcontroller  Exlain t!e "eatures and elements o" t!e %rduino

    and (artronics Exerimenter (!ield )(E(* Exlain t!e concets o" microcontroller ins as

    inuts and oututs +onvert bet,een binar$ and !exadecimal digits

  • 8/18/2019 ARDUINO MICROCONTROLLER.ppt

    3/55

    Mec!atronics +oncet Ma

    +ontroller )-ard,are (o"t,are*

    ($stem to

    +ontrol

    (ensor 

    (ignal

    +onditioning

    /o,er nter"ace

     %ctuator 

    ser nter"ace

    /o,er 

    (ource

    B Furman 22%11

    ME 10ME 154

    ME 156

    ME 175

    ME 120

    ME 276%

    ME 10

    ME 120

    ME 10

    ME 170

    ME 1'6

    ME 110

    ME 13

    ME 154

    ME 156

    ME 1'2

    ME 1'7

    ME 175

    ME 10

    ME 120

    ME 10

     INTEGRATION

  • 8/18/2019 ARDUINO MICROCONTROLLER.ppt

    4/55

    8!at is a Microcontroller9

    http://www.freescale.com/files/microcontrollers/doc/ref_manual/M68HC05TB.pdf 

     %%LO:

    /&(

    What is the difference !etween a " Digital  #nput$ and an " Analog  #nput$%

  • 8/18/2019 ARDUINO MICROCONTROLLER.ppt

    5/55

     %&mega32' nternal %rc!itecture

     %&mega32' data s!eet . 2; 5

    !tt

  • 8/18/2019 ARDUINO MICROCONTROLLER.ppt

    6/55

     %&mega32' Features

     %&mega32' data s!eet . 1

    !ttdatas!eet.d"

  • 8/18/2019 ARDUINO MICROCONTROLLER.ppt

    7/55

     %rduino @uemilanove

    http://www.arduino.cc/en/Main/&rduinoBoard'uemilano(e

    !tt

  • 8/18/2019 ARDUINO MICROCONTROLLER.ppt

    8/55

     %rduino no C3

    !tt

  • 8/18/2019 ARDUINO MICROCONTROLLER.ppt

    9/55

     %rduino @ue

     %tmel (%M3'E rocessor )32 bit %CM +ortex M3 arc!itecture; '4M-*

    !tt

  • 8/18/2019 ARDUINO MICROCONTROLLER.ppt

    10/55

     %rduino @uemilanove=no Features

    Microcontroer ATmega!"8#328

    Oerating oltage 5

    nut oltage )recommended* 6A12

    nut oltage )limits* A20

    @igital =O /ins 14 )o" ,!ic! rovide /8M outut*

     %nalog nut /ins

    @+ +urrent er =O /in 40 m%

    @+ +urrent "or 3.3 /in 50 m%

    Flas! Memor$1 HB )%&mega1'* or 32 HB )%&mega32'* o" ,!ic! 2

    HB used b$ bootloader 

    (C%M 1 HB )%&mega1'* or 2 HB )%&mega32'*

    EE/COM 512 b$tes )%&mega1'* or 1 HB )%&mega32'*

    +loc# (eed 1 M-

    !tt

  • 8/18/2019 ARDUINO MICROCONTROLLER.ppt

    11/55!tt

  • 8/18/2019 ARDUINO MICROCONTROLLER.ppt

    12/55

     %&mega32' Microcontroller PinnumberPin

    name

    Specialfunction

    )ource:http://www.atmel.com/d*n/products/product_card.asp%+,-&Tmea8+

     ,ote the

    limitations1

    p. 316

  • 8/18/2019 ARDUINO MICROCONTROLLER.ppt

    13/55

     %bsolute Maximums

     %&mega32' data s!eet . 31

  • 8/18/2019 ARDUINO MICROCONTROLLER.ppt

    14/55

    Microcontroller /orts and /ins

    &!e communication c!annels

    t!roug! ,!ic! in"ormation

    "lo,s into or out o" t!e

    microcontroller  Ex. /OC&B

    /ins /B0 I /B6

    Ma$ not be contiguous O"ten biAdirectional 

    +

    )ee ne2t slides1

  • 8/18/2019 ARDUINO MICROCONTROLLER.ppt

    15/55

    /ort /in @ata @irectionalit$

    nut 8!en $ou ,ant to ta#e in"ormation "rom t!e external

    ,orld )sensors* into t!e M+

    Outut 8!en $ou ,ant to c!ange t!e state o" somet!ing

    out side t!e M+ )turn a motor on or o""; etc.*

    /ins de"ault to inut direction on o,erAu or

    reset Jour rogram can set or c!ange t!e directionalit$

    o" a in at an$ time

  • 8/18/2019 ARDUINO MICROCONTROLLER.ppt

    16/55

     %&mega32'

    Bloc# @iagram

    Input

    Output

  • 8/18/2019 ARDUINO MICROCONTROLLER.ppt

    17/55

    M'-+11 microcontroller 

  • 8/18/2019 ARDUINO MICROCONTROLLER.ppt

    18/55

    (etting t!e /in @ata @irection

     %rduino inMode' pin_no() dir *

    Ex. Ma#e %rduino in 3 )/@3* an output 

     pinMode(3, OUTPUT); pinMode(PIN_D3, OUTPUT); // with me106.h

    ote< one in at a time (uose $ou ,anted %rduino ins 3; 5; and 6 )/@3;

    /@5; and /@6* to be oututs9 s t!ere a ,a$ to ma#e t!em all oututs at t!e same

    time9 JesG %ns,er coming laterK

  • 8/18/2019 ARDUINO MICROCONTROLLER.ppt

    19/55

    /in sed as an Outut

    &urn on an LE@; ,!ic! isconnected to in %rduino in 0

    )/@0* )note t!e resistorG*

    8!at s!ould t!e datadirection be "or in 0 )/@0*9 pinMode(____, ____);

    &urn on t!e LE@ digit!"#ite(0,$I%$);

    &urn o"" t!e LE@ digit!"#ite(0,&O");

    ATmega328

    Arduino

    pin 0(PD0)

  • 8/18/2019 ARDUINO MICROCONTROLLER.ppt

    20/55

    /in oltages

    Microcontrollers are "undamentall$ digital  

    devices n"ormation is coded in t,o discrete states<

    -:- or LO8 )logic< 1 or 0* oltages

    &&L

    5 )"or -:-*

    0 )"or LO8*

    3.3 +MO(

    3.3 )"or -:-*

    0 )"or LO8*

  • 8/18/2019 ARDUINO MICROCONTROLLER.ppt

    21/55

    /ins as nuts and /ullAu Cesistors A 1

    sing a s,itc! as a sensor  Ex. (eat belt sensor 

    @etect t!e s,itc! state

    8!at s!ould t!e data directionbe "or %rduino in 3 )/@3*9

     pinMode(____, ____); 8!at ,ill t!e voltage be on /@3

    ,!en t!e s,itc! is closed9 8!at ,ill t!e voltage be on /@3

    ,!en t!e s,itc! is oen9 ndeterminateG

    ATmega328

    Arduino

    pin 3(PD3)

  • 8/18/2019 ARDUINO MICROCONTROLLER.ppt

    22/55

    /ins as nuts and /ullAu Cesistors A 2

    (,itc! as a sensor; cont. Ma#e t!e voltage on t!e in

    determinate b$ turning on t!e

    ullAu resistor "or /@3 %ssuming /@3 is an inut<

    digit!"#ite(3,$I%$); turns on t!eNullAu resistor 

     pinMode(3,INPUT_PU&&UP);

    8!at ,ill t!e voltage on /@3 be

    ,!en t!e s,itc! is oen9 &:

    8!at ,ill t!e voltage on /@3 be

    ,!en t!e s,itc! is closed9

    ATmega328

    PD3

    VTG= +5V

    0

    1

  • 8/18/2019 ARDUINO MICROCONTROLLER.ppt

    23/55

    /ins as nuts and /ullAu Cesistors A 3

    (,itc! as a sensor; cont. &o turn off  t!e ullAu

    resistor 

     %ssuming /@3 is an inut< digit!"#ite(3,&O"); turns

    t!e NullAu resistor o"" 

    ATmega328

    PD3

    VTG= +5V

    0

    1

  • 8/18/2019 ARDUINO MICROCONTROLLER.ppt

    24/55

    /ins as nuts and /ullAu Cesistors A 4

    /ossibilit$ o" ,ea# drive /in set as an input  ,it! a

    ullAu resistor turned on

    can source a small current Cemember t!isG

    ATmega328

    PD3

    VTG= +5V

    0

    1

    iweak 

  • 8/18/2019 ARDUINO MICROCONTROLLER.ppt

    25/55

    (artronics Exerimenter (!ield

    R*BLED

    R$ ser+oheader

     Teperature

    sensor

    Digital pinsheader

    Resetbutton

    Pie,ospeake

    r

    Analog pinsheader

    Potentioet

    er

    Photoresistor

    P%r&gnd

    header

    RedLEDs

     Tacts%itches

    Red&R*B juper

  • 8/18/2019 ARDUINO MICROCONTROLLER.ppt

    26/55

    -andling t!e %rduino A -o, NOT  to @o tG

    mroer -andling A NEVER GGG

  • 8/18/2019 ARDUINO MICROCONTROLLER.ppt

    27/55

    -andling t!e %rduino A &!e /roer 8a$

    /roer -andling A b$ t!e edgesGGG

  • 8/18/2019 ARDUINO MICROCONTROLLER.ppt

    28/55

    (artronics Exerimenter LE@ /inout

    /in and LE@ ma 11 A LE@0 )red*

    7 A LE@1 )red* or C:B )green*

    A LE@2 )red* or C:B )blue*

    3 A LE@3 )red* or C:B )red* 13 A LE@ on %rduino

    umer determines ,!et!er ins

    ma to red LE@s or t!e C:B

    !! $ " 3

  • 8/18/2019 ARDUINO MICROCONTROLLER.ppt

    29/55

    13 12 11 10 9 8 7 6 5 4 3 2 1 0

    )C3 M#)4 M4)# )) 4C #C+ , ,0 T T0 #,T #,T0 T' 7'

    9' 9' 9'

     pwm pwm pwm pwm pwm pwm

    9'0 9' 9' 9'

    reen !lue red

    piezo

    ser(o

    )W0 )W )W )W

    (artronics Exerimenter @igital /in

     %ssignments

    (ee t!e Introduction to the Arduino Microcontroer  laborator$ exercise

  • 8/18/2019 ARDUINO MICROCONTROLLER.ppt

    30/55

    7 6 5 4 3 2 1 0

    photocell POT tep !e"!o#

    (artronics Exerimenter %nalog /in

     %ssignments

    (ee t!e Introduction to the Arduino Microcontroer  laborator$ exercise

  • 8/18/2019 ARDUINO MICROCONTROLLER.ppt

    31/55

    Binar$ and -exadecimal umbers A 1

    Microcontrollers are "undamentall$ digital)as oosed to analog* and use binary  logic

    &,o states< !ig! and lo,; 1 or 0; on or o""  O"ten 5 or 0

    One binar$ digit is called a bit  t can ta#e on t,o ossible states< 1 or 0

    Eig!t binar$ digits are called a byte Four binar$ digits are called a nibble

  • 8/18/2019 ARDUINO MICROCONTROLLER.ppt

    32/55

    Binar$ and -exadecimal umbers A 2

    1 1 0 0 1 1 0 1

    6 5 4 3 2 1 0$it %o.

    &owe# "i''le

    (4 'it!)

    *ppe# "i''le

    (4 'it!)

    &$(&ea!t i,"i-ica"t $it)

    $(o!t i,"i-ica"t $it)

    B$te and bits

  • 8/18/2019 ARDUINO MICROCONTROLLER.ppt

    33/55

    Binar$ and -exadecimal umbers A 3

    1 1 3 ' ($a!e 10 o# decimal  "/'e#)

    55/88/05005000

    50850/505505  05/

    =+++

    ×+×+×+×

    1 1 0 1($a!e 2 o# binary "/'e# )

    08

    0   0

    =+++

    ×+×+×+×

    ($a!e 10)

    3 2 ! %$it %o.

    &ace Vaue

    P 'hat range o( decima )aues can * +its represent,P -o man/ )aues in tota can * +its represent,

    ($a!e 10)

    0 to 15

    16

  • 8/18/2019 ARDUINO MICROCONTROLLER.ppt

    34/55

    Binar$ and -exadecimal umbers A 4

    0 0 0 0 %

    0 0 0 1 !

    0 0 1 0 2

    0 0 1 1 3

    0 1 0 0 *

    0 1 0 1 0

    0 1 1 0 "

    0 1 1 1 1

    1 0 0 0 8

    1 0 0 1 $

    1 0 1 0 A1 0 1 1

    1 1 0 0

    1 1 0 1 4

    1 1 1 0 E

    1 1 1 1 5

    H9Binar*

     "h' i he impo#tnt*

    One he digit +n e-ed shorthand  to#ep#eent o-# in#'

    digit

    Two he digit +n e-ed shorthand  to#ep#eent eight

     in#' digit o# one 'te

  • 8/18/2019 ARDUINO MICROCONTROLLER.ppt

    35/55

    sing -ex alues

  • 8/18/2019 ARDUINO MICROCONTROLLER.ppt

    36/55

    /ractice

    0b11000111 in !ex )0b is + notation t!at sa$s;Ninterret ,!at "ollo,s as a binar$ number*

    0b10011001 in !ex

    0b10011001 as a base 10 number 

    0x5% in binar$ )use ' bits*

    0b11111111 in !ex and as a base 10 number

    )36*10 in binar$ and !ext!e re"ix Q0xQ is + notation t!at means t!at t!e digits ,!ic! "ollo, are !ex digits

    t!e re"ix Q0bQ means t!at t!e digits ,!ic! "ollo, are binar$ digits

    Bac to PO!T

  • 8/18/2019 ARDUINO MICROCONTROLLER.ppt

    37/55

    (olution

    1100 0111 in !ex ? 0x+6

    1001 1001 in !ex ? 0x77

    1001 1001 in base 10 ? 153

    0x5% in binar$ ? 0b0101 1010

    0b1111 1111 ? 0xFF or 255

    )36* ? 0b0010 0101 or 0x25

  • 8/18/2019 ARDUINO MICROCONTROLLER.ppt

    38/55

    (o 8!at9

    Cecall t!e Duestion< s t!ere a ,a$ c!ange t!e data direction "or a set o"

    ins all at t!e same time9

     %ll t!e ,or# o" M+ !aens t!roug! registers )secial memor$ locations* Cegisters on t!e %tmega32' are 'Abits ,ide

    &!e data direction register )@@Cx* !andles t!e

    data directions "or ins in /OC&x

    )ource:http://www.atmel.com/d*n/products/product_card.asp%+,-&Tmea8+ p. 93

  • 8/18/2019 ARDUINO MICROCONTROLLER.ppt

    39/55

    @ata @irection Cegister 

    " t!e bit is zero AR in ,ill be an input  Ma#ing a bit to be ero ?? cearing t!e bit

    " t!e bit is one AR in ,ill be an output 

    Ma#ing a bit to be one ?? setting t!e bit &o c!ange t!e data direction "or a set o" ins

    belonging to /OC&x at t!e same time<

    1. @etermine ,!ic! bits need to be set and cleared in

    @@Cx

    2. (tore t!e binar$ number or its eDuivalent )in an

    alternate base; suc! as !ex* into @@Cx

  • 8/18/2019 ARDUINO MICROCONTROLLER.ppt

    40/55

    /OC& /in and

    register details %&mega32' datas!eet; . 6A74

     #ump to

  • 8/18/2019 ARDUINO MICROCONTROLLER.ppt

    41/55

     %&mega32' /ort /in @etails

    (ee t!e %&mega32' data s!eet; . 6A74 /ort in "unctionalit$ is controlled b$ t!ree

    register  )secial memor$ location* bits<

    @@Cx @ata @irection bit in @@Cx register )read=,rite*

    /OC&xn /OC&xn bit in /OC&x data register )read=,rite*

    /xn /xn bit in /x register )read onl$*

  • 8/18/2019 ARDUINO MICROCONTROLLER.ppt

    42/55

    Examle 1

     %rduino aroac!    %lternate aroac!

    Ma#e %rduino ins 3; 5; and 6 )/@3; /@5; and/@6* to be oututs

    inMode)3; O&/&*SinMode)5; O&/&*S

    inMode)6; O&/&*S

    @@C@ ? 0b10101000S

    or 

    @@C@ ? 0x%'S

    or 

     @@C@ T ? 1UU/@6 T 1UU/@5 T 1UU/@3S

    More on t!is next lectureG

    Or i" me10.! is used<

    inMode)/>@3; O&/&*S

    inMode)/>@5; O&/&*S

    inMode)/>@6; O&/&*S

  • 8/18/2019 ARDUINO MICROCONTROLLER.ppt

    43/55

    Examle 2

     %rduino aroac!    %lternate aroac!

    Ma#e ins %rduino ins 0 and 1 )/@0 and /@1*inuts; and turn on ullAu resistors

    inMode)0; /&*S

    inMode)1; /&*S

    digital8rite)0; -:-*S

    digital8rite)1; -:-*S

    @@C@ ? 0S == all /OC&@ ins inuts

    /OC&@ ? 0b00000011S

    or 

    /OC&@ ? 0x03S

    or better $et<@@C@ ? V)1UU/@1 T 1UU/@0*S

    /OC&@ T ? )1UU/@1 T 1UU/@0*S

    More on t!is next lectureG

    Or i" me10.! is used<

    inMode)/>@0; /&*S

    inMode)/>@1; /&*S

    digital8rite)/>@0; -:-*S

    digital8rite)/>@1; -:-*S

  • 8/18/2019 ARDUINO MICROCONTROLLER.ppt

    44/55

    (tructure o" an %rduino /rogram

     %n arduino rogram ?? s6etch Must !ave<

    setu)*

    loo)*

    setu)* con"igures in modes and

    registers

    loo)* runs t!e main bod$ o" t!e

    rogram "orever  li#e ,!ile)1* WKX

    8!ere is main)* 9  %rduino simli"ies t!ings

    @oes t!ings "or $ou

    =Y in6  A turns on an LE@ "or @EL%J>O msec;

    t!en o"" "or @EL%J>OFF msec; and reeats

    B Furman rev. 1.1 Last rev< 22%2011

    Y=

    Zde"ine LE@>/ 13 == LE@ on digital in 13

    Zde"ine @EL%J>O 1000

    Zde"ine @EL%J>OFF 1000

    void setu)*

    W== initialie t!e digital in as an outut<

      inMode)LE@>/; O&/&*S

    X

    == loo)* met!od runs "orever;

    == as long as t!e %rduino !as o,er 

    void loo)*

    W

      digital8rite)LE@>/; -:-*S == set t!e LE@ on

      dela$)@EL%J>O*S == ,ait "or @EL%J>O msec

      digital8rite)LE@>/; LO8*S == set t!e LE@ o"" 

      dela$)@EL%J>OFF*S == ,ait "or @EL%J>OFF msec

    X

  • 8/18/2019 ARDUINO MICROCONTROLLER.ppt

    45/55

    @igital O I /ractice 1

    Ceading a in 8rite some lines o" + code "or t!e

     %rduino to determine a course o"

    action i" t!e seat belt !as been

    latc!ed )s,itc! closed*. " latc!ed; t!e ignition s!ould be

    enabled t!roug! a call to a "unction

    ig>enable)*.

    " not latc!ed; t!e ignition s!ould be

    disabled t!roug! a call to a "unctionig>disable)*

    8rite seudocode "irst

    ATmega328

    PD3

  • 8/18/2019 ARDUINO MICROCONTROLLER.ppt

    46/55

    @igital O I /ractice 1 (olution

    Ceading a in /seudocode<

    (et u /@3 as an inut

    &urn on /@3 ullAu resistor 

    Cead voltage on %rduino in 3 )/>@3*

    F />@3 voltage is LO8 )latc!ed*; &-E

    call "unction ig>enable)*

    EL(E

    call "unction ig>disable)*

    ATmega328

    PD3

    VTG= +5V

    0

    1

  • 8/18/2019 ARDUINO MICROCONTROLLER.ppt

    47/55

    @igital O I /ractice 1 (olution

    Ceading a in /seudocode<(et u /@3 as an inut

    &urn on /@3 ullAu resistor 

    Cead voltage on %rduino in 3 )/>@3*

    F />@3 voltage is LO8 )latc!ed*; &-E

    call "unction ig>enable)*

    EL(E

    call "unction ig>disable)*

    ATmega328

    PD3

    VTG= +5V

    0

    1

    deine PIN_"IT$ 3

    deine &2T$D &O"  pinMode(PIN_"IT$,INPUT_PU&&UP); e!t_tte 4 digit!5ed(PIN_"IT$);i (e!t_tte 44 &2T$D) ig_en!e(); 7e!e ig_di!ed(); 7

    One ,a$

    )sniet; not "ull rogram*

  • 8/18/2019 ARDUINO MICROCONTROLLER.ppt

    48/55

    @igital O I /ractice 2

    Ceading "rom and ,riting to ain 8rite some lines o" + code "or

    t!e %rduino to turn on a lam

    )/@2* and buer )/@3* i" t!e#e$ is in t!e ignition )/@0

    closed*; but seat belt is not

    latc!ed )/@1 oen*

    )diagram s!o,s onl$ one o" t!e t,os,itc!es; but bot! are similar*

    /seudocode "irst

    ATmega328

    PD0$ PD%

    PD2

    PD3

  • 8/18/2019 ARDUINO MICROCONTROLLER.ppt

    49/55

    @igital O I /ractice 2 /seudocode /seudocode<

    (et u data direction o" ins

    Ma#e /@0 and /@1 inuts

    &urn on ull u resistors "or /@0 and /@1

    Ma#e /@2 and /@3 oututs

    Loo "orever 

    F #e$ is in ignition &-E

    F belt is latc!ed; &-E

    &urn o"" buer 

    &urn o"" lam

    EL(E

    &urn on lam

    &urn on buer 

    EL(E

    &urn o"" buer 

    &urn o"" lam

    ATmega328

    PD0$ PD%

    VTG

    = +5V

    0

    1

    PD2

    PD3

  • 8/18/2019 ARDUINO MICROCONTROLLER.ppt

    50/55

    @igital O I /ractice 2 )%rduino st$le code*Zde"ine />:&O 0

    Zde"ine />(E%&BEL& 1

    Zde"ine />LE@ 2Zde"ine />B[[EC 3

    Zde"ine (E%&BEL&>L%&+-E@ LO8

    Zde"ine HEJ>>:&O LO8

    Zde"ine LE@>O -:-

    Zde"ine LE@>OFF LO8

    Zde"ine B[[EC>O -:-

    Zde"ine B[[EC>OFF LO8void setu)*

    W

    inMode)/>:&O; /&>/LL/*S == #e$ s,itc!

    inMode)/>(E%&BEL&; /&>/LL/*S == belt latc! s,itc!

    inMode)/>LE@; O&/&*S == lam

    inMode)/>B[[EC; O&/&*S == buer 

    Xvoid loo)*

    W =Y see next age "or code Y=X

    ATmega328

    PD0$ PD%

    VTG

    = +5V

    0

    1

    PD2

    PD3

  • 8/18/2019 ARDUINO MICROCONTROLLER.ppt

    51/55

    @igital O I /ractice 2 )%rduino st$le code*=Y see revious age "or code be"ore loo)* Y=

    void loo)*

    Wint #e$>state ? digitalCead)/>:&O*S

    int belt>state ? digitalCead)/>(E%&BEL&*S

    i" )#e$>state ?? HEJ>>:&O*

      W

      i" )belt>state ?? (E%&BEL&>L%&+-E@*

      W

      digital8rite)/>B[[EC; B[[EC>OFF*S  digital8rite)/>LE@; LE@>OFF*S

      X

      else

      W

      digital8rite)/>B[[EC; B[[EC>O*S

      digital8rite)/>LE@; LE@>O*S

      X  else

      W

      digital8rite)/>B[[EC; B[[EC>OFF*S

      digital8rite)/>LE@; LE@>OFF*S

      X

      X

    X

    ATmega328

    PD0$ PD%

    VTG= +5V

    0

    1

    PD2

    PD3

  • 8/18/2019 ARDUINO MICROCONTROLLER.ppt

    52/55

    @igital O I /ractice 3 )/ort st$le code*

    =Y O&E< Zde"ines use rede"ined /OC& in numbers "or %&mega32' Y=

    Zde"ine />:&O /@0

    Zde"ine />(E%&BEL& /@1

    Zde"ine />LE@ /@2

    Zde"ine />B[[EC /@3

    Zde"ine (E%&BEL&>L%&+-E@ LO8

    Zde"ine HEJ>>:&O LO8

    Zde"ine LE@>O -:-

    Zde"ine LE@>OFF LO8

    Zde"ine B[[EC>O -:-

    Zde"ine B[[EC>OFF LO8

    Zde"ine >B&>M%(H) bit * ) 1 UU )bit* * == same as >B) bit*

    void setu)*

    W

    /OC&@ ? 0S == all /OC&@ ullus o"" @@C@ T ? >B&>M%(H)/>LE@* T >B&>M%(H)/>B[[EC*S == LE@ and buer 

    /OC&@ T ? >B)/>:&O* T >B)/>(E%&BEL&*S == ullus "or s,itc!es

    X

     

    =Y (ee next age "or loo)* code Y=

    ATmega328

    PD0$ PD%

    VTG= +5V

    0

    1

    PD2

    PD3

  • 8/18/2019 ARDUINO MICROCONTROLLER.ppt

    53/55

    @igital O I /ractice 3 )/ort st$le code*=Y see revious age "or setu)* code Y=

    void loo)*

    Wuint'>t current>/OC&@>state; #e$>state; belt>stateS

    current>/OC&@>state ? /@S == snas!ot o" /OC&@ ins

    #e$>state ? current>/OC&@>state >B)/>:&O*S

    belt>state ? current>/OC&@>state >B)/>(E%&BEL&*S

    i" )#e$>state ?? HEJ>>:&O*

    W

      i" )belt>state ?? (E%&BEL&>L%&+-E@*

    W

     /OC&@ ? V) >B)/>LE@* T >B)/>B[[EC* *S

    X

    else

    W

    /OC&@ T ? ) >B)/>LE@* T >B)/>B[[EC* *SX

    X

    else

    W

      /OC&@ ? V) >B)/>LE@* T >B)/>B[[EC* *S

    XX

    ATmega328

    PD0$ PD%

    VTG= +5V

    0

    1

    PD2

    PD3

  • 8/18/2019 ARDUINO MICROCONTROLLER.ppt

    54/55

    (ummar$

    @ata direction nut is de"ault; but o#a$ to set exlictl$

    Outut

     %rduino st$le< inMode) pin_no, mode* %lternate< (et bits in @@Cx

    /ullAu resistors

    /in must be an inut %rduino st$le< digital8rite) pin_no, state*

     %lternate st$le< (et bits in /OC&x

  • 8/18/2019 ARDUINO MICROCONTROLLER.ppt

    55/55

    (ummar$; cont.

    Cead digital state o" a in %rduino st$le< digitalCead) pin_no*

    /ortAst$le< need to "orm a bit mas# and use it

    to singleAout t!e bit o" interest 8rite to a in )assuming it is an outut*

     %rduino st$le< digital8rite) pin_no, state*

    /ortAst$le< use a bit mas# and bitmaniulation tec!niDues to set or clear onl$

    t!e bits o" interest