📄 lcd1602.c
字号:
/*
***************************************************************************
*
* (C) Copyright 2007,单片机初学者园地
* All Rights reserved.
*
*项目名称: 51单片机学习开发系统
*
*本文件名称:LCD1602.c
*
* 完成作者 : 歪歪
* 当前版本 : V1.0
* 完成日期 : 2007年4月1日
* 描 述 :
* 此程序实现,完成液晶的驱动程序,可以显示任意的ASCII字符
*
*
****************************************************************************
*/
#include <reg52.h> //调用头文件(单片机内部的寄存器定义)
/******本段为硬件I/O口定义********/
sbit KEY0 = P0 ^ 0; //KEY和发光管复用,请将4个跳线插到KEY端
sbit KEY1 = P0 ^ 1;
sbit KEY2 = P0 ^ 2;
sbit KEY3 = P0 ^ 3;
sbit DIG0 = P0 ^ 4; //数码管位0
sbit DIG1 = P0 ^ 5; //数码管位1
sbit BUZZ = P3 ^ 3; //蜂鸣器
sbit LCD_RW = P3 ^ 6;
sbit LCD_RS = P3 ^ 7;
sbit LCD_E = P3 ^ 4;
#define LED_SEG P1 //数码管
#define LCD_DATA P2 //LCD
#define LCD_BUSY 0x80 // 用于检测LCD的BUSY标识
void lcd_init(void);
void display_string(unsigned char x,unsigned char y,unsigned char *s);
//LCD
unsigned char code LcdBuf1[]= {"welcom!"};
unsigned char code LcdBuf2[]= {"MCU-STUDY-BOARD"};
//unsigned char code LcdBuf2[]= {"current temp: C"};
//函数声明
void disp_selec(unsigned char bit_selec,unsigned char seg);
/**************************************************
** 函数名称: dellay
** 入口参数:h(unsigned int型)
** 出口参数:无
** 功能描述: 短暂延时,使用11.0592晶体,约0.01MS
****************************************************/
void dellay(unsigned int h)
{
while(h--); //0.01MS
}
unsigned char time_temp; //全局变量,定时器加1
unsigned char time_num; //全局变量,按一次键加1
/************主程序**************/
main()
{
lcd_init();
display_string(3,0,LcdBuf1);
display_string(0,1,LcdBuf2);
while(1); //单片机在此反复执行
}
//写数据
void WriteDataLcd(unsigned char wdata)
{
LCD_DATA=wdata;
LCD_RS=1;
LCD_RW=0;
LCD_E=0;
dellay(100); //短暂延时
LCD_E=1;
}
//写命令
void WriteCommandLcd(unsigned char wdata)
{
LCD_DATA=wdata;
LCD_RS=0;
LCD_RW=0;
LCD_E=0;
dellay(100); //短暂延时
LCD_E=1;
}
//LCD初始化
void lcd_init(void)
{
LCD_DATA=0;
WriteCommandLcd(0x38);
dellay(1000);
WriteCommandLcd(0x38);
dellay(1000);
WriteCommandLcd(0x38); //显示模式设置
WriteCommandLcd(0x08); //关闭显示
WriteCommandLcd(0x01); //显示清屏
WriteCommandLcd(0x06); //显示光标移动设置
WriteCommandLcd(0x0c); //显示开及光标移动设置
}
//设置光标
void display_xy(unsigned char x,unsigned char y)
{
if(y==1)
x+=0x40;
x+=0x80;
WriteCommandLcd(x);
}
//显示单个字符
void display_char(unsigned char x,unsigned char y,unsigned char dat)
{
display_xy(x,y);
WriteDataLcd(dat);
}
//显示字符串
void display_string(unsigned char x,unsigned char y,unsigned char *s)
{
display_xy(x,y);
while(*s)
{
WriteDataLcd(*s);
s++;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -