当前位置:网站首页>Modular programming of LCD1602 LCD controlled by single chip microcomputer
Modular programming of LCD1602 LCD controlled by single chip microcomputer
2022-06-25 07:46:00 【Assassin ari】
Here I write STC12C5A60S2 control LCD1602 Share your program , If there is any deficiency , Please point out !

Code structure diagram :

Program download link :https://download.csdn.net/download/yagnruinihao/16676666 Click to download
Proteus Simulation diagram download link :https://download.csdn.net/download/yagnruinihao/16676677 Click to download
The test program :LCD1602 test .c
#include <string.h>
#include "common.h"
#include "LCD1602.h"
void main(void)
{
unsigned char i ;
unsigned char author[] = "Author:YangRui" ;
LCD1602_Init();
for(i=0 ; i<strlen(author) ; i++)
{
LCD1602_AddressWriteByte(LCD1602_ROW0,i,author[i]);
}
LCD1602_AddressWriteString(LCD1602_ROW1,0,"QQ:279729201") ;
while(1);
}/*################LCD1602.h ################*/
#ifndef __LCD1602_H__
#define __LCD1602_H__
#include "common.h"
// Hardware interface definition , Corresponding to hardware circuit
sbit LCD1602_RS_Bit = P2^6;//LCD1602 Of RS port , High level indicates data 、 Low level indicates command
sbit LCD1602_RW_Bit = P2^5;//LCD1602 Of RW port , A high level indicates a read operation 、 A low level indicates a write operation
sbit LCD1602_EN_Bit = P2^7;//LCD1602 Of EN port , High level indicates read status or read data 、 High pulses indicate write instructions or write data
#define LCD1602_DATA_PORT P0//LCD1602 Data port D0~D7, among Px^0 Port connection DB0,Px^7 Port connection DB7, Other interfaces, and so on
/* RS And RW Set up :
RS Rw
0 0 ----> Write orders
0 1 ----> Read status
1 0 ----> Writing data
1 1 ----> Reading data
*/
//LCD1602 Of RS Port selection
#define LCD1602_DATA_OPERATION 1// Data patterns
#define LCD1602_COMMAND_OPERATION 0// Command mode
//LCD1602 Of RW Port selection
#define LCD1602_READ_OPERATION 1// Read operations
#define LCD1602_WRITE_OPERATION 0// Write operations
//LCD1602 Of EN Port selection
#define LCD1602_ENABLE 1// Allow to operate LCD1602
#define LCD1602_DISABLE 0// Do not operate LCD1602
// display mode :16X2 Characters , 5X7 Lattice ,8 Bit data port
#define LCD1602_DEFAULT_DISPALY_MODE 0x38
/*
====================================================
LCD1602 Set up : Show allowed or prohibited 、 The cursor displays or disables 、 The cursor flashes or disables
====================================================
/*
0 0 0 0 1 D C B
| | |------ The cursor blinks : 1--> allow ; 0--> prohibit
| |---------- The cursor displays : 1--> allow ; 0--> prohibit
|------------- LCD display : 1--> allow ; 0--> prohibit
*/
#define LCD1602_DISPLAY_ENABLE 0X04 // Allow LCD display
#define LCD1602_DISPLAY_DISABLE (0X04 & (~(0x01<<2)))// LCD display is prohibited
#define LCD1602_CURSOR_DISPLAY_ENABLE 0X02 // Allows the cursor to display
#define LCD1602_CURSOR_DISPLAY_DISABLE (0X02 & (~(0x01<<1)))// Disable cursor display
#define LCD1602_CURSOR_BLINK_ENABLE 0X01 // Allow the cursor to flash
#define LCD1602_CURSOR_BLINK_DISABLE (0X01 & (~(0x01<<0)))// Prevent the cursor from blinking
// default setting :lcd Show , The cursor does not show , The cursor does not flash
#define LCD1602_DEFAULT_DISPLAY_AND_CURSOR_MODE 0x08 | \
LCD1602_DISPLAY_ENABLE | \
LCD1602_CURSOR_DISPLAY_DISABLE | \
LCD1602_CURSOR_BLINK_DISABLE
/*
====================================================
LCD1602 Set up : Pointer plus or minus one 、 Add one or subtract one from the cursor position
====================================================
0 0 0 0 0 1 N S
| |----- Screen movement :1--> allow ; 0--> prohibit
|--------- N=1: After reading or writing a character, the address pointer is added 1, And the cursor adds 1
N=0: After reading or writing a character, the address pointer is subtracted 1, And the cursor decreases 1
When S=1 when :
if N=1, Full screen shift left
N=0, The whole screen moves to the right
So that the cursor does not move , And the effect of moving the whole screen
*/
#define LCD1602_POINT_AND_CURSOR_PLUS 0X02 // Pointer plus one 、 Add one to the cursor
#define LCD1602_POINT_AND_CURSOR_MINUS (0X02 & (~(0X01<<1)))// Pointer minus one 、 The cursor minus one
#define LCD1602_SCREEN_MOVE_ENABLE 0x01 // Allow the screen to move
#define LCD1602_SCREEN_MOVE_DISABLE (0x01 & (~(0X01<<0)))// Prohibit screen movement
// default setting : Cursor and pointer plus one , The screen doesn't move
#define LCD1602_DEFAULT_POINT_AND_POINT_ADDRESS_MODE 0X04 | \
LCD1602_POINT_AND_CURSOR_PLUS| \
LCD1602_SCREEN_MOVE_DISABLE
// Row position parameter , Note that the line address is from 0 Start
#define LCD1602_ROW0 0 // The first 0 That's ok
#define LCD1602_ROW1 1 // The first 1 That's ok
#define LCD1602_MIN_ROW LCD1602_ROW0// Minimum number of rows , by LCD1602_ROW0, namely 0*/
#define LCD1602_MAX_ROW LCD1602_ROW1// Maximum number of rows , by LCD1602_ROW1, namely 1*/
// Column position parameter , Note that the column address is from 0 Start
#define LCD1602_ROW_LENGHT 0x28 // The length of each line is 0x28, namely 40
#define LCD1602_MIN_COLUMN 0 // Minimum number of columns , by 0
#define LCD1602_MAX_COLUMN (LCD1602_ROW_LENGHT-1)// Maximum number of columns , by 39
// The starting address of each line */
#define LCD1602_ROW0_ADDRESS_START 0x80 // The first 0 Line start address
#define LCD1602_ROW1_ADDRESS_START (0x80+0x40) // The first 1 Line start address
/* The end address of each line */
#define LCD1602_ROW0_ADDRESS_END (LCD1602_ROW0_ADDRESS_START+LCD1602_ROW_LENGHT)// The first 0 Line end address
#define LCD1602_ROW1_ADDRESS_END (LCD1602_ROW1_ADDRESS_START+LCD1602_ROW_LENGHT)// The first 1 Line end address
// Command set directive
#define LCD1602_CLEAN_ALL_DISPALY 0x01// Clear screen instruction
#define LCD1602_CURSOR_RETURN_TO_ORIGIN 0x02// Cursor homing instruction
/***************** External interface functions ******************/
//LCD1602 Initialization function
extern void LCD1602_Init(void) ;
// Clear screen operation
extern void LCD1602_CleanAll(void) ;
// Cursor homing operation
extern void LCD1602_CursorHoming(void);
//LCD1602 Address write byte operation
extern char LCD1602_AddressWriteByte(unsigned char Row,unsigned char Column,unsigned char DataValue);
//LCD1602 Address write string operation
extern char LCD1602_AddressWriteString(unsigned char Row,unsigned char Column,unsigned char *String);
// Write command operation
extern void LCD1602_WriteCommand(unsigned char CommandValue);
// Write data operations
extern void LCD1602_WriteData(unsigned char DataValue);
/**********************************************/
#endif /*__LCD1602_H__*//*################ LCD1602.c################*/
/***************************************************************************
model block :LCD1602.c
say bright :LCD1602 The driver
edition Ben :Version3.0 2021/01/01 06:00
Compile environment :Keil_C51 V9.55
Master chip :STC12C5A60S2 @11.0592MHZ
STC89C52RC @11.0592MHZ
AT89C51 @11.0592MHZ @Proteus8.9 Simulation
do person : Yang Rui
Contact information :【 QQ 】279729201
【 mailbox 】[email protected]
[email protected]
【 Telephone 】13630279531
Modify the record :
=================
2021/01/01 06:06
Record :
1. Specification of partial functions .
2. Cancel Delay80usForLcd1602(), Call function Delay_SomeNop(10) Substitution elimination Delay80usForLcd1602() function .
3. increase ST89C52RC @11.0592MHZ and AT89C51 @11.0592MHZ @Proteus8.9 The simulation test , This code has been tested
Can be applied to .
=================
=================
2017/01/6 12:08
Record :
1. Add Chinese Notes .
2. Modify the function name pattern , from " Module name "( Capitalization )+"_"+ The function name constitutes , for example lcd1602ReadStatus It is amended as follows LCD1602_ReadStatus.
3. Modify the delay function , take Delay1msForLcd1602 The function is changed to Delay80usForLcd1602 function . Be careful , Both of these functions are
from STC-ISP Generate ,Delay80usForLcd1602 The function is defined by STC-ISP V6.85Q Of “ Software delay calculator ” in the light of STC-Y3 Instruction set
@11.0592MHZ Automatic generation . After testing , Time delay 59us There may be situations where only part of the content is written , Time delay 60us Just right .
For the sake of safety , You can also modify this delay longer .
=================
=================
2014/04/31 20:09
Record :
1. Solve the problem of , Because it is not installed LCD1602 The resulting project is stuck in the function
lcd1602CheckBusy() The problem of , take
do{
;
}while( (lcd1602ReadStatus()) & 0x80));
It is amended as follows
do{
i++;
}while( ((lcd1602ReadStatus()) & 0x80) && (i<10));
Because... Is not installed in LCD1602 when , adopt " Read status " function lcd1602ReadStatus() Reading back is always 0xff,
If the first algorithm is used , It will cause the program to " stuck ", Not enough “ Security ”.
=================
=================
2014/02/24 23:44
Record :
1. Add function lcd1602AddressWriteString(...)
=================
=================
2014/02/24 15:00
Record :
1. Add function delay1msForLcd1602()
STC12C5A60S2 The operation speed of single chip microcomputer is faster than that of traditional 8051 fast , Certain operating delays must be sufficient .
=================
***************************************************************************/
#include <string.h>
#include "lcd1602.h"
/* The external interface function is in lcd1602.h In a statement */
/***************** Internal function ******************/
unsigned char LCD1602_ReadStatus(void) ;
void LCD1602_CheckBusy(void) ;
/**********************************************/
/******************************************************************
- The name of the function :LCD1602_ReadStatus
- Function description : Read LCD1602 The status value
- Function attribute : Internal function
- Parameter description : nothing
- Return instructions :LCD1602 The state of the value
******************************************************************/
unsigned char LCD1602_ReadStatus(void)
{
unsigned char StatusCode ;
LCD1602_EN_Bit = LCD1602_DISABLE ; // Do not operate LCD1602
LCD1602_RS_Bit = LCD1602_COMMAND_OPERATION ;// Command mode
LCD1602_RW_Bit = LCD1602_READ_OPERATION ; // Read operations
LCD1602_DATA_PORT = 0xff ;
Delay_SomeNop(10); // Be careful : The delay here cannot be less , Otherwise it won't work
LCD1602_EN_Bit = LCD1602_ENABLE ; // Allow to operate LCD1602
Delay_SomeNop(10); // Be careful : The delay here cannot be less , Otherwise it won't work
StatusCode = LCD1602_DATA_PORT ; // Read status values */
LCD1602_EN_Bit = LCD1602_DISABLE ; // Do not operate LCD1602
return StatusCode ;
}
/******************************************************************
- The name of the function :LCD1602_CheckBusy
- Function description : Judge LCD1602 Are you busy : If busy, wait ; If idle, execute the task
- Function attribute : Internal function
- Parameter description : nothing
- Return instructions : nothing
- notes : When LCD1602_DATA_PORT[7] be equal to 1, Express LCD1602 Be busy , You need to wait for a while
When LCD1602_DATA_PORT[7] be equal to 0, Express LCD1602 Free , You can continue to perform the following tasks
- repair charge : there “10” It's tested , In the test , Use serial port to print i The value of is 1, Write here 10 It's big enough .
******************************************************************/
void LCD1602_CheckBusy(void)
{
unsigned char i=0;
do{
i++;
}while( ((LCD1602_ReadStatus()) & 0x80) && (i<10));
}
/******************************************************************
- The name of the function :LCD1602_WriteCommand
- Function description :LCD1602 Write orders
- Function attribute : External function , For users to call
- Parameter description :CommandValue, It needs to be sent to LCD1602 Command value of
- Return instructions : nothing
******************************************************************/
void LCD1602_WriteCommand(unsigned char CommandValue)
{
LCD1602_CheckBusy();
LCD1602_EN_Bit = LCD1602_DISABLE ; /* Do not operate LCD1602*/
LCD1602_RS_Bit = LCD1602_COMMAND_OPERATION ;/* Command mode */
LCD1602_RW_Bit = LCD1602_WRITE_OPERATION ; /* Write operations */
LCD1602_DATA_PORT = CommandValue ; /* Send command value */
//Delay_SomeNop(10);
LCD1602_EN_Bit = LCD1602_ENABLE ; /* Allow to operate LCD1602*/
//Delay_SomeNop(10);
LCD1602_EN_Bit = LCD1602_DISABLE ; /* Do not operate LCD1602*/
}
/******************************************************************
- The name of the function :LCD1602_WriteData
- Function description :LCD1602 Writing data
- Function attribute : External function , For users to call
- Parameter description :DataValue, It needs to be sent to LCD1602 The data of
- Return instructions : nothing
******************************************************************/
void LCD1602_WriteData(unsigned char DataValue)
{
LCD1602_CheckBusy() ;
LCD1602_EN_Bit = LCD1602_DISABLE ; /* Do not operate LCD1602*/
LCD1602_RS_Bit = LCD1602_DATA_OPERATION ; /* Data patterns */
LCD1602_RW_Bit = LCD1602_WRITE_OPERATION ; /* Write operations */
LCD1602_DATA_PORT = DataValue; /* send data */
//Delay_SomeNop(10);
LCD1602_EN_Bit = LCD1602_ENABLE ; /* Allow to operate LCD1602*/
//Delay_SomeNop(10);
LCD1602_EN_Bit = LCD1602_DISABLE ; /* Do not operate LCD1602*/
}
/******************************************************************
- The name of the function :LCD1602_CleanAll
- Function description : Clear the screen display
- Function attribute : External function , For users to call
- Parameter description : nothing
- Return instructions : nothing
- notes : Clear the screen display , Cursor homing ( top left corner ), Address counter AC Set to 0
******************************************************************/
void LCD1602_CleanAll(void)
{
LCD1602_WriteCommand(LCD1602_CLEAN_ALL_DISPALY);
}
/******************************************************************
- The name of the function :LCD1602_CursorHoming
- Function description : Cursor homing
- Function attribute : External function , For users to call
- Parameter description : nothing
- Return instructions : nothing
- notes : The cursor is , When the screen moves the display ,lcd1602 After all data is displayed ,
Call this function , Everything on the screen will go back . The cursor is on the first
A place (0x80).
******************************************************************/
void LCD1602_CursorHoming(void)
{
LCD1602_WriteCommand(LCD1602_CURSOR_RETURN_TO_ORIGIN);
}
/******************************************************************
- The name of the function :LCD1602_Init
- Function description :LCD1602 initialization
- Function attribute : External function , For users to call
- Parameter description : nothing
- Return instructions : nothing
- notes : Set up (1) display mode
(2) LCD display allows or disables 、 The cursor displays allow or disable 、 Cursor blinking allows or disables
(3) Address pointer plus or minus one 、 Cursor pointer plus or minus one 、 The screen moves left or right
Can be modified by LCD1602.H Medium LCD1602_DEFAULT_DISPALY_MODE、LCD1602_DEFAULT_DISPLAY_AND_CURSOR_MODE、
LCD1602_DEFAULT_POINT_AND_POINT_ADDRESS_MODE Achieve different display effects .
******************************************************************/
void LCD1602_Init(void)
{
LCD1602_CleanAll();
LCD1602_WriteCommand(LCD1602_DEFAULT_DISPALY_MODE);
LCD1602_WriteCommand(LCD1602_DEFAULT_DISPLAY_AND_CURSOR_MODE);
LCD1602_WriteCommand(LCD1602_DEFAULT_POINT_AND_POINT_ADDRESS_MODE);
/* Negligible , stay LCD1602_CleanAll() This function is implicit in */
LCD1602_CursorHoming();
}
/******************************************************************
- The name of the function :LCD1602_AddressWriteByte
- Function description : stay LCD1602 Of Row That's ok Column Number of column writes DataValue
- Function attribute : External function , For users to call
- Parameter description :Row--> Line address , Valid values are LCD1602_ROW0 or LCD1602_ROW1
Column--> Column address , Valid values are 0~39 Integer between
DataValue--> Data to be written .
- Return instructions :0( success ) perhaps -1( Failure )
- notes :1. Both the row address and the column address are from 0 At the beginning .
Need to be in LCD1602 Of the 0 Xing di 2 The column shows the aribo number 5, The call mode is
LCD1602_AddressWriteByte(LCD1602_ROW0,2,‘5’);
2. stay DEBUG In mode , Will judge the validity of the input parameters , Assist in the analysis of . After commissioning, it can pass
shielding common.h Medium "#define DEBUG 1" Reduce the size of the target file .
******************************************************************/
char LCD1602_AddressWriteByte(unsigned char Row,unsigned char Column,unsigned char DataValue)
{
#ifdef DEBUG
if((Column< LCD1602_MIN_COLUMN)|| (Column > LCD1602_MAX_COLUMN)|| \
( (Row != LCD1602_ROW0) &&(Row != LCD1602_ROW1)))
return -1 ;
#endif
if(LCD1602_ROW0 == Row) // Note that there == The order of the two sides of , skill !
LCD1602_WriteCommand(LCD1602_ROW0_ADDRESS_START+Column) ;
else if(LCD1602_ROW1 == Row)// Note that there == The order of the two sides of , skill !
LCD1602_WriteCommand(LCD1602_ROW1_ADDRESS_START+Column) ;
LCD1602_WriteData(DataValue);
return 0;
}
/******************************************************************
- The name of the function :LCD1602_AddressWriteString
- Function description : from LCD1602 Of Row That's ok Column Column starts writing string String
- Function attribute : External function , For users to call
- Parameter description :Row--> Line address , Valid values are LCD1602_ROW0 or LCD1602_ROW1
Column--> Column address , Valid values are 0~39 Integer between
String--> String to write .
- Return instructions :0( success ) perhaps -1( Failure )
- notes : Both the row address and the column address are from 0 At the beginning .
Want to be in LCD1602 Of the 0 Xing di 2 Columns show "test", The call mode is
LCD1602_AddressWriteString(LCD1602_ROW0,2,"test");
What we use here is strlen, instead of sizeof.
stay DEBUG In mode , Will judge the validity of the input parameters , Assist in the analysis of . After commissioning, it can pass
shielding common.h Medium "#define DEBUG 1" Reduce the size of the target file .
******************************************************************/
char LCD1602_AddressWriteString(unsigned char Row,unsigned char Column,unsigned char *String)
{
unsigned char length = strlen(String) ;
#ifdef DEBUG
if(Column< LCD1602_MIN_COLUMN|| (Column+strlen(String)-1) > LCD1602_MAX_COLUMN|| \
( (Row != LCD1602_ROW0) &&(Row != LCD1602_ROW1)))
return -1 ;
#endif
if(LCD1602_ROW0 == Row) // Note that there == The order of the two sides of , skill !
LCD1602_WriteCommand(LCD1602_ROW0_ADDRESS_START+Column) ;
else if(LCD1602_ROW1 == Row)// Note that there == The order of the two sides of , skill !
LCD1602_WriteCommand(LCD1602_ROW1_ADDRESS_START+Column) ;
while(length--)
LCD1602_WriteData(*String++);
return 0;
}/*################common.h################*/
#ifndef __COMMON_H__
#define __COMMON_H__
#include <reg51.h>
#define DEBUG 1 // For early commissioning , This line of code can be masked later , Achieve the goal of reducing the target file size
/***************** External interface functions ******************/
// Time delay ( Inaccurate delay ) function
void Delay_SomeNop(long time);
#endif /*__COMMON_H__*//*################common.c################*/
#include <intrins.h>
#include "common.h"
/******************************************************************
- The name of the function :Delay_SomeNop
- Function description : The time delay function ( Inaccurate delay )
- Function attribute : External function , For users to call
- Parameter description :time Delay time required
- Return instructions : nothing
- notes : nothing
******************************************************************/
void Delay_SomeNop(long time)
{
while(time--)
{
_nop_();
}
}
边栏推荐
- [batch dos-cmd command - summary and summary] - CMD window setting and operation commands (CD, title, mode, color, pause, CHCP, exit)
- Static bit rate (CBR) and dynamic bit rate (VBR)
- C# 读取web上的xml
- 【QT】qtcreator便捷快捷键以及QML介绍
- LeetCode_哈希表_中等_454.四数相加 II
- Cglib dynamic proxy
- Introduction to Sichuan Tuwei ca-is3082w isolated rs-485/rs-422 transceiver
- 一“石”二“鸟”,PCA有效改善机载LiDAR林下地面点部分缺失的困局
- VectorDraw Web Library 10.10
- Vscode official configuration synchronization scheme
猜你喜欢

