chapter 4 pic io programming

Upload: andy-wo

Post on 07-Jul-2018

232 views

Category:

Documents


1 download

TRANSCRIPT

  • 8/19/2019 Chapter 4 PIC IO Programming

    1/28

    Chapter 4Chapter 4

    PIC18 I/O ProgrammingPIC18 I/O Programming

    1

  • 8/19/2019 Chapter 4 PIC IO Programming

    2/28

    PIC18 Parallel PortsPIC18 Parallel Ports

    • I/O ports are available in PIC18 toI/O ports are available in PIC18 tointeract, monitor and controlinteract, monitor and control

    peripherals.peripherals.

    LEDLED !"egment LED!"egment LED2

  • 8/19/2019 Chapter 4 PIC IO Programming

    3/28

    PIC18 Parallel PortsPIC18 Parallel Ports

    • #he n$mber o% ports in the PIC18 %amilhe n$mber o% ports in the PIC18 %amil&

    depends on the n$mber o% pins on thedepends on the n$mber o% pins on the

    chip.chip.

    • PIC18'4()* has %ive ports+ Port !EPIC18'4()* has %ive ports+ Port !E

    • Di%%erent ports have di%%erent n$mber o%Di%%erent ports have di%%erent n$mber o%

    pins+pins+

     – Port , -, C and D+ 8 pinsPort , -, C and D+ 8 pins

     – Port E+ pinsPort E+ pins

    3

  • 8/19/2019 Chapter 4 PIC IO Programming

    4/28

    PIC18 Parallel PortsPIC18 Parallel Ports

    • #he behavio$r o% each port is controlled#he behavio$r o% each port is controlled

    b& to special %$nction registers 0"'s2+b& to special %$nction registers 0"'s2+

     – PO#3 indicates the voltage levels on thePO#3 indicates the voltage levels on the

    pins o% the devicepins o% the device – #I"3 data direction register #I"3 data direction register 

    • e.g., 'or Port -, e have PO#- ande.g., 'or Port -, e have PO#- and

    #I"-#I"-

    4

  • 8/19/2019 Chapter 4 PIC IO Programming

    5/28

    #I"3 and PO#3 "'s#I"3 and PO#3 "'s

    • Each o% the Ports !E can be $sed %orEach o% the Ports !E can be $sed %or

    inp$t and o$tp$t.inp$t and o$tp$t.

    • #I"3 "' is $sed %or designating the#I"3 "' is $sed %or designating the

    direction o% a portdirection o% a port – * %or o$tp$t 0e.g., Controlling LED state2* %or o$tp$t 0e.g., Controlling LED state2

     – 1 %or inp$t 0e.g., 5e& scan21 %or inp$t 0e.g., 5e& scan2

    • e.g., #o o$tp$t data to Port -+e.g., #o o$tp$t data to Port -+ – 6rite *s to #I"- "'6rite *s to #I"- "'

     – 6rite data to PO#- "'6rite data to PO#- "'

    5

  • 8/19/2019 Chapter 4 PIC IO Programming

    6/28

    #I"3 and PO#3 "'s#I"3 and PO#3 "'s

    • e.g., o$tp$t the he3 val$e *3)7 to Port Ce.g., o$tp$t the he3 val$e *3)7 to Port C

    clrf TRISCclrf TRISC

     movlw 0x26 movlw 0x26

     movwf PORTC movwf PORTC

    • e.g., ead the c$rrent val$e o% Port De.g., ead the c$rrent val$e o% Port D

    into 6E register into 6E register setf TRISDsetf TRISD

     movf PORTD, W  movf PORTD, W 

    6

  • 8/19/2019 Chapter 4 PIC IO Programming

    7/28

    I/O ports and bit!addressabilit&I/O ports and bit!addressabilit&

    • O%ten need to access individ$al bit o% theO%ten need to access individ$al bit o% theport instead o% the entire 8 bits.port instead o% the entire 8 bits.

    • PIC18 provides instr$ctions that alterPIC18 provides instr$ctions that alterindivid$al bits itho$t altering the rest o%individ$al bits itho$t altering the rest o%the bits in the port.the bits in the port.

    • 9ost common bit!oriented instr$ctions+9ost common bit!oriented instr$ctions+

    Instr$ctionsInstr$ctions '$nction'$nction

    bs% %ileeg, bitbs% %ileeg, bit -it "et %ileeg-it "et %ileegbc% %ileeg, bitbc% %ileeg, bit -it Clear %ileeg-it Clear %ileeg

    btg %ileeg, bitbtg %ileeg, bit -it #oggle %ileeg-it #oggle %ileeg

    bt%sc %ileeg, bitbt%sc %ileeg, bit -it test %ileeg, s:ip i% clear -it test %ileeg, s:ip i% clear 

    bt%ss %ileeg, bitbt%ss %ileeg, bit -it test %ileeg, s:ip i% set-it test %ileeg, s:ip i% set7

  • 8/19/2019 Chapter 4 PIC IO Programming

    8/28

    bs%, bc% and btgbs%, bc% and btg

    • bs% %ileeg, bitbs% %ileeg, bit

     – e.g.,e.g., bsf PortB, 5 bsf PortB, 5 sets -it ( o% Port - to besets -it ( o% Port - to be

    11

    • bc% %ileeg, bitbc% %ileeg, bit – e.g.,e.g., bcf PortB, 5 bcf PortB, 5 sets -it ( o% Port - to besets -it ( o% Port - to be

    *.*.

    • btg %ileeg, bitbtg %ileeg, bit – e.g.,e.g., btg PortB, 5 btg PortB, 5 toggles -it ( o% Port -toggles -it ( o% Port -

    0i.e., sets it to 1 i% the c$rrent val$e is * ; vice0i.e., sets it to 1 i% the c$rrent val$e is * ; vice

    versa2versa2

    8

  • 8/19/2019 Chapter 4 PIC IO Programming

    9/28

    Chec:ing the state o% an I/O portChec:ing the state o% an I/O port

    • bt%sc 0bit test %ile, s:ip i% clear2 and bt%ssbt%sc 0bit test %ile, s:ip i% clear2 and bt%ss

    0bit test %ile, s:ip i% set2 are $sed to ma:e0bit test %ile, s:ip i% set2 are $sed to ma:e

    branching decision based on the stat$sbranching decision based on the stat$s

    o% a given bit.o% a given bit.• e.g., bt%sc PO#D, ) s:ips the ne3te.g., bt%sc PO#D, ) s:ips the ne3t

    instr$ction i% -it ) o% Port D e

  • 8/19/2019 Chapter 4 PIC IO Programming

    10/28

    E3ample+ bt%ssE3ample+ bt%ss

    • e.g., 6rite a program to 0a2 :eepe.g., 6rite a program to 0a2 :eep

    monitoring -) bit $ntil it becomes highmonitoring -) bit $ntil it becomes high

    0b2 6hen -) becomes high, rite 4(=0b2 6hen -) becomes high, rite 4(=

    to Port Cto Port C bsf TRISB, 2; set RB2 s !"#$t bsf TRISB, 2; set RB2 s !"#$t

    clrf TRISC; set Port C s o$t#$tclrf TRISC; set Port C s o$t#$t

     movlw 0x%5 movlw 0x%5

      &g!"' btfss PORTB, 2 &g!"' btfss PORTB, 2

     br &g!" br &g!"

     movwf PORTC movwf PORTC

    10

  • 8/19/2019 Chapter 4 PIC IO Programming

    11/28

    E3ample+ bt%ssE3ample+ bt%ss

    • e.g., 6rite a program to chec: -).e.g., 6rite a program to chec: -). – I% -) > *, send the letter ?@A to Port DI% -) > *, send the letter ?@A to Port D

     – I% -) > 1, send the letter ?BA to Port DI% -) > 1, send the letter ?BA to Port D

     bsf TRISB, 2; set RB2 s !"#$t bsf TRISB, 2; set RB2 s !"#$tclrf TRISD; set Port D s o$t#$tclrf TRISD; set Port D s o$t#$t

     &g!"' btfss PORTB, 2 &g!"' btfss PORTB, 2

     br Over br Over

     movlw &()( movlw &()(

     movwf PORTD movwf PORTD br &g!" br &g!"

    Over' movlw &(*(Over' movlw &(*(

     movwf PORTD movwf PORTD

     br &g!" br &g!"

    11

  • 8/19/2019 Chapter 4 PIC IO Programming

    12/28

    E3ample+ bt%scE3ample+ bt%sc

    • e.g., Per%orm the same %$nction $singe.g., Per%orm the same %$nction $sing

    bt%scbt%sc

     bsf TRISB, 2; set RB2 s !"#$t bsf TRISB, 2; set RB2 s !"#$t

    clrf TRISD; set Port D s o$t#$tclrf TRISD; set Port D s o$t#$t &g!"' btfsc PORTB, 2 &g!"' btfsc PORTB, 2

     br Over br Over

     movlw &+*( movlw &+*(

     movwf PORTD movwf PORTD

     br &g!" br &g!"

    Over' movlw &+)(Over' movlw &+)(

     movwf PORTD movwf PORTD

     br &g!" br &g!"

    12

  • 8/19/2019 Chapter 4 PIC IO Programming

    13/28

    Inter%acing ith LEDInter%acing ith LED

    • #o a&s o% connecting LEDs to I/O ports+#o a&s o% connecting LEDs to I/O ports+ – Common CathodeCommon Cathode+ LED cathodes are gro$nded+ LED cathodes are gro$nded

    and logic 1 %rom the I/O port t$rns on the LEDs.and logic 1 %rom the I/O port t$rns on the LEDs.

     – Common node+ LED anodes are connected to theCommon node+ LED anodes are connected to the

    poer s$ppl& and logic * %rom the I/O port t$rns onpoer s$ppl& and logic * %rom the I/O port t$rns onthe LEDs.the LEDs.

    Common Cathode

     Active high Active low

    Common Anode

    13

  • 8/19/2019 Chapter 4 PIC IO Programming

    14/28

    Inter%acing ith !"egment LEDInter%acing ith !"egment LED

    • O%ten $sed to displa& -CDO%ten $sed to displa& -CDn$mbers 0* thro$gh 2 and an$mbers 0* thro$gh 2 and a

    %e alphabets%e alphabets

    • gro$p o% eight LEDs gro$p o% eight LEDsph&sicall& mo$nted in theph&sicall& mo$nted in the

    shape o% the n$mber eightshape o% the n$mber eight

    pl$s a decimal pointpl$s a decimal point

    • Each LED is called aEach LED is called asegment and labeled as ?aAsegment and labeled as ?aA

    thro$gh ?gA.thro$gh ?gA.

    14

  • 8/19/2019 Chapter 4 PIC IO Programming

    15/28

    Inter%acing ith !"egment LEDInter%acing ith !"egment LED

    • In a common cathodeIn a common cathodeseven!segment LEDseven!segment LED – ll cathodes arell cathodes are

    connected together toconnected together togro$nd and the anodesgro$nd and the anodesare connected to dataare connected to datalineslines

    • Logic 1 t$rns on aLogic 1 t$rns on asegment.segment.

    • E3ample+ #o displa&E3ample+ #o displa&digit 1, all segmentsdigit 1, all segmentse3cept b and c sho$lde3cept b and c sho$ldbe o%%.be o%%.

    • -&te *****11* > *7 ill-&te *****11* > *7 illdispla& digit 1.displa& digit 1.

    15

  • 8/19/2019 Chapter 4 PIC IO Programming

    16/28

    Inter%acing ith !"egment LEDInter%acing ith !"egment LED

    16

  • 8/19/2019 Chapter 4 PIC IO Programming

    17/28

    Lab ) #as: 1+ "ingle digit !"egment LEDLab ) #as: 1+ "ingle digit !"egment LED

    • #as:+ 6rite a loop to sho digits *! se ** 0n$mber to be displa&ed2 – Instr$ctions to be repeated+ 012 call "@"E 0)2 Displa& digitInstr$ctions to be repeated+ 012 call "@"E 0)2 Displa& digit

    pattern stored in 6E 02 call Dela& 042 Increment @$mipattern stored in 6E 02 call Dela& 042 Increment @$mi – #his is an in%inite loop and e do not need to trac: n$mber o%#his is an in%inite loop and e do not need to trac: n$mber o%

    repetitionsrepetitions

    17

  • 8/19/2019 Chapter 4 PIC IO Programming

    18/28

    Inter%acing ith 4!digit !"egment LEDInter%acing ith 4!digit !"egment LED

    • Decoder selects the position here digitDecoder selects the position here digitpattern is displa&ed.pattern is displa&ed.

    • Gse time m$ltiple3ing i% e need toGse time m$ltiple3ing i% e need to

    displa& all %o$r digits.displa& all %o$r digits.

    18

    Inp$t 1+ Digit PatternInp$t 1+ Digit Pattern

    Inp$t )+ 6hich digit &o$Inp$t )+ 6hich digit &o$6ant Inp$t 1 to be6ant Inp$t 1 to be

    displa&edHdispla&edH

  • 8/19/2019 Chapter 4 PIC IO Programming

    19/28

    Lab ) #as: )+ 4!digit !"egment LEDLab ) #as: )+ 4!digit !"egment LED

    #as:+ Displa& &o$r gro$p#as:+ Displa& &o$r gro$pn$mber o% the 4!digit !n$mber o% the 4!digit !segment LED.segment LED.

    lgorithmlgorithm

    •InitialiFationInitialiFation – 4 %ile registers storing the 4 digits o% &o$r gro$p4 %ile registers storing the 4 digits o% &o$r gro$p

    n$mber+ @$m1, @$m), , @$m4n$mber+ @$m1, @$m), , @$m4 – Loc+ 6hich digit the n$mber sho$ld be displa&edHLoc+ 6hich digit the n$mber sho$ld be displa&edH

    •Loop+ 'or the %irst digit, get the digit patternLoop+ 'or the %irst digit, get the digit patterncorresponding to J@$m1K b& calling "@"Ecorresponding to J@$m1K b& calling "@"E DigitDigitpattern stored in J6EK. "et Inp$t1 > J6EK andpattern stored in J6EK. "et Inp$t1 > J6EK andInp$t ) > *. Per%orm similar operations %or the second,Inp$t ) > *. Per%orm similar operations %or the second,third and %o$rth digits.third and %o$rth digits.•epeat the loop in%initel&.epeat the loop in%initel&.

    19

  • 8/19/2019 Chapter 4 PIC IO Programming

    20/28

    Lab #as: 1+ 8!b$tton :e&padLab #as: 1+ 8!b$tton :e&pad

    • Con%ig$ration o%Con%ig$ration o%:e&pad+ P$lled $p:e&pad+ P$lled $p0i.e., 12 hen not0i.e., 12 hen notpressed. * i%pressed. * i%

    pressed.pressed.• #as:+ "ho MN#as:+ "ho MN

    hen -$tton PMNhen -$tton PMNis pressed. MN>1is pressed. MN>1

    to 8to 8

    20

  • 8/19/2019 Chapter 4 PIC IO Programming

    21/28

    Lab #as: )+ 434 5e& 9atri3Lab #as: )+ 434 5e& 9atri3

    • Con%ig$ration o% :e&Con%ig$ration o% :e&matri3matri3 – Port C connects toPort C connects to

    :e&pad matri3:e&pad matri3•

    C*! o$tp$ts, C4!C*! o$tp$ts, C4!inp$tsinp$ts

    • "ho MN i% -$tton"ho MN i% -$tton5M1N is pressed5M1N is pressedhere MN > * to '.here MN > * to '.

    21

  • 8/19/2019 Chapter 4 PIC IO Programming

    22/28

    Program "tr$ct$re %or -oth #as:sProgram "tr$ct$re %or -oth #as:s

    • "et I/O direction o% appropriate ports."et I/O direction o% appropriate ports.• call 5e&scancall 5e&scan

     – #as: 1+ O$tp$t %rom 1 to , indicating the b$tton#as: 1+ O$tp$t %rom 1 to , indicating the b$ttonthat is pressed. indicates no b$tton is pressed.that is pressed. indicates no b$tton is pressed.

     – #as: )+ O$tp$t %rom * to 17, indicating the b$tton#as: )+ O$tp$t %rom * to 17, indicating the b$ttonthat is pressed. * indicates that no b$tton isthat is pressed. * indicates that no b$tton ispressed.pressed.

    • call "@"Ecall "@"E – Inp$t+ O$tp$t %rom :e&scanInp$t+ O$tp$t %rom :e&scan

     – O$tp$t+ Digit pattern corresponding to inp$t val$eO$tp$t+ Digit pattern corresponding to inp$t val$e – Implemented $sing a loo:$p tableImplemented $sing a loo:$p table

    • Port connecting 1!digit !seg LED > O$tp$tPort connecting 1!digit !seg LED > O$tp$t%rom "@"E%rom "@"E

    22

  • 8/19/2019 Chapter 4 PIC IO Programming

    23/28

    5e&scan s$bro$tine %or #as: 15e&scan s$bro$tine %or #as: 1

    • "can P1 to P8 se

  • 8/19/2019 Chapter 4 PIC IO Programming

    24/28

    #as: )+ "canning 434 :e& matri3#as: )+ "canning 434 :e& matri3

    24

  • 8/19/2019 Chapter 4 PIC IO Programming

    25/28

    6hen 5( is pressed6hen 5( is pressed

    25

    0

    1

    1

    1

    1   1   1 1

    0

    0

    1

  • 8/19/2019 Chapter 4 PIC IO Programming

    26/28

    6hen 51( is pressed6hen 51( is pressed

    26

    0

    1

    1

    1

    1 1 1 1

    0

    1

    0

    0

    0

  • 8/19/2019 Chapter 4 PIC IO Programming

    27/28

    5e&scan s$bro$tine %or 434 :e& matri35e&scan s$bro$tine %or 434 :e& matri3

    1.1.InitialiFe 5EBCODE to *.InitialiFe 5EBCODE to *.).)."et C* to logic * and C1+ to logic 1"et C* to logic * and C1+ to logic 1

    ..call DetectCol, read the states o% C4+call DetectCol, read the states o% C4+ – *111*111

     

    DetectColO$tp$t > 1DetectColO$tp$t > 1 – 1*111*11

     

    DetectColO$tp$t > )DetectColO$tp$t > )

     – 11*111*1  DetectColO$tp$t > DetectColO$tp$t >  – 111*111*

     

    DetectColO$tp$t > 4DetectColO$tp$t > 4 – 11111111

     

    @o b$tton is pressed, DetectColO$tp$t > *@o b$tton is pressed, DetectColO$tp$t > *

    4.4.#o cases+#o cases+ – I% o$tp$t o% DetectColO$tp$t > *, no b$tton is pressed, scanI% o$tp$t o% DetectColO$tp$t > *, no b$tton is pressed, scan

    ne3t ro. dd 4 to 5EBCODE.ne3t ro. dd 4 to 5EBCODE. – I% o$tp$t o% DetectColO$tp$t > *, a b$tton is pressed,I% o$tp$t o% DetectColO$tp$t > *, a b$tton is pressed,

    ret$rn 5EBCODE > 5EBCODE DetectColO$tp$tret$rn 5EBCODE > 5EBCODE DetectColO$tp$t

    (.(.I% no b$tton is detected to be pressed a%ter scanning %o$rI% no b$tton is detected to be pressed a%ter scanning %o$rros, 5EBCODE > * and e3it the s$bro$tine.ros, 5EBCODE > * and e3it the s$bro$tine.

    27

  • 8/19/2019 Chapter 4 PIC IO Programming

    28/28

     Bo$ sho$ld be able to ... Bo$ sho$ld be able to ...

    • Code assembl& lang$age to $se theCode assembl& lang$age to $se theports %or inp$t or o$tp$tports %or inp$t or o$tp$t

    • Code I/O bit manip$lation programs %orCode I/O bit manip$lation programs %or

    the PICthe PIC• Chec: the state o% an I/O port and ma:eChec: the state o% an I/O port and ma:e

    branching decision based on itbranching decision based on it

    • Inter%ace ith 1!digit/4!digit !segmentInter%ace ith 1!digit/4!digit !segmentLED and :e&pad matri3.LED and :e&pad matri3.

    •  Bo$ can no start Labs ) and  Bo$ can no start Labs ) and

    28