/*************************************************************************
LCD 1602头文件
**************************************************************************/
#include
#include
#define uchar unsigned char
#define uint unsigned int
***it RS=P2^6;
***it RW=P2^5;
***it EN=P2^7;
void delay(uint t)
{
uint i,j;
for(i=t;i>0;i--)
for(j=125;j>0;j--);
}
busy_check()//检测忙函数
{
bit flag;
RS=0;
RW=1;
_nop_();//延时1us
EN=1;
_nop_();//延时1us
flag=(bit)(P0&0x80);
EN=0;
return flag;
}
void LCD_write_command(uchar cmd)//写指令
{
while(busy_check());
RS=0;
RW=0;
P0=cmd;
EN=1;
EN=0;
}
void LCD_write_data(uchar dat)//写数据
{
while(busy_check());
RS=1;
RW=0;
P0=dat;
EN=1;
EN=0;
}
uchar code Xword[]={
0x18,0x18,0x07,0x08,0x08,0x08,0x07,0x00, //℃,代码 0x00
0x00,0x00,0x00,0x00,0xff,0x00,0x00,0x00, //一,代码 0x01
0x00,0x00,0x00,0x0e,0x00,0xff,0x00,0x00, //二,代码 0x02
0x00,0x00,0xff,0x00,0x0e,0x00,0xff,0x00, //三,代码 0x03
0x00,0x00,0xff,0xf5,0xfb,0xf1,0xff,0x00, //四,代码 0x04
0x00,0xfe,0x08,0xfe,0x0a,0x0a,0xff,0x00, //五,代码 0x05
0x00,0x04,0x00,0xff,0x00,0x0a,0x11,0x00, //六,代码 0x06
0x00,0x1f,0x11,0x1f,0x11,0x11,0x1f,0x00, //日,代码 0x07
};
void CgramWrite(void)
{ // 装入CGRAM //
uchar i;
LCD_write_command(0x06); // CGRAM地址自动加1
LCD_write_command(0x40); // CGRAM地址设为00处
for(i=0;i<64;i++)
{
LCD_write_data(Xword[i]);// 按数组写入数据
}
}
void LCD_position(uchar pos)//显示位置设定
{
LCD_write_command(0x80|pos);
}
void LCD_init()//初始化
{
delay(10);//等待电源稳定
LCD_write_command(0x38);
delay(1);
LCD_write_command(0x0f);
delay(1);
LCD_write_command(0x06);
delay(1);
CgramWrite();
LCD_write_command(0x01);
delay(1);
}
/*********************************************************
滚动显示
*********************************************************/
#include"1602.h"
uchar code table1[]="longdechuanren";
void main()
{
uchar i,j;
LCD_init();
while(1)
{
LCD_position(0x10);
for(i=0;i<14;i++)
{
LCD_write_data(table1[i]);
delay(50);
}
for(j=0;j<16;j++) //向左移动16格
{
LCD_write_command(0x18); //字符同时左移一格
delay(500); //控制移动时间
}
}
} |