Embedded System Spring, 2011 Lecture 9: I/O Programming Eng. Wazen M. Shbair
description
Transcript of Embedded System Spring, 2011 Lecture 9: I/O Programming Eng. Wazen M. Shbair
![Page 1: Embedded System Spring, 2011 Lecture 9: I/O Programming Eng. Wazen M. Shbair](https://reader035.fdocuments.in/reader035/viewer/2022062316/56814cb9550346895db9c4c2/html5/thumbnails/1.jpg)
Embedded SystemSpring, 2011Lecture 9: I/O Programming Eng. Wazen M. Shbair
![Page 2: Embedded System Spring, 2011 Lecture 9: I/O Programming Eng. Wazen M. Shbair](https://reader035.fdocuments.in/reader035/viewer/2022062316/56814cb9550346895db9c4c2/html5/thumbnails/2.jpg)
2IUG- Embedded System
Today’s Lecture
I/O Port Programming in PIC Microcontroller I/O Bit Manipulation Programming
![Page 3: Embedded System Spring, 2011 Lecture 9: I/O Programming Eng. Wazen M. Shbair](https://reader035.fdocuments.in/reader035/viewer/2022062316/56814cb9550346895db9c4c2/html5/thumbnails/3.jpg)
3
Objective
List all the ports of the PIC18 Describe the dual role of PIC18 pins Code Assembly to use ports for input or
output Code PIC instructions for I/O handling Code I/O bit-manipulation Programs for PIC Explain the bit addressability of PIC ports
![Page 4: Embedded System Spring, 2011 Lecture 9: I/O Programming Eng. Wazen M. Shbair](https://reader035.fdocuments.in/reader035/viewer/2022062316/56814cb9550346895db9c4c2/html5/thumbnails/4.jpg)
4
I/O Port Programming in PIC18
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
![Page 5: Embedded System Spring, 2011 Lecture 9: I/O Programming Eng. Wazen M. Shbair](https://reader035.fdocuments.in/reader035/viewer/2022062316/56814cb9550346895db9c4c2/html5/thumbnails/5.jpg)
5
I/O Port Programming in PIC18
![Page 6: Embedded System Spring, 2011 Lecture 9: I/O Programming Eng. Wazen M. Shbair](https://reader035.fdocuments.in/reader035/viewer/2022062316/56814cb9550346895db9c4c2/html5/thumbnails/6.jpg)
6
![Page 7: Embedded System Spring, 2011 Lecture 9: I/O Programming Eng. Wazen M. Shbair](https://reader035.fdocuments.in/reader035/viewer/2022062316/56814cb9550346895db9c4c2/html5/thumbnails/7.jpg)
7
I/O SFR
Each port has three registers for its operation: TRIS register (Data Direction register)
If the corresponding bit is 0 Output If the corresponding bit is 1 Input
PORT register (reads the levels on the pins of the device)
LAT register (output latch) The Data Latch (LAT) register is useful for read-
modify-write operations on the value that the I/O are driving.
![Page 8: Embedded System Spring, 2011 Lecture 9: I/O Programming Eng. Wazen M. Shbair](https://reader035.fdocuments.in/reader035/viewer/2022062316/56814cb9550346895db9c4c2/html5/thumbnails/8.jpg)
8
I/O SFR
PICC18F458 has 5 Ports Upon reset, all ports are
configured as input. TRISx register has 0FFH
![Page 9: Embedded System Spring, 2011 Lecture 9: I/O Programming Eng. Wazen M. Shbair](https://reader035.fdocuments.in/reader035/viewer/2022062316/56814cb9550346895db9c4c2/html5/thumbnails/9.jpg)
9
Port A
PORTA is a 7-bit wide, bidirectional port. The corresponding Data Direction register
is TRISA. Setting a TRISA bit (= 1) will make the
corresponding PORTA pin an input Clearing a TRISA bit (= 0) will make the
corresponding PORTA pin an output On a Power-on Reset, these pins are
configured as inputs and read as ‘0’
![Page 10: Embedded System Spring, 2011 Lecture 9: I/O Programming Eng. Wazen M. Shbair](https://reader035.fdocuments.in/reader035/viewer/2022062316/56814cb9550346895db9c4c2/html5/thumbnails/10.jpg)
10
Example: Port A as Output
![Page 11: Embedded System Spring, 2011 Lecture 9: I/O Programming Eng. Wazen M. Shbair](https://reader035.fdocuments.in/reader035/viewer/2022062316/56814cb9550346895db9c4c2/html5/thumbnails/11.jpg)
11
Example: Port A as Input
![Page 12: Embedded System Spring, 2011 Lecture 9: I/O Programming Eng. Wazen M. Shbair](https://reader035.fdocuments.in/reader035/viewer/2022062316/56814cb9550346895db9c4c2/html5/thumbnails/12.jpg)
12
PORT B, PORT C, PORT D and PORT E
PORTB is 8 pins PORTC is 8 pins PORTD is 8 pins PORTE is 3 pins
![Page 13: Embedded System Spring, 2011 Lecture 9: I/O Programming Eng. Wazen M. Shbair](https://reader035.fdocuments.in/reader035/viewer/2022062316/56814cb9550346895db9c4c2/html5/thumbnails/13.jpg)
13
Read followed by write operation
Be carful Don’t 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
![Page 14: Embedded System Spring, 2011 Lecture 9: I/O Programming Eng. Wazen M. Shbair](https://reader035.fdocuments.in/reader035/viewer/2022062316/56814cb9550346895db9c4c2/html5/thumbnails/14.jpg)
14
![Page 15: Embedded System Spring, 2011 Lecture 9: I/O Programming Eng. Wazen M. Shbair](https://reader035.fdocuments.in/reader035/viewer/2022062316/56814cb9550346895db9c4c2/html5/thumbnails/15.jpg)
15
I/O Bit Manipulation Programming
I/O ports and bit-addressability Monitoring a single bit Reading a single bit
![Page 16: Embedded System Spring, 2011 Lecture 9: I/O Programming Eng. Wazen M. Shbair](https://reader035.fdocuments.in/reader035/viewer/2022062316/56814cb9550346895db9c4c2/html5/thumbnails/16.jpg)
16
I/O ports and bit addressability
![Page 17: Embedded System Spring, 2011 Lecture 9: I/O Programming Eng. Wazen M. Shbair](https://reader035.fdocuments.in/reader035/viewer/2022062316/56814cb9550346895db9c4c2/html5/thumbnails/17.jpg)
17
Bit Oriented Instruction for PIC18
![Page 18: Embedded System Spring, 2011 Lecture 9: I/O Programming Eng. Wazen M. Shbair](https://reader035.fdocuments.in/reader035/viewer/2022062316/56814cb9550346895db9c4c2/html5/thumbnails/18.jpg)
18
BSF (bit set fileReg)
Used to set HIGH for a single bit in fileReg The Syntax :
BSF fileReg, bit_num
![Page 19: Embedded System Spring, 2011 Lecture 9: I/O Programming Eng. Wazen M. Shbair](https://reader035.fdocuments.in/reader035/viewer/2022062316/56814cb9550346895db9c4c2/html5/thumbnails/19.jpg)
19
![Page 20: Embedded System Spring, 2011 Lecture 9: I/O Programming Eng. Wazen M. Shbair](https://reader035.fdocuments.in/reader035/viewer/2022062316/56814cb9550346895db9c4c2/html5/thumbnails/20.jpg)
20
BCF(bit clear fileReg)
Used to clear a sigle bit of given fileReg The Syntax:
BCF fileReg, bit_number
![Page 21: Embedded System Spring, 2011 Lecture 9: I/O Programming Eng. Wazen M. Shbair](https://reader035.fdocuments.in/reader035/viewer/2022062316/56814cb9550346895db9c4c2/html5/thumbnails/21.jpg)
21
![Page 22: Embedded System Spring, 2011 Lecture 9: I/O Programming Eng. Wazen M. Shbair](https://reader035.fdocuments.in/reader035/viewer/2022062316/56814cb9550346895db9c4c2/html5/thumbnails/22.jpg)
22
Checking an input pin
To make decisions base on the status of a given 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 to monitor a single bit and make decision whether it is 0 or 1.
![Page 23: Embedded System Spring, 2011 Lecture 9: I/O Programming Eng. Wazen M. Shbair](https://reader035.fdocuments.in/reader035/viewer/2022062316/56814cb9550346895db9c4c2/html5/thumbnails/23.jpg)
23
![Page 24: Embedded System Spring, 2011 Lecture 9: I/O Programming Eng. Wazen M. Shbair](https://reader035.fdocuments.in/reader035/viewer/2022062316/56814cb9550346895db9c4c2/html5/thumbnails/24.jpg)
24
Reading a single bit
We can use bit test instruction to read the status of the single bit and send it to another bit of save it
![Page 25: Embedded System Spring, 2011 Lecture 9: I/O Programming Eng. Wazen M. Shbair](https://reader035.fdocuments.in/reader035/viewer/2022062316/56814cb9550346895db9c4c2/html5/thumbnails/25.jpg)
25
![Page 26: Embedded System Spring, 2011 Lecture 9: I/O Programming Eng. Wazen M. Shbair](https://reader035.fdocuments.in/reader035/viewer/2022062316/56814cb9550346895db9c4c2/html5/thumbnails/26.jpg)
26
Reading input pins VS. LATx port
There are two possibilities to read port’s value Through reading the status of the input pin Through reading the internal latch of the LAT register. Some instructions do that
The action is The instruction read the latch instead of the pin Execute the instruction Write back the result to the Latch The data on the pins are changed only if the TRISx The PIC uCs bits are cleared.
![Page 27: Embedded System Spring, 2011 Lecture 9: I/O Programming Eng. Wazen M. Shbair](https://reader035.fdocuments.in/reader035/viewer/2022062316/56814cb9550346895db9c4c2/html5/thumbnails/27.jpg)
27
Reading input pins VS. LATx port
![Page 28: Embedded System Spring, 2011 Lecture 9: I/O Programming Eng. Wazen M. Shbair](https://reader035.fdocuments.in/reader035/viewer/2022062316/56814cb9550346895db9c4c2/html5/thumbnails/28.jpg)
28IUG- Embedded System 28
References
Jie Hu , ECE692 Embedded Computing Systems , Fall 2010.
PIC Microcontroller And Embedded Systems: using Assembly and C for PIC 18, M. Mazidi, R. McKinlay and D. Causey, Prentice Fall, 2008.
Eng. Husam Alzaq, Embedded System Course, IUG, 2010