📄 1602.h
字号:
/* 用法:
LCD_init();
LCD_write_string(列,行,"字符串");
LCD_write_char(列,行,'字符');
---------------------------------------------------------------
下面是AVR与LCD连接信息
PC4 ->RS
PC5 ->EN
地 ->RW
PC0 ->D4
PC1 ->D5
PC2 ->D6
PC3 ->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 PORTA //以下3个要设为同一个口
#define LCD_DATA_DDR DDRA //默认情况下连线必须使用高四位端口,如果不是请注意修改
#define LCD_DATA_PIN PINA
#define LCD_RS (1<<PC6) //0x20 portC6 out
#define LCD_EN (1<<PC7) //0x40 portC7 out
#define LCD_DATA ((1<<PA4)|(1<<PA5)|(1<<PA6)|(1<<PA7)) //0xf0 portA 4/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);
//-----------------------------------------------------------------------------------------
#include <macros.h>
#include "delay.h"
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();
delay_nus(40);
LCD_write_command(0x28); //4位显示
LCD_write_command(0x0c); //显示开
LCD_write_command(0x01); //清屏
delay_nms(2);
}
void LCD_en_write(void) //液晶使能
{
LCD_EN_PORT|=LCD_EN;
delay_nus(1);
LCD_EN_PORT&=~LCD_EN;
}
void LCD_write_command(unsigned char command) //写指令
{
//连线为高4位的写法
delay_nus(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();
/*
//连线为低四位的写法
delay_nus(16);
LCD_RS_PORT&=~LCD_RS; //RS=0
LCD_DATA_PORT&=0xf0; //清高四位
LCD_DATA_PORT|=(command>>4)&0x0f; //写高四位
LCD_en_write();
LCD_DATA_PORT&=0xf0; //清高四位
LCD_DATA_PORT|=command&0x0f; //写低四位
LCD_en_write();
*/
}
void LCD_write_data(unsigned char data) //写数据
{
//连线为高4位的写法
delay_nus(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();
/*
//连线为低四位的写法
delay_nus(16);
LCD_RS_PORT|=LCD_RS; //RS=1
LCD_DATA_PORT&=0Xf0; //清高四位
LCD_DATA_PORT|=(data>>4)&0x0f; //写高四位
LCD_en_write();
LCD_DATA_PORT&=0Xf0; //清高四位
LCD_DATA_PORT|=data&0x0f; //写低四位
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_string(unsigned char X,unsigned char Y,unsigned char *s) //列x=0~15,行y=0,1
{
LCD_set_xy( X, Y ); //写地址
while (*s) // 写显示字符
{
LCD_write_data( *s );
s ++;
}
}
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);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -