1602 display based on 51 single chip microcomputer

Publisher:GoldenHarmonyLatest update time:2022-10-11 Source: csdn Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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. 

Reference address:1602 display based on 51 single chip microcomputer

Previous article:51 single chip microcomputer basics five-wire four-phase stepper motor
Next article:51 Microcontroller Basics Dot Matrix LED8X8

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号