📄 lcd.h
字号:
#define LCD_EN_PORT PORTB //以下2个要设为同一个口
#define LCD_EN_DDR DDRB
#define LCD_RS_PORT PORTB //以下2个要设为同一个口
#define LCD_RS_DDR DDRB
#define LCD_DATA_PORT PORTB //以下3个要设为同一个口
#define LCD_DATA_DDR DDRB //一定要用高4位
#define LCD_DATA_PIN PINB
#define LCD_RS (1<<PB7) //0x80 portB7 out
#define LCD_EN (1<<PB6) //0x60 portB6 out
#define LCD_DATA ((1<<PB5)|(1<<PB4)|(1<<PB3)|(1<<PB2))
unsigned char tab[]={'0','1','2','3','4','5','6','7','8','9'};
/*--------------------------------------------------------------------------------------------------
函数声明
--------------------------------------------------------------------------------------------------*/
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 LCD_write_shu(unsigned int x,unsigned int t,unsigned int n);
/*补充*/
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();
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) //写指令
{
unsigned char temp;
command=((command&0b10000000)>>7)|((command&0b01000000)>>5)|((command&0b00100000)>>3)|((command&0b00010000)>>1)|((command&0b00001000)<<1)|((command&0b00000100)<<3)|((command&0b00000010)<<5)|((command&0b00000001)<<7);
delay_nus(16);
temp=command<<2;
LCD_RS_PORT&=~LCD_RS; //RS=0
LCD_DATA_PORT&=0b11000011; //清高四位
LCD_DATA_PORT|=temp&0b00111100; //写高四位
LCD_en_write();
command=command>>2; //低四位移到高四位
LCD_DATA_PORT&=0b11000011; //清高四位
LCD_DATA_PORT|=command&0b00111100; //写低四位
LCD_en_write();
}
void LCD_write_data(unsigned char data) //写数据
{
unsigned char temp;
data=((data&0b10000000)>>7)|((data&0b01000000)>>5)|((data&0b00100000)>>3)|((data&0b00010000)>>1)|((data&0b00001000)<<1)|((data&0b00000100)<<3)|((data&0b00000010)<<5)|((data&0b00000001)<<7);
delay_nus(16);
temp=data<<2;
LCD_RS_PORT|=LCD_RS; //RS=1
LCD_DATA_PORT&=0b11000011; //清高四位
LCD_DATA_PORT|=temp&0b00111100; //写高四位
LCD_en_write();
data=data>>2; //低四位移到高四位
LCD_DATA_PORT&=0b11000011; //清高四位
LCD_DATA_PORT|=data&0b00111100; //写低四位
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);
}
void delay_1us(void) //1us延时函数
{
asm("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();
}
unsigned char *Format(unsigned char data,unsigned char *str)
{
str[0]=tab[(data/10)%10];
str[1]=tab[data%10];
return str;
}
void LCD_write_shu(unsigned int x,unsigned int t,unsigned int n)
{
unsigned int wan;
unsigned int qian;
unsigned int bai;
unsigned int shi;
unsigned int wei;
wan=n/10000;
qian=(n-wan*10000)/1000;
bai=(n-wan*10000-qian*1000)/100;
shi=(n-wan*10000-qian*1000-bai*100)/10;
wei=(n-wan*10000-qian*1000-bai*100-shi*10);
LCD_write_char(x++,t,tab[wan]);
LCD_write_char(x++,t,tab[qian]);
LCD_write_char(x++,t,tab[bai]);
LCD_write_char(x++,t,tab[shi]);
LCD_write_char(x,t,tab[wei]);
x=0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -