SlideShare a Scribd company logo
LCD Example
See the project PWM_asl.X
Open it with MPLABX
Example: Reads three Analog Values
Then Changes the PWM value to be Equal to Pot 1 (AN0)
The PWM1 is connected to the Fan so it controls the speed of the Fan
Values are Displayed on the Screen ( 4 Rows).
We will Focus on the LCD interface.
The LCD Datasheet is in the Display_1602a1_stc.pdf
1
LCD Block Diagram
Typical Pin Assignment
3
LCD Basic Interface
• Two Ways to Interface the LCD:
• 8-bit Mode: Requires 8-bit of Data (DB0—DB7) , RS, E, and R/W. Sends one byte in
one write or read
• 4-bit Mode: Requires 4-bit of Data (DB4—DB7) , RS, E, and R/W. Sends one nibble in
one write or read. Writing/Reading a byte Requires 2 read or write operations..
Notice that we must use DB4—DB7 of the LCD NOT bits 0—4. This mode is also
called Nibble mode.
• To Save pins we usually use 4-bit mode,
• R/W : 1 means read, 0 means Write .
• RS : 1 Data, means we write data to the current location of the Cursor.
• RS : 0 Means we write command such as go to location, clear display. New
Line etc.
• E : Enable (clock)
4
LCD Basic Interface
• We need also to connect VCC, GND
• We need to connect contrast control, usually called V0 or Vlc or other,
This is connected to a potentiometer. See the Next Slide.
• We can connect the data bits, RS, R/W, and E to any digital outputs
but we usually use one port ( PORTD usually. Or sometimes we use
PORTD for data and Port E for the RS, R/W, and E.
• See next page. See the basic_circuit.png for an example.
• In the Example we are using 4 bit mode ( nibble mode)
5
6
Four Bit Mode
Back Light(K,A)
Connect A to VCC (5V)
and K to GND
7
Eight Bit Mode
Back Light(K,A)
Connect A to VCC (5V)
and K to GND
Instruction Table
(Commands)
and
Data Mode
See The Data Sheet
for details.
8
LCD Example. We will use the same example
that will be used for pulse width modulation
• Here we will focus on LCD code.
• The files that we need are lcd_x8.h, lcd_x8.c
• You can add these to any new project by just copying these files to
your project and use add existing to add the lcd_x8.h to the header
and the lcd_x8.c to the sources.
• You can run it on the simulator.
• Look at the top of the header lcd_x8.h.
• You practically do not need to change anything in these files except
the mapping
9
Redefine some variable for this to work
Port D and Port E (lcd_x8.h)
struct lcd_pin_map { // look at the hardware on slide 4
unsigned un1 : 1; //unused on to an I/O port to gain, should be cleared
unsigned rs : 1; // rs =1 : data, 0 means rd
unsigned rw : 1; // the RD/WR .. We keep this as zero. Just write
unsigned enable : 1; // The E pin
unsigned data : 4; // data pins pins d0,1,2,3
} lcd __at(0xF83); //PORTD // this is new syntax
//@ 0x0F83; // ; PORTD // old Syntax
// followings are defined , the above for the actual circuit
// but for the simulator we change to thus
#define lcd_output_enable(x) PORTEbits.RE1 = x // For the simulator
//lcd.enable = x // for the actual circuit, use this
#define lcd_output_rs(x) PORTEbits.RE2 = x //For the Simulator
//lcd.rs = x // For the actual circuit.
10
Other functions needed for LCD
void delay_cycles(unsigned char n);
void delay_ms(unsigned int n);
void lcd_send_nibble(unsigned char n);
void lcd_send_byte(unsigned char cm_data, unsigned char n);
void lcd_init(void);
void lcd_gotoxy(unsigned char x, unsigned char y);
void lcd_putc(char c);
void lcd_puts(char *s);
void Lcd_Shift_Right(void);
void Lcd_Shift_Left(void);
11
Initialization void lcd_init()
• This is a strange code of initialization. It starts in 8 it mode then
changes to 4 bit mode. Do not worry about it just use as is. Works for
all of these displays. Just use it. Described in Data sheets.
• It sends some commands and then turns the LCD on.
• See the next page
12
Lcd_x8.h
void delay_cycles(unsigned char n);
void delay_ms(unsigned int n);
void lcd_send_nibble(unsigned char n);
void lcd_send_byte(unsigned char cm_data, unsigned char n);
void lcd_init(void);
void lcd_gotoxy(unsigned char x, unsigned char y);
void lcd_putc(char c);
void lcd_puts(char *s);
void Lcd_Shift_Right(void);
void Lcd_Shift_Left(void);
13
14
void lcd_init() {
unsigned char i;
lcd_output_rs(0);
//lcd_output_rw(0);
lcd_output_enable(0);
delay_ms(25); //
for (i = 1; i <= 3; ++i) {
lcd_send_nibble(3);
// lcd_send_nibble(0);
delay_ms(6); //5
}
lcd_send_nibble(2);
// lcd_send_nibble(0);
for (i = 0; i <= 3; ++i)
lcd_send_byte(0, LCD_INIT_STRING[i]);
}
Lcd_send_byte : Use it to send a byte or command. 0 command, 1 data ,
void lcd_send_byte(unsigned char cm_data, unsigned char n) {
// n is the byte to be written, cm_data:0 command, 1: data
// notice that it writes
lcd_output_rs(cm_data);
delay_cycles(1);
delay_cycles(1);
lcd_output_enable(0);
lcd_send_nibble(n >> 4); //write high nibble first
lcd_send_nibble(n & 0x0f); // then lower nibble
if (cm_data) __delaywdt_us(200);
else
delay_ms(2); //added by raed
}
15
Lcd_gotoxy(x, y) : x is character number 0—15 ( can be more for larger displays 0—19 ..etc)
y is the line number 1 or 2 ( up to 4 for 4 lines). Sets the cursor position at location x,y
void lcd_gotoxy(unsigned char x, unsigned char y) {
unsigned char address;
switch (y) {
case 1: address = 0x80; // line 1 , x is the x location usually 0 --15
break;
case 2: address = 0xc0; // line 2
break;
case 3: address = 0x80 + LCD_LINE_SIZE; // line 3 for displays with > 2 lines
break;
case 4: address = 0xc0 + LCD_LINE_SIZE; // line 4 for displays with 4 lines
break;
}
address += x - 1;
lcd_send_byte(0, (unsigned char) (0x80 | address));
}
16
Lcd_putc writes a character to of the cursor current location. Or sends some commands. We use this
to write main commands or bytes.
void lcd_putc(char c) {
switch (c) {
case 'f': lcd_send_byte(0, 1); //clears the display, command
delay_ms(2);
break;
case 'n': lcd_gotoxy(1, 2); // goes to line 2 , command
break;
case 'b': lcd_send_byte(0, 0x10); //one character back, command
break;
default: lcd_send_byte(1, c); // writes a data byte at current location.
break;
}
}
17
Lcd_puts: writes a string of data at current location
void lcd_puts(char *s) {
while (*s) {
lcd_putc(*s);
s++;
}
}
There are 2 more functions to shift left and right. Take a look
18
Use of LCD in main Function
Top of main:
--- some codehere
lcd_init(); // call lc_init
init_adc_no_lib();
init_pwm1();
//PORTCbits.RC4 =1;
PORTCbits.RC5 = 1;
send_string_no_lib((unsigned char *) "rrReading AN0, AN1, AN2rr");
lcd_putc('f’); //make sure you start by clearing the display
19
Use of LCD in main functin
Raw_val = read_adc_raw_no_lib(0); // read raw value for POT1
set_pwm1_raw(raw_val); // set the Pwm to that value 0--1023
lcd_gotoxy(1, 1); // position 1 line 1
sprintf(Buffer, "V0=%4.2fVnV1=%4.2fV", AN[0], AN[1]); // write using sprintf
lcd_puts(Buffer); // write it to the LCD. Notice the n goes to line 2
lcd_gotoxy(1, 3); // go to first location in line 3
RPS = RPS_count;
sprintf(Buffer, "Speed=%6.2f RPSn", RPS/7.0); // Display Speed and RPS on lines 3
lcd_puts(Buffer); // speed = Revolution per second
lcd_gotoxy(1, 4); ); // Go to line 4, the n works for line 2 only
sprintf(Buffer, "D=%5d,%6.2f", raw_val, (raw_val * 100.0) / 1023.0
lcd_puts(Buffer); // Above displays duty 0--1023, and also as percentage
lcd_gotoxy(15, 4); // at location 15 write %, see next line
lcd_putc('%');
20
Running the Example on the Simulator.
• Load the hex (pwm_asl.X.production.hex) file from the
pwm_asl.Xdistdefaultproduction
• Select Board 4, Processor PIC18F4620, 4MHz
• From file Configure select COM1, On Tera term select COM2
• Select hd44780 16x4 for LCD ( 4 lines)
• Change the Value on Pot. P1 ( Slider) represnts a 0—5 Potentiometer
which is An0. This value is set to PWM2 which controls the Speed of
the Fan.
• The Display displays all Values, AN0, AN1, Fan Speed, Duty cycle as
raw value ( 0—1023) and also as percentage 0—100.
• U should see results on LCD. See sample on next slide.
21
22

More Related Content

Similar to LCD_Example.pptx

Moving message display
Moving message displayMoving message display
Moving message display
viraj1989
 
Lcd n PIC16F
Lcd n PIC16FLcd n PIC16F
Lcd n PIC16F
hairilfaiz86
 
EMBEDDED SYSTEMS 4&5
EMBEDDED SYSTEMS 4&5EMBEDDED SYSTEMS 4&5
EMBEDDED SYSTEMS 4&5PRADEEP
 
Embedded System Practical manual (1)
Embedded System Practical manual (1)Embedded System Practical manual (1)
Embedded System Practical manual (1)
Niraj Bharambe
 
m.tech esd lab manual for record
m.tech esd lab manual for recordm.tech esd lab manual for record
m.tech esd lab manual for record
G Lemuel George
 
Experiment 16 x2 parallel lcd
Experiment   16 x2 parallel lcdExperiment   16 x2 parallel lcd
Experiment 16 x2 parallel lcd
Prashanta Chowdhury
 
Analog to Digital Converter
Analog to Digital ConverterAnalog to Digital Converter
Analog to Digital Converter
Ariel Tonatiuh Espindola
 
Micro c lab6(lcd)
Micro c lab6(lcd)Micro c lab6(lcd)
Micro c lab6(lcd)
Mashood
 
DEF CON 23 - Rodringo Almeida - embedded system design from electronics
DEF CON 23 - Rodringo Almeida - embedded system design from electronics DEF CON 23 - Rodringo Almeida - embedded system design from electronics
DEF CON 23 - Rodringo Almeida - embedded system design from electronics
Felipe Prado
 
Product catlog
Product catlogProduct catlog
Product catlog
Aarya Technologies
 
CC2500 Wireless Trans-receiver Module
CC2500 Wireless Trans-receiver ModuleCC2500 Wireless Trans-receiver Module
CC2500 Wireless Trans-receiver Module
Aarya Technologies
 
Data Acquisition
Data AcquisitionData Acquisition
Data Acquisition
azhar557
 
Arduino: Arduino lcd
Arduino: Arduino lcdArduino: Arduino lcd
Arduino: Arduino lcd
SANTIAGO PABLO ALBERTO
 
Verilog Lecture2 thhts
Verilog Lecture2 thhtsVerilog Lecture2 thhts
Verilog Lecture2 thhts
Béo Tú
 
EEE UNIT-2 PPT.pdf
EEE UNIT-2 PPT.pdfEEE UNIT-2 PPT.pdf
EEE UNIT-2 PPT.pdf
VishalPatil57559
 
Analog To Digital Conversion (ADC) Programming in LPC2148
Analog To Digital Conversion (ADC) Programming in LPC2148Analog To Digital Conversion (ADC) Programming in LPC2148
Analog To Digital Conversion (ADC) Programming in LPC2148
Omkar Rane
 
Interfacing with LCD
Interfacing with LCDInterfacing with LCD
Interfacing with LCD
Ariel Tonatiuh Espindola
 

Similar to LCD_Example.pptx (20)

Moving message display
Moving message displayMoving message display
Moving message display
 
Lcd n PIC16F
Lcd n PIC16FLcd n PIC16F
Lcd n PIC16F
 
EMBEDDED SYSTEMS 4&5
EMBEDDED SYSTEMS 4&5EMBEDDED SYSTEMS 4&5
EMBEDDED SYSTEMS 4&5
 
Embedded System Practical manual (1)
Embedded System Practical manual (1)Embedded System Practical manual (1)
Embedded System Practical manual (1)
 
m.tech esd lab manual for record
m.tech esd lab manual for recordm.tech esd lab manual for record
m.tech esd lab manual for record
 
Experiment 16 x2 parallel lcd
Experiment   16 x2 parallel lcdExperiment   16 x2 parallel lcd
Experiment 16 x2 parallel lcd
 
Analog to Digital Converter
Analog to Digital ConverterAnalog to Digital Converter
Analog to Digital Converter
 
Micro c lab6(lcd)
Micro c lab6(lcd)Micro c lab6(lcd)
Micro c lab6(lcd)
 
DEF CON 23 - Rodringo Almeida - embedded system design from electronics
DEF CON 23 - Rodringo Almeida - embedded system design from electronics DEF CON 23 - Rodringo Almeida - embedded system design from electronics
DEF CON 23 - Rodringo Almeida - embedded system design from electronics
 
Product catlog
Product catlogProduct catlog
Product catlog
 
CC2500 Wireless Trans-receiver Module
CC2500 Wireless Trans-receiver ModuleCC2500 Wireless Trans-receiver Module
CC2500 Wireless Trans-receiver Module
 
Em s7 plc
Em s7 plcEm s7 plc
Em s7 plc
 
Data Acquisition
Data AcquisitionData Acquisition
Data Acquisition
 
Picmico
PicmicoPicmico
Picmico
 
Arduino: Arduino lcd
Arduino: Arduino lcdArduino: Arduino lcd
Arduino: Arduino lcd
 
Verilog Lecture2 thhts
Verilog Lecture2 thhtsVerilog Lecture2 thhts
Verilog Lecture2 thhts
 
report cs
report csreport cs
report cs
 
EEE UNIT-2 PPT.pdf
EEE UNIT-2 PPT.pdfEEE UNIT-2 PPT.pdf
EEE UNIT-2 PPT.pdf
 
Analog To Digital Conversion (ADC) Programming in LPC2148
Analog To Digital Conversion (ADC) Programming in LPC2148Analog To Digital Conversion (ADC) Programming in LPC2148
Analog To Digital Conversion (ADC) Programming in LPC2148
 
Interfacing with LCD
Interfacing with LCDInterfacing with LCD
Interfacing with LCD
 

Recently uploaded

The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
Steve Thomason
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
Jheel Barad
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
Col Mukteshwar Prasad
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
EduSkills OECD
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
GeoBlogs
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
PedroFerreira53928
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
GeoBlogs
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
BhavyaRajput3
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)
rosedainty
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
bennyroshan06
 

Recently uploaded (20)

The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
 

LCD_Example.pptx

  • 1. LCD Example See the project PWM_asl.X Open it with MPLABX Example: Reads three Analog Values Then Changes the PWM value to be Equal to Pot 1 (AN0) The PWM1 is connected to the Fan so it controls the speed of the Fan Values are Displayed on the Screen ( 4 Rows). We will Focus on the LCD interface. The LCD Datasheet is in the Display_1602a1_stc.pdf 1
  • 4. LCD Basic Interface • Two Ways to Interface the LCD: • 8-bit Mode: Requires 8-bit of Data (DB0—DB7) , RS, E, and R/W. Sends one byte in one write or read • 4-bit Mode: Requires 4-bit of Data (DB4—DB7) , RS, E, and R/W. Sends one nibble in one write or read. Writing/Reading a byte Requires 2 read or write operations.. Notice that we must use DB4—DB7 of the LCD NOT bits 0—4. This mode is also called Nibble mode. • To Save pins we usually use 4-bit mode, • R/W : 1 means read, 0 means Write . • RS : 1 Data, means we write data to the current location of the Cursor. • RS : 0 Means we write command such as go to location, clear display. New Line etc. • E : Enable (clock) 4
  • 5. LCD Basic Interface • We need also to connect VCC, GND • We need to connect contrast control, usually called V0 or Vlc or other, This is connected to a potentiometer. See the Next Slide. • We can connect the data bits, RS, R/W, and E to any digital outputs but we usually use one port ( PORTD usually. Or sometimes we use PORTD for data and Port E for the RS, R/W, and E. • See next page. See the basic_circuit.png for an example. • In the Example we are using 4 bit mode ( nibble mode) 5
  • 6. 6 Four Bit Mode Back Light(K,A) Connect A to VCC (5V) and K to GND
  • 7. 7 Eight Bit Mode Back Light(K,A) Connect A to VCC (5V) and K to GND
  • 8. Instruction Table (Commands) and Data Mode See The Data Sheet for details. 8
  • 9. LCD Example. We will use the same example that will be used for pulse width modulation • Here we will focus on LCD code. • The files that we need are lcd_x8.h, lcd_x8.c • You can add these to any new project by just copying these files to your project and use add existing to add the lcd_x8.h to the header and the lcd_x8.c to the sources. • You can run it on the simulator. • Look at the top of the header lcd_x8.h. • You practically do not need to change anything in these files except the mapping 9
  • 10. Redefine some variable for this to work Port D and Port E (lcd_x8.h) struct lcd_pin_map { // look at the hardware on slide 4 unsigned un1 : 1; //unused on to an I/O port to gain, should be cleared unsigned rs : 1; // rs =1 : data, 0 means rd unsigned rw : 1; // the RD/WR .. We keep this as zero. Just write unsigned enable : 1; // The E pin unsigned data : 4; // data pins pins d0,1,2,3 } lcd __at(0xF83); //PORTD // this is new syntax //@ 0x0F83; // ; PORTD // old Syntax // followings are defined , the above for the actual circuit // but for the simulator we change to thus #define lcd_output_enable(x) PORTEbits.RE1 = x // For the simulator //lcd.enable = x // for the actual circuit, use this #define lcd_output_rs(x) PORTEbits.RE2 = x //For the Simulator //lcd.rs = x // For the actual circuit. 10
  • 11. Other functions needed for LCD void delay_cycles(unsigned char n); void delay_ms(unsigned int n); void lcd_send_nibble(unsigned char n); void lcd_send_byte(unsigned char cm_data, unsigned char n); void lcd_init(void); void lcd_gotoxy(unsigned char x, unsigned char y); void lcd_putc(char c); void lcd_puts(char *s); void Lcd_Shift_Right(void); void Lcd_Shift_Left(void); 11
  • 12. Initialization void lcd_init() • This is a strange code of initialization. It starts in 8 it mode then changes to 4 bit mode. Do not worry about it just use as is. Works for all of these displays. Just use it. Described in Data sheets. • It sends some commands and then turns the LCD on. • See the next page 12
  • 13. Lcd_x8.h void delay_cycles(unsigned char n); void delay_ms(unsigned int n); void lcd_send_nibble(unsigned char n); void lcd_send_byte(unsigned char cm_data, unsigned char n); void lcd_init(void); void lcd_gotoxy(unsigned char x, unsigned char y); void lcd_putc(char c); void lcd_puts(char *s); void Lcd_Shift_Right(void); void Lcd_Shift_Left(void); 13
  • 14. 14 void lcd_init() { unsigned char i; lcd_output_rs(0); //lcd_output_rw(0); lcd_output_enable(0); delay_ms(25); // for (i = 1; i <= 3; ++i) { lcd_send_nibble(3); // lcd_send_nibble(0); delay_ms(6); //5 } lcd_send_nibble(2); // lcd_send_nibble(0); for (i = 0; i <= 3; ++i) lcd_send_byte(0, LCD_INIT_STRING[i]); }
  • 15. Lcd_send_byte : Use it to send a byte or command. 0 command, 1 data , void lcd_send_byte(unsigned char cm_data, unsigned char n) { // n is the byte to be written, cm_data:0 command, 1: data // notice that it writes lcd_output_rs(cm_data); delay_cycles(1); delay_cycles(1); lcd_output_enable(0); lcd_send_nibble(n >> 4); //write high nibble first lcd_send_nibble(n & 0x0f); // then lower nibble if (cm_data) __delaywdt_us(200); else delay_ms(2); //added by raed } 15
  • 16. Lcd_gotoxy(x, y) : x is character number 0—15 ( can be more for larger displays 0—19 ..etc) y is the line number 1 or 2 ( up to 4 for 4 lines). Sets the cursor position at location x,y void lcd_gotoxy(unsigned char x, unsigned char y) { unsigned char address; switch (y) { case 1: address = 0x80; // line 1 , x is the x location usually 0 --15 break; case 2: address = 0xc0; // line 2 break; case 3: address = 0x80 + LCD_LINE_SIZE; // line 3 for displays with > 2 lines break; case 4: address = 0xc0 + LCD_LINE_SIZE; // line 4 for displays with 4 lines break; } address += x - 1; lcd_send_byte(0, (unsigned char) (0x80 | address)); } 16
  • 17. Lcd_putc writes a character to of the cursor current location. Or sends some commands. We use this to write main commands or bytes. void lcd_putc(char c) { switch (c) { case 'f': lcd_send_byte(0, 1); //clears the display, command delay_ms(2); break; case 'n': lcd_gotoxy(1, 2); // goes to line 2 , command break; case 'b': lcd_send_byte(0, 0x10); //one character back, command break; default: lcd_send_byte(1, c); // writes a data byte at current location. break; } } 17
  • 18. Lcd_puts: writes a string of data at current location void lcd_puts(char *s) { while (*s) { lcd_putc(*s); s++; } } There are 2 more functions to shift left and right. Take a look 18
  • 19. Use of LCD in main Function Top of main: --- some codehere lcd_init(); // call lc_init init_adc_no_lib(); init_pwm1(); //PORTCbits.RC4 =1; PORTCbits.RC5 = 1; send_string_no_lib((unsigned char *) "rrReading AN0, AN1, AN2rr"); lcd_putc('f’); //make sure you start by clearing the display 19
  • 20. Use of LCD in main functin Raw_val = read_adc_raw_no_lib(0); // read raw value for POT1 set_pwm1_raw(raw_val); // set the Pwm to that value 0--1023 lcd_gotoxy(1, 1); // position 1 line 1 sprintf(Buffer, "V0=%4.2fVnV1=%4.2fV", AN[0], AN[1]); // write using sprintf lcd_puts(Buffer); // write it to the LCD. Notice the n goes to line 2 lcd_gotoxy(1, 3); // go to first location in line 3 RPS = RPS_count; sprintf(Buffer, "Speed=%6.2f RPSn", RPS/7.0); // Display Speed and RPS on lines 3 lcd_puts(Buffer); // speed = Revolution per second lcd_gotoxy(1, 4); ); // Go to line 4, the n works for line 2 only sprintf(Buffer, "D=%5d,%6.2f", raw_val, (raw_val * 100.0) / 1023.0 lcd_puts(Buffer); // Above displays duty 0--1023, and also as percentage lcd_gotoxy(15, 4); // at location 15 write %, see next line lcd_putc('%'); 20
  • 21. Running the Example on the Simulator. • Load the hex (pwm_asl.X.production.hex) file from the pwm_asl.Xdistdefaultproduction • Select Board 4, Processor PIC18F4620, 4MHz • From file Configure select COM1, On Tera term select COM2 • Select hd44780 16x4 for LCD ( 4 lines) • Change the Value on Pot. P1 ( Slider) represnts a 0—5 Potentiometer which is An0. This value is set to PWM2 which controls the Speed of the Fan. • The Display displays all Values, AN0, AN1, Fan Speed, Duty cycle as raw value ( 0—1023) and also as percentage 0—100. • U should see results on LCD. See sample on next slide. 21
  • 22. 22