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

📄 lcd1602.h

📁 一个时钟程序
💻 H
字号:
/*---------------------------------------------------------
液晶LCD1602C  使用4条数据线(D4~D7)
-----------------------------------------------------------*/
/*-------------------------------------------------------------
LCD引脚定义
1---GND 
2---VCC
3---VO
4---RS
5---RW
6---EN
7到14--D0-D7
15--背景灯+
16--背景灯-
-----------------------------------------------------------------*/
#include <at89X52.h>
#include <intrins.h>


#define LCD_DATA P2
sbit LCD1602_RS=P2^2;
sbit LCD1602_EN=P2^3;

int p=0;

/*--------------------------------------------------------------------------------------------------
函数说明
--------------------------------------------------------------------------------------------------*/
void LCD_init(void);
void LCD_en_write(void);
void LCD_write_command(unsigned  char command) ;
void LCD_write_data(unsigned char Recdata);
void LCD_set_xy (unsigned char x, unsigned char y);
void LCD_write_string(unsigned char X,unsigned char Y,unsigned char *s);
void LCD_write_char(unsigned char X,unsigned char Y,unsigned char Recdata);
void delay_nus(unsigned int n);
void delay_nms(unsigned int n);

void delay_1us(void)                 //1us延时函数
  {
   _nop_();
  }

void delay_nus(unsigned int n)       //N us延时函数
  {
   unsigned int i=0;
   for (i=0;i<n;i++)
   delay_1us();
  }
  
void delay_1ms(void)                 //1ms延时函数
  {
   unsigned int i;
   for (i=0;i<1140;i++);
  }
  
void delay_nms(unsigned int n)       //N ms延时函数
  {
   unsigned int i=0;
   for (i=0;i<n;i++)
   delay_1ms();
  }


void LCD_init(void)         //液晶初始化
{

  LCD_write_command(0x28);
  delay_nus(40); 
  LCD_write_command(0x28);
  delay_nus(40); 
  LCD_write_command(0x28);
  delay_nus(40); 
  
  LCD_en_write();
  delay_nus(40);
  LCD_write_command(0x28);  //4位显示
  LCD_write_command(0x0c);  //显示开
  LCD_write_command(0x01);  //清屏
  delay_nms(2);
}

void LCD_en_write(void)  //液晶使能
{
  LCD1602_EN=1;
  _nop()_;
  LCD1602_EN=0;
}

void LCD_write_command(unsigned char command) //写指令
{
  delay_nus(16);
  LCD1602_RS=0;        //RS=0
  LCD_DATA&=0X0f;         //清高四位
  LCD_DATA|=command&0xf0; //写高四位
  LCD_en_write();
  command=command<<4;          //低四位移到高四位
  LCD_DATA&=0x0f;         //清高四位
  LCD_DATA|=command&0xf0; //写低四位
  LCD_en_write();
  
}

void LCD_write_data(unsigned char Recdata) //写数据
{
  delay_nus(16);
  LCD1602_RS=1;       //RS=1
  LCD_DATA&=0X0f;       //清高四位
  LCD_DATA|=Recdata&0xf0;  //写高四位
  LCD_en_write();
  Recdata=Recdata<<4;               //低四位移到高四位
  LCD_DATA&=0X0f;        //清高四位
  LCD_DATA|=Recdata&0xf0;   //写低四位
  LCD_en_write();
}


void LCD_set_xy( unsigned char x, unsigned char y )  //写地址函数
{
    unsigned char address;
    if (y == 0) address = 0x80 + x;
    else address = 0xc0 + x;
    LCD_write_command(address);
}

void  LCD_write_char(unsigned char X,unsigned char Y,unsigned char Recdata) //列x=0~15,行y=0,1
{
  LCD_set_xy(X, Y); //写地址
  LCD_write_data(Recdata);
}

int LCD_PutStr(unsigned char *DData,int pos){	
unsigned char i;	
	if(pos==-1){		
		LCD_write_command(0x01);  //清屏
		delay_nms(2);
		pos=0;	
		}	
	while((*DData)!='\0'){		
	switch(*DData){			
				case '\n':	//如果是\n,则换行			
				{				
				if(pos<17){					
						for(i=pos;i<16;i++)
								LCD_write_char(i%16, i/16, ' ');
								pos=16;
								}
								else{					
								for(i=pos;i<32;i++) LCD_write_char(i%16, i/16, ' ');
								pos=32;
								}				
				break;			
				}
			
				case '\b':	//如果是\b,则退格			
				{
				if(pos>0) pos--;			
				LCD_write_char(pos%16, pos/16, ' ');				
				break;			
				}
			
				default:			
				{				
				
				if((*DData)<0x20){
				*DData=' ';
				}
				
				LCD_write_char(pos%16, pos/16,*DData);
				pos++;				
				break;			
				}		
					}		
					DData++;	
						}	
						return(pos);
						}

//----------------------------以下函数用于输出数字---------------------
int LCD_PutNum(unsigned long num,int XS,int pos){    //从右边数,保留几位小数
	unsigned long tmp=0;	
	unsigned char numbits=0;	
	if(pos==-1){		
	LCD_write_command(0x01);
	delay_nms(2);		
	pos=0;	
	}	
	if(num==0){		
		LCD_write_char(pos%16, pos/16, '0');
		pos++;
		}
		else{		
		if(num<0){			
		LCD_write_char(pos%16, pos/16, '-');			
		num*=(-1);			
		pos++;		
		}		
		while(num){			
		tmp=tmp*10+(num%10);			
		num=num/10;			
		numbits++;
		}		
		while(tmp){			
		LCD_write_char(pos%16, pos/16, (tmp%10)+48);
		tmp=tmp/10;
		pos++;
		numbits--;
		if(numbits==XS) pos=LCD_PutStr(".",pos); //显示小数点
		}
		while(numbits--){
		LCD_write_char(pos%16, pos/16, '0');
		pos++;
		}
		}
		return(pos);
}

⌨️ 快捷键说明

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