實(shí)驗(yàn)6 LCD接口
1.利用單片機(jī)控制LCD1602,在LCD1602上顯示字符串,并使其整屏左移。
#include
#define uchar unsigned char
#define uint unsigned int
uchar code table[]="Hello World!";
uchar code table1[]="I am a boy!";
sbit lcden=P2^7; //液晶使能端
sbit lcdrs=P2^6;
sbit LCDWR=P2^5;
uchar num;
/***********************************************************
延時(shí)函數(shù)
參數(shù)說(shuō)明:
z代表要延時(shí)z ms
***********************************************************/
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
/***********************************************************
寫(xiě)命令或數(shù)據(jù)
參數(shù)說(shuō)明:
isRs=1代表寫(xiě)數(shù)據(jù),isRs=0代表寫(xiě)命令
***********************************************************/
void write(uchar mdata, uchar isRs)
{
lcdrs=isRs;
P0=mdata;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
/***********************************************************
寫(xiě)命令
參數(shù)說(shuō)明:
data代表要寫(xiě)的命令
***********************************************************/
void write_command(uchar com)
{
write(com,0);
}
/***********************************************************
寫(xiě)數(shù)據(jù)
參數(shù)說(shuō)明:
data代表要寫(xiě)的數(shù)據(jù)
***********************************************************/
void write_data(uchar mdata)
{
write(mdata,1);
}
void init()
{
lcden=0;
LCDWR=0;
write_command(0x38);//設(shè)置16X2顯示,5X7點(diǎn)陣,8位數(shù)據(jù)接口
write_command(0x0c);//設(shè)置開(kāi)顯示,不顯示光標(biāo)
write_command(0x06);//寫(xiě)一個(gè)字符后地址指針加1
write_command(0x01);//顯示清零,數(shù)據(jù)指針清零
}
void main()
{
init();
write_command(0x80+0x10);
for(num=0;num<15;num++)
{
write_data(table[num]);
delay(5);
}
write_command(0x80+0x50);
for(num=0;num<16;num++)
{
write_data(table1[num]);
delay(5);
}
for(num=0;num<16;num++)
{
write_command(0x18);
delay(200);
}
while(1);
}
思考:如果向CGRAM寫(xiě)入數(shù)據(jù)?
向CGRAM寫(xiě)入數(shù)據(jù)的步驟:
1.建立一個(gè)字符數(shù)組
Uchar LCD_Data1[]={0x01,0x03,0x1D,0x11,0x1D,0x03,0x01,0x00};
2.設(shè)置CGRAM地址,寫(xiě)指令.
Write_cmd(0x40);
3.把數(shù)據(jù)送入CGRAM地址內(nèi).
for(i=0;i
{ write_cmd(0x40+i);
write_date(table[i]);
delay(5);
}
4.寫(xiě)需要顯示的位置指令. Write_cmd(0x80);
5.把CGRAM的0位的數(shù)據(jù)送向LCD1602,顯示數(shù)據(jù)存儲(chǔ)的數(shù)據(jù). for(i=0;i<8;i++) //顯示自定義字符
{
write_date(i); //顯示第i個(gè)字符
delay(5);
}
擴(kuò)展:
1.利用單片機(jī)控制LCD1602,使其可以漢字“中華人民共和國(guó)”并控制整屏移動(dòng)
#include
#define uchar unsigned char
#define uint unsigned int
sbit lcden=P2^7; //液晶使能端
sbit lcdrs=P2^6; //數(shù)據(jù)或命令控制(0代表命令,1代表數(shù)據(jù))
sbit LCDWR=P2^5; //讀寫(xiě)控制(0代表寫(xiě),1代表讀)
uchar num;
uchar code table_HanZi[]={
0x04,0x04,0x1F,0x15,0x15,0x1F,0x04,0x04,//中
0x0A,0x1B,0x0E,0x0B,0x04,0x1F,0x04,0x04,//華
0x00,0x04,0x04,0x04,0x0A,0x11,0x00,0x00,//人
0x1F,0x11,0x1F,0x18,0x16,0x16,0x11,0x18,//民
0x0A,0x1F,0x0A,0x1F,0x00,0x0A,0x11,0x11,//共
0x04,0x08,0x18,0x1F,0x0D,0x1F,0x1C,0x08,//和
0x1F,0x11,0x1F,0x15,0x1F,0x17,0x1F,0x1F,//國(guó)
};
uchar code table1[]={0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07}; //自定義字符數(shù)據(jù)地址
/***********************************************************
延時(shí)函數(shù)
參數(shù)說(shuō)明:
z代表要延時(shí)z ms
***********************************************************/
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
/***********************************************************
寫(xiě)命令或數(shù)據(jù)
參數(shù)說(shuō)明:
isRs=1代表寫(xiě)數(shù)據(jù),isRs=0代表寫(xiě)命令
***********************************************************/
void write(uchar mdata, uchar isRs)
{
lcdrs=isRs;
P0=mdata;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
/***********************************************************
寫(xiě)命令
參數(shù)說(shuō)明:
data代表要寫(xiě)的命令
***********************************************************/
void write_command(uchar com)
{
write(com,0);
}
/***********************************************************
寫(xiě)數(shù)據(jù)
參數(shù)說(shuō)明:
data代表要寫(xiě)的數(shù)據(jù)
***********************************************************/
void write_data(uchar mdata)
{
write(mdata,1);
}
void init()
{
lcden=0;
LCDWR=0;
write_command(0x38);//設(shè)置16X2顯示,5X7點(diǎn)陣,8位數(shù)據(jù)接口
write_command(0x0c);//設(shè)置開(kāi)顯示,不顯示光標(biāo)
write_command(0x06);//寫(xiě)一個(gè)字符后地址指針加1
write_command(0x01);//顯示清零,數(shù)據(jù)指針清零
delay(1);
}
void main()
{
uchar i;
init();
while(1)
{
write_command(0x40); //開(kāi)始寫(xiě)入要顯示的自定義字符、漢字代碼
for(i=0;i<56;i++)
{
write_data(table_HanZi[i]);
delay(5);
}
write_command(0x80); //從第一行第一列開(kāi)始顯示
for(i=0;i<8;i++) //顯示自定義字符
{
write_data(table1[i]);
delay(5);
}
}
}