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

📄 lcd.c

📁 51单片机的实时系统
💻 C
字号:
#include <reg52.h>
#include <lcd.h>
#define	LCD_STROBE	((LCD_EN = 1),(LCD_EN=0))

void DelayUs(unsigned char cnt)  reentrant
{
while(cnt!=0)
 {cnt--;}
 }
void DelayMs(unsigned char cnt)  reentrant   //延时1ms,使用12M晶振
{
	unsigned char	i;
	do {
		i = 4;
		do {
			DelayUs(250);
		} while(--i);
	} while(--cnt);
}
	
	
	
/* write a byte to the LCD in 4 bit mode */

void
lcd_write(unsigned char c)	  reentrant
{
	P1 = (P1 & 0xF0) |  (c >> 4);
	LCD_STROBE;
	P1 = (P1 & 0xF0) |  (c & 0x0F);
	LCD_STROBE;
	DelayUs(40);
}

/*
 * 	Clear and home the LCD
 */

void
lcd_clear(void) reentrant
{
	LCD_RS = 0;
	lcd_write(0x1);
	DelayMs(2);
}

/* write a string of chars to the LCD */

void
lcd_puts(const char * s) reentrant
{
	LCD_RS = 1;	// write characters
	while(*s)
		lcd_write(*s++);
}

/* write one character to the LCD */

void
lcd_putch(char c)	reentrant
{
	LCD_RS = 1;	        // write characters
	P1 = (P1 & 0xF0) |  (c >> 4);
	LCD_STROBE;
	P1 = (P1 & 0xF0) |  (c & 0x0F);
	LCD_STROBE;
	DelayUs(40);
}


/*
 * Go to the specified position
 */

void
lcd_goto(unsigned char pos)	 reentrant
{
	LCD_RS = 0;
	lcd_write(0x80+pos);
}
	
/* initialise the LCD - put into 4 bit mode */

void
lcd_init(void)	  reentrant
{
	LCD_RS = 0;	// write control bytes
	DelayMs(15);	// power on delay
	P1 = 0x3;	// attention!
	LCD_STROBE;
	DelayMs(5);
	LCD_STROBE;
	DelayUs(100);
	LCD_STROBE;
	DelayMs(5);
	P1 = 0x2;	// set 4 bit mode
	LCD_STROBE;
	DelayUs(40);
	lcd_write(0x28);	// 4 bit mode, 1/16 duty, 5x8 font
	lcd_write(0x08);	// display off
	lcd_write(0x0c);	// display on, blink curson off
	lcd_write(0x06);	// entry mode
}

⌨️ 快捷键说明

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