📄 lcd.c
字号:
#include <string.h>
#define LCD_GLOBALS
#include "Includes.h"
sbit PIN_RS = P2 ^ 2;
sbit PIN_RW = P2 ^ 1;
sbit PIN_EN = P2 ^ 0;
static void CheckBusy(void)
{
UWORD cnt = 0;
P0 = 0xff; // ready for input
while (1) {
PIN_RS = 0;
PIN_RW = 1;
PIN_EN = 1;
if (((P0 & 0x80) != 0x80) || ++cnt > 500)
break;
PIN_EN = 0;
}
PIN_EN = 0;
}
#define WriteIns LcdWriteIns
void LcdWriteIns(UBYTE i)
{
CheckBusy();
PIN_RS = 0;
PIN_RW = 0;
PIN_EN = 1;
P0 = i;
PIN_EN = 0;
}
void LcdWriteData(UBYTE d)
{
CheckBusy();
PIN_RS = 1;
PIN_RW = 0;
PIN_EN = 1;
P0 = d;
PIN_EN = 0;
}
#if 0
static void WriteIns(UBYTE i)
{
CheckBusy();
PIN_RS = 0;
PIN_RW = 0;
PIN_EN = 1;
P0 = i;
PIN_EN = 0;
}
static void WriteData(UBYTE d)
{
CheckBusy();
PIN_RS = 1;
PIN_RW = 0;
PIN_EN = 1;
P0 = d;
PIN_EN = 0;
}
void LcdDispCtl(UBYTE c)
{
WriteIns(0x08 + (c & 0x07));
}
void LcdDispClr(UBYTE type)
{
if (type == LCD_CLR_ALL)
WriteIns(0x01);
else {
UBYTE i;
if (type == LCD_CLR_FL)
WriteIns(0x80);
else if (type == LCD_CLR_SL)
WriteIns(0xc0);
else
return;
for (i = 0; i < LINE_FACT_WIDTH; i ++)
WriteData(' ');
}
}
void LcdCurPos(UBYTE row, UBYTE col)
{
UBYTE n = row * LINE_MAX_WIDTH + col;
LcdDispCtl(LCD_BLK_ON | LCD_CUR_ON | LCD_DISP_ON);
WriteIns(0x80 + row * LINE_MAX_WIDTH + col);
}
//#endif
void LcdDispBuf(UBYTE row, UBYTE col, UBYTE len, const void *buf)
{
UBYTE *p = (UBYTE *)buf;
LcdWriteIns(0x80 + row * DISPLAY_MLEN + col);
while (len --) {
LcdWriteData(*p);
if ((row == 0) && (++ col >= DISPLAY_FLEN)) {
row = 1;
LcdWriteIns(0x80 + DISPLAY_MLEN);
}
p ++;
}
}
//#if 0
void LcdDispMult(UBYTE row, UBYTE col, UBYTE n, UBYTE asc)
{
WriteIns(0x80 + row * LINE_MAX_WIDTH + col);
while (n --) {
WriteData(asc);
if ((row == 0) && (++ col >= LINE_FACT_WIDTH)) {
row = 1;
WriteIns(0x80 + LINE_MAX_WIDTH);
}
}
}
void LcdDispAsc(UBYTE row, UBYTE col, const void *buf)
{
UBYTE len = strlen(buf);
LcdDispBuf(row, col, len, buf);
}
UBYTE LcdDispNum(UBYTE row, UBYTE col, ULONG num)
{
UBYTE i, buf[10];
for (i = 0; i < 10; i ++) {
buf[9 - i] = num % 10 + 0x30;
num /= 10;
}
for (i = 0; i < 9; i ++) {
if (buf[i] != 0x30)
break;
}
LcdDispBuf(row, col, 10 - i, &buf[i]);
return 10 - i;
}
#endif
// initial
void LcdInit(void)
{
PIN_RS = 0;
PIN_RW = 0;
PIN_EN = 0;
// set function
WriteIns(0x38); // 8 bit initerface, 2 line display, 5 * 7 dots
// display on/off
WriteIns(0x0c); // display on, cursor off, blink off
// display clear
WriteIns(0x01);
// entry mode set
WriteIns(0x06); // increase, display is not shifted
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -