0% found this document useful (0 votes)
6 views61 pages

Lecture-5-PIC IO

Uploaded by

Sheri Gaming
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views61 pages

Lecture-5-PIC IO

Uploaded by

Sheri Gaming
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 61

The University of Texas at Arlington

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

• Only two states 5V


– ON / OFF
0V
– HIGH / LOW
–1/0
– 5V / 0V
Output
– 3.3V / 0V
– etc.

Input

2
Chapter 4 – PIC I/O PORT
PROGRAMMING

• Ports are not only used for simple I/O, but


also can be used other functions
– ADC (analog-to-digital conversion)
– Timers
– Oscillator Input
– Interrupts
– Serial communication
– Capturing and Generating PWM Signals
– Programming the PIC
3
PIC18F452 Pin Diagram

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

• For example, the PIC18F452


– Port A has 7 pins
– Ports B, C, and D each have 8 pins
– Port E has only 3 pins
34 total digital IO pins
• Each port has three SFRs associated
– PORTx
– TRISx (TRIState)
– LATx (LATch) 8
SFRs in the File Registers

9
SFRs in the File Registers

RAM
FileReg

SFRs

10
SFRs in the File Registers

11
TRISx SFR

• Each of the Ports A-E in the PIC18F452 can


be used for input or output
– TRISx is used solely for the purpose of making a
given port an input or output port

• TRISx bit = 0  PORTx bit is an OUTPUT


– Can now write to the PORTx bit(s)

• TRISx bit = 1  PORTx bit is an INPUT


– Can now read in from the PORTx bit(s)

– Can set I/O bit-by-bit or whole TRIS byte at once12


PORTx and LATx SFRs

• 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

PORTB EQU 0XF81 ;in .H header file


TRISB EQU 0XF93 ;in .H header file

ORG 0x00

MOVLW 0 ;All 0’s to WREG


MOVWF TRISB ;All of PORTB is an OUTPUT

MOVLW B’10101010’
24
MOVWF PORTB ;Write 1/0 to PORTB pins
PORTB Example

Direction Pin Value


(TRISB) (PORTB)
? ?
? ?
? ?
? ?
? ?
? ?
? ?
? ?

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

In order to make all the bits of PORTB an input, TRISB


must be programmed by writing 1 to all the bits. In the
code below, PORTB is configured first as an input port
by writing all 1s to register TRISB, and then data is
received from PORTB and saved in some RAM location
of the file registers:

MYREG EQU 0X20 ;Program location (RAM)

MOVLW B‘11111111’ ;All 1’s to WREG


MOVWF TRISB ;PORTB as INPUT port (1 for In)

MOVF PORTB, W ;move from filereg of PORTB to WREG


MOVWF MYREG ;save in fileReg of MYREG
33
Accessing SFRs in .ASM
PORTB as an INPUT
ORG 0x00
MYREG = ?
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

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

• Bit set flag


– BSF filereg, bit BSF TRISB, 4
• Bit clear flag
– BCF filereg, bit BCF PORTB, 2
• Bit toggle flag
– BTF filereg, bit
• Bit test filereg skip next instruction if clear (0)
– BTFSC filereg, bit
• Bit test filereg skip next instruction if set (1)
– BTFSS filereg, bit

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

if(mybyte < 100)


PORTB = mybyte; //send it to PORTB is it is less than 100
else
PORTD = mybyte; //otherwise, send to PORTD
} 42
}
Working with I/O Ports in C
Single BITS 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
TRISBbits.RB4 = 1;
TRISD = 0b00000000; //PORTD is output

while(1)
{
mybyte = PORTC; //load the value of PORTC

if(mybyte < 100)


PORTB = mybyte; //send it to PORTB is it is less than 100
else
PORTD = mybyte; //otherwise, send to PORTD

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

Driving LCD Controllers


(textbook chapter 12,
PICBook chapter 7)

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)

Elon Musk has lost


his mind

Image Source: http://www.gadgetronicx.com/lcd-interface-with-atmega32-avr/ 50


LCD Controller

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

• All commands (RS=0) where the MSB is


set are cursor positioning commands
• Row 1 begins with 0x80 (1000 0000)
• Row 2 begins with 0XC0 (1100 0000)
• Positions are counted left to right and auto
increment can be enabled (no need for
cursor positioning for short strings)

Hitachi HD44780 LCD controllers are by far the most used 58


Special Characters

• ASCII lower 128 characters are easy to display


(just send ASCII codes) with a few exceptions
• Japanese characters at codes 0xa0 to 0xff
• Eight user defined characters 0x0 to 0x7
• All command codes (RS=0) with MSBs ’01’ are
character generating commands
• 5x8 characters are then defined by sending their
bitmaps (sending 8 bytes where upper three bits
are always ignored)
Hitachi HD44780 LCD controllers are by far the most used 59
Debugging

• LCDs (as they are displays) are a great


tool for debugging embedded code
• Of course we need to assume that the
microcontroller works
• Displaying variables and port statuses can
be very helpful

Hitachi HD44780 LCD controllers are by far the most used 60


Questions?

• Textbook Ch. 4.1 and 4.2 for PIC IO examples


and more details
• Textbook Ch. 12.1 for LCD details
• LCD Videos
– https://www.youtube.com/watch?v=mo4_5vG8bbU
– https://www.youtube.com/watch?v=ZP0KxZl5N2o
– https://www.youtube.com/watch?v=85LvW1QDLLw

• Start reading Chapter 7


– PIC Programming in C
61

You might also like