51单片机lcd1602清屏代码
时间: 2023-12-25 18:05:07 浏览: 209
以下是51单片机清屏LCD1602的代码示例:
```c
#include <reg52.h>
#define LCD_RS P2_0
#define LCD_RW P2_1
#define LCD_EN P2_2
void delay(unsigned int t) {
while(t--);
}
void lcd_write_cmd(unsigned char cmd) {
LCD_RS = 0; // 设置为命令模式
LCD_RW = 0; // 设置为写模式
LCD_EN = 1; // 使能LCD
P0 = cmd; // 发送命令
delay(100); // 延时一段时间
LCD_EN = 0; // 关闭使能
}
void lcd_clear() {
lcd_write_cmd(0x01); // 发送清屏命令
}
void main() {
lcd_clear(); // 清屏
while(1);
}
```
相关问题
51单片机LCD1602时钟代码
以下是51单片机LCD1602时钟代码,可以通过串口调试助手进行调试:
```c
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
sbit RS = P2^6; //RS引脚定义
sbit RW = P2^5; //RW引脚定义
sbit EN = P2^7; //EN引脚定义
void delay(uint i) //延时函数
{
while(i--);
}
void write_com(uchar com) //写入命令函数
{
RS = 0;
RW = 0;
P0 = com;
EN = 1;
delay(10);
EN = 0;
}
void write_data(uchar dat) //写入数据函数
{
RS = 1;
RW = 0;
P0 = dat;
EN = 1;
delay(10);
EN = 0;
}
void init() //初始化函数
{
write_com(0x38); //16*2显示,5*7点阵,8位数据接口
write_com(0x0c); //显示开,不显示光标
write_com(0x06); //光标右移,字符不移动
write_com(0x01); //清屏
}
void display(uchar hour, uchar minute, uchar second) //显示函数
{
uchar i;
uchar time[8] = {'2','0','1','9','-',0,0,' '}; //时间格式:2019-00-00
time[5] = hour/10+0x30; //显示小时
time[6] = hour%10+0x30;
write_com(0x80); //设置光标位置
for(i=0;i<8;i++)
{
write_data(time[i]); //显示时间
}
write_com(0x8d); //设置光标位置
write_data(':'); //显示冒号
write_data(minute/10+0x30); //显示分钟
write_data(minute%10+0x30);
write_data(':'); //显示冒号
write_data(second/10+0x30); //显示秒数
write_data(second%10+0x30);
}
void main() //主函数
{
uchar hour = 0; //小时
uchar minute = 0; //分钟
uchar second = 0; //秒数
init(); //初始化
while(1)
{
display(hour,minute,second); //显示时间
second++; //秒数加一
if(second==60) //如果秒数等于60
{
second=0; //秒数清零
minute++; //分钟加一
if(minute==60) //如果分钟等于60
{
minute=0; //分钟清零
hour++; //小时加一
if(hour==24) //如果小时等于24
{
hour=0; //小时清零
}
}
}
delay(1000); //延时1秒
}
}
```
51单片机lcd1602垂直滚动代码
以下是51单片机LCD1602垂直滚动代码:
```c
#include <reg51.h>
#include <intrins.h>
#define LCD1602_DB P0
sbit LCD1602_RS = P2^6;
sbit LCD1602_RW = P2^5;
sbit LCD1602_EN = P2^7;
void delay(unsigned int n)
{
unsigned int i;
while (n--)
{
for (i = 0; i < 125; ++i)
;
}
}
void lcd1602_write_cmd(unsigned char cmd)
{
LCD1602_RS = 0; // 选择指令寄存器
LCD1602_RW = 0; // 写操作
LCD1602_EN = 1;
LCD1602_DB = cmd;
_nop_();
_nop_();
LCD1602_EN = 0;
delay(5);
}
void lcd1602_write_data(unsigned char dat)
{
LCD1602_RS = 1; // 选择数据寄存器
LCD1602_RW = 0; // 写操作
LCD1602_EN = 1;
LCD1602_DB = dat;
_nop_();
_nop_();
LCD1602_EN = 0;
delay(5);
}
void lcd1602_init()
{
lcd1602_write_cmd(0x38); // 8位数据总线,2行显示,5x8点阵字符
lcd1602_write_cmd(0x0c); // 开显示,无光标,不闪烁
lcd1602_write_cmd(0x06); // 光标右移,字符不移动
lcd1602_write_cmd(0x01); // 清屏
}
void lcd1602_display_string(unsigned char x, unsigned char y, unsigned char *str)
{
unsigned char i;
if (x < 16)
{
if (y == 0)
{
lcd1602_write_cmd(0x80 + x); // 第一行
}
else
{
lcd1602_write_cmd(0x80 + 0x40 + x); // 第二行
}
}
while (*str != '\0')
{
lcd1602_write_data(*str++);
}
}
void lcd1602_scroll_string(unsigned char x, unsigned char y, unsigned char *str)
{
unsigned char i, j;
while (*str != '\0')
{
if (x < 16)
{
if (y == 0)
{
lcd1602_write_cmd(0x80 + x); // 第一行
}
else
{
lcd1602_write_cmd(0x80 + 0x40 + x); // 第二行
}
}
for (i = 0; i < 16 && *str != '\0'; ++i)
{
lcd1602_write_data(*str++);
}
if (*str != '\0')
{
for (j = 0; j < 16; ++j)
{
lcd1602_write_cmd(0x18); // 屏幕向上滚动
delay(100);
}
}
}
}
void main()
{
unsigned char str[] = "Hello, World! This is a scroll demo.";
lcd1602_init();
lcd1602_scroll_string(0, 0, str);
while (1)
;
}
```
这个代码使用了51单片机控制LCD1602显示屏,并实现了垂直滚动字幕的功能。在主函数中,调用了`lcd1602_scroll_string`函数来滚动字符串。该函数会先显示屏上显示一行,然后将第二行向上滚动,并在第二行显示下一个字符串。当第二行滚到底部时,将屏幕向上滚动一行,以此类推。