通过 I2C 驱动 LCD1602 液晶屏(51单片机)

通过 I2C 驱动 LCD1602 液晶屏(51单片机)

  1. 硬件实物

正面
在这里插入图片描述

  1. 原理图

在这里插入图片描述
3.程序

#include <reg51.h>
#include <intrins.h>

#define uint	unsigned int
#define uchar	unsigned char
#define	C51_SCL		P3^0			//SCL引脚
#define	C51_SDA		P3^1			//SDA引脚
#define ADDR		0X4E			//设备地址
#define String_len1		10
#define String_len2		8

sbit SCL = C51_SCL;
sbit SDA = C51_SDA;
uchar lcd_show1[String_len1] = "Hello Word";
uchar lcd_show2[String_len2] = "fuxiujun";

static void delay_us()
{
	;;  //用两个空语句实现短时间延时,当晶振为11.0592MHz时,约4~5微秒
}
void delay(uchar n)
{
	int i,j;
	for(i=0;i<n;i++)
		for(j=0;j<120;j++);
}

/**
 * @brief	IIC协议的起始信号
 * @param	None
 * @retval	None
 */
void IIC_Start()  
{
	SDA=1;
	SCL=1;
	delay_us();
	SDA=0;
	delay_us();
}

/**
 * @brief	IIC协议的应答信号
 * @param	None
 * @retval	None
 */
void IIC_Ack()  
{
	uchar i;
	SCL=1;
	delay_us();
	while((SDA==1)&&(i<250))i++;
	SCL=0;
	delay_us();
}

/**
 * @brief	写入一个字节到I2C总线
 * @param	date:将要被写入的数据
 * @retval	None
 */
void IIC_Write_Byte(uchar date) 
{
	uchar i,temp;
	temp=date;
	for(i=0;i<8;i++)
	{
		temp=temp<<1;
		SCL=0;
		delay_us();
		SDA=CY;
		delay_us();
		SCL=1;
		delay_us();
	}
	SCL=0;
	delay_us();
	SDA=1;
	delay_us();
}

/**
 * @brief	通过IIC协议写一个命令到1602液晶上
 * @param	comm:将要被写入的命令
 * @retval	None
 */
void IIC_Write_Comm_LCD(uchar comm)
{
	uchar data_h = comm & 0xf0;
	uchar data_l = (comm & 0x0f) << 4;
	IIC_Write_Byte(0x00+data_h);
	IIC_Ack();
	IIC_Write_Byte(0x04+data_h);
	IIC_Ack();
	IIC_Write_Byte(0x00+data_h);
	IIC_Ack();
	delay(5);
	IIC_Write_Byte(0x00+data_l);
	IIC_Ack();
	IIC_Write_Byte(0x04+data_l);
	IIC_Ack();
	IIC_Write_Byte(0x00+data_l);
	IIC_Ack();
	delay(5);
}

/**
 * @brief	通过IIC写一个数据到1602液晶上
 * @param	date:将要被写入的数据
 * @retval	None
 */
void IIC_Write_Date_LCD(uchar date)
{
	uchar data_h = date & 0xf0;
	uchar data_l = (date & 0x0f) << 4;
	IIC_Write_Byte(0x01+data_h);
	IIC_Ack();
	IIC_Write_Byte(0x05+data_h);
	IIC_Ack();
	IIC_Write_Byte(0x01+data_h);
	IIC_Ack();
	delay(5);
	IIC_Write_Byte(0x01+data_l);
	IIC_Ack();
	IIC_Write_Byte(0x05+data_l);
	IIC_Ack();
	IIC_Write_Byte(0x01+data_l);
	IIC_Ack();
	delay(5);	
}

/**
 * @brief	初始化1602液晶,由于只有一个设备,所以一直占用总线
 * @param	None
 * @retval	None
 */
void LCD1602_Init()
{
	uchar i = 0;
	IIC_Start();
	IIC_Write_Byte(ADDR);
	IIC_Ack();
	IIC_Write_Comm_LCD(0x02);	//设置四线发送数据
	IIC_Write_Comm_LCD(0x28);	//设置显示模式
	IIC_Write_Comm_LCD(0x08);	//显示关闭
	IIC_Write_Comm_LCD(0x0c);	//设置光标开关
	IIC_Write_Comm_LCD(0x06);	//设置光标移动
	IIC_Write_Comm_LCD(0x01);	//清屏
	for(i=0;i<String_len1;i++)
	{
		IIC_Write_Date_LCD(lcd_show1[i]);
	}
	IIC_Write_Comm_LCD(0xc0);
	for(i=0;i<String_len2;i++)
	{
		IIC_Write_Date_LCD(lcd_show2[i]);
	}
}
void main()
{
	LCD1602_Init();
	while(1);
}
/*******************************************************
uchar IIC_Read_Byte() //从I2C读一个字节
{
	uchar i,j,k;
	SCL=0;
	delay_us();
	for(i=0;i<8;i++)
	{
		SCL=1;
		delay_us();
		j=SDA;
		k=(k<<1)|j;
		SCL=0;
		delay_us();
	}
	return k;
}
void nIIC_Ack()  //无应答信号
{
	SCL=1;
	delay_us();
	SDA=1;
	SCL=0;
	delay_us();
}
void IIC_Stop()  //终止信号
{
	SDA=0;
	SCL=1;
	delay_us();
	SDA=1;
	delay_us();
}
***********************************************************************/

4.仿真
在这里插入图片描述

  • 16
    点赞
  • 102
    收藏
    觉得还不错? 一键收藏
  • 11
    评论
好的,我可以给你一些思路。首先你需要连接好51单片机LCD1602液晶显示屏,然后在单片机中编写程序。以下是一个简单的示例程序,可以让LCD1602显示一行的英文字符缩写和学生学号: ``` #include <reg52.h> #define LCD_DB P0 sbit RS = P2^0; // RS引脚 sbit RW = P2^1; // RW引脚 sbit EN = P2^2; // EN引脚 void delay(unsigned int t) { while(t--); } void write_cmd(unsigned char cmd) { RS = 0; // 告诉LCD这是指令 RW = 0; // 告诉LCD这是写入 LCD_DB = cmd; // 发送指令 EN = 1; // 使能 delay(10); EN = 0; // 关闭使能 } void write_data(unsigned char dat) { RS = 1; // 告诉LCD这是数据 RW = 0; // 告诉LCD这是写入 LCD_DB = dat; // 发送数据 EN = 1; // 使能 delay(10); EN = 0; // 关闭使能 } void init_lcd() { write_cmd(0x38); // 8位,2行,5x7点阵 write_cmd(0x0c); // 开启显示,关闭光标 write_cmd(0x06); // 右移光标 write_cmd(0x01); // 清屏 } void display_string(unsigned char x, unsigned char y, unsigned char *str) { unsigned char i; if(x < 16) { if(y == 0) { write_cmd(0x80 + x); // 第一行 } else { write_cmd(0xc0 + x); // 第二行 } } for(i = 0; str[i] != '\0'; i++) { write_data(str[i]); // 逐个发送字符 } } void main() { unsigned char str[] = "CSDN 123456"; // 要显示的字符串 init_lcd(); display_string(0, 0, str); // 在第一行显示 while(1); } ``` 在这个程序中,我们使用了51单片机的P0口作为LCD1602的数据口,P2.0、P2.1、P2.2分别作为RS、RW、EN引脚。程序中的init_lcd()函数用于初始化LCD1602,display_string()函数用于在LCD1602上显示字符串。 你可以根据需要修改display_string()函数,来显示你想要的英文字符缩写和学生学号。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值