chapter 3 - input output ports programming

Upload: jiachyi-yeoh

Post on 02-Jun-2018

244 views

Category:

Documents


1 download

TRANSCRIPT

  • 8/10/2019 Chapter 3 - Input Output Ports Programming

    1/29

  • 8/10/2019 Chapter 3 - Input Output Ports Programming

    2/29

    PIC18 has many ports Depending on the family member Depending on the number of pins on the chip

    Each port can be configured as input or output. Bidirectional port

    Each port has some other functions Such as timer , ADC, interrupts and serial communication.

    Some ports have 8 bits, while others have not

  • 8/10/2019 Chapter 3 - Input Output Ports Programming

    3/29

    PIC18F4550 connection diagram

  • 8/10/2019 Chapter 3 - Input Output Ports Programming

    4/29

    Features and Functions

    In order pins operation can match internal 8-bitorganization, (all of them are similar to registers)grouped into five so called ports denoted by A, B, C, Dand E. They all have several features in common: many I/O pins have two or three functions. If a pin is

    used as any other function, it may not be used as ageneral purpose input/output pin

    Every port has its corresponding TRIS register:TRISA, TRISB, TRISC etc. which determinesperformance, but not the contents of the port bits.

  • 8/10/2019 Chapter 3 - Input Output Ports Programming

    5/29

    By clearing some bit of the TRIS register(bit=0), the corresponding port pin isconfigured as output .

    By setting some bit of the TRIS register(bit=1), the corresponding port pin isconfigured as input .

    This rule is easy to remember 0 = Output, 1 = Input

  • 8/10/2019 Chapter 3 - Input Output Ports Programming

    6/29

    PIC pin functionsetting

  • 8/10/2019 Chapter 3 - Input Output Ports Programming

    7/29

  • 8/10/2019 Chapter 3 - Input Output Ports Programming

    8/29

    Example: Port A as Output

  • 8/10/2019 Chapter 3 - Input Output Ports Programming

    9/29

    Example: Port A as Input

  • 8/10/2019 Chapter 3 - Input Output Ports Programming

    10/29

    Example: more efficient way to set port asinput or output

  • 8/10/2019 Chapter 3 - Input Output Ports Programming

    11/29

  • 8/10/2019 Chapter 3 - Input Output Ports Programming

    12/29

    Read followed by writeoperation

    Be careful Dont have two I/O operations one right after

    the others.

    Data Dependency A NOP is needed to make that data is written

    in the WREG before it read for outputting to

    PortB

  • 8/10/2019 Chapter 3 - Input Output Ports Programming

    13/29

    Dual role of microcontroller port

    Port A and B Port A usually multiplexed with Analog to Digital converter to

    save I/O pins. Port A seldom used for simple I/O operation

    Port A Port B

  • 8/10/2019 Chapter 3 - Input Output Ports Programming

    14/29

    Port C and D (alternate function)

    Dual role of microcontroller port

  • 8/10/2019 Chapter 3 - Input Output Ports Programming

    15/29

    Dual role of microcontroller port

    Port E (alternate function) Port E occupies a total of 3 pin (RE0-RE2) for

    3 additional analog input or I/O.

    AN5, AN6 and AN7

  • 8/10/2019 Chapter 3 - Input Output Ports Programming

    16/29

    I/O Bit Manipulation Programming

    I/O ports and bit-addressability

    Monitoring a single bit Reading a single bit

  • 8/10/2019 Chapter 3 - Input Output Ports Programming

    17/29

    I/O ports and bit addressabilitySingle bit addressability for PIC18

  • 8/10/2019 Chapter 3 - Input Output Ports Programming

    18/29

    Bit Oriented Instruction for PIC18

    Single bit (Bit oriented) instruction for PIC18

  • 8/10/2019 Chapter 3 - Input Output Ports Programming

    19/29

    BSF (bit set fileReg)

    Used to set HIGH for a single bit in fileReg The Syntax :

    BSF fileReg, bit_num

  • 8/10/2019 Chapter 3 - Input Output Ports Programming

    20/29

    20

  • 8/10/2019 Chapter 3 - Input Output Ports Programming

    21/29

    BCF(bit clear fileReg)

    Used to clear a sigle bit of given fileReg The Syntax:

    BCF fileReg, bit_number

  • 8/10/2019 Chapter 3 - Input Output Ports Programming

    22/29

  • 8/10/2019 Chapter 3 - Input Output Ports Programming

    23/29

    Checking an input pin

    To make decisions base on the status of agiven bit in the file register, we use: BTFSC (bit test fileReg skip if clear) BTFSS (bit test fileReg skip if set)

    These single-bit instruction allow tomonitor a single bit and make decisionwhether it is 0 or 1.

  • 8/10/2019 Chapter 3 - Input Output Ports Programming

    24/29

  • 8/10/2019 Chapter 3 - Input Output Ports Programming

    25/29

    Reading a single bit

    We can use bit test instruction to read thestatus of the single bit and send it toanother bit of save it

  • 8/10/2019 Chapter 3 - Input Output Ports Programming

    26/29

  • 8/10/2019 Chapter 3 - Input Output Ports Programming

    27/29

  • 8/10/2019 Chapter 3 - Input Output Ports Programming

    28/29

    Reading input pins VS. LATxport

  • 8/10/2019 Chapter 3 - Input Output Ports Programming

    29/29