求大神帮忙写一个用LCD1602液晶的程序
希望对你有帮助
#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit rs = P2^5;
sbit rw = P2^6;
sbit en = P2^7;
#define LCD P0
uchar s, m, h; //秒, 分, 时
uint a;
uchar code table[] = "I am dean";
uchar code my_table[] = { //自定义字符码
0x00,0x00,0x00,0x10,0x10,0x00,0x10,0x10, //:
0x04,0x0f,0x1f,0x0a,0x1f,0x02,0x02,0x02, //年
0x1f,0x11,0x1f,0x11,0x1f,0x11,0x11,0x11, //月
0x00,0x1f,0x11,0x11,0x1f,0x11,0x11,0x1f //日
};
void delay_ns(void); //延时函数
void delay(uint num);
void lcd_wcom(uchar com); //写命令
void lcd_wdat(uchar dat); //写数据
void lcd_init(void); //初始化LCD
void lcd_dispchar(uchar x, uchar y, uchar a); //LCD显示单个字符
void lcd_dispstring(uchar x, uchar y, uchar *q); //LCD显示字符串
void time1_init(void) //定时器初始化
{
TMOD = 0x10; //定时器1模式1
TH1 = 0xd8; //定时10MS
TL1 = 0x00;
EA = 1; //开总中断
ET1 = 1; //开定时器1
TR1 = 1; //启动定时器1
}
void main(void)
{
uchar i;
s = 50;
m = 5;
h = 10;
lcd_init();
time1_init();
lcd_wcom(0x40); //自定义的字符
for(i = 0; i < 32; i++)
{
lcd_wdat(my_table[i]);
}
lcd_dispstring(2, 1, "2013"); //显示2013年3月5日
lcd_dispchar(6, 1, 1);
lcd_dispstring(7, 1, "03");
lcd_dispchar(9, 1, 2);
lcd_dispstring(10, 1, "05");
lcd_dispchar(12, 1, 3);
while(1)
{
lcd_dispchar(10, 2, h / 10 + 0x30); //h/10 + 0x30为要显示的数字字符
lcd_dispchar(11, 2, h % 10 + 0x30); //显示小时
lcd_dispchar(12, 2, 0);
lcd_dispchar(13, 2, m / 10 + 0x30);
lcd_dispchar(14, 2, m % 10 + 0x30); //显示分钟
lcd_dispchar(15, 2, 0);
lcd_dispchar(16, 2, s / 10 + 0x30);
lcd_dispchar(17, 2, s % 10 + 0x30); //显示秒
}
while(1);
}
void delay_ns(void)
{
uchar i = 5;
while(i--);
}
void delay(uint num)
{
uchar i;
while(num--)
{
for(i = 0; i < 100; i++);
}
}
void lcd_wcom(uchar com)
{
rs = 0;
rw = 0;
LCD = com;
delay_ns();
en = 1;
en = 0;
delay(10);
}
void lcd_wdat(uchar dat)
{
rs = 1;
rw = 0;
LCD = dat;
delay_ns();
en = 1;
en = 0;
delay(10);
}
void lcd_init(void)
{
en = 0;
lcd_wcom(0x38);
lcd_wcom(0x0c);
lcd_wcom(0x06);
lcd_wcom(0x01);
}
void lcd_dispchar(uchar x, uchar y, uchar a)
{
if(y == 1)
x = 0x80 + x;
else if(y == 2)
x = 0xc0 + x;
else
return;
lcd_wcom(x);
lcd_wdat(a);
}
void lcd_dispstring(uchar x, uchar y, uchar *q)
{
if(y == 1)
x = 0x80 + x;
else if(y == 2)
x = 0xc0 + x;
else
return;
lcd_wcom(x);
while(*q != '\0')
{
lcd_wdat(*q);
q++;
}
}
void time1_isr(void) interrupt 3 using 1 //中断处理函数
{
TH1 = 0xd8;
TL1 = 0x00;
a++;
if(a == 60)
{
a = 0;
s++;
if(s == 60)
{
s = 0;
m++;
if(m == 60)
{
m = 0;
h++;
if(h == 24)
h = 0;
}
}
}
}
显像程序被是什么
就是用LCD1602 显示数字 0-9 A-F 自定义字符