Professional Documents
Culture Documents
ESD Ch7 - 2016 p1
ESD Ch7 - 2016 p1
1. Digital parallel input / output
• Commonly used input peripherals
– DIP switches, push‐button keys, keyboards, and A/D converters
• DIP switch
– One side of the switch is tied high
• To a power supply through a resistor called a pull‐up resistor
– The other side is grounded
– The logic level changes when the position is switched
• Push‐button key
– Same as the DIP switch except that contact is momentary
1
8/9/2016
SWITCH ON I/O PORTS
Switch Input
2
8/9/2016
Digital input for PIC
Switch hardware debouncing
Switch hardware debounce
(a) without debounce capacitor
(b) with debounce capacitor
Ex: With a 10k pull‐up resistor, a 10 nF
capacitor would give a time constant of 100 ms,
which should be more than adequate
Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 7 - p1 66
3
8/9/2016
Software Debouncing
• Debouncing the key
– Software technique
• Wait for 20 ms.
• Read the port pin again.
• If the reading is still 0 (for eg..) , it
indicates that a key is pressed.
Interfacing Dip Switches
4
8/9/2016
Test Circuit with Input Switch
Tests an input with IF Statement
// IFIN.C Tests an input
#include " 16F877A.h "
void main()
{
int x; // Declare variable
output_D(0); // Clear all outputs
while(1) // Loop always
{
x = input(PIN_C0); // Get input state
if(x = = 1)output_high(PIN_D0); // Change output
}
}
5
8/9/2016
Tests an input with conditional loop
// WHILOOP.C Input switch controls output flashing
#include " 16F877A.h "
#use delay (clock = 1000000) // MCU clock = 1 MHz
void main()
{
while(1)
{
while(input(PIN_C0)) // Repeat while switch open
{
output_high(PIN_D0);
delay_ms(300); // Delay 0.3s
output_low(PIN_D0);
delay_ms(500); // Delay 0.5s
}
output_low(PIN_D0); // Switch off LED
}
}
Tests an input with FOR loop
// FORLOOP.C Repeat loop a set number of times
#include " 16F877A.h "
#use delay (clock = 1000000)
void main() {
int x;
while(input(PIN_C0)) { } ; // Wait until switch closed
for (x = 0; x < 5; x ++) // For loop conditions
{
output_high(PIN_D0); // Flash sequence
delay_ms(500);
output_low(PIN_D0);
delay_ms(500);
}
while(1); // Wait for reset
}
Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 7 - p1 12
6
8/9/2016
Matrix Keypads
• Matrix keypads consist of keys interconnected in the shape of a matrix.
Each key is a simple mechanical switch located at the crossing between the
matrix rows and columns.
The procedure to service matrix keypads
7
8/9/2016
Explore the matrix keypad
• Sequential exploration of rows. This method sets the first row to 0 and
reads all the columns. If none of the columns is read at 0 it means that the
pressed key is not in that row. Then the next row is set to 0, and the
columns are read again. This process is repeated until a 0 is found in a
column. This determines the row and column for the pressed key, thus
giving the exploration code for that key.
• Simultaneous exploration of rows and columns. This method sets all the
rows to 0 and reads all the columns. This detects the column that contains
the pressed key but not its row. Then the process is inverted: all the
columns are set to 0 and the rows are read. This detects the row that
contains the pressed key. This gives the row and column for the pressed
key, and thus the exploration code for the key.
8
8/9/2016
16‐key matrix keypad connected to port B
Fig.13 A 16-key matrix keypad connected to port B in a PIC. Diodes are used to
avoid short circuits between two exploration lines if two or more keys in the same
column are pressed simultaneously. This configuration uses the internal pull-up
(not shown) available in port B to keep a logic 1 in the return lines when no keys
are pressed.
Digital Output for PIC
9
8/9/2016
LED ON I/O PORTS
LED Interface
(Current Sink
Mode)
Connecting an LED in
Current Source Mode
(Current
Source Mode)
10
8/9/2016
Digital Output for PIC
Higher Current Load Interface
Digital Output for PIC
Relay Interface
11
8/9/2016
Using ULN (Driver IC)
•The ULN2803 can provides much higher drive-current.
•The ULN2803 also has internal diode protection that eliminates the need for
the fly-back diode
•ULN is better choice for simple design of circuit & PCB
Interfacing Seven‐Segment LEDs as an Output (1/3)
• Seven‐segment LEDs
– Often used to display BCD numbers (1 through 9) and a few alphabets
– A group of eight LEDs physically mounted in the shape of the number
eight plus a decimal point)
– Each LED is called a segment and labeled as ‘a’ through ‘g’.
decimal point
12
8/9/2016
Interfacing Seven‐Segment LEDs
• In a common anode seven‐
segment LED
– All anodes are connected
together to a power supply
and cathodes are
connected to data lines
• Logic 0 turns on a segment.
• Example: To display digit 1, all
segments except b and c
should be off.
• Byte 11111001 = F9H will
display digit 1.
Interfacing Seven‐Segment LEDs
• In a common cathode seven‐
segment LED
– All cathodes are connected
together to ground and the
anodes are connected to
data lines
• Logic 1 turns on a segment.
• Example: To display digit 1, all
segments except b and c
should be off.
• Byte 00000110 = 06H will
display digit 1.
13
8/9/2016
Example : Interfacing Seven‐Segment LEDS to
PORTB and PORTC
Interfacing to Multiple 7‐Segments (1/2)
Chapter 7 - p1 28
14
8/9/2016
Interfacing to Multiple 7‐Segments (2/2)
Keypad Connection to the Microcontroller
15
8/9/2016
Class Assignment
2. Interface LCD
• LCDs (liquid‐crystal displays) are alphanumeric displays
which are frequently used in microcontroller‐based
applications.
– Parallel LCDs are connected to the microcontroller I/O ports
using 4 or 8 data wires and data is transferred from the
microcontroller to the LCD in parallel form.
– Serial LCDs are connected to the microcontroller using only
one data line and data is transferred to the LCD using the
standard RS232 asynchronous data communication protocols.
Serial LCDs are easier to use but they usually cost more than
the parallel ones.
16
8/9/2016
Parallel LCDs
• Following figure shows a typical parallel LCD. The programming of a
parallel LCD is usually a complex task and requires a good understanding
of the internal operation of the LCDs,including the timing requirements.
A Typical Parallel LCD
• Fortunately, the CCS C language provides special commands for
displaying data on HD44780 or compatible LCDs
• All the user has to do is connect the LCD to the appropriate I/O ports
and then use these special commands to simply send data to the LCD.
Alphanumeric LCD Interfacing
• Pinout Microcontroller
– 8 data pins D7:D0
– RS: Data or Command
E communications
R/W bus
Register Select RS
DB7–DB0
– R/W: Read or Write 8
– E: Enable (Latch data)
LCD
controller
– RS = 0 Command Register
– RS = 1 Data Register
• R/W = 0 Write, R/W = 1 Read
• E – Enable
– Used to latch the data present on the data pins.
• D0 – D7
– Bi‐directional data/command pins.
– Alphanumeric characters are sent in ASCII format.
Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 7 - p1 34
17
8/9/2016
4‐bit mode LCD Connections to a PIC
Microcontroller
16F84 to LCD 8‐bit Mode Circuit
18
8/9/2016
LCD Commands
• The LCD’s internal controller can accept several
commands and modify the display accordingly. These
commands would be things like:
– Clear screen
– Return home
– Decrement/Increment cursor
• After writing to the LCD, it takes some time for it to
complete its internal operations. During this time, it
will not accept any new commands or data.
– We need to insert time delay between any two commands
or data sent to LCD
19
8/9/2016
LCD ‐ Cursor Addresses for Some LCDs
Hitachi 44780 Based LCD Types and Character Locations
20
8/9/2016
LCD Timing
21
8/9/2016
Interfacing Text LCD
• Problem statement
– Interface a 2‐line x 20 character LCD module with the built‐in
HD44780 controller to I/O ports of the PIC18 microcontroller
• Multi‐LCDs refer to LCDs with different interfaces
22
8/9/2016
Converting to ASCII
• The LCD can represent characters in ASCII
• For example number 0x08 must be converted to 0x38
• To perform this:
– If W=0x08 then ASCII=XORLW 0x30W=38
Interfacing LCD
• Hardware
– 20 x 2‐line LCD displays (two lines
with 20 characters per line)
– LCD has a display Data RAM
(registers) that stores data in 8‐bit
character code.
– Each register in Data RAM has its
own address that corresponds to
its position on the line. PICDEMO
• The address range for Line 1 is 00
to 13H and Line 2 is 40H to 53H.
23
8/9/2016
Interfacing LCD
• Driver HD77480
– Three control signals:
• RS – Register Select (RA3)
• R/W – Read/Write (RA2)
• E – Enable (RA1)
– Three power connections
• Power, ground, and the
variable register to
control the brightness
Interfacing LCD
• Can be interfaced either in the 8‐bit mode or the 4‐bit
mode
– In the 8‐bit mode, all eight data lines are connected for data
transfer
– In the 4‐bit mode, only four data lines (DB7‐DB4 or DB3‐DB0)
are connected and two transfers per character (or instruction)
are needed
• Driver (HD77480) has two 8‐bit internal registers
– Instruction Register (IR) to write instructions to set up LCD
– Data Register (DR) to write data (ASCII characters)
IR REGISTER
DR REGISTER
24
8/9/2016
Interfacing LCD
• LCD Operation
– When the MPU writes an instruction to IR or data to DR, the
controller:
• Sets the data line DB7 high as a flag indicating that the controller is
busy completing the operation
• Sets the data line DB7 low after the completion of the operation
– The MPU should always check whether DB7 is low before
sending an instruction or a data byte
– After the power up, DB7 cannot be checked for the first two
initialization instructions.
Interfacing LCD
• Writing to or reading from LCD
• The MPU:
• Asserts RS low to select IR
• Reads from LCD by asserting the R/W signal high
• Asserts the E signal high and then low (toggles) to latch a data byte or an
instruction
• Asserts RS high to select DR
• Writes into LCD by asserting the R/W signal low
• Asserts the E signal high and then low (toggles) to latch a data byte or an
instruction
LCD data
write waveform
25
8/9/2016
HD44780 Bus Timing
Interfacing LCD (Write)
• Software
– To write into the LCD, the program should:
• Send the initial instructions (commands) before it can
check DB7 to set up the LCD in the 4‐bit or the 8‐bit
mode.
• Check DB7 and continue to check until it goes low.
• Write instructions to IR to set up the LCD parameters
such as the number of display lines and cursor status.
• Write data to display a message.
26
8/9/2016
Resetting LCD
• In 4‐bit mode the data is sent in nibbles
– First we send the higher nibble and then the lower nibble.
• To enable the 4‐bit mode of LCD, we need to follow special
sequence of initialization that tells the LCD controller that user
has selected 4‐bit mode of operation:
– Wait for about 20mS
– Send the first init value (0x30)
– Wait for about 10mS
– Send second init value (0x30)
– Wait for about 1mS
– Send third init value (0x30)
– Wait for 1mS
– Select bus width (0x30 ‐ for 8‐bit and 0x20 for 4‐bit)
– Wait for 1mS
Display Data RAM (DDRAM)
• Display data RAM (DDRAM) stores display data represented in 8‐bit character
codes. Its extended capacity is 80 x 8 bits, or 80 characters. The area in display data
RAM (DDRAM) that is not used for display can be used as general data RAM. See
Figure 1 for the relationships between DDRAM addresses and positions on the
liquid crystal display.
1-Line Display
27
8/9/2016
LCD – 8‐Bit Interface
Chapter 7 - p1 55
8‐bit LCD Interface (1/2)
// CCS C
28
8/9/2016
8‐bit LCD Interface (2/2)
lcd_write(lcd_Command_mode, 0x38);
lcd_write(lcd_Command_mode, 0x08);
lcd_write(lcd_Command_mode, 0x01);
// Display Clear
lcd_write(lcd_Command_mode, 0x06);
Ex: 8‐bit LCD interface ‐ HW
29
8/9/2016
Ex: 8‐bit LCD interface ‐ SW (1/3)
#include <16F877.h>
#use delay(clock=20000000)
#use fast_io (C)
#use fast_io (D)
#define lcd_RS pin_C1
#define lcd_RW pin_C0
#define lcd_EN pin_C2
#define lcd_Command_mode 0
#define lcd_Data_mode 1
void lcd_write(byte mode, byte data)
{ // 8‐bit data mode
output_d(data);
output_bit(lcd_RS, mode); // mode = 0 ‐‐> COMMAND; 1 ‐‐> DATA
output_bit(lcd_RW,0) ; // write to register
output_bit(lcd_EN,1); // Enable LCD.
delay_ms(1);
output_bit(lcd_EN, 0); // Disable LCD.
delay_ms(1);
}
Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 7 - p1 59
Ex: 8‐bit LCD interface ‐ SW (2/3)
void lcd_init() {
delay_ms(40);
lcd_write(lcd_Command_mode, 0x30); // Set as 8 bit data mode
delay_ms(5); // Delay 5 ms
lcd_write(lcd_Command_mode, 0x30); // Set as 8 bit data mode
delay_us(200); // Delay 200 us
lcd_write(lcd_Command_mode, 0x30); // Set as 8 bit data mode
lcd_write(lcd_Command_mode, 0x38);
lcd_write(lcd_Command_mode, 0x08);
lcd_write(lcd_Command_mode, 0x01); // Display Clear
lcd_write(lcd_Command_mode, 0x06);
}
void lcd_write_string(byte position, char *ptr, int delay_time)
{ lcd_write(lcd_Command_mode, 0x80 | position );
while ( *ptr != 0)
{
lcd_write(lcd_Data_mode, *ptr++);
delay_ms(delay_time);
}
}
Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 7 - p1 60
30
8/9/2016
Ex: 8‐bit LCD interface ‐ SW (3/3)
void main() { // 4‐bit mode LCD interface ( 2 x 16)
char text1[]="Hello the world!";
char text2[]="Number: ";
char text3[]="0123456789";
char *ptr;
set_tris_c(0x00); // Port C : output
set_tris_d(0x00); // Port D : output
lcd_init();
lcd_write(lcd_Command_mode, 0x0E);
lcd_write_string(0x00, text1, 20); // First line
lcd_write_string(0x40, text2, 20); // Second line
ptr = text3;
while (1)
{ if (*ptr == 0)
ptr = text3;
lcd_write(lcd_Command_mode, 0x80 | 0x48 );
lcd_write(lcd_Data_mode, *ptr++);
delay_ms(100);
}
}
Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 7 - p1 61
LCD – 4‐Bit Interface
31
8/9/2016
4‐bit LCD interface ‐ HW
U1
13 33 LCD1
OSC1/CLKIN RB0/INT LM016L
14 34
OSC2/CLKOUT RB1
1 35
MCLR/Vpp/THV RB2
36
RB3/PGM
2 37
RA0/AN0 RB4
3 38
RA1/AN1 RB5
4 39
RA2/AN2/VREF- RB6/PGC
5 40
RA3/AN3/VREF+ RB7/PGD
6
RA4/T0CKI
VDD
VSS
VEE
7 15
RW
RS
D0
D1
D2
D3
D4
D5
D6
D7
RA5/AN4/SS RC0/T1OSO/T1CKI
E
16
RC1/T1OSI/CCP2
8 17
RE0/AN5/RD RC2/CCP1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
9 18
RE1/AN6/WR RC3/SCK/SCL
10 23
RE2/AN7/CS RC4/SDI/SDA
24
RC5/SDO
25
RC6/TX/CK
26
RC7/RX/DT
19
RD0/PSP0
20
RD1/PSP1
21
RD2/PSP2
22
RD3/PSP3
27
RD4/PSP4
28
RD5/PSP5
29
RD6/PSP6
30
RD7/PSP7
PIC16F877
4‐bit LCD interface
#include <16F877.h> char *ptr;
#use delay(clock=20000000) set_tris_d(0x00);
#use fast_io (D) lcd_init();
ptr = text3;
#define LCD_ENABLE_PIN PIN_E0 lcd_gotoxy(1,1);
#define LCD_RS_PIN PIN_E1 // lcd_putc("Hello the world!");
#define LCD_RW_PIN PIN_E2 printf(lcd_putc,"%s",text1);
#define LCD_DATA4 PIN_D4 lcd_gotoxy(1,2);
#define LCD_DATA5 PIN_D5 printf(lcd_putc,"%s",text2);
#define LCD_DATA6 PIN_D6 while (1)
#define LCD_DATA7 PIN_D7 { if (*ptr == 0)
#include <LCD.C> ptr = text3;
void main() { // 4‐bit mode LCD ( 2 x 16) lcd_gotoxy(8,2);
char text1[]="Hello the world!"; lcd_putc(*ptr++);
delay_ms(100);
char text2[]="Number: "; }
char text3[]="0123456789"; }
Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 7 - p1 64
32
8/9/2016
Using Library LCD.C
• Define LCD pins:
#define LCD_ENABLE_PIN PIN_E0
….
(don’t need to define LCD pins if the default pins are used)
• Functions
– lcd_init(): Must be called before any other function.
– lcd_putc(c): Will display c on the next position of the LCD.
• \f Clear display
• \n Go to start of second
• \b Move back one position
– lcd_gotoxy(x,y): Set write position on LCD (upper left is 1,1)
– lcd_getc(x,y): Returns character at position x,y on LCD
Serial LCD
• The LCD has its own controller, which is compatible with the
Hitachi 44780 MCU, the standard for this interface.
• When the system is started, the LCD takes some time to initialize
itself; its own MCU needs time to get ready to receive data. A
delay of about 500 ms should be allowed in the main controller
before attempting to access the LCD.
• The program uses library routines to generate the RS232 output,
which are called up by the directive #use RS232.
• The baud rate must be specified and the send (TX) and receive
(RX) pins specified as arguments of this directive.
• The directive must be preceded by a #use delay, which specifies
the clock rate in the target system.
33
8/9/2016
Ex: MILFORD Serial LCD Interface – HW
Serial LCD
34
8/9/2016
Serial LCD
• Control Codes
Serial LCD
35
8/9/2016
Class Assignment
36