📄 lcd.c
字号:
#include "lcd.h"
#include <REGX51.H>
#define LCD_E P3_5
#define LCD_RS P3_7
#define LCD_RW P3_6
#define LCD_DBX P1
////////////////////////////////////////////////////////////////////////////
//
// 物理接口层
void lcdSetBus(BIT rs, BYTE dbx)
{
LCD_E = 1;
LCD_RS = rs;
LCD_RW = 0;
LCD_DBX = dbx;
LCD_E = 0;
}
/*
BYTE lcdGetBus(BIT rs)
{
LCD_E = 1;
LCD_RS = rs;
LCD_RW = 1;
LCD_E = 0;
LCD_E = 0;
LCD_DBX = 0xff;
return LCD_DBX;
}
*/
////////////////////////////////////////////////////////////////////////////
//
// 逻辑控制层
/*
BYTE lcdReadBusyFlagAddress(void)
{
return lcdGetBus(0);
}
*/
void lcdClearDisplay(void)
{
// while ( lcdReadBusyFlagAddress() & 0x80 );
DelayX1ms(2);
lcdSetBus(0,0x01);
DelayX1ms(2);
}
/*
void lcdReturnHome(void)
{
// while ( lcdReadBusyFlagAddress() & 0x80 );
lcdSetBus(0,0x02);
}
*/
void lcdEntryModeSet(BIT id, BIT s)
{
// while ( lcdReadBusyFlagAddress() & 0x80 );
lcdSetBus(0, 0x04|(id?0x02:0)|(s?0x01:0) );
DelayX1ms(1);
}
void lcdDisplayOnOffControl(BIT d, BIT c, BIT b)
{
// while ( lcdReadBusyFlagAddress() & 0x80 );
lcdSetBus(0,0x08|(d?0x04:0)|(c?0x02:0)|(b?0x01:0));
DelayX1ms(1);
}
/*
void lcdCursorDisplayShift(BIT sc, BIT rl)
{
// while ( lcdReadBusyFlagAddress() & 0x80 );
lcdSetBus(0,0x10|(sc?0x08:0)|(rl?0x04:0));
}
*/
void lcdFunctionSet(BIT dl, BIT n, BIT f)
{
// while ( lcdReadBusyFlagAddress() & 0x80 );
lcdSetBus(0,0x20|(dl?0x10:0)|(n?0x08:0)|(f?0x04:0));
DelayX1ms(1);
}
/*
void lcdSetCGRAMAddress(BYTE acg)
{
// while ( lcdReadBusyFlagAddress() & 0x80 );
lcdSetBus(0,0x40|(acg&0x3f));
}
*/
void lcdSetDDRAMAddress(BYTE add)
{
// while ( lcdReadBusyFlagAddress() & 0x80 );
lcdSetBus(0,0x80|(add&0x7f));
DelayX1ms(1);
}
void lcdWriteDataToCGDDRAM(BYTE bData)
{
// while ( lcdReadBusyFlagAddress() & 0x80 );
lcdSetBus(1,bData);
DelayX1ms(1);
}
/*
BYTE lcdReadDataFromCGDDRAM(void)
{
// while ( lcdReadBusyFlagAddress() & 0x80 );
return lcdGetBus(1);
}
*/
////////////////////////////////////////////////////////////////////////////
//
// 应用接口层
void lcdInit(void)
{
/////////////////////////
// 软复位
DelayX1ms(50);
lcdSetBus(0,0x30); DelayX1ms(5);
lcdSetBus(0,0x30); DelayX1ms(1);
lcdSetBus(0,0x30); DelayX1ms(1);
lcdFunctionSet(1,1,0); //(BIT dl, BIT n, BIT f) set n(lines) & f(font)
lcdDisplayOnOffControl(1,0,0); //(BIT d, BIT c, BIT b)
lcdClearDisplay();
lcdEntryModeSet(1,0); //(BIT id, BIT s)
/////////////////////////
// 初始化
DelayX1ms(10);
}
void lcdTextAddOut(char* pStr)
{
BYTE i = 0;
while (pStr[i])
{
lcdWriteDataToCGDDRAM( pStr[i] );
i++;
if (i>200) break;
}
}
void lcdTextOut(BYTE x, BYTE y, char* pStr)
{
BYTE i;
if (y & 0x01) x+=0x40;
if (y & 0x02) x+=20;
lcdSetDDRAMAddress( x );
i = 0;
while (pStr[i])
{
lcdWriteDataToCGDDRAM( pStr[i] );
i++;
if (i>200) break;
}
}
void lcdHexOut(BYTE byte)
{
BYTE h;
h = (byte>>4)&0x0f;
if (h < 0x0a) h += '0';
else h += ('A' - 0x0a);
lcdWriteDataToCGDDRAM( h );
h = byte&0x0f;
if (h < 0x0a) h += '0';
else h += ('A' - 0x0a);
lcdWriteDataToCGDDRAM( h );
}
void lcdUIntOut(BYTE byte, BYTE width)
{
while (width)
{
switch (width)
{
case 1: // x1
lcdWriteDataToCGDDRAM( '0'+ (byte % 10) );
break;
case 2: // x10
lcdWriteDataToCGDDRAM( '0'+ ((byte/10) % 10) );
break;
case 3: // x100
lcdWriteDataToCGDDRAM( '0'+ ((byte/100) % 10) );
break;
default:;
}
width--;
}
}
void lcdDateOut(DATETIME * pDtt)
{
//year
lcdTextAddOut("20");
lcdUIntOut(pDtt->yy,2);
lcdTextAddOut("-");
//month
lcdUIntOut(pDtt->mo,2);
lcdTextAddOut("-");
//day
lcdUIntOut(pDtt->dd,2);
}
void lcdTimeOut(DATETIME * pDtt)
{
//year
lcdUIntOut(pDtt->hh,2);
lcdTextAddOut(":");
//month
lcdUIntOut(pDtt->mi,2);
lcdTextAddOut(":");
//day
lcdUIntOut(pDtt->ss,2);
}
void lcdTempeOut(TEMPE* pT, BIT bConvertToF)
//-mmm.nn
{
BYTE m,n;
TEMPE tempe;
// 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111
const BYTE code div[] = { 00, 06, 13, 19, 25, 31, 38, 44, 50, 56, 63, 69, 75, 81, 88, 94, };
// const BYTE -div[] = { 94, 88, 81, 75, 69, 63, 56, 50, 44, 38, 31, 25, 19, 16, 06, 00, }; //正的倒序
tempe = *pT;
if (bConvertToF) ConvertCtoF(pT);
m = ((pT->th << 4) & 0xf0) | ((pT->tl >> 4) & 0x0f);
if (pT->th & 0x80)
{
lcdTextAddOut("-");
m = ~m;
n = div[(16 - pT->tl) & 0x0f];
}
else
{
lcdTextAddOut("+");
n = div[pT->tl & 0x0f];
}
lcdUIntOut( m / 100, 1 );
m%=100;
lcdUIntOut( m / 10, 1 );
m%=10;
lcdUIntOut( m, 1 );
lcdTextAddOut( "." );
lcdUIntOut( n / 10, 1 );
lcdUIntOut( n % 10, 1 );
lcdTextAddOut( bConvertToF ? "F" : "C" );
*pT = tempe;
}
// keyboard.c
#include "keyboard.h"
#include <REGX51.H>
#include "lcd.h"
#define KBD P0
BYTE kbdReadPort(void)
{
BYTE in,out;
out = 0;
KBD = 0x0f;
in = KBD;
while (in & 0x08)
{
in<<=1;
out++;
if (out>=4) return 0xff;
}
KBD = 0xf0;
in = KBD;
while (in & 0x80)
{
in<<=1;
out+=4;
if (out>=16) return 0xff;
}
// lcdTextOut(15,3,""); lcdHexOut(out); // for debug only
return out;
}
BYTE kbdGetKeyPress(void)
{
static BYTE lastStatus = 0xff;
BYTE now,rt;
rt = 0xff;
now = kbdReadPort();
if (now < 16)
{
if (lastStatus >= 16)
{
//key down
rt = now;
DelayX1ms(50);
}
}
else
{
if (lastStatus < 16)
{
//key up
DelayX1ms(50);
}
}
lastStatus = now;
return rt;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -