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

📄 1602.c

📁 51事例原代码 很多很好用 51事例原代码 很多很好用
💻 C
字号:
/***************************************************
*
*标题:1602显示程序
*文件:1602.c
*作者:真永远
*日期:2007.6.10
*修改:2007.6.25
*软件:Keil C51
*芯片:AT89s52
*说明:
****************************************************/
#define LCD_DATA P2
sbit LCD_RS=P3^7;  //p3.7
sbit LCD_RW=P3^6;  //p3.6
sbit LCD_E =P3^5;  //p3.5
sbit LCD_BF=P2^7;  //LCD_DATA.7

unsigned char lcd_cursor_position=0;
unsigned char lcd_temp[16]={0}; 

//函数声明

//向LCD写命令字
void put_cmd(unsigned char cmd);

//
void clr(void);

//向LCD写字符
extern char putchar(char c);

//设置显示位置(即写入显示地址),行列均从0开始
void goto_xy(unsigned char x,unsigned char y);

//LCD初始化
void init_lcd(void);



//功能函数的实现
void delay_us(unsigned char us)//延时微妙,us最大值255
{
	while(--us)
		{
			;
		}
}

void delay_ms(unsigned char ms)//延时毫秒,ms最大值255
{
	unsigned char i=124;
	while(--ms)
		{
			while(--i)
				{
					;
				}
		}
}



//产生一个使能脉冲

void lcd_e_toggle(void)
{
	LCD_E=1;
	delay_us(3);
	LCD_E=0;
}

//循环检查LCD忙标志(BF),直到其值为0,方可执行下一条指令

void lcd_wait_until_finish(void)
{
	LCD_RW=1;
	LCD_RS=0;
	LCD_E=1;
	LCD_BF=1;
	while(LCD_BF)
	{
		;
	}
	LCD_E=0;
}


void put_cmd(unsigned char cmd)
{
	LCD_RW=0; //rw置0,状态为写
	LCD_RS=0; //rs置0,写入命令字
	LCD_DATA=cmd;	  //将命令字cmd送入LCD的数据端口
	lcd_e_toggle();
	lcd_wait_until_finish();
}

//设置显示位置,x,y均从0开始
void goto_xy(unsigned char x,unsigned char y)
{
	unsigned char p;
	if(y==0)
	{
		p=0x00+x;
		lcd_cursor_position=x;
	}
	else
	{
		p=0x40+x;
		lcd_cursor_position=16+x;
	}
	put_cmd(p+0x80);
}

//写字符
void put_char(char buff)
{
	LCD_RW=0;
	LCD_RS=1;
	LCD_DATA=buff;
	lcd_e_toggle();
	if(lcd_cursor_position>15&&lcd_cursor_position<32)
		lcd_temp[lcd_cursor_position-16]=buff; 
	lcd_cursor_position++;
	lcd_wait_until_finish();
}

extern char putchar(char c)
{
	if(lcd_cursor_position==16) goto_xy(0,1);
	if(lcd_cursor_position==32) 
	{
		unsigned char i;
		goto_xy(0,0);
		clr();
		for(i=0;i<16;i++) put_char(lcd_temp[i]);
		goto_xy(0,1);
	}
	if(c=='\n')
	{
		if(lcd_cursor_position<16)
			{
				while(lcd_cursor_position!=16) put_char(' ');
			}
		else
			{
				while(lcd_cursor_position!=32) put_char(' ');
			}
	}
	else put_char(c);

	return c;
}

//LCD初始化
void init_lcd(void)
{
	delay_ms(1);
	put_cmd(0x38);//设为8位接口模式,显示2行字符
	put_cmd(0x06);//写入新数据后光标右移
	put_cmd(0x0c);//显示功能开,不显示光标
	put_cmd(0x01);//清屏
}

void clr(void)
{
	put_cmd(0x01);
	goto_xy(0,0);
}

⌨️ 快捷键说明

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