📄 lcd.h
字号:
#ifndef __LCD__H
#define __LCD__H
#include <iom32v.h>
#include <macros.h>
#include "delay.h"
//定义MCU与LCD的接口
#define LCD_DATA_PORT PORTA
#define LCD_DATA_DDR DDRA
#define LCD_DATA_PIN PINA
#define LCD_DATA 0xf0 //portd4/5/6/7 out
#define LCD_EN_DDR DDRB
#define LCD_EN_PORT PORTB
#define LCD_EN (1<<PB0)
#define LCD_RW_DDR DDRB
#define LCD_RW_PORT PORTB
#define LCD_RW (1<<PB1)
#define LCD_RS_DDR DDRB
#define LCD_RS_PORT PORTB
#define LCD_RS (1<<PB2)
#define E_H LCD_EN_PORT|=LCD_EN
#define E_L LCD_EN_PORT&= ~LCD_EN
#define RW_H LCD_RW_PORT|=LCD_RW
#define RW_L LCD_RW_PORT&= ~LCD_RW
#define RS_H LCD_RS_PORT|=LCD_RS
#define RS_L LCD_RS_PORT&= ~LCD_RS
//函数声明
void LCD16xx_init (void);
void LCD16xx_en_write (void);
/************************************************************************/
/* LCD16xx清屏 */
/************************************************************************/
void LCD16xx_clr(void) ;
void LCD16xx_write_char (unsigned char command,unsigned char data);
void LCD16xx_wait_Ready (void);
void LCD16xx_set_xy (unsigned char x, unsigned char y);
void LCD16xx_write_string (unsigned char X,unsigned char Y,unsigned char *s);
void LCD_write_char(unsigned char x, unsigned char y, unsigned char da);
/**********************************************************
光标命令
LCD16xx_write_char(1,0x0e); //光标开
LCD16xx_write_char(1,0x0d); //光标所在字符闪烁
LCD16xx_write_char(1,0x0c); //光标关
**********************************************************/
/**********************************************************
TC16xxB LCD DISPLAY
建立时间:2006年08月30日
修改日期:2006年08月30日
LCD_write函数功能:当command=0时,向LCD写入数据,否则向LCD写
入命令
LCD第一行显示寄存器地址:0x80-0x8F
LCD第二行显示寄存器地址:0xC0-0xCF
**********************************************************/
void LCD16xx_init(void)
{
LCD_DATA_DDR |= LCD_DATA; //#define LCD_DATA 0xf0 //portd4/5/6/7 out 即是让A的高四位输出,低四位输入
LCD_EN_DDR |= LCD_EN; //#define LCD_EN (1<<PB0) 即是让对应EN的PB0为输出
LCD_RS_DDR |= LCD_RS; //#define LCD_RS (1<<PB2) 即是让对应RS的PB2为输出
LCD_RW_DDR |= LCD_RW; //#define LCD_RW (1<<PB1) 即是让对应RW的PS1为输出
RW_L; //#define RW_L LCD_RW_PORT&= ~LCD_RW 即是选择写入模式
LCD_DATA_PORT = 0x20; //#define LCD_DATA_PORT PORTA 结合上一行,指令6:DL=0(4位总线)、N=0(单行显示)、F=0(5*7)
LCD16xx_en_write(); //执行指令,给EN端一个脉冲
delay_nus(50);
LCD16xx_write_char(1,0x28); //4bit test
delay_nms(5);
LCD16xx_write_char(1,0x0c); //显示开,指令4:D=1(显示开)、C=1(光标开)、B=0(闪烁(光标?)关)
LCD16xx_write_char(1,0x01); //显示清屏,指令1
LCD16xx_write_char(1,0x06); //显示光标移动设置,指令3:I/D=1(光标递增移动)、SH=0(显示不移动)
}
void LCD16xx_en_write(void) //EN端产生一个高电平脉冲,写LCD
{
E_H; //#define E_H LCD_EN_PORT|=LCD_EN
delay_nus(4); //如果晶振频率太高,则可相应提高延时
E_L; //#define E_L LCD_EN_PORT&= ~LCD_EN
}
/************************************************************************/
/* LCD16xx清屏 */
/************************************************************************/
void LCD16xx_clr(void)
{
LCD16xx_write_char(1,0x01); //指令1
}
/*-----------------------------------------------------------------------
LCD16xx_write_char : 英文字符串显示函数
输入参数:*s :英文字符串指针;
X、Y : 显示字符串的位置,X:0-15,Y:0-1
LCD第一行显示寄存器地址:0x80-0x8F
LCD第一行显示寄存器地址:0xC0-0xCF
编写日期 :2006年08月30日
最后修改日期 :2006年08月30日
-----------------------------------------------------------------------*/
void LCD16xx_write_char(unsigned char command,unsigned char data)
{
unsigned char datah,datal;
datah = data; //采用四位总线,先送高四位,再送低四位
datal = data<<4 ;
LCD16xx_wait_Ready();
RW_L; //RW=0,选择写入
if (command == 0) //data
RS_H; //RS=1,选择数据寄存器
else //command
RS_L; //RS=0,选择命令寄存器
LCD_DATA_PORT &= 0x0F; //使得单片机数据端口的高四位清零(准备传输),低四位保持(没用到,别影响)
LCD_DATA_PORT |= datah&0xf0; //send high 4bit,使datah的低四位清零,然后与数据端口(高四位已清零)或
LCD16xx_en_write(); //执行指令
LCD_DATA_PORT &= 0x0F;
LCD_DATA_PORT |= datal&0xf0; //send low 4bit
LCD16xx_en_write();
}
void LCD16xx_wait_Ready(void) //等待LCD空闲
{
LCD_DATA_DDR &= ~0x80; //PD7 I/O口方向设置为输入
RW_H; //RW=1,选读出
RS_L; //RS=0,选指令寄存器
E_H; //EN=1,与下面的E_L一起,起执行指令的作用
while (!( LCD_DATA_PIN&0x80 ) == 0); //RW=1,读PD7,为0表示空闲;
E_L; //EN=0,与下面的E_H一起,起执行指令的作用
LCD_DATA_DDR |= LCD_DATA; //恢复数据口的传输方向为,11110000B
}
/*-----------------------------------------------------------------------
LCD_set_xy : 设置LCD显示的起始位置
输入参数:x、y : 显示字符串的位置,X:0-15,Y:0-1
LCD第一行显示寄存器地址:0x80-0x8F
LCD第一行显示寄存器地址:0xC0-0xCF
编写日期 :2006年08月30日
最后修改日期 :2006年08月30日
-----------------------------------------------------------------------*/
void LCD16xx_set_xy( unsigned char x, unsigned char y )
{
unsigned char address;
if (y == 0)
address = 0x80 + x;
else if(y == 1)
address = 0xc0 + x;
else if(y == 2)
address = 0x90 + x;
else
address = 0xd0 + x;
LCD16xx_write_char( 1,address );
}
/*-----------------------------------------------------------------------
LCD16xx_write_string : 英文字符串显示函数
输入参数:*s :英文字符串指针;
X、Y : 显示字符串的位置
编写日期 :2006年08月30日
最后修改日期 :2006年08月30日
-----------------------------------------------------------------------*/
void LCD16xx_write_string(unsigned char X,unsigned char Y,unsigned char *s) //调用的时候把数组的首地址
{ //送到指针s所指的内存,即是 unsigned char *s=a;(unsigned char *s=&a[0]),C语言中规定数组名代表数组的首地址
LCD16xx_set_xy( X, Y );
while (*s) //*s为指针s所指内存的内容
{
delay_nus(5);
LCD16xx_write_char( 0, *s ); //*s为指针s所指内存的内容
s++;
}
}
//C语言中的字符串是用空字符NUL('\0')结束的字符数组,例如"blue"包含了字符'b'、'l'、'u'、'e'和'\0'。
//详情请参明龙的书《C HOW TO PROGRAM Vloume 1 Fourth Edition》p260
void LCD_write_char(unsigned char x, unsigned char y, unsigned char da)
{
unsigned char X,Y;
unsigned char data;
X=x;Y=y;data=da;
LCD16xx_set_xy(X,Y);
delay_nus(5);
LCD16xx_write_char(0,data);
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -