基于51单片机的lcd1602滚动显示长英文代码
时间: 2023-07-28 14:07:05 浏览: 114
以下是基于51单片机和lcd1602显示器的滚动显示长英文代码的示例代码:
```c
#include<reg51.h>
#define LCD_data P2
#define LCD_com P0
sbit RS = P1^0;
sbit RW = P1^1;
sbit EN = P1^2;
void LCD_Init(void);
void LCD_Cmd_Write(unsigned char cmd);
void LCD_Data_Write(unsigned char dat);
void LCD_String_Write(unsigned char *str);
void delay_ms(unsigned int ms);
void LCD_Init(void)
{
LCD_Cmd_Write(0x38); // Function set: 8-bit, 2-line, 5x8 dots
LCD_Cmd_Write(0x0C); // Display ON; Cursor OFF
LCD_Cmd_Write(0x06); // Entry mode set: Increment, No shift
LCD_Cmd_Write(0x01); // Clear display and return to home
delay_ms(2); // Wait for LCD to complete command
}
void LCD_Cmd_Write(unsigned char cmd)
{
LCD_data = cmd; // Write data to data port
RS = 0; // RS low for command
RW = 0; // RW low for write
EN = 1; // EN high to enable
delay_ms(1); // Wait for LCD to complete command
EN = 0; // EN low to disable
}
void LCD_Data_Write(unsigned char dat)
{
LCD_data = dat; // Write data to data port
RS = 1; // RS high for data
RW = 0; // RW low for write
EN = 1; // EN high to enable
delay_ms(1); // Wait for LCD to complete command
EN = 0; // EN low to disable
}
void LCD_String_Write(unsigned char *str)
{
while(*str) // Loop through each character in string
{
LCD_Data_Write(*str++); // Write character to LCD
}
}
void delay_ms(unsigned int ms)
{
unsigned int i,j;
for(i=0;i<ms;i++)
{
for(j=0;j<123;j++);
}
}
void main()
{
unsigned char longStr[] = "This is a very long string that needs to be scrolled on the LCD display. ";
unsigned char i, j;
LCD_Init();
while(1)
{
for(i=0; i<sizeof(longStr)-15; i++) // Loop through each character in string
{
LCD_Cmd_Write(0x01); // Clear display and return to home
delay_ms(2); // Wait for LCD to complete command
for(j=i; j<i+16; j++) // Show 16 characters at a time
{
LCD_Data_Write(longStr[j]); // Write character to LCD
}
delay_ms(500); // Delay for scrolling effect
}
}
}
```
此代码将在lcd1602上滚动显示一条长字符串。在主循环中,该代码通过循环显示16个字符的子字符串来滚动字符串。您可以在此基础上进行修改,以便滚动任何文本。
阅读全文