Code:
main.c file
#include "public.h"
#include "lcd1602.h"
/*******************************************************************************
* Function name: main
* Function : Main function
* Input: None
* Output : None
*******************************************************************************/
void main()
{
int a=111;
lcd1602_init(); //LCD1602 initialization
lcd1602_clear(); //full screen
lcd1602_show_string(0,0,"Hello World!");//First line display
lcd1602_show_string(0,1,"0123456789");//The second line is displayed
while(1)
{
LcdSetCursor(12,1); //You can set where to start writing
LCDNumPrint(a); //Can display variables, this sets three numbers, which can be changed
}
}
lcd1602.c file
#include "lcd1602.h"
/*******************************************************************************
* Function name: lcd1602_write_cmd
* Function : LCD1602 write command
* Input: cmd: command
* Output : None
*******************************************************************************/
#if (LCD1602_4OR8_DATA_INTERFACE==0)//8位LCD
void lcd1602_write_cmd(u8 cmd)
{
LCD1602_RS=0; //Select command
LCD1602_RW=0; //Select write
LCD1602_E=0;
LCD1602_DATAPORT=cmd; //Prepare command
delay_ms(1);
LCD1602_E=1; //Enable pin E to write on rising edge first
delay_ms(1);
LCD1602_E=0; // enable pin E and then negative jump to complete writing
}
#else //4th LCD
void lcd1602_write_cmd(u8 cmd)
{
LCD1602_RS=0; //Select command
LCD1602_RW=0; //Select write
LCD1602_E=0;
LCD1602_DATAPORT=cmd; //Prepare command
delay_ms(1);
LCD1602_E=1; //Enable pin E to write on rising edge first
delay_ms(1);
LCD1602_E=0; // enable pin E and then negative jump to complete writing
LCD1602_DATAPORT=cmd<<4; //Prepare command
delay_ms(1);
LCD1602_E=1; //Enable pin E to write on rising edge first
delay_ms(1);
LCD1602_E=0; // enable pin E and then negative jump to complete writing
}
#endif
/*******************************************************************************
* Function name: lcd1602_write_data
* Function : LCD1602 write data
* Input: dat: data
* Output : None
*******************************************************************************/
#if (LCD1602_4OR8_DATA_INTERFACE==0)//8位LCD
void lcd1602_write_data(u8 dat)
{
LCD1602_RS=1; //Select data
LCD1602_RW=0; //Select write
LCD1602_E=0;
LCD1602_DATAPORT=dat; //Prepare data
delay_ms(1);
LCD1602_E=1; //Enable pin E to write on rising edge first
delay_ms(1);
LCD1602_E=0; // enable pin E and then negative jump to complete writing
}
#else
void lcd1602_write_data(u8 dat)
{
LCD1602_RS=1; //Select data
LCD1602_RW=0; //Select write
LCD1602_E=0;
LCD1602_DATAPORT=dat; //Prepare data
delay_ms(1);
LCD1602_E=1; //Enable pin E to write on rising edge first
delay_ms(1);
LCD1602_E=0; // enable pin E and then negative jump to complete writing
LCD1602_DATAPORT=dat<<4; //Prepare data
delay_ms(1);
LCD1602_E=1; //Enable pin E to write on rising edge first
delay_ms(1);
LCD1602_E=0; // enable pin E and then negative jump to complete writing
}
#endif
/*******************************************************************************
* Function name: lcd1602_init
* Function : LCD1602 initialization
* Input: None
* Output : None
*******************************************************************************/
#if (LCD1602_4OR8_DATA_INTERFACE==0)//8位LCD
void lcd1602_init(void)
{
lcd1602_write_cmd(0x38); //Data bus 8 bits, display 2 lines, 5*7 dots/character
lcd1602_write_cmd(0x0c); //Display function on, no cursor, cursor flashing
lcd1602_write_cmd(0x06); //After writing new data, the cursor moves right, but the display screen does not move
lcd1602_write_cmd(0x01);//清屏
}
#else
void lcd1602_init(void)
{
lcd1602_write_cmd(0x28); //Data bus 4 bits, display 2 lines, 5*7 dots/character
lcd1602_write_cmd(0x0c); //Display function on, no cursor, cursor flashing
lcd1602_write_cmd(0x06); //After writing new data, the cursor moves right, but the display screen does not move
lcd1602_write_cmd(0x01);//清屏
}
#endif
/*******************************************************************************
* Function name: lcd1602_clear
* Function : LCD1602 clear screen
* Input: None
* Output : None
*******************************************************************************/
void lcd1602_clear(void)
{
lcd1602_write_cmd(0x01);
}
/*******************************************************************************
* Function name: lcd1602_show_string
* Function : LCD1602 displays characters
* Input: x,y: display coordinates, x=0~15, y=0~1;
str: Display string
* Output : None
*******************************************************************************/
void lcd1602_show_string(u8 x,u8 y,u8 *str)
{
u8 i=0;
if(y>1||x>15)return; //Force exit if row and column parameters are incorrect
if(y<1) // Display the first line
{
while(*str!='')//The string ends with '', so it will be displayed as long as there is content in front of it
{
if(i<16-x) //If the character length exceeds the display range of the first line, continue to display in the second line
{
lcd1602_write_cmd(0x80+i+x); //The first line displays the address setting
}
else
{
lcd1602_write_cmd(0x40+0x80+i+x-16); //The second line displays the address setting
}
lcd1602_write_data(*str);//display content
str++; // pointer increment
i++;
}
}
else //Line 2 displays
{
while(*str!='')
{
if(i<16-x) //If the character length exceeds the display range of the second line, continue to display it in the first line
{
lcd1602_write_cmd(0x80+0x40+i+x);
}
else
{
lcd1602_write_cmd(0x80+i+x-16);
}
lcd1602_write_data(*str);
str++;
i++;
}
}
}
/*Set the display RAM start address, that is, the cursor position*/
void LcdSetCursor(u8 x,u8 y)
{
u8 addr;
if(y == 0)
addr = 0x00 + x;
else
addr = 0x40 + x;
lcd1602_write_cmd(addr | 0x80);
}
/*The LCD output number is currently three digits*/
void LCDNumPrint(int num)
{
lcd1602_write_data(num / 100 + 0x30); //hundreds digit of num
lcd1602_write_data(num % 100 /10 + 0x30); //tens digit of num
lcd1602_write_data(num % 10 + 0x30); // the unit digit of num
}
lcd1602.h file
#ifndef _lcd1602_H
#define _lcd1602_H
#include "public.h"
//LCD1602 data port 4-bit and 8-bit definition, if it is 1, it is LCD1602 4-bit data port drive, otherwise it is 8-bit
#define LCD1602_4OR8_DATA_INTERFACE 0 //Default use 8-bit data port LCD1602
//Pin definition
sbit LCD1602_RS=P2^6; //Data command selection
sbit LCD1602_RW=P2^5; //Read and write selection
sbit LCD1602_E=P2^7; //enable signal
#define LCD1602_DATAPORT P0 //Macro definition of LCD1602 data port
// Function declaration
void lcd1602_init(void);
void lcd1602_clear(void);
void lcd1602_show_string(u8 x,u8 y,u8 *str);
void LCDNumPrint(int num);
void LcdSetCursor(u8 x,u8 y);
#endif
public.c file
#include "public.h"
/*******************************************************************************
* Function name: delay_10us
* Function : Delay function, when ten_us=1, the delay is about 10us
* Input: ten_us
* Output : None
*******************************************************************************/
//void delay_10us(u16 ten_us)
//{
// while(ten_us--);
//}
/*******************************************************************************
* Function name: delay_ms
* Function : ms delay function, when ms=1, the delay is about 1ms
* Input: ms: ms delay time
* Output : None
*******************************************************************************/
void delay_ms(u16 ms)
{
u16 i,j;
for(i=ms;i>0;i--)
for(j=110;j>0;j--);
}
public.h file
#ifndef _public_H
#define _public_H
#include "reg52.h"
typedef unsigned int u16; //Redefine the system default data type
typedef unsigned char u8;
typedef unsigned long u32;
void delay_10us(u16 ten_us);
void delay_ms(u16 ms);
#endif
operation result:
This code can display character constant numbers and variable numbers.
Previous article:51 single chip microcomputer basics five-wire four-phase stepper motor
Next article:51 Microcontroller Basics Dot Matrix LED8X8
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- &FF0000 and &880000 are both red. What is the difference between these two reds?
- Internet radio design based on RSIC-V RVB2601 (3)
- LIS2MDL three-axis magnetometer package and code
- Live broadcast with prizes: Enter the TI robot production class, starting on Friday, reservations are in progress! (Share 100% to get gifts)
- Do you have any use for STM8S005C6T6? Special price for delivery
- Explanation on DSP devices and general-purpose processors (GPP)
- Impedance matching of power amplifiers
- About electromagnetic shielding
- About Phase Modulation
- I am confused about K60, hope for your advice