Summary of small problems in smartbugs installation

Estimation of dense forest volume based on LIDAR point cloud with few ground points

Path planner based on time potential function in dynamic environment

Sichuan Tuwei ca-is3105w fully integrated DC-DC converter

栅格地图(occupancy grid map)构建

【批处理DOS-CMD命令-汇总和小结】-文件与目录操作命令(md、rd、xcopy、dir、cd、set、move、copy、del、type、sort)
![[batch dos-cmd command - summary and summary] - application startup and call, service and process operation commands (start, call, and)](/img/19/b8c0fb72f1c851a6b97f2c17a18665.png)
[batch dos-cmd command - summary and summary] - application startup and call, service and process operation commands (start, call, and)

Ca-is1200u current detection isolation amplifier has been delivered in batch

Home environment monitoring system design (PC version) (mobile app version to be determined)

【QT】Qt 5 的程序:打印文档
随机推荐
WinForm implementation window is always at the top level
Notes: [open class] neural network and deep learning -- tensorflow2.0 actual combat [Chinese course]
NPM install reports an error: gyp err! configure error
What if there is no point in data visualization?
Modular programming of oled12864 display controlled by single chip microcomputer
AttributeError: ‘Upsample‘ object has no attribute ‘recompute_scale_factor‘
VectorDraw Developer Framework 10.10
57. 插入区间
C#获取exe的版本号-文件版本and程序集版本
MySQL facet 01
Lebel only wants an asterisk in front of it, but doesn't want to verify it
npm install 报错 : gyp ERR! configure error
ts环境搭建
useMemo模拟useCallback
Sichuan earth microelectronics 8-channel isolated digital input receiver
Application of point cloud intelligent drawing in intelligent construction site
Pytorch遇到的坑:为什么模型训练时,L1loss损失无法下降?
Summary of small problems in smartbugs installation
不同路径II[针对DFS的动态规划改进]
C reads XML on the web