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

📄 lcd_4.c

📁 基于msc单片机仿真机的仿真程序
💻 C
字号:
/*
 *	LCD interface example
 *	Uses routines from delay.c
 *	This code will interface to a standard LCD controller
 *	like the Hitachi HD44780. It uses it in 4 or 8 bit mode
 *	
 */

#include"reg51.h"
#include"lcd.h"
#include"delay.h"
 

static bit fourbit;		// four or eight bit mode?

#ifdef CHECKBUSY

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

	//LCD_DATA_TRIS	 =  INPUT_DATA;

	LCD_RW = 1; // Read LCD
	//asm("nop"); // short propagation delay
	//asm("nop"); // short propagation delay

	if (fourbit)
	{
		LCD_STROBE_READ(readc); // Read high nibble
		// Move 4 bits to high nibble while zeroing low nibble
		c = ( ( readc << 4 ) & 0xF0 ); 
		LCD_STROBE_READ(readc); // Read low nibble
    		c |= ( readc & 0x0F ); // Or in 4 more bits to low nibble
	}
	else
	{
		LCD_STROBE_READ(readc); 
		c = readc;
	}
	LCD_RW = 0; // Return to default mode of writing LCD
	//LCD_DATA_TRIS	 = OUTPUT_DATA; // Return to default mode of writing LCD

	return(c);
}

void lcd_check_busy(void) // Return when the LCD is no longer busy, or we've waiting long enough!
{
	// To avoid hanging forever in event there's a bad or 
	// missing LCD on hardware.  Will just run SLOW, but still run.
	unsigned int retry; 
	unsigned char c;

	for (retry=1000; retry-- > 0; ) {
		c = lcd_read_cmd_nowait();
		if (0==(c&0x80)) break; // Check busy bit.  If zero, no longer busy
	}
}

#endif

/* send a command to the LCD */
void lcd_cmd(unsigned char c)
{
	//LCD_WAIT; // may check LCD busy flag, or just delay a little, depending on lcd.h

	if (fourbit)
	{
		LCD_DATA = ( ( c >> 4 ) & 0x0F );
		LCD_STROBE();
    		LCD_DATA = ( c & 0x0F );
		LCD_STROBE();
	}
	else
	{
		LCD_DATA = c;
		LCD_STROBE();
	}
}

/* send data to the LCD */
void lcd_data(unsigned char c)
{
	LCD_WAIT; // may check LCD busy flag, or just delay a little, depending on lcd.h

	LCD_DATA = 0;
	LCD_RS = 1;
	if (fourbit)
	{
    		LCD_DATA |= ( ( c >> 4 ) & 0x0F );      
		LCD_STROBE();
		LCD_DATA &= 0xF0;
		LCD_DATA |= ( c & 0x0F ); 
		LCD_STROBE();
	}
	else
	{
		LCD_DATA = c;
		LCD_STROBE();
	}
	LCD_RS = 0;
}

/* write a string of chars to the LCD */
void lcd_puts(const char * s)
{
	while(*s)
	lcd_data(*s++);
}

/* initialize the LCD */
void lcd_init(unsigned char mode)
{
	char init_value;

	fourbit		= 0;
	if (mode == FOURBIT_MODE){
		fourbit = 1;
		init_value = 0x3;
	}else{
		init_value = 0x3F;
	}
	LCD_RS = 0;
	LCD_EN = 0;
	LCD_RW = 0;
	//LCD_RS_TRIS	 = OUTPUT_PIN;
	//LCD_EN_TRIS	 = OUTPUT_PIN;
	//LCD_RW_TRIS	 = OUTPUT_PIN;
	//LCD_DATA_TRIS	 = OUTPUT_DATA;
	delay(15000);
	LCD_DATA	 = init_value;
	LCD_STROBE();
	delay(5000);
	LCD_DATA	 = init_value;
	LCD_STROBE();
	delay(200);
	LCD_DATA	 = init_value;
	LCD_STROBE();
	
	if (fourbit){
		LCD_WAIT; //may check LCD busy flag, or just delay a little, depending on lcd.h
		LCD_DATA = 0x2; // Set 4-bit mode
		LCD_STROBE();
		lcd_cmd(0x28); // Function Set
	}else{
		lcd_cmd(0x38);
	}
	lcd_cmd(0x0f); //Display On, Cursor On, Cursor Blink
	lcd_clear(); //Display Clear
	lcd_cmd(0x6); //Entry Mode
	lcd_home();
	lcd_puts("                ");
	lcd_home2();
	lcd_puts("                ");
	//lcd_goto(0); //Initialize DDRAM address to zero
}

void main(void)
{
    lcd_init(0);         //0x0:四位;0x1:八位
    while(1)
       {
	lcd_home2();
	lcd_puts("Learning to......");
	lcd_home();	// select line 2
	lcd_puts(" Welcome to RYG ");
	delay(5000);
	lcd_home2();
	lcd_puts("1234567890......");
	lcd_home();	// select line 2
	lcd_puts("0988abcdefg....");	
	delay(5000);
       }	
}

⌨️ 快捷键说明

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