{
LCD_1602_EN=0
LCD_1602_RW=0 //这句话你忘写了,液晶数据也不进去的。。
write_1602_com(0x38) //设置16X2显示,5X7点阵,8位数据接口
write_1602_com(0x0e) //设置开显示,不显示光标
write_1602_com(0x06) //写一个字符后地址指针加1
write_1602_com(0x01) //显示清0,数据指针清0
write_1602_com(0x80) //设置初始显示为第一行第一列
}
这是为你量身制作的,希望你能喜欢,光标根据按键在数字下面闪烁,一定符合你的要求,给你截了一张效果图
#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
uchar code table[]="1602"
sbit key1=P1^0
sbit key2=P1^1
sbit key3=P1^2
sbit key4=P1^3
sbit rs=P2^0
sbit rw=P2^1
sbit en=P2^2
void delay(uint z)
{
uint x,y
for(x=zx>0x--)
for(y=110y>0y--)
}
void write_com(uchar com)
{
rs=0
rw=0
en=0
P0=com
delay(5)
en=1
delay(5)
en=0
}
void write_dat(uchar dat)
{
rs=1
rw=0
en=0
P0=dat
delay(5)
en=1
delay(5)
en=0
}
void init()
{
uchar i
write_com(0x38)
write_com(0x0c)
write_com(0x06)
write_com(0x01)
write_com(0x86)
for(i=0i<4i++)
{
write_dat(table[i])
delay(5)
}
}
void main()
{
init()
while(1)
{
if(key1==0)
{
delay(10)
if(key1==0)
{
while(key1==0)
write_com(0x86)
write_com(0x0f)
}
}
if(key2==0)
{
delay(10)
if(key2==0)
{
while(key2==0)
write_com(0x87)
write_com(0x0f)
}
}
if(key3==0)
{
delay(10)
if(key3==0)
{
while(key3==0)
write_com(0x88)
write_com(0x0f)
}
}
if(key4==0)
{
delay(10)
if(key4==0)
{
while(key4==0)
write_com(0x89)
write_com(0x0f)
}
}
}
}
给你推荐一本书《MCS51单片机应用设计》哈尔滨工业大学出版社,里面很详细的#include "reg52.h"
#include "intrins.h"
unsigned char text1[] = "TTT TTTT add:qhd"
unsigned char text2[] = "TEL:13333333333 "
sbit RS = P2^0
sbit RW = P2^1
sbit EN = P1^2
void delay_ms(unsigned char t)
void init_1602(void)
void write_command(unsigned char cmd)
void write_data(unsigned char dat)
void dsp_onechar(unsigned char pos,unsigned char c)
void dsp_string(unsigned char line,unsigned char *p,unsigned char length)
void main(void)
{
init_1602()
delay_ms(15)
dsp_string(0,text1,16)
dsp_string(1,text2,16)
while(1)
}
void delay_ms(unsigned char t)//毫秒延时大约延时1ms
{
unsigned char i
while(t--)
{
for(i=0i<112i++)
}
}
void write_command(unsigned char cmd)//写指令函数(按规定RS=0,RW=0为写指令)
{
delay_ms(5)// check busy flag
EN = 0
RS = 0
RW = 0
_nop_()
EN = 1
P0 = cmd
EN = 0
}
void write_data(unsigned char dat)//往1602写数据
{
delay_ms(5)//busy flag check
EN = 0
RS = 1
RW = 0
_nop_()
EN = 1
P0 = dat
EN = 0
RS = 0
}
void dsp_onechar(unsigned char pos,unsigned char c)//写一个字节函数
{
unsigned char p
if (pos>=0x10)
p=pos+0xb0
else
p=pos+0x80
write_command(p)
write_data(c)
}
void dsp_string(unsigned char line,unsigned char *p,unsigned char length)
{
unsigned char l,i
l=line<<4
for (i=0i<lengthi++)
dsp_onechar(l++,*(p+i))
}
void init_1602(void)//1602初始化
{
delay_ms(15)
write_command(0x38)
write_command(0x38)
write_command(0x38)
write_command(0x06)
write_command(0x0c)
write_command(0x01)
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)