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

📄 1602.h

📁 avr单片机编写的1602液晶显示屏程序
💻 H
字号:
/********************************************************
*  函数库说明:LCD1602B基本驱动函数库                   
*  版本:      v0.0                                     
*  作者:      cht                                      
*  日期:      2007年11月14日                           
*  修改:      cht                                      
*  修改日期:  2007年11月14日                           
*                                                     
*  说明:                                               
*	LCD驱动采用4线驱动方法,数据的双向传输由PA口的PA7-PA6-PA5-PA4 
*   LCD_write函数功能:当command=0时,向LCD写入数据,否则向LCD写入命令  
*   LCD第一行显示寄存器地址:0X80-0X8F  
*   LCD第二行显示寄存器地址:0XC0-0XCF  
*********************************************************/
#include <util/delay.h>
#include <avr/io.h>
#include "avr_m16.h"

#ifndef LCD_1602
#define LCD_1602

/*--------------------------------------
    常 数 宏 定 义    
----------------------------------------*/
#define LCD_RS_PORT PORTB
#define LCD_WR_PORT PORTB
#define LCD_EN_PORT PORTB

#define LCD_DATA_PORT PORTD
#define LCD_DATA_PIN  PIND
#define LCD_DATA_DDR DDRD

#define LCD_RS_H  _BV(PB5)
#define LCD_WR_H  _BV(PB6)
#define LCD_EN_H  _BV(PB7)


#define LCD_RS_L ~_BV(PB5)
#define LCD_WR_L ~_BV(PB6)
#define LCD_EN_L ~_BV(PB7)

/*--------------------------------------
	函 数 声 明
----------------------------------------*/

void LCD_init (void);
void LCD_write_en (void);
void LCD_write(uint8_t cmd ,uint8_t data);
void LCD_waite (void);
void LCD_set_xy(uint8_t x,uint8_t y);
void LCD_write_string(uint8_t x,uint8_t y,char *s);
void LCD_self_def(uint8_t add,uint8_t table[]);
void LCD_write_char(uint8_t x,uint8_t y,uint8_t c);
/*---------------------------------------------------
void LCD_write_en (void)
说明:允许向LCD写
	1、是写“命令”还是“数据”要看 LCD_ write函数中的 cmd 参数
		
-----------------------------------------------------*/
void LCD_write_en (void)
{
	LCD_EN_PORT |=LCD_EN_H;
	wait_us(15);
	LCD_EN_PORT &=LCD_EN_L;
}

/*---------------------------------------------------
void LCD_write (uint8_t cmd ,uint8_t data);
说明:向LCD写
	 1、 cmd<>0 写命令 cmd为指令,data=0
	 2、 cmd=0   写数据 data 为数据	
-----------------------------------------------------*/
 void LCD_write(uint8_t cmd ,uint8_t data)
 {
	uint8_t cmd_temp,data_temp;
	cmd_temp = cmd;
	data_temp = data;
	LCD_waite();
	LCD_WR_PORT &=LCD_WR_L;
	LCD_DATA_PORT &= 0X0f;  
	if(cmd==0)
	{
		 LCD_RS_PORT |= LCD_RS_H;             //RS=1   
         LCD_DATA_PORT |= data_temp&0xf0;   //送数据的高 4bit  
	}
	else
	{
		 LCD_RS_PORT &= LCD_RS_L;             //RS=0  
         LCD_DATA_PORT |= cmd_temp&0xf0;   //送命令的高4bit  		
	};
	LCD_write_en();  
    LCD_DATA_PORT &= 0X0f;  
    cmd_temp=cmd_temp << 4;         //send low 4bit  
    data_temp=data_temp << 4;  
           
    if (cmd==0)  
        LCD_DATA_PORT |= data_temp&0xf0;  
    else  
        LCD_DATA_PORT |= cmd_temp&0xf0;
  
    LCD_write_en();  
    LCD_WR_PORT |= LCD_WR_H;  
    LCD_RS_PORT &= LCD_RS_L; 	
	
 }
 
 
 
