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

📄 lcd1602.h

📁 很合用的LCD1602的程序
💻 H
字号:
/*---------------------------------------------------------
液晶LCD1602C  使用4条数据线(D4~D7)
编译器:ICC-AVR v6.31A 日期: 2005-11-24 20:29:57
目标芯片 : M16
时钟: 8.0000Mhz
-----------------------------------------------------------*/
/*-------------------------------------------------------------
LCD引脚定义
1---GND 
2---VCC
3---VO
4---RS
5---RW
6---EN
7到14--D0-D7
15--背景灯+
16--背景灯-
-----------------------------------------------------------------*/
#include <iom16v.h>
#include <macros.h>

/*---------------------------------------------------------------
下面是AVR与LCD连接信息
PA2 ->RS
PA3 ->EN
地  ->RW
PA4 ->D4
PA5 ->D5
PA6 ->D6
PA7 ->D7
要使用本驱动,改变下面配置信息即可
-----------------------------------------------------------------*/
#define LCD_EN_PORT    PORTC   //以下2个要设为同一个口
#define LCD_EN_DDR     DDRC
#define LCD_RS_PORT    PORTC   //以下2个要设为同一个口
#define LCD_RS_DDR     DDRC
#define LCD_DATA_PORT  PORTC   //以下3个要设为同一个口
#define LCD_DATA_DDR   DDRC    //一定要用高4位
#define LCD_DATA_PIN   PINC
#define LCD_RS         (1<<PC0) //   portC0       out
#define LCD_EN         (1<<PC1) //   portC1       out
#define LCD_DATA       ((1<<PC4)|(1<<PC5)|(1<<PC6)|(1<<PC7)) //0xf0   portC4/5/6/7 out
/*--------------------------------------------------------------------------------------------------
函数说明
--------------------------------------------------------------------------------------------------*/
void LCD_init(void);
void LCD_en_write(void);
void LCD_write_command(unsigned  char command) ;
void LCD_write_data(unsigned char data);
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 data);
//void delay_nus(unsigned int n);
//void delay_nms(unsigned int n);


void LCD_init(void)         //液晶初始化
{
  LCD_DATA_DDR|=LCD_DATA;   //数据口方向为输出
  LCD_EN_DDR|=LCD_EN;       //设置EN方向为输出
  LCD_RS_DDR|=LCD_RS;       //设置RS方向为输出
  LCD_write_command(0x28); 
  LCD_en_write();
  Delayus(40);
  LCD_write_command(0x28);  //4位显示
  LCD_write_command(0x0c);  //显示开
  LCD_write_command(0x01);  //清屏
  Delayms(2);
}

void LCD_en_write(void)  //液晶使能
{
  LCD_EN_PORT|=LCD_EN;
  Delayus(1);
  LCD_EN_PORT&=~LCD_EN;
}

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

void LCD_write_data(unsigned char data) //写数据
{
  Delayus(16);
  LCD_RS_PORT|=LCD_RS;       //RS=1
  LCD_DATA_PORT&=0X0f;       //清高四位
  LCD_DATA_PORT|=data&0xf0;  //写高四位
  LCD_en_write();
  data=data<<4;               //低四位移到高四位
  LCD_DATA_PORT&=0X0f;        //清高四位
  LCD_DATA_PORT|=data&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 data) //列x=0~15,行y=0,1
{
  LCD_set_xy(X, Y); //写地址
  LCD_write_data(data);
}

int LCD_PutStr(unsigned char *DData,int pos)
{	
 unsigned char i;	
	if(pos==-1){		
		LCD_write_command(0x01);  //清屏
		Delayms(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);     //返回的pos为下一个显示的地址
						}

//----------------------------以下函数用于输出数字---------------------
int LCD_PutNum(unsigned long num,int XS,int pos)  //从右边数,保留几位小数  pos为0-31
{    
	unsigned long tmp=0;	
	unsigned char numbits=0;	
	if(pos==-1){		
	LCD_write_command(0x01);
	Delayms(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);       //返回的pos为下一个显示的地址
}

//void  LCD_write_char(unsigned char X,unsigned char Y,unsigned char data) //列x=0~15,行y=0,1
//int LCD_PutStr(unsigned char *DData,int pos)
//int LCD_PutNum(unsigned long num,int XS,int pos)  //从右边数,保留几位小数  pos为0-31
/*
void main(void)
{
  unsigned int q;
  LCD_init();
   init_devices();
   LCD_PutStr("Dear I Love You",0);
   LCD_PutStr("!",15);
   LCD_PutNum(1234,2,16);
   LCD_write_char(5,1,'!');
   q=0xfafa;
   LCD_PutNum(q,3,25);
//   LCD_write_char(7,1,q);
}
*/

⌨️ 快捷键说明

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