📄 lcd.c
字号:
outb(LCD_DATA_POUT, (inb(LCD_DATA_POUT)&0x0F) | (data<<4) ); // output data, low 4 bits
LCD_DELAY; // wait
LCD_DELAY; // wait
cbi(LCD_CTRL_PORT, LCD_CTRL_E); // clear "E" line
#else
// 8 bit write
sbi(LCD_CTRL_PORT, LCD_CTRL_E); // set "E" line
outb(LCD_DATA_DDR, 0xFF); // set data I/O lines to output (8bit)
outb(LCD_DATA_POUT, data); // output data, 8bits
LCD_DELAY; // wait
LCD_DELAY; // wait
cbi(LCD_CTRL_PORT, LCD_CTRL_E); // clear "E" line
#endif
// leave data lines in input mode so they can be most easily used for other purposes
#ifdef LCD_DATA_4BIT
outb(LCD_DATA_DDR, inb(LCD_DATA_DDR)&0x0F); // set data I/O lines to input (4bit)
outb(LCD_DATA_POUT, inb(LCD_DATA_POUT)|0xF0); // set pull-ups to on (4bit)
#else
outb(LCD_DATA_DDR, 0x00); // set data I/O lines to input (8bit)
outb(LCD_DATA_POUT, 0xFF); // set pull-ups to on (8bit)
#endif
#else
// memory bus write
//sbi(MCUCR, SRW); // enable RAM waitstate
lcdBusyWait(); // wait until LCD not busy
*((volatile unsigned char *) (LCD_DATA_ADDR)) = data;
//cbi(MCUCR, SRW); // disable RAM waitstate
#endif
}
u08 lcdDataRead(void)
{
// read a data byte from the display
register u08 data;
#ifdef LCD_PORT_INTERFACE
lcdBusyWait(); // wait until LCD not busy
#ifdef LCD_DATA_4BIT
outb(LCD_DATA_DDR, inb(LCD_DATA_DDR)&0x0F); // set data I/O lines to input (4bit)
outb(LCD_DATA_POUT, inb(LCD_DATA_POUT)|0xF0); // set pull-ups to on (4bit)
#else
outb(LCD_DATA_DDR, 0x00); // set data I/O lines to input (8bit)
outb(LCD_DATA_POUT, 0xFF); // set pull-ups to on (8bit)
#endif
sbi(LCD_CTRL_PORT, LCD_CTRL_RS); // set RS to "data"
sbi(LCD_CTRL_PORT, LCD_CTRL_RW); // set R/W to "read"
#ifdef LCD_DATA_4BIT
// 4 bit read
sbi(LCD_CTRL_PORT, LCD_CTRL_E); // set "E" line
LCD_DELAY; // wait
LCD_DELAY; // wait
data = inb(LCD_DATA_PIN)&0xF0; // input data, high 4 bits
cbi(LCD_CTRL_PORT, LCD_CTRL_E); // clear "E" line
LCD_DELAY; // wait
LCD_DELAY; // wait
sbi(LCD_CTRL_PORT, LCD_CTRL_E); // set "E" line
LCD_DELAY; // wait
LCD_DELAY; // wait
data |= inb(LCD_DATA_PIN)>>4; // input data, low 4 bits
cbi(LCD_CTRL_PORT, LCD_CTRL_E); // clear "E" line
#else
// 8 bit read
sbi(LCD_CTRL_PORT, LCD_CTRL_E); // set "E" line
LCD_DELAY; // wait
LCD_DELAY; // wait
data = inb(LCD_DATA_PIN); // input data, 8bits
cbi(LCD_CTRL_PORT, LCD_CTRL_E); // clear "E" line
#endif
// leave data lines in input mode so they can be most easily used for other purposes
#else
// memory bus read
//sbi(MCUCR, SRW); // enable RAM waitstate
lcdBusyWait(); // wait until LCD not busy
data = *((volatile unsigned char *) (LCD_DATA_ADDR));
//cbi(MCUCR, SRW); // disable RAM waitstate
#endif
return data;
}
/*************************************************************/
/********************* PUBLIC FUNCTIONS **********************/
/*************************************************************/
void lcdInit()
{
// initialize hardware
lcdInitHW();
// LCD function set
lcdControlWrite(LCD_FUNCTION_DEFAULT);
// clear LCD
lcdControlWrite(1<<LCD_CLR);
delay(60000); // wait 60ms
// set entry mode
lcdControlWrite(1<<LCD_ENTRY_MODE | 1<<LCD_ENTRY_INC);
// set display to on
//lcdControlWrite(1<<LCD_ON_CTRL | 1<<LCD_ON_DISPLAY | 1<<LCD_ON_BLINK);
lcdControlWrite(1<<LCD_ON_CTRL | 1<<LCD_ON_DISPLAY );
// move cursor to home
lcdControlWrite(1<<LCD_HOME);
// set data address to 0
lcdControlWrite(1<<LCD_DDRAM | 0x00);
// load the first 8 custom characters
lcdLoadCustomChar((u08*)LcdCustomChar,0,0);
lcdLoadCustomChar((u08*)LcdCustomChar,1,1);
lcdLoadCustomChar((u08*)LcdCustomChar,2,2);
lcdLoadCustomChar((u08*)LcdCustomChar,3,3);
lcdLoadCustomChar((u08*)LcdCustomChar,4,4);
lcdLoadCustomChar((u08*)LcdCustomChar,5,5);
lcdLoadCustomChar((u08*)LcdCustomChar,6,6);
lcdLoadCustomChar((u08*)LcdCustomChar,7,7);
}
void lcdHome(void)
{
// move cursor to home
lcdControlWrite(1<<LCD_HOME);
}
void lcdClear(void)
{
// clear LCD
lcdControlWrite(1<<LCD_CLR);
}
void lcdGotoXY(u08 x, u08 y)
{
register u08 DDRAMAddr;
// remap lines into proper order
switch(y)
{
case 0: DDRAMAddr = LCD_LINE0_DDRAMADDR+x; break;
case 1: DDRAMAddr = LCD_LINE1_DDRAMADDR+x; break;
case 2: DDRAMAddr = LCD_LINE2_DDRAMADDR+x; break;
case 3: DDRAMAddr = LCD_LINE3_DDRAMADDR+x; break;
default: DDRAMAddr = LCD_LINE0_DDRAMADDR+x;
}
// set data address
lcdControlWrite(1<<LCD_DDRAM | DDRAMAddr);
}
void lcdLoadCustomChar(u08* lcdCustomCharArray, u08 romCharNum, u08 lcdCharNum)
{
register u08 i;
u08 saveDDRAMAddr;
// backup the current cursor position
saveDDRAMAddr = lcdControlRead() & 0x7F;
// multiply the character index by 8
lcdCharNum = (lcdCharNum<<3); // each character occupies 8 bytes
romCharNum = (romCharNum<<3); // each character occupies 8 bytes
// copy the 8 bytes into CG (character generator) RAM
for(i=0; i<8; i++)
{
// set CG RAM address
lcdControlWrite((1<<LCD_CGRAM) | (lcdCharNum+i));
// write character data
lcdDataWrite( PRG_RDB(lcdCustomCharArray+romCharNum+i) );
}
// restore the previous cursor position
lcdControlWrite(1<<LCD_DDRAM | saveDDRAMAddr);
}
void lcdPrintData(char* data, u08 nBytes)
{
register u08 i;
// check to make sure we have a good pointer
if (!data) return;
// print data
for(i=0; i<nBytes; i++)
{
lcdDataWrite(data[i]);
}
}
void lcdProgressBar(u16 progress, u16 maxprogress, u08 length)
{
u08 i;
u32 pixelprogress;
u08 c;
// draw a progress bar displaying (progress / maxprogress)
// starting from the current cursor position
// with a total length of "length" characters
// ***note, LCD chars 0-5 must be programmed as the bar characters
// char 0 = empty ... char 5 = full
// total pixel length of bargraph equals length*PROGRESSPIXELS_PER_CHAR;
// pixel length of bar itself is
pixelprogress = ((progress*(length*PROGRESSPIXELS_PER_CHAR))/maxprogress);
// print exactly "length" characters
for(i=0; i<length; i++)
{
// check if this is a full block, or partial or empty
// (u16) cast is needed to avoid sign comparison warning
if( ((i*(u16)PROGRESSPIXELS_PER_CHAR)+5) > pixelprogress )
{
// this is a partial or empty block
if( ((i*(u16)PROGRESSPIXELS_PER_CHAR)) > pixelprogress )
{
// this is an empty block
// use space character?
c = 0;
}
else
{
// this is a partial block
c = pixelprogress % PROGRESSPIXELS_PER_CHAR;
}
}
else
{
// this is a full block
c = 5;
}
// write character to display
lcdDataWrite(c);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -