51单片机控制LCD1602显示(含电路图,代码)

源代码如下:

#include<reg51.h>
#include<intrins.h>
#define out P0
sbit RS=P2^0;
sbit RW=P2^1;
sbit E =P2^2;          //定义控制引脚

//延时函数
void delay(unsigned long n)
{
	while(n)
	{
		_nop_();
		n--;
	}
}

//检查当前1602是否处于忙状态
void LCDcheckbusy(void)
{
	unsigned char dt;
	do
	{
		dt=0xff;
		E=0;
		RS=0;
		RW=1;
		E=1;
		dt=out;
	}while(dt&0x80);
	E=0;
}

//LCD写命令函数
void LCDwritecmd(unsigned char com)
{
	LCDcheckbusy();
	E=0;
	RS=0;
	RW=0;
	out=com;
	E=1;
	_nop_();
	E=0;
	delay(1);
}

//LCD显示函数
void LCDwritedata(unsigned char dat)
{
	LCDcheckbusy();
	E=0;
	RS=1;
	RW=0;
	out=dat;
	E=1;
	_nop_();
	E=0;
	delay(1);
}

void LCDstr(char *str)
{
	while(*str)
	{
		LCDwritedata(*str);
		str++;
	}
}

//LCD初始化函数
void LCDinitial(void)
{
	LCDwritecmd(0x38);
	LCDwritecmd(0x0c);
	LCDwritecmd(0x06);
	LCDwritecmd(0x01);
	delay(1);
}

//主函数,1602显示单个字符和字符串
void main()
{
	unsigned char *str="hello world!";	  //定义要显示的字符串
	unsigned char code temp[]="hello world!";	  //定义要显示的字符串

	LCDinitial();

	LCDwritecmd(0x80);     //在第一行显示
	LCDwritedata('1');     //显示单个字符     

	LCDwritecmd(0x80+0x40);      //在第二行显示

//	LCDstr("hello world!");

//	LCDstr(str);

	LCDstr(temp);

	while(1);
}




电路图及运行结果如下:
在这里插入图片描述

  • 2
    点赞
  • 39
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是利用单片机C51的LCD1602设计一个时钟的流程和示例代码: 1. 准备硬件:LCD1602显示屏、DS1302时钟模块、单片机C51芯片、杜邦线若干、电容若干、晶振一个、面包板一个。 2. 连接硬件:将DS1302模块的VCC、GND、SCLK、DATA、RST分别连接到单片机C51的5V、GND、P1.0、P1.1、P1.2引脚上;将LCD1602的VSS、VDD、V0、RS、RW、E、D0-D7分别连接到单片机C51的GND、5V、电容、P2.0、P2.1、P2.2、P0口引脚上。 3. 编写代码:利用Keil C51编写程序,通过调用DS1302模块的函数获取当前时间,并将时间信息显示LCD1602上。 4. 测试程序:将代码编译并下载到单片机C51芯片上,连接电源,观察LCD1602是否正常显示当前时间。 以下是示例代码,您可以根据自己的需要进行修改和优化: ```C #include <reg52.h> #include <intrins.h> #define uchar unsigned char #define uint unsigned int sbit RS=P2^0; sbit RW=P2^1; sbit E=P2^2; sbit DSIO=P1^1; sbit SCLK=P1^0; sbit RST=P1^2; uchar code table[]={'0','1','2','3','4','5','6','7','8','9'}; uchar date[8]={0}; uchar time[8]={0}; uchar seconds,minutes,hours,day,month,year,week; void delay(uint z) { uint x,y; for(x=z;x>0;x--) for(y=110;y>0;y--); } void write_com(uchar com) { RS=0; RW=0; P0=com; delay(1); E=1; delay(1); E=0; } void write_data(uchar date) { RS=1; RW=0; P0=date; delay(1); E=1; delay(1); E=0; } void init_LCD1602() { write_com(0x38); delay(5); write_com(0x0c); delay(5); write_com(0x06); delay(5); write_com(0x01); delay(5); } void write_string(uchar x,uchar y,uchar *p) { uchar i=0; if(x<16) { if(y==0) { write_com(0x80+x); } else { write_com(0x80+0x40+x); } } while(p[i]!='\0') { write_data(p[i++]); } } void write_date(uchar *p) { uchar i=0; write_com(0x80+0x40+3); while(p[i]!='\0') { write_data(p[i++]); } } void read_time() { uchar i; RST=1; _nop_(); _nop_(); _nop_(); _nop_(); RST=0; _nop_(); _nop_(); _nop_(); _nop_(); DSIO=1; for(i=0;i<8;i++) { SCLK=0; _nop_(); _nop_(); _nop_(); _nop_(); if(i==7) { DSIO=0; } else { DSIO=seconds&0x01; seconds>>=1; } SCLK=1; _nop_(); _nop_(); _nop_(); _nop_(); } for(i=0;i<8;i++) { SCLK=0; _nop_(); _nop_(); _nop_(); _nop_(); if(i==7) { DSIO=0; } else { DSIO=minutes&0x01; minutes>>=1; } SCLK=1; _nop_(); _nop_(); _nop_(); _nop_(); } for(i=0;i<8;i++) { SCLK=0; _nop_(); _nop_(); _nop_(); _nop_(); if(i==7) { DSIO=0; } else { DSIO=hours&0x01; hours>>=1; } SCLK=1; _nop_(); _nop_(); _nop_(); _nop_(); } for(i=0;i<8;i++) { SCLK=0; _nop_(); _nop_(); _nop_(); _nop_(); if(i==7) { DSIO=0; } else { DSIO=day&0x01; day>>=1; } SCLK=1; _nop_(); _nop_(); _nop_(); _nop_(); } for(i=0;i<8;i++) { SCLK=0; _nop_(); _nop_(); _nop_(); _nop_(); if(i==7) { DSIO=0; } else { DSIO=month&0x01; month>>=1; } SCLK=1; _nop_(); _nop_(); _nop_(); _nop_(); } for(i=0;i<8;i++) { SCLK=0; _nop_(); _nop_(); _nop_(); _nop_(); if(i==7) { DSIO=0; } else { DSIO=(year&0x01); year>>=1; } SCLK=1; _nop_(); _nop_(); _nop_(); _nop_(); } for(i=0;i<8;i++) { SCLK=0; _nop_(); _nop_(); _nop_(); _nop_(); if(i==7) { DSIO=0; } else { DSIO=(week&0x01); week>>=1; } SCLK=1; _nop_(); _nop_(); _nop_(); _nop_(); } RST=1; } void main() { uchar i; init_LCD1602(); while(1) { read_time(); time[0]=table[hours/10]; time[1]=table[hours%10]; time[2]=':'; time[3]=table[minutes/10]; time[4]=table[minutes%10]; time[5]=':'; time[6]=table[seconds/10]; time[7]=table[seconds%10]; write_string(0,0,time); date[0]='2'; date[1]='0'; date[2]=table[year/10]; date[3]=table[year%10]; date[4]='-'; date[5]=table[month/10]; date[6]=table[month%10]; date[7]='\0'; write_date(date); for(i=0;i<100;i++) { delay(10000); } } } ``` 希望这个流程和示例代码对您有所帮助,如果您有其他问题,请随时问我。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值