📄 lcd.c.txt
字号:
00238 outb(LCD_DATA_POUT, (inb(LCD_DATA_POUT)&0x0F) | (data<<4) ); // output data, low 4 bits
00239 LCD_DELAY; // wait
00240 LCD_DELAY; // wait
00241 cbi(LCD_CTRL_PORT, LCD_CTRL_E); // clear "E" line
00242 #else
00243 // 8 bit write
00244 sbi(LCD_CTRL_PORT, LCD_CTRL_E); // set "E" line
00245 outb(LCD_DATA_DDR, 0xFF); // set data I/O lines to output (8bit)
00246 outb(LCD_DATA_POUT, data); // output data, 8bits
00247 LCD_DELAY; // wait
00248 LCD_DELAY; // wait
00249 cbi(LCD_CTRL_PORT, LCD_CTRL_E); // clear "E" line
00250 #endif
00251 // leave data lines in input mode so they can be most easily used for other purposes
00252 #ifdef LCD_DATA_4BIT
00253 outb(LCD_DATA_DDR, inb(LCD_DATA_DDR)&0x0F); // set data I/O lines to input (4bit)
00254 outb(LCD_DATA_POUT, inb(LCD_DATA_POUT)|0xF0); // set pull-ups to on (4bit)
00255 #else
00256 outb(LCD_DATA_DDR, 0x00); // set data I/O lines to input (8bit)
00257 outb(LCD_DATA_POUT, 0xFF); // set pull-ups to on (8bit)
00258 #endif
00259 #else
00260 // memory bus write
00261 //sbi(MCUCR, SRW); // enable RAM waitstate
00262 lcdBusyWait(); // wait until LCD not busy
00263 *((volatile unsigned char *) (LCD_DATA_ADDR)) = data;
00264 //cbi(MCUCR, SRW); // disable RAM waitstate
00265 #endif
00266 }
00267
00268 u08 lcdDataRead(void)
00269 {
00270 // read a data byte from the display
00271 register u08 data;
00272 #ifdef LCD_PORT_INTERFACE
00273 lcdBusyWait(); // wait until LCD not busy
00274 #ifdef LCD_DATA_4BIT
00275 outb(LCD_DATA_DDR, inb(LCD_DATA_DDR)&0x0F); // set data I/O lines to input (4bit)
00276 outb(LCD_DATA_POUT, inb(LCD_DATA_POUT)|0xF0); // set pull-ups to on (4bit)
00277 #else
00278 outb(LCD_DATA_DDR, 0x00); // set data I/O lines to input (8bit)
00279 outb(LCD_DATA_POUT, 0xFF); // set pull-ups to on (8bit)
00280 #endif
00281 sbi(LCD_CTRL_PORT, LCD_CTRL_RS); // set RS to "data"
00282 sbi(LCD_CTRL_PORT, LCD_CTRL_RW); // set R/W to "read"
00283 #ifdef LCD_DATA_4BIT
00284 // 4 bit read
00285 sbi(LCD_CTRL_PORT, LCD_CTRL_E); // set "E" line
00286 LCD_DELAY; // wait
00287 LCD_DELAY; // wait
00288 data = inb(LCD_DATA_PIN)&0xF0; // input data, high 4 bits
00289 cbi(LCD_CTRL_PORT, LCD_CTRL_E); // clear "E" line
00290 LCD_DELAY; // wait
00291 LCD_DELAY; // wait
00292 sbi(LCD_CTRL_PORT, LCD_CTRL_E); // set "E" line
00293 LCD_DELAY; // wait
00294 LCD_DELAY; // wait
00295 data |= inb(LCD_DATA_PIN)>>4; // input data, low 4 bits
00296 cbi(LCD_CTRL_PORT, LCD_CTRL_E); // clear "E" line
00297 #else
00298 // 8 bit read
00299 sbi(LCD_CTRL_PORT, LCD_CTRL_E); // set "E" line
00300 LCD_DELAY; // wait
00301 LCD_DELAY; // wait
00302 data = inb(LCD_DATA_PIN); // input data, 8bits
00303 cbi(LCD_CTRL_PORT, LCD_CTRL_E); // clear "E" line
00304 #endif
00305 // leave data lines in input mode so they can be most easily used for other purposes
00306 #else
00307 // memory bus read
00308 //sbi(MCUCR, SRW); // enable RAM waitstate
00309 lcdBusyWait(); // wait until LCD not busy
00310 data = *((volatile unsigned char *) (LCD_DATA_ADDR));
00311 //cbi(MCUCR, SRW); // disable RAM waitstate
00312 #endif
00313 return data;
00314 }
00315
00316
00317
00318 /*************************************************************/
00319 /********************* PUBLIC FUNCTIONS **********************/
00320 /*************************************************************/
00321
00322 void lcdInit()
00323 {
00324 // initialize hardware
00325 lcdInitHW();
00326 // LCD function set
00327 lcdControlWrite(LCD_FUNCTION_DEFAULT);
00328 // clear LCD
00329 lcdControlWrite(1<<LCD_CLR);
00330 delay(60000); // wait 60ms
00331 // set entry mode
00332 lcdControlWrite(1<<LCD_ENTRY_MODE | 1<<LCD_ENTRY_INC);
00333 // set display to on
00334 //lcdControlWrite(1<<LCD_ON_CTRL | 1<<LCD_ON_DISPLAY | 1<<LCD_ON_BLINK);
00335 lcdControlWrite(1<<LCD_ON_CTRL | 1<<LCD_ON_DISPLAY );
00336 // move cursor to home
00337 lcdControlWrite(1<<LCD_HOME);
00338 // set data address to 0
00339 lcdControlWrite(1<<LCD_DDRAM | 0x00);
00340
00341 // load the first 8 custom characters
00342 lcdLoadCustomChar((u08*)LcdCustomChar,0,0);
00343 lcdLoadCustomChar((u08*)LcdCustomChar,1,1);
00344 lcdLoadCustomChar((u08*)LcdCustomChar,2,2);
00345 lcdLoadCustomChar((u08*)LcdCustomChar,3,3);
00346 lcdLoadCustomChar((u08*)LcdCustomChar,4,4);
00347 lcdLoadCustomChar((u08*)LcdCustomChar,5,5);
00348 lcdLoadCustomChar((u08*)LcdCustomChar,6,6);
00349 lcdLoadCustomChar((u08*)LcdCustomChar,7,7);
00350 }
00351
00352 void lcdHome(void)
00353 {
00354 // move cursor to home
00355 lcdControlWrite(1<<LCD_HOME);
00356 }
00357
00358 void lcdClear(void)
00359 {
00360 // clear LCD
00361 lcdControlWrite(1<<LCD_CLR);
00362 }
00363
00364 void lcdGotoXY(u08 x, u08 y)
00365 {
00366 register u08 DDRAMAddr;
00367
00368 // remap lines into proper order
00369 switch(y)
00370 {
00371 case 0: DDRAMAddr = LCD_LINE0_DDRAMADDR+x; break;
00372 case 1: DDRAMAddr = LCD_LINE1_DDRAMADDR+x; break;
00373 case 2: DDRAMAddr = LCD_LINE2_DDRAMADDR+x; break;
00374 case 3: DDRAMAddr = LCD_LINE3_DDRAMADDR+x; break;
00375 default: DDRAMAddr = LCD_LINE0_DDRAMADDR+x;
00376 }
00377
00378 // set data address
00379 lcdControlWrite(1<<LCD_DDRAM | DDRAMAddr);
00380 }
00381
00382 void lcdLoadCustomChar(u08* lcdCustomCharArray, u08 romCharNum, u08 lcdCharNum)
00383 {
00384 register u08 i;
00385 u08 saveDDRAMAddr;
00386
00387 // backup the current cursor position
00388 saveDDRAMAddr = lcdControlRead() & 0x7F;
00389
00390 // multiply the character index by 8
00391 lcdCharNum = (lcdCharNum<<3); // each character occupies 8 bytes
00392 romCharNum = (romCharNum<<3); // each character occupies 8 bytes
00393
00394 // copy the 8 bytes into CG (character generator) RAM
00395 for(i=0; i<8; i++)
00396 {
00397 // set CG RAM address
00398 lcdControlWrite((1<<LCD_CGRAM) | (lcdCharNum+i));
00399 // write character data
00400 lcdDataWrite( pgm_read_byte(lcdCustomCharArray+romCharNum+i) );
00401 }
00402
00403 // restore the previous cursor position
00404 lcdControlWrite(1<<LCD_DDRAM | saveDDRAMAddr);
00405
00406 }
00407
00408 void lcdPrintData(char* data, u08 nBytes)
00409 {
00410 register u08 i;
00411
00412 // check to make sure we have a good pointer
00413 if (!data) return;
00414
00415 // print data
00416 for(i=0; i<nBytes; i++)
00417 {
00418 lcdDataWrite(data[i]);
00419 }
00420 }
00421
00422 void lcdProgressBar(u16 progress, u16 maxprogress, u08 length)
00423 {
00424 u08 i;
00425 u32 pixelprogress;
00426 u08 c;
00427
00428 // draw a progress bar displaying (progress / maxprogress)
00429 // starting from the current cursor position
00430 // with a total length of "length" characters
00431 // ***note, LCD chars 0-5 must be programmed as the bar characters
00432 // char 0 = empty ... char 5 = full
00433
00434 // total pixel length of bargraph equals length*PROGRESSPIXELS_PER_CHAR;
00435 // pixel length of bar itself is
00436 pixelprogress = ((progress*(length*PROGRESSPIXELS_PER_CHAR))/maxprogress);
00437
00438 // print exactly "length" characters
00439 for(i=0; i<length; i++)
00440 {
00441 // check if this is a full block, or partial or empty
00442 // (u16) cast is needed to avoid sign comparison warning
00443 if( ((i*(u16)PROGRESSPIXELS_PER_CHAR)+5) > pixelprogress )
00444 {
00445 // this is a partial or empty block
00446 if( ((i*(u16)PROGRESSPIXELS_PER_CHAR)) > pixelprogress )
00447 {
00448 // this is an empty block
00449 // use space character?
00450 c = 0;
00451 }
00452 else
00453 {
00454 // this is a partial block
00455 c = pixelprogress % PROGRESSPIXELS_PER_CHAR;
00456 }
00457 }
00458 else
00459 {
00460 // this is a full block
00461 c = 5;
00462 }
00463
00464 // write character to display
00465 lcdDataWrite(c);
00466 }
00467
00468 }
00469
--------------------------------------------------------------------------------
Generated on Sun Oct 29 03:41:07 2006 for Procyon AVRlib by 1.4.2
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -