⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hd44780.h

📁 这是一个lm016l液晶显示的驱动文件
💻 H
字号:
#ifndef _HD44780_H_
#define _HD44780_H_

#include	"delay.h"

//***** Must be defined by user ******

	#define RS			RB0
	#define EN			RB2
	#define RW			RB1 
	#define DB			PORTC 
	#define TRIS_RS		TRISB0
	#define TRIS_EN		TRISB2
	#define TRIS_RW		TRISB1 
	#define TRIS_DB		TRISC

	#define DB_OUT     	0xf0	
	#define DB_IN      	0xff

//************************************

//	#define CHECKBUSY	1

#ifdef CHECKBUSY
	#define	LCD_WAIT lcd_check_busy()
#else
	#define LCD_WAIT DelayMs(5)

#endif

#define MESSAGE_LINE		0x0

#define MODE_4BIT	0
#define MODE_8BIT	1
#define PIN_OUT      0	
#define PIN_IN       1

#define DONE()	EN = 1; asm("nop"); asm("nop"); EN = 0  //给出一个下降沿(有效)

  
       #define READ(value)	EN = 1; \
asm("nop"); asm("nop"); \
			value=DB; \
				EN = 0; 

#define ClearScreen()			SetCommand(0x1)

#define PutChar(x,y,s)			GotoXY(x,y);\
								SetData(s);

#define GotoXY(x,y)			SetCommand((0x7f+y)|((x>>1)<<6));
													
#define lcd_cursor_right()		SetCommand(0x14)
#define lcd_cursor_left()		SetCommand(0x10)
#define lcd_display_shift()		SetCommand(0x1C)
#define FirstLine()			SetCommand(0x2)
#define SecondLine()			SetCommand(0xC0)

extern void SetCommand(unsigned char);
extern void SetData(unsigned char);
extern void PutString(unsigned char x,unsigned char y,const char * s);
extern void LCD_Begin(unsigned char);
extern void SetChar(unsigned char n,const char *c);

#endif

static bit fourbit;

#ifdef CHECKBUSY

unsigned char 
lcd_read_cmd_nowait(void)
{
	unsigned char c, readc;

	TRIS_DB	 =  DB_IN;

	RW = 1;
	asm("nop");
	asm("nop");

	if (fourbit)
	{
		READ(readc);
		c = ( ( readc << 4 ) & 0xF0 ); 
		READ(readc);
    		c |= ( readc & 0x0F );
	}
	else
	{
		READ(readc); 
		c = readc;
	}
	RW = 0;
	TRIS_DB	 = DB_OUT;
	
	return(c);
}

void
lcd_check_busy(void)
{
	unsigned int retry; 
	unsigned char c;

	for (retry=1000; retry-- > 0; ) {
		c = lcd_read_cmd_nowait();
		if (0==(c&0x80)) break;      //读时的最高位为BF 忙标志位  为1表示忙
	}
}

#endif

void
SetCommand(unsigned char c)
{
	LCD_WAIT;

	if (fourbit)
	{
		DB = ( ( c >> 4 ) & 0x0F );
		DONE();
    		DB = ( c & 0x0F );
		DONE();
	}
	else
	{
		DB = c;
		DONE();
	}
}

void
SetData(unsigned char c)
{
	LCD_WAIT;

	DB = 0;
	RS = 1;
	if (fourbit)
	{
    	DB |= ( ( c >> 4 ) & 0x0F );      
		DONE();
		DB &= 0xF0;
		DB |= ( c & 0x0F ); 
		DONE();
	}
	else
	{
		DB = c;
		DONE();
	}
	RS = 0;
}

void
PutString(unsigned char x,unsigned char y,const char  *s)
{	
	GotoXY(x,y);
	while(*s)
	SetData(*s++);//
}

void 
SetChar(unsigned char n,const char *c)
{
	unsigned char addres=0x40;
	n<<=3;
	
	while(n--)
	{
		SetCommand(addres++);
		SetData(*c++);
	}
}	

void
LCD_Begin(unsigned char mode)
{
	char init_value;

	fourbit		= 0;
	if (mode == MODE_4BIT){
		fourbit = 1;
		init_value = 0x3;
	}else{
		init_value = 0x3F;
	}
	RS = 0;
	EN = 0;
	RW = 0;
	TRIS_RS	 = PIN_OUT;
	TRIS_EN	 = PIN_OUT;
	TRIS_RW	 = PIN_OUT;
	TRIS_DB	 = DB_OUT;
	DelayMs(15);
	DB	 = init_value;
	DONE();           //输出一个写操作所需的下降沿信号
	DelayMs(5);
	DB	 = init_value;
	DONE();
	DelayUs(200);
	DB	 = init_value;
	DONE();
	
	if (fourbit){
		LCD_WAIT; 
		DB = 0x2;
		DONE();
		SetCommand(0x28);
	}else{
		SetCommand(0x38);
	}
	SetCommand(0xC);
	ClearScreen(); 
	SetCommand(0x6);
	GotoXY(1,1); 
	
	ADCON1=0x07;
}



⌨️ 快捷键说明

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