/*----------------------------------------------------------
void LCD_waite (void)
说明:等待LCD不忙

------------------------------------------------------------*/ 
void LCD_waite (void)
{
    LCD_DATA_DDR &= ~0x80;                 //PA7 I/O口方向设置为输入    
    LCD_WR_PORT |= LCD_WR_H;                 //RW=1  
    LCD_RS_PORT &= LCD_RS_L;                //RS=0  
    LCD_EN_PORT |= LCD_EN_H;                 //EN=1  
    while (!( LCD_DATA_PIN&0x80 ) == 0);   //RW=1,读PD7,为0表示空闲;  
    LCD_EN_PORT &= LCD_EN_L;                //EN=0  
    LCD_DATA_DDR |= 0xf0; 
}

/*-------------------------------------------
void LCD_init(void)
说明:LCD初始化设置

---------------------------------------------*/
void LCD_init (void)
{
	_delay_ms(50);
	
	LCD_write(0x30,0); 
    _delay_ms(6); 
    LCD_write(0x30,0);       
    _delay_ms(1); 
    LCD_write(0x30,0);        
    _delay_ms(1); 
    LCD_write(0x02,0);                
    _delay_ms(1);
	
	
	LCD_write(0x28,0);      //4bit Display  
    _delay_ms(1); 
    LCD_write(0x08,0);      // 显示关闭  
    _delay_ms(1); 
        
    LCD_write(0x01,0);      // 显示清屏   
    _delay_ms(1); 
         
    LCD_write(0x06,0);      // 显示光标移动设置  
    _delay_ms(1);         
         
    LCD_write(0x0c,0);     // 显示开及光标设置  
    _delay_ms(30); 
	
}


/*-----------------------------------------------------
void LCD_set_xy(uint8_t x,uint8_t y)
说明:设置LCD起始显示位置
		显示字符串的位置,X:0-15,Y:0-1  
        LCD第一行显示寄存器地址:0X80-0X8F  
        LCD第一行显示寄存器地址:0XC0-0XCF 	
-------------------------------------------------------*/
void LCD_set_xy(uint8_t x,uint8_t y)
{
   uint8_t address;  
    if (y == 0) 
		address = 0x80 + x; //第一行显示 
    else   
        address = 0xc0 + x; //第二行显示 
    LCD_write( address, 0 ); 
}
 
/*-------------------------------------------------------
void LCD_write_string(uint8_t x,uint8_t y,char *s)
说明: 英文字符串显示函数  
       输入参数:*s(英文字符串指)  
        X、Y    : 显示字符串的位置 
---------------------------------------------------------*/ 
void LCD_write_string(uint8_t x,uint8_t y,char *s)
{
    LCD_set_xy( x, y );  
    while (*s)   
    {  
        LCD_write( 0, *s );  
        s ++;  
    }  
}
/*-----------------------------------------------------------
void LCD_self_def(uint8_t add,uint8_t *table)
说明:把自定义的点阵符号数据写入CGRAM中
	1 ADD为数据的地址;
	2 table为相应的数据
-------------------------------------------------------------*/
void LCD_self_def(uint8_t add,uint8_t table[])
{
 uint8_t i;

 for (i=0;i<8;i++)
  {
	LCD_write(add+i,0); // 设置自定义字符的 CGRAM 地址  
    _delay_ms(2);
    LCD_write(0,table[i]); // 向CGRAM写入自定义字符表的数据
	_delay_ms(2);
  };
}

/*------------------------------------------------------------
void LCD_write_char(uint8_t x,uint8_t y,uint8_t c)
说明:向LCD写一个字符;
	x,y为坐标 ;c 为所写字符
--------------------------------------------------------------*/
void LCD_write_char(uint8_t x,uint8_t y,uint8_t c)
{
   LCD_set_xy(x,y );
  _delay_ms(2); 
  LCD_write(0,c);
}  
#endif

⌨️ 快捷键说明

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