LCD display module 1602 driver source code
//lcd1602 drive program for 51 mcu
//designed by Wangchao
//2006-6-14
#i nclude "reg52.h"
/********************************************************************/
//lcd part
#define LINE1 0
#define LINE2 1
#define LINE1_HEAD 0x80
#define LINE2_HEAD 0xC0
#define LCD_DELAY_TIME 40
#define DATA_MODE 0x38
#define OPEN_SCREEN 0x0C
#define DISPLAY_ADDRESS 0x80
#define CLEARSCREEN LCD_en_command(0x01)
//common part
#define HIGH 1
#define LOW 0
#define TRUE 1
#define ZERO 0
/*******************************************************************/
//change this part at different board
#define LCDIO P2
sbit LCD1602_RS=P0^7; //data command select 1 data 0 command pin 4
sbit LCD1602_RW=P0^6; //read write select 1 read 0 write pin 5
sbit LCD1602_EN=P0^5; //LCD enable signal pin 6
/********************************************************************/
void LCD_delay(void);//lcd delay
void LCD_en_command(unsigned char command);//write command
void LCD_en_dat(unsigned char temp);//write data
void LCD_set_xy( unsigned char x, unsigned char y );//set display address
void LCD_write_char( unsigned x,unsigned char y,unsigned char dat);//write lcd a character
void LCD_write_string(unsigned char X,unsigned char Y,unsigned char *s);//write lcd string
void LCD_init(void);//lcd initize
/********************************************************************/
void delay_nms(unsigned int n);//delay
/********************************************************************/
void main(void)
{
LCD_init();
while(TRUE )
{
CLEARSCREEN;
delay_nms(2);
LCD_write_string(0,LINE1,"Wellcome to DLUT");
LCD_write_string(0,LINE2,"www.dlut.edu.cn!");
delay_nms(500);
CLEARSCREEN;
delay_nms(2);
LCD_write_string(0,LINE1," lcd test ");
LCD_write_string(0,LINE2," successful! ");
delay_nms(500);
CLEARSCREEN;
delay_nms(2);
LCD_write_string(0,LINE1," I'm WangChao. ");
LCD_write_string(0,LINE2," Thank You! ");
delay_nms(500);
}
}
/********************************************************************/
/******************** LCD PART *************************************/
void LCD_delay(void)
{
unsigned char i;
for(i=LCD_DELAY_TIME;i>ZERO;i--) //be sure lcd reset
;
}
/********************************************************************/
void LCD_en_command(unsigned char command)
{
LCDIO=command;
LCD1602_RS=LOW;
LCD1602_RW=LOW;
LCD1602_EN=LOW;
LCD_delay();
LCD1602_EN=HIGH;
}
/********************************************************************/
void LCD_en_dat(unsigned char dat)
{
LCDIO=dat;
LCD1602_RS=HIGH;
LCD1602_RW=LOW;
LCD1602_EN=LOW;
LCD_delay();
LCD1602_EN=HIGH;
}
/********************************************************************/
void LCD_set_xy( unsigned char x, unsigned char y )
{
unsigned char address;
if (y == LINE1)
address = LINE1_HEAD + x;
else
address = LINE2_HEAD + x;
LCD_en_command(address);
}
/********************************************************************/
void LCD_write_char( unsigned x,unsigned char y,unsigned char dat)
{
LCD_set_xy( x, y );
LCD_en_dat(dat);
}
/********************************************************************/
void LCD_write_string(unsigned char X,unsigned char Y,unsigned char *s)
{
LCD_set_xy( X, Y ); //set address
while (*s) // write character
{
LCDIO=*s;
LCD_en_dat(*s);
s ++;
}
}
/********************************************************************/
void LCD_init(void)
{
CLEARSCREEN;//clear screen
LCD_en_command(DATA_MODE);//set 8 bit data transmission mode
LCD_en_command(OPEN_SCREEN);//open display (enable lcd display)
LCD_en_command(DISPLAY_ADDRESS);//set lcd first display address
CLEARSCREEN;//clear screen
}
/********************************************************************/
/*********************** OTHER PART *********************************/
void delay_nms(unsigned int n)
{
unsigned int i=0,j=0;
for (i=n;i>0;i--)
for (j=0;j<1140;j++);
}
/********************************************************************/
Previous article:Operational amplifier design principle and circuit description
Next article:Optimize algorithm design using customizable microcontrollers
Recommended ReadingLatest update time:2024-11-16 17:29
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- 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
- 【Silicon Labs Development Kit Review】+ Unboxing Experience
- IIC Timing Diagram
- Please recommend a 5V to positive and negative 15V op amp power supply circuit or chip to share
- Lock-in Amplifier Design
- EEWORLD University ----RISC-V 5th workshop
- What is transparent transmission and point-to-multipoint transmission? What are their classic applications and advantages?
- How to build an undervoltage protection circuit using discrete components
- [Mill MYB-YT507 development board trial experience] + some problems in the compilation experience
- How to configure the pins used by LCD as GPIO?
- 【GD32E231_DIY】②DS1302 real-time clock module information