📄 lcd1602.h
字号:
//1602B驱动库函数
//在主程序中加入: #include<stdio.h>
//语句:fdevopen(LCD_putc,0,0);
//即可使用printf()实现标准格式输出
//Redstone编写-2005.11.8
#define LCD_EN_PORT PORTC
#define LCD_RW_PORT PORTC
#define LCD_RS_PORT PORTC
#define LCD_CROL_DDR DDRC
#define LCD_DATA_PORT PORTD
#define LCD_DATA_DDR DDRD
#define LCD_DATA_PIN PIND
#define LCD_POWER_PORT PORTC
#define LCD_POWER_DDR DDRC
#define LCD_EN 0B00001000 //portd3 out
#define LCD_RW 0B00000100 //portd2 out
#define LCD_RS 0B00000010 //portc1 out
#define LCD_DATA 0x0f //porta4/5/6/7 out
#define LCD_VCC 0B00000001 //portd0 out
#define LCD_GND 0B00000100 //portd2 out
unsigned char X=0,Y=0;
void LCD_init (void);
void LCD_en_write (void);
void LCD_write_char (unsigned command,unsigned data);
void LCD_wait_busy (void);
void LCD_set_xy (unsigned char x, unsigned char y);
void LCD_init(void)
{
unsigned int i=0;
for (i=0;i<10;i++)
_delay_ms(100);
for (i=0;i<20;i++)
{
LCD_write_char(0x28,0); //4位显示
_delay_ms(15);
}
LCD_write_char(0x0c,0); //显示开
_delay_ms(5);
LCD_write_char(0x01,0);
}
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_char( address, 0 );
}
void LCD_en_write(void)
{
//LCD_wait_busy();
LCD_EN_PORT|=LCD_EN;
_delay_us(20);
LCD_EN_PORT&=~LCD_EN;
}
void LCD_write_char(unsigned command,unsigned data)
{
unsigned command_temp,data_temp,temp;
command_temp=command;
data_temp=data;
_delay_us(20);
if(command==0)
{
LCD_RS_PORT|=LCD_RS; //RS=1
temp=data_temp;
data_temp=data_temp>>4;
LCD_DATA_PORT&=~LCD_DATA;
LCD_DATA_PORT|=data_temp&LCD_DATA;
LCD_en_write();
LCD_DATA_PORT&=~LCD_DATA;
LCD_DATA_PORT|=temp&LCD_DATA; //
LCD_en_write();
}
else
{
LCD_RS_PORT&=~LCD_RS; //RS=0
temp=command_temp;
command_temp=command_temp>>4;
LCD_DATA_PORT&=~LCD_DATA;
LCD_DATA_PORT|=command_temp&LCD_DATA;
LCD_en_write();
LCD_DATA_PORT&=~LCD_DATA;
LCD_DATA_PORT|=temp&LCD_DATA;
LCD_en_write();
}
}
int LCD_putc(unsigned char c)//标准输入流
{
if (c=='\n') //换行
{Y^=1;
X=0;
}
else if (c=='\t') Y^=1;//下移一格
else if(c=='\v') X++;//左移一格
if (c<32) return 1;
LCD_set_xy(X,Y);
LCD_write_char(0,c);
X++;//左移一格
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -