52单片机lcd1602上下滚动
时间: 2023-06-21 07:05:40 浏览: 171
下面是51单片机控制LCD1602上下滚动的示例代码:
```
#include<reg52.h>
#include<intrins.h>
typedef unsigned char uint8;
typedef unsigned int uint16;
#define LCD1602_DB P0
sbit LCD1602_RS = P2^6; //定义LCD1602的RS引脚
sbit LCD1602_RW = P2^5; //定义LCD1602的RW引脚
sbit LCD1602_E = P2^7; //定义LCD1602的E引脚
void delay(uint16 n)
{
while(n--);
}
void writeCommand(uint8 command) //写指令函数
{
LCD1602_RS = 0;
LCD1602_RW = 0;
LCD1602_DB = command;
LCD1602_E = 1;
_nop_();
_nop_();
LCD1602_E = 0;
delay(5);
}
void writeData(uint8 dat) //写数据函数
{
LCD1602_RS = 1;
LCD1602_RW = 0;
LCD1602_DB = dat;
LCD1602_E = 1;
_nop_();
_nop_();
LCD1602_E = 0;
delay(5);
}
void initLCD1602() //初始化函数
{
writeCommand(0x38); //设置数据位数 8 位,显示行数 2 行,字符大小 5x8
writeCommand(0x0C); //开显示,关光标,不闪烁
writeCommand(0x06); //写入字符后地址自动加 1,不移动屏幕
writeCommand(0x01); //清屏
}
void displayString(uint8 x,uint8 y,uint8 *str) //显示字符串函数
{
uint8 i = 0;
if(x < 16)
{
writeCommand(0x80 + y*0x40 + x); //设置显示位置
while(str[i] != '\0')
{
writeData(str[i]); //写入数据
i++;
}
}
}
void scrollDisplay(uint8 x,uint8 y,uint8 *str) //滚动显示函数
{
uint8 i,j,k,len;
len = strlen(str);
if(len < 16)
{
displayString(x,y,str);
}
else
{
for(i=0;i<len;i++)
{
displayString(x,y,str+i);
delay(50000);
writeCommand(0x01); //清屏
}
}
}
void main()
{
uint8 str[] = "hello world! This is a scrolling text!";
initLCD1602(); //初始化LCD1602
scrollDisplay(0,0,str); //显示滚动文字
while(1);
}
```
注:以上代码为51单片机控制LCD1602上下滚动的示例代码,52单片机也可参考该代码实现。