LCD (Liquid Crystal Display) : LCD screen is
an electronic display module and find a wide range of applications. A 16x2 LCD
display is very basic module and is very commonly used in various devices and
circuits. These modules are preferred over seven segment and other multi
segment LED's. The reasons being: LCDs are economical; easily programmable; have
no limitation of displaying special & even custom character (unlike in
seven segments), animation and so on. A 16x2
LCD means it can display 16 characters per line and there are 2 such
lines. In this LCD each character is displayed in 5x7 pixel matrix. This LCD
has two registers, namely, Command and Data.
Interfacing of LCD with ARM Cortex:
Program1:
#include <stdio.h>
#include <string.h>
#include "NUC1xx.h"
#include "GPIO.h"
#include "SYS.h"
// LCM16x2
// pin1 Vss: (to Gnd)
// pin2 Vcc: to +5V (to +5V)
// pin3 Vee : brightness control (to Gnd)
// pin4 RS : 1=Data, 0=Instruction (to GPA0)
// pin5 RW : 1=Read, 0=Write (to GPA1)
// pin6 E : Chip Enable (to GPA2)
// pin7~14 : D0~D7 (to GPE0~7)
#include <stdio.h>
#include <string.h>
#include "NUC1xx.h"
#include "GPIO.h"
#include "SYS.h"
#define LCD_DATA GPIOE->DOUT // D0~7 pin = GPE0~7
#define LCD_RS_SET DrvGPIO_SetBit(E_GPA,0) // RS pin = GPA0
#define LCD_RS_CLR DrvGPIO_ClrBit(E_GPA,0)
#define LCD_RW_SET DrvGPIO_SetBit(E_GPA,1) // RW pin = GPA1
#define LCD_RW_CLR DrvGPIO_ClrBit(E_GPA,1)
#define LCD_E_SET DrvGPIO_SetBit(E_GPA,2) // E pin = GPA2
#define LCD_E_CLR DrvGPIO_ClrBit(E_GPA,2)
void init_GPIO(void)
{
DrvGPIO_Open(E_GPA, 0, E_IO_OUTPUT);
DrvGPIO_Open(E_GPA, 1, E_IO_OUTPUT);
DrvGPIO_Open(E_GPA, 2, E_IO_OUTPUT);
DrvGPIO_ClrBit(E_GPA, 0);
DrvGPIO_ClrBit(E_GPA, 1);
DrvGPIO_ClrBit(E_GPA, 2);
}
void lcdWriteData(unsigned char wdata)
{
LCD_DATA=wdata | 0xFF00;
LCD_RS_SET;
LCD_RW_CLR;
LCD_E_SET;
DrvSYS_Delay(50000);
LCD_E_CLR;
//DrvSYS_Delay(1000000);
}
void lcdWriteCommand(unsigned char wdata)
{
LCD_DATA=wdata;
LCD_RS_CLR;
LCD_RW_CLR;
LCD_E_SET;
DrvSYS_Delay(50000);
LCD_E_CLR;
//DrvSYS_Delay(1000000);
}
void init_LCM16x2(void)
{
init_GPIO();
LCD_DATA=0;
DrvSYS_Delay(40000);// wait time >40ms after Vcc>4.5V
//lcdWriteCommand(0x38);
//DrvSYS_Delay(1000);
lcdWriteCommand(0x38);
DrvSYS_Delay(37);
lcdWriteCommand(0x06); // Cursor Move to right, no shift
DrvSYS_Delay(37);
lcdWriteCommand(0x0e); // Display ON, Cursor Off, Blinking off
DrvSYS_Delay(37);
lcdWriteCommand(0x01); // Display Clear
DrvSYS_Delay(1520);
}
void lcdSetAddr(uint8_t x,uint8_t y)
{
if(y==1) x+=0x40;
x+=0x80;
lcdWriteCommand(x);
}
void printC(uint8_t x, uint8_t y, unsigned char dat)
{
lcdSetAddr(x,y);
lcdWriteData(dat);
}
void print_Line(int8_t line, char text[])
{
int8_t i;
for (i=0;i<strlen(text);i++)
printC(i*8,line*16,text[i]);
}
void printS(int16_t x, int16_t y, char text[])
{
int8_t i;
for (i=0;i<strlen(text);i++)
printC(x+i*8, y,text[i]);
}
int main()
{
char TEXT0[16]= "welcome to all";
char TEXT1[16]= "GOD BLESSS YOU";
int i;
init_LCM16x2();
while(1)
{
for(i=0;TEXT1[i]!='\0';i++)
{
printC(i,0,TEXT1[i]);
}
for(i=0;TEXT0[i]!='\0';i++)
{
printC(i,1,TEXT0[i]);
}
}
return 0;
}
Program2: Using Pointer
#include <stdio.h>
#include <string.h>
#include "NUC1xx.h"
#include "GPIO.h"
#include "SYS.h"
// LCM16x2
// pin1 Vss: (to Gnd)
// pin2 Vcc: to +5V (to +5V)
// pin3 Vee : brightness control (to Gnd)
// pin4 RS : 1=Data, 0=Instruction (to GPA0)
// pin5 RW : 1=Read, 0=Write (to GPA1)
// pin6 E : Chip Enable (to GPA2)
// pin7~14 : D0~D7 (to GPE0~7)
#define LCD_DATA GPIOE->DOUT // D0~7 pin = GPE0~7
#define LCD_RS_SET DrvGPIO_SetBit(E_GPA,0) // RS pin = GPA0
#define LCD_RS_CLR DrvGPIO_ClrBit(E_GPA,0)
#define LCD_RW_SET DrvGPIO_SetBit(E_GPA,1) // RW pin = GPA1
#define LCD_RW_CLR DrvGPIO_ClrBit(E_GPA,1)
#define LCD_E_SET DrvGPIO_SetBit(E_GPA,2) // E pin = GPA2
#define LCD_E_CLR DrvGPIO_ClrBit(E_GPA,2)
void init_GPIO(void)
{
DrvGPIO_Open(E_GPA, 0, E_IO_OUTPUT);
DrvGPIO_Open(E_GPA, 1, E_IO_OUTPUT);
DrvGPIO_Open(E_GPA, 2, E_IO_OUTPUT);
DrvGPIO_ClrBit(E_GPA, 0);
DrvGPIO_ClrBit(E_GPA, 1);
DrvGPIO_ClrBit(E_GPA, 2);
}
void lcdWriteData(unsigned char wdata)
{
LCD_DATA=wdata | 0xFF00;
LCD_RS_SET;
LCD_RW_CLR;
LCD_E_SET;
DrvSYS_Delay(50000);
LCD_E_CLR;
//DrvSYS_Delay(1000000);
}
void lcdWriteCommand(unsigned char wdata)
{
LCD_DATA=wdata;
LCD_RS_CLR;
LCD_RW_CLR;
LCD_E_SET;
DrvSYS_Delay(50000);
LCD_E_CLR;
//DrvSYS_Delay(1000000);
}
void stringg(unsigned char *p)
{
while(*p!='\0')
{
lcdWriteData(*p);
p++;
}
}
int main()
{
unsigned char arr[]={0x38,0x0e,0x01,0x80,0x06,'\0'};
int i;
init_GPIO();
while(1)
{
for(i=0;arr[i]!='\0';i++)
{
lcdWriteCommand(arr[i]);
DrvSYS_Delay(37);
}
stringg("WELCOME TO ALL");
lcdWriteCommand(0xc0);
DrvSYS_Delay(37);
stringg("GOD BLESS YOU");
}
return 0;
}
The command register stores the command instructions given to the LCD. A
command is an instruction given to LCD to do a predefined task like
initializing it, clearing its screen, setting the cursor position, controlling
display etc. The data register stores the data to be displayed on the LCD. The
data is the ASCII value of the character to be displayed on the LCD. Click to
learn more about internal structure of a LCD.
Block Diagram of LCD:
LCD stands for liquid crystal display. They come in many sizes 8x1 , 8x2
,10x2, 16x1 , 16x2 , 16x4 , 20x2 , 20x4 ,24x2 , 30x2 , 32x2 , 40x2 etc
. Many multinational companies
like Philips Hitachi Panasonic make their own special kind
of lcd's to be used in their products. All the lcd’s performs the same
functions (display characters numbers special characters ASCII characters
etc).Their programming is also same and they all have same 14 pins (0-13)
or 16 pins (0 to 15).
ALL LCDs have
- Eight(8) Data pins
- VCC (Apply 5v here)
- GND (Ground this pin)
- RS (Register select)
- RW (read - write)
- EN (Enable)
- V0 (Set Lcd contrast)
8-Data pins carries 8-bit data or command from an external unit such as
microcontroller.
Lcd have two registers:
1) Command Register
2) Data Register
1 1) Command Register: When we send commands to lcd these commands go to Command
register and are processed their. Commands with their full description are
given in the picture below.
When RS=0 Command Register is Selected.
When RS=0 Command Register is Selected.
2) Data Register: When we send Data to lcd it goes to data register and is processed
their.
When RS=1 Data Register is selected.
Pin diagram of LCD:
Pin description:
Pin No:
|
Name
|
Function
|
1
|
VSS
|
This pin must be connected to the
ground
|
2
|
VCC
|
Positive supply voltage pin
(5V DC)
|
3
|
VEE
|
Contrast adjustment
|
4
|
RS
|
Register selection
|
5
|
R/W
|
Read or write
|
6
|
E
|
Enable
|
7
|
DB0
|
Data
|
8
|
DB1
|
Data
|
9
|
DB2
|
Data
|
10
|
DB3
|
Data
|
11
|
DB4
|
Data
|
12
|
DB5
|
Data
|
13
|
DB6
|
Data
|
14
|
DB7
|
Data
|
15
|
LED+
|
Back light LED+
|
16
|
LED-
|
Back light LED-
|
16×2 LCD module commands:
16×2 LCD module has a set of preset command instructions. Each command
will make the module to do a particular task. These commands are very important for displaying data in LCD. The list
of commands given below:
Command
|
Function
|
0F
|
For switching on LCD, blinking the cursor.
|
1
|
Clearing the screen
|
2
|
Return home.
|
4
|
Decrement cursor
|
6
|
Increment cursor
|
E
|
Display on and also cursor on
|
80
|
Force cursor to beginning of the first line
|
C0
|
Force cursor to beginning of second line
|
38
|
Use two lines and 5x7 matrix
|
83
|
Cursor line 1 position 3
|
3C
|
Activate second line
|
0C3
|
Jump to second line position 3
|
0C1
|
Jump to second line position1
|
LCD initialization:
The steps that has to be done for initializing the LCD display is given
below and these steps are common for almost all applications.
§ Send 38H to the 8 bit data line for
initialization
§ Send 0FH for making LCD ON, cursor ON
and cursor blinking ON.
§ Send 06H for incrementing cursor
position.
§ Send 01H for clearing the display and
return the cursor.
Sending data to the LCD:
The steps for sending data to the LCD module is given below. I have
already said that the LCD module has pins namely RS, R/W and E. It is the logic
state of these pins that make the module to determine whether a given data
input is a command or data to be displayed.
- Make R/W low.
- Make RS=0 if data byte is a command and make RS=1 if the data byte is a
data to be displayed.
- Place data byte on
the data register.
- Pulse E from high to low.
- Repeat above steps
for sending another data.
Interfacing of LCD with ARM Cortex:
Program1:
#include <stdio.h>
#include <string.h>
#include "NUC1xx.h"
#include "GPIO.h"
#include "SYS.h"
// LCM16x2
// pin1 Vss: (to Gnd)
// pin2 Vcc: to +5V (to +5V)
// pin3 Vee : brightness control (to Gnd)
// pin4 RS : 1=Data, 0=Instruction (to GPA0)
// pin5 RW : 1=Read, 0=Write (to GPA1)
// pin6 E : Chip Enable (to GPA2)
// pin7~14 : D0~D7 (to GPE0~7)
#include <stdio.h>
#include <string.h>
#include "NUC1xx.h"
#include "GPIO.h"
#include "SYS.h"
#define LCD_DATA GPIOE->DOUT // D0~7 pin = GPE0~7
#define LCD_RS_SET DrvGPIO_SetBit(E_GPA,0) // RS pin = GPA0
#define LCD_RS_CLR DrvGPIO_ClrBit(E_GPA,0)
#define LCD_RW_SET DrvGPIO_SetBit(E_GPA,1) // RW pin = GPA1
#define LCD_RW_CLR DrvGPIO_ClrBit(E_GPA,1)
#define LCD_E_SET DrvGPIO_SetBit(E_GPA,2) // E pin = GPA2
#define LCD_E_CLR DrvGPIO_ClrBit(E_GPA,2)
void init_GPIO(void)
{
DrvGPIO_Open(E_GPA, 0, E_IO_OUTPUT);
DrvGPIO_Open(E_GPA, 1, E_IO_OUTPUT);
DrvGPIO_Open(E_GPA, 2, E_IO_OUTPUT);
DrvGPIO_ClrBit(E_GPA, 0);
DrvGPIO_ClrBit(E_GPA, 1);
DrvGPIO_ClrBit(E_GPA, 2);
}
void lcdWriteData(unsigned char wdata)
{
LCD_DATA=wdata | 0xFF00;
LCD_RS_SET;
LCD_RW_CLR;
LCD_E_SET;
DrvSYS_Delay(50000);
LCD_E_CLR;
//DrvSYS_Delay(1000000);
}
void lcdWriteCommand(unsigned char wdata)
{
LCD_DATA=wdata;
LCD_RS_CLR;
LCD_RW_CLR;
LCD_E_SET;
DrvSYS_Delay(50000);
LCD_E_CLR;
//DrvSYS_Delay(1000000);
}
void init_LCM16x2(void)
{
init_GPIO();
LCD_DATA=0;
DrvSYS_Delay(40000);// wait time >40ms after Vcc>4.5V
//lcdWriteCommand(0x38);
//DrvSYS_Delay(1000);
lcdWriteCommand(0x38);
DrvSYS_Delay(37);
lcdWriteCommand(0x06); // Cursor Move to right, no shift
DrvSYS_Delay(37);
lcdWriteCommand(0x0e); // Display ON, Cursor Off, Blinking off
DrvSYS_Delay(37);
lcdWriteCommand(0x01); // Display Clear
DrvSYS_Delay(1520);
}
void lcdSetAddr(uint8_t x,uint8_t y)
{
if(y==1) x+=0x40;
x+=0x80;
lcdWriteCommand(x);
}
void printC(uint8_t x, uint8_t y, unsigned char dat)
{
lcdSetAddr(x,y);
lcdWriteData(dat);
}
void print_Line(int8_t line, char text[])
{
int8_t i;
for (i=0;i<strlen(text);i++)
printC(i*8,line*16,text[i]);
}
void printS(int16_t x, int16_t y, char text[])
{
int8_t i;
for (i=0;i<strlen(text);i++)
printC(x+i*8, y,text[i]);
}
int main()
{
char TEXT0[16]= "welcome to all";
char TEXT1[16]= "GOD BLESSS YOU";
int i;
init_LCM16x2();
while(1)
{
for(i=0;TEXT1[i]!='\0';i++)
{
printC(i,0,TEXT1[i]);
}
for(i=0;TEXT0[i]!='\0';i++)
{
printC(i,1,TEXT0[i]);
}
}
return 0;
}
Program2: Using Pointer
#include <stdio.h>
#include <string.h>
#include "NUC1xx.h"
#include "GPIO.h"
#include "SYS.h"
// LCM16x2
// pin1 Vss: (to Gnd)
// pin2 Vcc: to +5V (to +5V)
// pin3 Vee : brightness control (to Gnd)
// pin4 RS : 1=Data, 0=Instruction (to GPA0)
// pin5 RW : 1=Read, 0=Write (to GPA1)
// pin6 E : Chip Enable (to GPA2)
// pin7~14 : D0~D7 (to GPE0~7)
#define LCD_DATA GPIOE->DOUT // D0~7 pin = GPE0~7
#define LCD_RS_SET DrvGPIO_SetBit(E_GPA,0) // RS pin = GPA0
#define LCD_RS_CLR DrvGPIO_ClrBit(E_GPA,0)
#define LCD_RW_SET DrvGPIO_SetBit(E_GPA,1) // RW pin = GPA1
#define LCD_RW_CLR DrvGPIO_ClrBit(E_GPA,1)
#define LCD_E_SET DrvGPIO_SetBit(E_GPA,2) // E pin = GPA2
#define LCD_E_CLR DrvGPIO_ClrBit(E_GPA,2)
void init_GPIO(void)
{
DrvGPIO_Open(E_GPA, 0, E_IO_OUTPUT);
DrvGPIO_Open(E_GPA, 1, E_IO_OUTPUT);
DrvGPIO_Open(E_GPA, 2, E_IO_OUTPUT);
DrvGPIO_ClrBit(E_GPA, 0);
DrvGPIO_ClrBit(E_GPA, 1);
DrvGPIO_ClrBit(E_GPA, 2);
}
void lcdWriteData(unsigned char wdata)
{
LCD_DATA=wdata | 0xFF00;
LCD_RS_SET;
LCD_RW_CLR;
LCD_E_SET;
DrvSYS_Delay(50000);
LCD_E_CLR;
//DrvSYS_Delay(1000000);
}
void lcdWriteCommand(unsigned char wdata)
{
LCD_DATA=wdata;
LCD_RS_CLR;
LCD_RW_CLR;
LCD_E_SET;
DrvSYS_Delay(50000);
LCD_E_CLR;
//DrvSYS_Delay(1000000);
}
void stringg(unsigned char *p)
{
while(*p!='\0')
{
lcdWriteData(*p);
p++;
}
}
int main()
{
unsigned char arr[]={0x38,0x0e,0x01,0x80,0x06,'\0'};
int i;
init_GPIO();
while(1)
{
for(i=0;arr[i]!='\0';i++)
{
lcdWriteCommand(arr[i]);
DrvSYS_Delay(37);
}
stringg("WELCOME TO ALL");
lcdWriteCommand(0xc0);
DrvSYS_Delay(37);
stringg("GOD BLESS YOU");
}
return 0;
}
0 comments:
Post a Comment
if you have any doubt please let me know