lcd.c

来自「本程序是为51系列单片机开发的16*2型液晶模块驱动程序。该驱动程序包含两个文件」· C语言 代码 · 共 244 行

C
244
字号
#pragma OT(8,speed)

//----LCD Driver Module----
//Designed by QTS.
//2005,8


//----Module Description----
//This module is a driver for LCD module 1602 which works in 16*2 char mode.


//----Module Includes----
//This module includes several functions below:
//(func) void LCD_INIT(void): This is the initial function called before using the LCD.
//(func) void LCD_WRCOM(char command): This function will send a command to LCD.
//(func) void LCD_WRDATA(char str): This function will make the LCD to display a char at the current position.
//(func) void LCD_SETPTR(char pos): This function will set the current position of the LCD cursor.
//(func) void LCD_STRING(char *string): This function will display a string at the current position. 
//                                      The string should only be defined in code memory.


#include "reg51.h"
//----Module Configuration----
sbit LCD_RS=P2^0;
sbit LCD_RW=P2^1;
sbit LCD_E=P2^2;
#define LCD_Data	P0
//User can set the pins of the LCD module linked to MCU.

void LCD_Wait15(void)
{
#pragma asm
	push	acc
	mov		a,r7
	push	acc
	mov		a,r6
	push	acc
	mov		r7,#01ah
LCD_Wait1:
	mov		r6,#0ffh
LCD_Wait2:
	nop
	djnz	r6,LCD_Wait2
	djnz	r7,LCD_Wait1
	pop		acc
	mov		r6,a
	pop		acc
	mov		r7,a
	pop		acc
#pragma endasm	
}

void LCD_INIT1(void)
{
#pragma asm
	mov		LCD_Data,#38H
	nop
	clr		LCD_RS
	nop
	clr		LCD_RW
	nop
	setb	LCD_E
	nop
	clr		LCD_E
	nop
#pragma endasm	
}

void LCD_STATUS(void)
{
#pragma asm
	mov		LCD_Data,#0ffh
	nop
	clr		LCD_E
	nop
	clr		LCD_RS
	nop
	setb	LCD_RW
	nop
	setb	LCD_E
	mov		a,LCD_Data
	clr		LCD_E
	nop
#pragma endasm	
}

void LCD_INIT(void)
{
	bit flag=0;
	if(EA==1)
	{
		EA=0;
		flag=1;
	}	
#pragma asm
	push	acc
	clr		LCD_RS
	clr		LCD_RW
	clr		LCD_E
	lcall	LCD_wait15
	clr		LCD_E
	lcall	LCD_INIT1
	lcall	LCD_wait15
	lcall	LCD_INIT1
	lcall	LCD_wait15
	lcall	LCD_INIT1
	mov		a,r7
	push	acc
	mov		r7,#38h
	lcall	_LCD_WRCOM
	mov		r7,#08h
	lcall	_LCD_WRCOM
	mov		r7,#01h
	lcall	_LCD_WRCOM
	mov		r7,#06h
	lcall	_LCD_WRCOM
	mov		r7,#0ch
	lcall	_LCD_WRCOM
	pop		acc
	mov		r7,a
	pop		acc
#pragma endasm	
	if(flag)
		EA=1;
}

void LCD_WRCOM(char command)
{
	bit flag=0;
	command=command;
	if(EA==1)
	{
		EA=0;
		flag=1;
	}	
#pragma asm
	push	acc
	mov		a,r7
	push	acc
LCD_LOOP1:
	lcall	LCD_STATUS
	anl		a,#80h
	jnz		LCD_LOOP1
	pop		acc
	clr		LCD_E
	nop
	clr		LCD_RS
	nop
	clr		LCD_RW
	nop
	mov		LCD_Data,a
	nop
	setb	LCD_E
	nop
	clr		LCD_E
	nop
	pop		acc
#pragma endasm	
	if(flag)
		EA=1;	
}

void LCD_WRDATA(char str)
{
	bit flag=0;
	str=str;
	if(EA==1)
	{
		EA=0;
		flag=1;
	}	
#pragma asm
	push	acc
	mov		a,r7
	push	acc
LCD_LOOP2:
	lcall	LCD_STATUS
	anl		a,#80h
	jnz		LCD_LOOP2
	pop		acc
	clr		LCD_E
	nop
	setb	LCD_RS
	nop
	clr		LCD_RW
	nop
	mov		LCD_Data,a
	nop
	setb	LCD_E
	nop
	clr		LCD_E
	nop
	pop		acc
#pragma endasm	
	if(flag)
		EA=1;	
}

void LCD_SETPTR(char pos)
{
	bit flag=0;
	if(EA==1)
	{
		EA=0;
		flag=1;
	}	
	pos=pos;
#pragma asm
	push	acc
	mov		a,r7
	push	acc
	jnb		acc.4,LCD_PASS
	add		a,#30h
LCD_PASS:
	add		a,#80h
	mov		r7,a
	lcall	_LCD_WRCOM
	pop		acc
	mov		r7,a
	pop		acc
#pragma endasm	
	if(flag)
		EA=1;	
}


void LCD_STRING(char *string)
{
	char n=0;
	bit flag=0;
	if(EA==1)
	{
		EA=0;
		flag=1;
	}	
	for(;string[n]!=0;n++)
	{
		LCD_WRDATA(string[n]);
	}
	if(flag)
		EA=1;	
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?