//需要注意的地方说明
//1;在定义数组时,要用字符串定义,即字符数组
//2:lcd_delay(30)作用:字符一个一个出来的速度,纯粹为显数据,可以关闭这个延时.并且换行延时也可以关闭。显的更快
//特别注意:所有显示数据,或者打印数据。都遵循ASSIC码,显示时都必须转换成字符型。
#ifndef ___1602_H__
#define ___1602_H__
#include
sbit lcd_RS=P2^5; //定义寄存器
sbit lcd_RW=P2^6; //定义是否读写
sbit lcd_E=P2^7; //定义使能端
sbit lcd_flag=P0^7; //定义是否Busy标志
sbit pd=P1^0;
sbit pu=P1^1;
sbit spdd=P1^2;
sbit spdu=P1^3;
unsigned char code lcd_Assic[10]={0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39};
unsigned char dis[2][16]={{0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20},{0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20}};
//0x20默认输出是空,即空格
void lcd_deal(unsigned char i,unsigned char j,unsigned char dat)
{
dis[i][j]=lcd_Assic[dat];
}
bit lcd_busy() //判断是否忙碌 RS=Low,RW=High,E=High:读状态
{
bit flag=0;
lcd_RS=0; //寄存器为Low
lcd_RW=1; //是否读写为High
lcd_E=1; //使能端为High
if (lcd_flag==1) flag=1;
else flag=0;
lcd_E=0;
return flag; //返回标志,判断是否LCD忙碌
}
void lcd_putcommand(unsigned char command) //写指令 RS=Low,RW=Low,E=High:写指令
{
while (lcd_busy()); //判断是否忙碌
lcd_RS=0;
lcd_RW=0;
lcd_E=0;
P0=command;
lcd_E=1;
lcd_E=0;
}
void lcd_putchar(unsigned char putchar) //写数据 RS=High,RW=Low,E=High:写数据
{
while(lcd_busy()); //判断是否忙碌
lcd_RS=1;
lcd_RW=0;
lcd_E=0;
P0=putchar;
lcd_E=1;
lcd_E=0;
}
void lcd_moveto(unsigned char x,unsigned char y) //显示字符的位置:第x行y列(X=1或2,Y=0~15)
{
if (x==0) lcd_putcommand(0x80|y); //第一行,并初始化从第y-1个字符开始显示
if (x==1) lcd_putcommand(0xc0|y); //第二行,并............
}
void lcd_delay(unsigned int ms) //延时函数
{
unsigned char i;
while(ms--)
{
for(i=0;i<250;i++)
{
_nop_();_nop_();_nop_();_nop_();
}
}
}
void lcd_init(void)
{
lcd_putcommand(0x38); lcd_delay(1); //LCD为2行,5×7字
// cd_putcommand(0x0e); //打开显示,显示光标,光标不闪
// lcd_putcommand(0x0f); //....................光标闪烁 如果加后续程序,修改数据时,比较直观
lcd_putcommand(0x0c); //纯粹是为了显数据,关闭光标比较好
lcd_delay(1);
lcd_putcommand(0x06); lcd_delay(1);//每次右移一字符
}
void _1602(void)
{
unsigned char *p;
lcd_init(); //初始化
lcd_delay(10);
//while(1)
//{
lcd_moveto(0,0);
for(p=dis[0];p
lcd_putchar(*p);
//lcd_delay(30); //作用:字符一个一个出来的速度
}
lcd_moveto(1,0);
for(p=dis[1];p
lcd_putchar(*p);
//lcd_delay(30);
}
//lcd_delay(600); //隔行显示的间隔时间
// }
}
这是个1602的头文件,加上MAIN就可以了,至于接线看看程序就 知道了。你的程序根本不对,去掉键盘也 不行
程序写的有很大问题,你的键盘只扫描过一次,不可能能用;液晶也只扫描一次,根本不行!