51单片机LCD1602两行移动显示

实现51单片机LCD1602两行移动显示

第一行:My name is Eagal

第二行:Music film Games

#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit lcden=P3^4;
sbit rs=P3^5;
sbit rw=P3^6;	
sbit dula=P2^6;
sbit wela=P2^7;
uchar table1[]="My name is Eagal";
uchar table2[]=" Music Flim Games ";
void delay(uint x)
{
	uint a,b;
	for(a=x;a>0;a--)
		for(b=10;b>0;b--); 
}

void write_com(uchar com)
{
	P0=com;
	rs=0;
	lcden=0;
	delay(10);
	lcden=1;
	delay(10);
	lcden=0;
	
}

void write_date(uchar date)
{
	P0=date;
	rs=1;
	lcden=0;
	delay(10);
	lcden=1;
	delay(10);
	lcden=0;
	
}

void init()
{
    rw=0;
	dula=0;
	wela=0;
	write_com(0x38);   //显示模式设置:16×2显示,5×7点阵,8位数据接口
	delay(20);
	write_com(0x0c);   //显示模式设置
	delay(20);
	write_com(0x06);   //显示模式设置:无光标
	delay(20);
	write_com(0x01);   //清屏幕指令,将以前的显示内容清除
	delay(20);	
}

void main()
{	uint k=1000;
	uchar *pa,*pb;
	init();

	write_com(0x80 | 0x0f);	 //将第一个字符写在向右偏移17个字符处,为后面的由右向左划入做准备。
	delay(20);
		pa = table1;
		while(*pa != '\0')
		{
			write_date(*pa++);
			while(k--);
		}

	write_com(0x07); 
	write_com(0x80 | 0x4f);
//	delay(50);
	pb = table2;
		while(*pb != '\0')
		{
			write_date(*pb++);
			while(k--);
		}	
	//while(1);
}

  • 6
    点赞
  • 41
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是51单片机使用LCD1602显示器实现两行显示的程序: ```c #include <reg52.h> #include <stdio.h> #define LCD1602_DATAPORT P0 sbit LCD1602_RS = P2^0; // RS引脚 sbit LCD1602_RW = P2^1; // RW引脚 sbit LCD1602_EN = P2^2; // EN引脚 void delay(unsigned int t) { while(t--); } void LCD1602_WriteCmd(unsigned char cmd) { LCD1602_RS = 0; LCD1602_RW = 0; LCD1602_DATAPORT = cmd; LCD1602_EN = 1; delay(100); LCD1602_EN = 0; } void LCD1602_WriteData(unsigned char dat) { LCD1602_RS = 1; LCD1602_RW = 0; LCD1602_DATAPORT = dat; LCD1602_EN = 1; delay(100); LCD1602_EN = 0; } void LCD1602_Init() { LCD1602_WriteCmd(0x38); // 8位数据线,2行显示,5x7点阵字符 LCD1602_WriteCmd(0x0c); // 显示开,无光标,不闪烁 LCD1602_WriteCmd(0x06); // 地址自动加1,不移动屏幕 LCD1602_WriteCmd(0x01); // 清屏 } void LCD1602_SetCursor(unsigned char x, unsigned char y) { unsigned char addr; if (y == 0) // 第一行 addr = 0x80 + x; else // 第二行 addr = 0xc0 + x; LCD1602_WriteCmd(addr); } void LCD1602_Print(const char *str) { while(*str != '\0') { LCD1602_WriteData(*str++); } } void main() { LCD1602_Init(); // 初始化 LCD1602_SetCursor(0, 0); // 设置光标位置 LCD1602_Print("Hello,"); // 输出第一行 LCD1602_SetCursor(0, 1); // 设置光标位置 LCD1602_Print("world!"); // 输出第二行 while(1); } ``` 程序中通过 `LCD1602_WriteCmd()` 函数来向LCD1602发送命令,通过 `LCD1602_WriteData()` 函数来向LCD1602发送数据。同时,还实现了一些辅助函数,如 `LCD1602_Init()` 用于初始化LCD1602, `LCD1602_SetCursor()` 用于设置光标位置, `LCD1602_Print()` 用于向LCD1602输出字符串。在主函数中,先进行了LCD1602的初始化,然后设置光标位置和输出两行字符串。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值