Lecture-5-PIC IO
Lecture-5-PIC IO
Lecture 5
PIC I/O and LCD Control
CSE 3442/5442
Embedded Systems I
Based heavily on slides by Dr. Gergely Záruba and Dr. Roger Walker
Digital Input/Output
Input
2
Chapter 4 – PIC I/O PORT
PROGRAMMING
4
Pin 5: RA3/AN3/Vref+
5
PIC18F452 (40 Pins) has 5 ports,
other Family Members Can Have
More or Less
6
PIC18F452 Pin Diagram
5 Ports
PORTA PORTB
PORTE
PORTD
PORTC
7
Number of Individual Port Pins
9
SFRs in the File Registers
RAM
FileReg
SFRs
10
SFRs in the File Registers
11
TRISx SFR
• PORTx
– For reading input coming into the PIC
• Digital High (1) or Low (0)
– For writing output from the PIC
• Writing a 1 pin is High, 0 pin is Low
• LATx
– For writing output from the PIC
• Writing a 1 pin is High, 0 pin is Low
• Point of the Latch??
13
PORTB Example
14
N and P Transistors
(MOSFET Logic)
5V 5V
5V 0V
0V 0V
15
N and P Transistors
(MOSFET Logic)
5V 5V
5V 0V 0V 5V
0V 0V
16
N and P Transistors
(MOSFET Logic)
5V 5V
5V 0V 0V 5V
0V 0V
17
Outputting a 0
18
MikroElektronika (img source)
http://learn.mikroe.com/ebooks/picbasicprogramming/chapter/input-output-ports/
19
MikroElektronika (img source)
http://learn.mikroe.com/ebooks/picbasicprogramming/chapter/input-output-ports/
20
PIC18F452 Pin Diagram
5 Ports
PORTA PORTB
PORTE
PORTD
PORTC
21
PORT/TRIS Functionality is
Mapped to the SFRs
RAM
FileReg
SFRs
22
Addresses of SFR, PORTx, TRISx
(TRIState), and LATx (LATch)
23
Accessing SFRs in .ASM
PORTB as an OUTPUT
ORG 0x00
MOVLW B’10101010’
24
MOVWF PORTB ;Write 1/0 to PORTB pins
PORTB Example
25
Accessing SFRs in .ASM
PORTB as an OUTPUT
PORTB EQU 0XF81 ;in .H header file
TRISB EQU 0XF93 ;in .H header file
WREG = ?
ORG 0x00 TRISB = ?
PORTB = ?
MOVLW 0 ;All 0’s to WREG
MOVWF TRISB ;PORTB is an OUTPUT Direction Pin Value
(TRISB) (PORTB)
MOVLW B’10101010’ ? ?
MOVWF PORTB ;Write 1/0 to PORTB pins ? ?
? ?
? ?
? ?
? ?
? ?
? ?
26
Accessing SFRs in .ASM
PORTB as an OUTPUT
PORTB EQU 0XF81 ;in .H header file
TRISB EQU 0XF93 ;in .H header file
WREG = 0000 0000
ORG 0x00 TRISB = ?
PORTB = ?
MOVLW 0 ;All 0’s to WREG
MOVWF TRISB ;PORTB is an OUTPUT Direction Pin Value
(TRISB) (PORTB)
MOVLW B’10101010’ ? ?
MOVWF PORTB ;Write 1/0 to PORTB pins ? ?
? ?
? ?
? ?
? ?
? ?
? ?
27
Accessing SFRs in .ASM
PORTB as an OUTPUT
PORTB EQU 0XF81 ;in .H header file
TRISB EQU 0XF93 ;in .H header file
WREG = 0000 0000
ORG 0x00 TRISB = 0000 0000
PORTB = ?
MOVLW 0 ;All 0’s to WREG
MOVWF TRISB ;PORTB is an OUTPUT Direction Pin Value
(TRISB) (PORTB)
MOVLW B’10101010’ 0 ?
MOVWF PORTB ;Write 1/0 to PORTB pins 0 ?
0 ?
0 ?
0 ?
0 ?
0 ?
0 ?
28
Accessing SFRs in .ASM
PORTB as an OUTPUT
PORTB EQU 0XF81 ;in .H header file
TRISB EQU 0XF93 ;in .H header file
WREG = 1010 1010
ORG 0x00 TRISB = 0000 0000
PORTB = ?
MOVLW 0 ;All 0’s to WREG
MOVWF TRISB ;PORTB is an OUTPUT Direction Pin Value
(TRISB) (PORTB)
MOVLW B’10101010’ 0 ?
MOVWF PORTB ;Write 1/0 to PORTB pins 0 ?
0 ?
0 ?
0 ?
0 ?
0 ?
0 ?
29
Accessing SFRs in .ASM
PORTB as an OUTPUT
PORTB EQU 0XF81 ;in .H header file
TRISB EQU 0XF93 ;in .H header file
WREG = 1010 1010
ORG 0x00 TRISB = 0000 0000
PORTB = 1010 1010
MOVLW 0 ;All 0’s to WREG
MOVWF TRISB ;PORTB is an OUTPUT Direction Pin Value
(TRISB) (PORTB)
MOVLW B’10101010’ 0 1
MOVWF PORTB ;Write 1/0 to PORTB pins 0 0
0 1
0 0
0 1
0 0
0 1
0 0
30
Accessing SFRs in .ASM
PORTB as an OUTPUT
PORTB EQU 0XF81 ;in .H header file
TRISB EQU 0XF93 ;in .H header file
WREG = 1010 1010
ORG 0x00 TRISB = 0000 0000
PORTB = 1010 1010
MOVLW 0 ;All 0’s to WREG
MOVWF TRISB ;PORTB is an OUTPUT Direction Pin Value
(TRISB) (PORTB)
MOVLW B’10101010’ 0 1
MOVWF PORTB ;Write 1/0 to PORTB pins 0 0
0 1
0 0
0 1
0 0
0 1
0 0
31
Accessing SFRs in .ASM
PORTB as an OUTPUT
PORTB EQU 0XF81 ;in .H header file
TRISB EQU 0XF93 ;in .H header file
WREG = 1010 1010
ORG 0x00 TRISB = 0000 0000
PORTB = 1010 1010
MOVLW 0 ;All 0’s to WREG
MOVWF TRISB ;PORTB is an OUTPUT Direction Pin Value
(TRISB) (PORTB)
MOVLW B’10101010’ 0 1
MOVWF PORTB ;Write 1/0 to PORTB pins 0 0
0 1
0 0
0 1
0 0
0 1
0 0
32
PORTB as an INPUT
34
Accessing SFRs in .ASM
PORTB as an INPUT
ORG 0x00
MYREG = 0000 0000 (at 0x20)
MYREG EQU 0X20 ;Program location (RAM) WREG = ?
TRISB = ?
MOVLW B‘11111111’ ;All 1’s to WREG PORTB = ?
MOVWF TRISB ;PORTB as INPUT port
Direction Pin Value
MOVF PORTB, W ;move from filereg of (TRISB) (PORTB)
PORTB to WREG 5V
? ? 5V
MOVWF MYREG ;save in fileReg of
MYREG ? ? 5V
? ? 5V
? ?
? ? 0V
? ? 0V
? ? 0V
? ? 0V
35
Accessing SFRs in .ASM
PORTB as an INPUT
ORG 0x00
MYREG = 0000 0000 (at 0x20)
MYREG EQU 0X20 ;Program location (RAM) WREG = 1111 1111
TRISB = ?
MOVLW B‘11111111’ ;All 1’s to WREG PORTB = ?
MOVWF TRISB ;PORTB as INPUT port
Direction Pin Value
MOVF PORTB, W ;move from filereg of (TRISB) (PORTB)
PORTB to WREG 5V
? ? 5V
MOVWF MYREG ;save in fileReg of
MYREG ? ? 5V
? ? 5V
? ?
? ? 0V
? ? 0V
? ? 0V
? ? 0V
36
Accessing SFRs in .ASM
PORTB as an INPUT
ORG 0x00
MYREG = 0000 0000 (at 0x20)
MYREG EQU 0X20 ;Program location (RAM) WREG = 1111 1111
TRISB = 1111 1111
MOVLW B‘11111111’ ;All 1’s to WREG PORTB = 1111 0000
MOVWF TRISB ;PORTB as INPUT port
Direction Pin Value
MOVF PORTB, W ;move from filereg of (TRISB) (PORTB)
PORTB to WREG 5V
1 1 5V
MOVWF MYREG ;save in fileReg of
MYREG 1 1 5V
1 1 5V
1 1
1 0 0V
1 0 0V
1 0 0V
1 0 0V
37
Accessing SFRs in .ASM
PORTB as an INPUT
ORG 0x00
MYREG = 0000 0000 (at 0x20)
MYREG EQU 0X20 ;Program location (RAM) WREG = 1111 0000
TRISB = 1111 1111
MOVLW B‘11111111’ ;All 1’s to WREG PORTB = 1111 0000
MOVWF TRISB ;PORTB as INPUT port
Direction Pin Value
MOVF PORTB, W ;move from filereg of (TRISB) (PORTB)
PORTB to WREG 5V
1 1 5V
MOVWF MYREG ;save in fileReg of
MYREG 1 1 5V
1 1 5V
1 1
1 0 0V
1 0 0V
1 0 0V
1 0 0V
38
Accessing SFRs in .ASM
PORTB as an INPUT
ORG 0x00
MYREG = 1111 0000 (at 0x20)
MYREG EQU 0X20 ;Program location (RAM) WREG = 1111 0000
TRISB = 1111 1111
MOVLW B‘11111111’ ;All 1’s to WREG PORTB = 1111 0000
MOVWF TRISB ;PORTB as INPUT port
Direction Pin Value
MOVF PORTB, W ;move from filereg of (TRISB) (PORTB)
PORTB to WREG 5V
1 1 5V
MOVWF MYREG ;save in fileReg of
MYREG 1 1 5V
1 1 5V
1 1
1 0 0V
1 0 0V
1 0 0V
1 0 0V
39
Register bit manipulation
40
MPLAB Example
• http://omega.uta.edu/~nbb0130/misc_files/
Main5_1.asm
41
Working with I/O Ports in C
Whole BYTES at a Time
#include <xc.h> //OLD #include <p18F452.h>
void main(void)
{
unsigned char mybyte;
TRISC = 0b11111111; //PORTC is input
TRISB = 0b00000000; //PORTB is output
TRISD = 0b00000000; //PORTD is output
while(1)
{
mybyte = PORTC; //load the value of PORTC
void main(void)
{
unsigned char mybyte;
TRISC = 0b11111111; //PORTC is input
TRISB = 0b00000000; //PORTB is output
TRISBbits.RB4 = 1;
TRISD = 0b00000000; //PORTD is output
while(1)
{
mybyte = PORTC; //load the value of PORTC
mybyte = PORTCbits.RC1;
43
}
}
Fan-out
• Current can flow in (pin at 0 level) and out (pin at 1 level)
of port pins.
• This current is limited by the design of the IC.
• Fan-out is really the number of logic gates a pin can
drive but is closely connected to the total current of pins.
• Arguably, for microcontrollers it is more important to
remember the total current drawn (see LEDs driven in
QwikFlash)
44
Example of Interfacing PIC to
Components on QwikFlash
Potentiometer
LED’s
LCD
45
Push Button Switch
Example of Interfacing PIC to
Components on QwikFlash
46
Example: Parallel Digital Output
LCD Control
47
LCDs
• Liquid Crystal Displays are frequently used with
microcontrollers and embedded devices
• Usually have their own controller for logic and
receiving commands
• Commonly have parallel digital inputs for interfacing
– Some have serial interfaces instead (SPI, I2C, etc.)
• LCD modules usually require an initialization
sequence when powered up before regular
commands can be sent
– So some small wait time should be expected (ms range)
48
Different LCD Types
49
Sample Image (not a PIC)
51
LCD Controller
52
LCD Common Pins
• Supply, “ground”, and LCD contrast voltage
• Register Select (RS)
– RS=0 for sending instructions (such as clear screen, or defining
characters)
– RS=1 for sending data to be displayed
• Enable (E)
– Essentially a clock input; a high-low transition will cause the LCD
to latch in the data on the data pins
• Data (D0-D7) or (D0-D3)
– The parallel interface pins (can use all 8 or just 4)
• Read/Write (R/W)
– Direction of I/O (if used only as a display, “grounding” this is
necessary)
Hitachi HD44780 LCD controllers are by far the most used 53
Connections to QwikFlash
LCD Module –
Hitachi HD44780 54
onboard controller
55
Typical LCD Timing for
Displaying (Write)
56
Example Initialization (Nibble
Interface)
1. Wait 100ms to make sure own initialization has
occurred
2. RS=0, (all commands)
3. 3 times: E=1,D=3,E=0, wait
4. 2 times: E=1,D=2,E=0, wait (set nibble iface)
5. E=1,D=8,E=0, wait, E=1,D=0,E=0 (two line display)
6. E=1,D=1,E=0, wait, E=1,D=0,E=0 (clear display)
7. E=1,D=0xC,E=0, wait, E=1,D=0,E=0 (Turn off cursor,
turn on display)
8. E=1,D=1,E=0, wait (auto cursor increment)
57
Cursor Positioning