51 microcontroller controls 1602LCD character scrolling three

51 microcontroller controls 1602LCD character scrolling three

1 Overview

This article introduces the microcontroller to control the scrolling display of characters on the 1602LCD screen

2.Character scrolling

2.1. The first way

Use the built-in LCD command to set the entire screen to move left or right. When reading characters, there must be a delay for each character read. Otherwise, because the speed is too fast, there will be no characters on the screen.

#define LCD_DIS_MODE_LEFT 0x07 	// AC自增,画面左移
#define LCD_DIS_MODE_RIGHT 0X05	// AC自增,画面右移
Set character left shift core code
void main(){
    
    
	unsigned char str1[] = "Beyound Self Beyound Time Beyound Sky";
	unsigned char i;
	LCD1602_Init();
	// 1.设置整屏幕左移
	LCD1602_WriteCMD(LCD_DIS_MODE_LEFT);
	
	while(1){
    
    
		//设置字符显示起始位置
		LCD1602_WriteCMD(0x80);
		//读取字符,注意每读取一个字符时都要有个延迟时间。
		for(i=0;i<strlen(str1);i++){
    
    
			LCD1602_WriteData(str1[i]);
			DELAY_MS(500);
		}
		
	}
	
}
Character movement complete code
#include <STC12C2052AD.H>
#include <string.h> 
typedef unsigned char uint8;
// 定义引脚
#define	LCD1602_DB0_DB7	P1			// 定义LCD1602的数据总线
sbit LCD1602_RS = P3 ^ 2;					// 定义LCD1602的RS控制线
sbit LCD1602_RW = P3 ^ 3;					// 定义LCD1602的RW控制线
sbit LCD1602_E  = P3 ^ 4;					// 定义LCD1602的E控制线
sbit LCD1602_Busy = P1 ^ 7;					// 定义LCD1602的测忙线(与LCD1602_DB0_DB7关联)


// 定义指令集
/*设置显示模式*/
#define LCD_MODE_PIN8 0x38	// 8位数据线,两行显示
#define LCD_MODE_PIN4 0x28	// 4位数据线,两个显示
#define LCD_SCREEN_CLR 0x01	// 清屏
#define LCD_CURSOR_RET 0x02	// 光标复位
#define LCD_CURSOR_RIGHT 0x06	// 光标右移,显示不移动
#define LCD_CURSOR_LEFT 0x04	// 光标左移,显示不移动
#define LCD_DIS_MODE_LEFT 0x07 	// AC自增,画面左移
#define LCD_DIS_MODE_RIGHT 0X05	// AC自增,画面右移


/*光标开关控制*/
#define LCD_DIS_CUR_BLK_ON 0x0f	// 显示开,光标开,光标闪烁
#define LCD_DIS_CUR_ON 0x0e	// 显示开,光标开,光标不闪烁
#define LCD_DIS_ON 0x0c	// 显示开,光标关,光标不闪烁
#define LCD_DIS_OFF 0x08	// 显示关,光标关,光标不闪烁

/*光标、显示移动*/
#define LCD_CUR_MOVE_LEFT 0x10	// 光标左移
#define LCD_CUR_MOVE_RIGHT 0x14	// 光标右移
#define LCD_DIS_MOVE_LEFT 0x18	// 显示左移
#define LCD_DIS_MOVE_RIGHT 0x1c	// 显示右移


/**
LCD1602忙碌状态不会接收新指令,因此在发送新指令前先检测是否忙碌。
判断LCD1602_Busy变量的值为低电平则为不忙。
*/
void LCD1602_TestBusy(void){
    
    
	LCD1602_DB0_DB7 = 0xff;	//将数据引脚置为高电平
	LCD1602_RS = 0; // 指令状态
	LCD1602_RW = 1;	// 读状态
	LCD1602_E = 1;	// 打开LCD显示器读指令
	while(LCD1602_Busy);	//读取LCD1602_Busy(P1.7)为低电平则结束循环
	LCD1602_E = 0;	// 关闭LCD显示器读指令
}


/********************************************************************************************
// 写指令程序 //
// 向LCD1602写命令 本函数需要1个指令集的入口参数 //
/********************************************************************************************/
void LCD1602_WriteCMD(uint8 LCD1602_command) {
    
     
	LCD1602_TestBusy();
	//输入的命令赋值给LCD1602_DB0_DB7
	LCD1602_DB0_DB7 = LCD1602_command;
	LCD1602_RS = 0;
	LCD1602_RW = 0;
	LCD1602_E = 1;
	LCD1602_E = 0;
}
/********************************************************************************************
// 写数据程序 //
// 向LCD1602写数据 //
/********************************************************************************************/
void LCD1602_WriteData(uint8 LCD1602_data){
    
     
	LCD1602_TestBusy();
	LCD1602_DB0_DB7 = LCD1602_data;
	LCD1602_RS = 1;
	LCD1602_RW = 0;
	LCD1602_E = 1;
	LCD1602_E = 0;
}


// LCD1602初始化
void LCD1602_Init(void){
    
    
	LCD1602_WriteCMD(LCD_MODE_PIN8);	// 显示模式设置:显示2行,每个字符为5*7个像素
	LCD1602_WriteCMD(LCD_DIS_ON); 	// 显示开及光标设置:显示开,光标关
	LCD1602_WriteCMD(LCD_CURSOR_RIGHT);		//显示光标移动设置:文字不动,光标右移
	LCD1602_WriteCMD(LCD_SCREEN_CLR);	// 显示清屏
}

void DELAY_MS (unsigned int a){
    
    
	unsigned int i;
	while( --a != 0){
    
    
		for(i = 0; i < 600; i++);
	}
}

void main(){
    
    
	unsigned char str1[] = "Beyound Self Beyound Time Beyound Sky";
	unsigned char i;
	LCD1602_Init();
	// 设置整屏幕左移
	LCD1602_WriteCMD(LCD_DIS_MODE_LEFT);
	while(1){
    
    
		LCD1602_WriteCMD(0x80);
		for(i=0;i<strlen(str1);i++){
    
    
			LCD1602_WriteData(str1[i]);
			DELAY_MS(500);
		}
		
	}
	
}

2.2. The second method of movement

This movement method does not require setting the movement method during initialization, but enables movement when needed and turns off movement when not needed.

core code

The built-in instructions of 1602 provide instructions for moving the display left or right #define LCD_DIS_MOVE_LEFT 0x18. Use this instruction to move the content.
The following is the core code to realize the left shift of the screen. Other codes are consistent with the above example code and will not be shown.

void main(){
    
    
	unsigned char str1[] = "Beyound Self Beyound Time Beyound Sky";
	unsigned char i;
	LCD1602_Init();
	// 输出字符内容
	LCD1602_WriteCMD(0x80);
	for(i=0;i<strlen(str1);i++){
    
    
		LCD1602_WriteData(str1[i]);
	}
		
	while(1){
    
    
		// 设置屏幕左移
		LCD1602_WriteCMD(LCD_DIS_MOVE_LEFT);
		DELAY_MS(500);
		
		
		
	}
	
}

Guess you like

Origin blog.csdn.net/m0_38039437/article/details/135000455
Recommended