📄 lcd.c
字号:
/* 文件名:LCD.c */
/* LCD 控制器为HD44780U的1601液晶模块*/
#include "hardware.h"
#ifndef __LCD_H__
#define __LCD_H__
typedef struct bits //定义可位操作的结构体
{
unsigned int bit0:1;
unsigned int bit1:1;
unsigned int bit2:1;
unsigned int bit3:1;
unsigned int bit4:1;
unsigned int bit5:1;
unsigned int bit6:1;
unsigned int bit7:1;
unsigned int bit8:1;
unsigned int bit9:1;
unsigned int bit10:1;
unsigned int bit11:1;
unsigned int bit12:1;
unsigned int bit13:1;
unsigned int bit14:1;
unsigned int bit15:1;
} BITS;
typedef union bit_word
{
unsigned int word;
BITS b;
} BIT_WORD;
#define BITSET(x, y) x.b.bit ## y = 1 //置位
#define BITCLR(x, y) x.b.bit ## y = 0 //清零
void Init_LCD(); // 初始化LCD
unsigned int ReadBF(); //读忙标志位
void LCD_Operate(unsigned char Operator, unsigned int Operand);//发数据或命令
void Toggle(); //E下降沿
void Display16CharString(unsigned char *str); //输出16个字符
#endif
/* 初始化LCD*/
void Init_LCD()
{
LCD_Operate('C', 0x38);//单行显示 5*7点阵
LCD_Operate('C', 0x01);//清LCD显示屏
LCD_Operate('C', 0x06);//地址计数器递增,显示屏不移动
LCD_Operate('C', 0x0c);//开显示,关光标,关闪烁
return;
}
//发数据或命令
void LCD_Operate(unsigned char Operator, unsigned int Operand)
{
unsigned int x;
BIT_WORD w;
while(ReadBF());
SP_Import(Port_IOB_Data, &x);
w.word = x & 0xff00;
switch(Operator)
{
case 'C':
{
BITCLR(w, 13);//RS=0;
BITCLR(w, 14);//RW=0;
break;
}
case 'D':
{
BITSET(w, 13);//RS=1;
BITCLR(w, 14);//RW=0;
break;
}
default:
return;
}
Operand |= w.word;
SP_Init_IOB(0xffff, 0x0000, 0xffff);
SP_Export(Port_IOB_Data, Operand);
Toggle();
SP_Init_IOB(0xff00, 0x0000, 0xff00);
return;
}
//读忙标志位
unsigned int ReadBF()
{
unsigned int x, LcdState;
BIT_WORD w;
SP_Import(Port_IOB_Data, &x);
w.word = x & 0xff00;
//RS=0;
BITCLR(w, 13);
//RW=1;
BITSET(w, 14);
SP_Export(Port_IOB_Data, w.word);
//E=1;
SP_Import(Port_IOB_Data, &x);
w.word = x & 0xff00;;
BITSET(w, 15);
SP_Export(Port_IOB_Data, w.word);
SP_Import(Port_IOB_Data, &LcdState);
LcdState &= 0x0080;
//E=0;
SP_Import(Port_IOB_Data, &x);
w.word = x & 0xff00;
BITCLR(w, 15);
SP_Export(Port_IOB_Data, w.word);
return LcdState;
}
/* This function toggles the E line (LCD). */
void Toggle()
{
unsigned int x;
BIT_WORD w;
//E=1;
SP_Import(Port_IOB_Data, &x);
w.word = x;
BITSET(w, 15);
SP_Export(Port_IOB_Data, w.word);
//E=0;
SP_Import(Port_IOB_Data, &x);
w.word = x;
BITCLR(w, 15);
SP_Export(Port_IOB_Data, w.word);
return;
}
/* 显示16个字符*/
void Display16CharString(unsigned char *str)
{
unsigned char position;
int i;
position = 0;
while (position<16)
{
LCD_Operate('C', 0x80);
for(i=0;i<8;i++)
{
LCD_Operate('D', str[position]);
position++;
}
LCD_Operate('C', 0xc0);
for(i = 0; i < 8; i++)
{
LCD_Operate('D', str[position]);
position++;
}
}
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -