📄 lcd.c
字号:
/* */
/****************************************************************************
Title : HD44780 LCD library 六线驱动
Authors:
Software: ICCAVR6.31A
Target: M8
Copyright:
*****************************************************************************/
#include <iom8v.h>
#include <macros.h>
#include "lcd.h"
#include "delay.h"
#define LCD_EN_high() LCD_EN_PORT |= (1<<LCD_EN_PIN)
#define LCD_EN_low() LCD_EN_PORT &=~(1<<LCD_EN_PIN)
#define LCD_CMD_mode() LCD_RS_PORT &=~(1<<LCD_RS_PIN) /* RS=0 command mode */
#define LCD_DATA_mode() LCD_RS_PORT |= (1<<LCD_RS_PIN) /* RS=1 data mode */
/**********************************************************
光标命令
LCD_write_char(0x0e,0); //光标开
LCD_write_char(0x0d,0); //光标所在字符闪烁
LCD_write_char(0x0c,0); //光标关
**********************************************************/
/**********************************************************
TC1602B LCD DISPLAY
建立时间:2003年11月9号
修改日期:2003年11月14号
LCD_write函数功能:当command=0时,向LCD写入数据,否则向LCD写
入命令
LCD第一行显示寄存器地址:0X80-0X8F
LCD第二行显示寄存器地址:0XC0-0XCF
**********************************************************/
void lcd_init(void)
{
unsigned int i;
LCD_DATA_DDR_D7 |= (1<<LCD_DATA_PIN_D7); //设置接口为输出
LCD_DATA_DDR_D6 |= (1<<LCD_DATA_PIN_D6);
LCD_DATA_DDR_D5 |= (1<<LCD_DATA_PIN_D5);
LCD_DATA_DDR_D4 |= (1<<LCD_DATA_PIN_D4);
LCD_RS_DDR |= (1<<LCD_RS_PIN); // RS pin as output
LCD_EN_DDR |= (1<<LCD_EN_PIN); // E pin as output
delay_ms(10);
//lcd_4BIT_enable();
for (i=20;i>0;i--)
{
lcd_write_char(0,0x28); //4bit test
delay_ms(15);
}
lcd_write_char(0,0x0c); //显示开
lcd_write_char(0,0x01); //显示清屏
delay_ms(100);
lcd_write_char(0,0x06); //显示光标移动设置
lcd_set_char();
}
/*-----------------------------------------------------------------------
LCD_write_char : 英文字符串显示函数
输入参数:*s :英文字符串指针;
X、Y : 显示字符串的位置,X:0-15,Y:0-1
LCD第一行显示寄存器地址:0X80-0X8F
LCD第一行显示寄存器地址:0XC0-0XCF
编写日期 :2003-11-19
最后修改日期 :2004-8-19
-----------------------------------------------------------------------*/
void lcd_write_char(unsigned command,unsigned data)
{
if (command == 0)
LCD_CMD_mode(); //RS=0
else
LCD_DATA_mode(); //RS=1
lcd_write_half_char(data);
delay_us(20);
data=data << 4;
lcd_write_half_char(data);
delay_us(20);
}
/*---------写高4bit到LCD----*/
void lcd_write_half_char(unsigned data)
{
if (data&0x80) LCD_DATA_PORT_D7 |= (1<<LCD_DATA_PIN_D7);
else LCD_DATA_PORT_D7 &=~(1<<LCD_DATA_PIN_D7);
if (data&0x40) LCD_DATA_PORT_D6 |= (1<<LCD_DATA_PIN_D6);
else LCD_DATA_PORT_D6 &=~(1<<LCD_DATA_PIN_D6);
if (data&0x20) LCD_DATA_PORT_D5 |= (1<<LCD_DATA_PIN_D5);
else LCD_DATA_PORT_D5 &=~(1<<LCD_DATA_PIN_D5);
if (data&0x10) LCD_DATA_PORT_D4 |= (1<<LCD_DATA_PIN_D4);
else LCD_DATA_PORT_D4 &=~(1<<LCD_DATA_PIN_D4);
LCD_EN_high(); //EN端产生一个由低电平变高电平,写LCD
delay_us(10);
LCD_EN_low(); //EN端产生一个由高电平变低电平,写LCD
delay_us(1);
}
/*-----------------------------------------------------------------------
LCD_set_xy : 设置LCD显示的起始位置
输入参数:x、y : 显示字符串的位置,X:0-15,Y:0-1
LCD第一行显示寄存器地址:0X80-0X8F
LCD第一行显示寄存器地址:0XC0-0XCF
编写日期 :2004-8-19
最后修改日期 :2004-8-19
-----------------------------------------------------------------------*/
void lcd_set_xy( unsigned char x, unsigned char y )
{
unsigned char address;
if (y == 0) address = LCD_START_LINE1 + x;
else
address = LCD_START_LINE2 + x;
lcd_write_char(0,address);
}
/*-----------------------------------------------------------------------
LCD_write_string : 英文字符串显示函数
输入参数:*s :英文字符串指针;
X、Y : 显示字符串的位置
编写日期 :2004-8-19
最后修改日期 :2004-8-19
-----------------------------------------------------------------------*/
void lcd_write_string(unsigned char X,unsigned char Y,unsigned char *s)
{
signed char i,j;
if(X>>(LCD_LINE_LENGTH-1)) return;
if(Y>>(LCD_LINES-1)) return;
i=LCD_LINE_LENGTH-X; //每行显示长度
lcd_set_xy( X, Y );
while (*s!=0 && i>0)
{
lcd_write_char(1,*s);
s ++;
i --;
//if(i--==0) return;
}
}
/*-----------------------------------------------------------------------
LCD_write_nchar : 英文n个字符串显示函数
输入参数:*s :英文字符串指针;
X、Y : 显示字符串的位置
编写日期 :2004-8-19
最后修改日期 :2004-8-19
-----------------------------------------------------------------------*/
void lcd_write_nchar(unsigned char X,unsigned char Y,unsigned char *s,unsigned int n)
{
lcd_set_xy( X, Y );
while (n)
{
lcd_write_char(1,*s);
s ++;
n --;
}
}
/*-----------------------------------------------------------------------
LCD_clear : 显示清屏函数
编写日期 :2005-10-14
最后修改日期 :2005-10-14
-----------------------------------------------------------------------*/
void lcd_clear(void)
{
lcd_write_char(0,0x01); //显示清屏
delay_ms(2);
}
/*-----------------------------------------------------------------------
LCD_4BIT_enable : 四位数据接口设定函数
编写日期 :2005-10-14
最后修改日期 :2005-10-14
-----------------------------------------------------------------------*/
void lcd_4BIT_enable(void)
{
LCD_DATA_PORT_D7 &=~(1<<LCD_DATA_PIN_D7);
LCD_DATA_PORT_D6 &=~(1<<LCD_DATA_PIN_D6);
LCD_DATA_PORT_D5 |= (1<<LCD_DATA_PIN_D5);
LCD_DATA_PORT_D4 &=~(1<<LCD_DATA_PIN_D4);
LCD_CMD_mode();
LCD_EN_high();
delay_ms(10);
LCD_EN_low();
delay_ms(1);
lcd_write_char(0,0x28);
}
/*-----------------------------------------------------------------------
LCD_set_char : 自定义字符函数
编写日期 :2005-10-14
最后修改日期 :2005-10-14
-----------------------------------------------------------------------*/
void lcd_set_char(void)
{
static const unsigned char set_char[64]=
{
0x18,0x18,0x07,0x08,0x08,0x08,0x07,0x00, // "℃" 代码=00H
0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10, // "* " 代码=01H
0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18, // "** " 代码=02H
0x1c,0x1c,0x1c,0x1c,0x1c,0x1c,0x1c,0x1c, // "*** " 代码=03H
0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e, // "**** " 代码=04H
0x08,0x0F,0x12,0x0F,0x0A,0x1F,0x02,0x02, // "年" 代码=05H
0x0F,0x09,0x0F,0x09,0x0F,0x09,0x11,0x00, // "月" 代码=06H
0x1F,0x11,0x11,0x1F,0x11,0x11,0x1F,0x00 // "日" 代码=07H
};
unsigned char i;
lcd_write_char(0,LCD_CGRAM_START); // 设定CGRAM首地址
for(i=0;i<64;i++)
{
lcd_write_char(1,set_char[i]);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -