![]() |
Interface for Dot-Matrix LCDs 0.8
|
|
⇒ back to Project Page |
00001 00002 /****************************************************************************/ 00040 /****************************************** 00041 \internal 00042 Include files 00043 *******************************************/ 00044 00045 #include <avr/io.h> 00046 #include <util/delay.h> 00047 #include <avr/pgmspace.h> 00048 00049 #include "port_definitions.h" 00050 #include "lcd.h" 00051 00052 00053 00054 00055 00056 /****************************************** 00057 \internal 00058 Global variables 00059 *******************************************/ 00060 00061 uint8_t next_ddram_address; 00062 uint8_t lcd_backlight_state; 00063 00064 00065 00066 00067 00068 /****************************************************************************/ 00086 /****************************************************************************/ 00087 00088 void lcd_set_backlight(uint8_t control) { 00089 00090 00091 switch (control) { 00092 00093 case LCD_BL_SWITCH_OFF: 00094 LCDBL_PORT = LCDBL_PORT & ~(1 << LCDBL_CTRL); //switch off backlight 00095 lcd_backlight_state = LCD_BL_OFF; 00096 break; 00097 00098 case LCD_BL_SWITCH_ON: 00099 LCDBL_PORT = LCDBL_PORT | (1 << LCDBL_CTRL); //switch on backlight 00100 lcd_backlight_state = LCD_BL_ON; 00101 break; 00102 00103 case LCD_BL_ALTERNATE: 00104 00105 if (lcd_backlight_state == LCD_BL_OFF) { 00106 LCDBL_PORT = LCDBL_PORT | (1 << LCDBL_CTRL); //switch on backlight 00107 lcd_backlight_state = LCD_BL_ON; 00108 } else { 00109 LCDBL_PORT = LCDBL_PORT & ~(1 << LCDBL_CTRL); //switch off backlight 00110 lcd_backlight_state = LCD_BL_OFF; 00111 } 00112 break; 00113 00114 //unknown control value --> switch on the backlight 00115 default: 00116 LCDBL_PORT = LCDBL_PORT | (1 << LCDBL_CTRL); 00117 lcd_backlight_state = LCD_BL_ON; 00118 00119 } 00120 00121 } 00122 00123 00124 /****************************************************************************/ 00138 /****************************************************************************/ 00139 00140 void lcd_enable(void) { 00141 00142 //raise Enable 00143 LCD_PORT = LCD_PORT | (1 << LCD_EN); 00144 _delay_us(DISPLAY_ENABLE_TIME_US); //Enable Pulse Width (at least 140 ns) 00145 00146 //set Enable to 0 00147 LCD_PORT = LCD_PORT & ~(1 << LCD_EN); 00148 _delay_us(DISPLAY_ADDRESS_HOLD_TIME_US); //Adress Hold Time (at least 10 ns) 00149 00150 } 00151 00152 00153 /****************************************************************************/ 00176 /****************************************************************************/ 00177 00178 void lcd_send_data(unsigned char data) { 00179 00180 00181 //check for newline 00182 00183 #if PROCESS_NEWLINE == TRUE 00184 00185 #if DISPLAY_RESOLUTION == HIGH_RES 00186 #if DISPLAY_NUMBER_ROWS == 2 00187 if (data == '\n') { 00188 lcd_set_cursor(0, 0); //wrap around to first line 00189 return; 00190 } 00191 #else 00192 if (data == '\n') { 00193 if (next_ddram_address <= (DISPLAY_NUMBER_COLUMNS - 1)) { 00194 lcd_set_cursor(0, 1); 00195 } else { 00196 lcd_set_cursor(0, 0); //wrap around to first line 00197 } 00198 return; 00199 } 00200 #endif 00201 #else 00202 #if DISPLAY_NUMBER_ROWS == 2 00203 if (data == '\n') { 00204 if (next_ddram_address <= DISPLAY_NUMBER_COLUMNS - 1) { 00205 lcd_set_cursor(0, 1); 00206 } else if (next_ddram_address <= (DDRAM_OFFSET + DISPLAY_NUMBER_COLUMNS - 1)) { 00207 lcd_set_cursor(0, 0); //wrap around to first line 00208 } 00209 return; 00210 } 00211 #else 00212 if (data == '\n') { 00213 if (next_ddram_address <= DISPLAY_NUMBER_COLUMNS - 1) { 00214 lcd_set_cursor(0, 1); 00215 } else if ((next_ddram_address <= (DISPLAY_NUMBER_COLUMNS + DISPLAY_NUMBER_COLUMNS - 1))) { 00216 lcd_set_cursor(0, 3); 00217 } else if (next_ddram_address <= (DDRAM_OFFSET + DISPLAY_NUMBER_COLUMNS - 1)) { 00218 lcd_set_cursor(0, 2); 00219 } else if (next_ddram_address <= (DDRAM_OFFSET + DISPLAY_NUMBER_COLUMNS + DISPLAY_NUMBER_COLUMNS - 1)) { 00220 lcd_set_cursor(0, 0); //wrap around to first line 00221 } 00222 return; 00223 } 00224 #endif 00225 #endif 00226 00227 #endif 00228 00229 00230 //send data byte 00231 #if USE_LCD_RW_PIN == TRUE 00232 LCD_PORT = LCD_PORT & ~(1 << LCD_RS) & ~(1 << LCD_RW) & ~(1 << LCD_EN) & ~(1 << LCD_DB4) & ~(1 << LCD_DB5) & ~(1 << LCD_DB6) & ~(1 << LCD_DB7); 00233 #else 00234 LCD_PORT = LCD_PORT & ~(1 << LCD_RS) & ~(1 << LCD_EN) & ~(1 << LCD_DB4) & ~(1 << LCD_DB5) & ~(1 << LCD_DB6) & ~(1 << LCD_DB7); 00235 #endif 00236 LCD_PORT = LCD_PORT | (1 << LCD_RS); //raise RS for writing data into LCD RAM 00237 LCD_PORT = LCD_PORT | (((data & 0x10) >> 4) << LCD_DB4); //Bit 4 00238 LCD_PORT = LCD_PORT | (((data & 0x20) >> 5) << LCD_DB5); //Bit 5 00239 LCD_PORT = LCD_PORT | (((data & 0x40) >> 6) << LCD_DB6); //Bit 6 00240 LCD_PORT = LCD_PORT | (((data & 0x80) >> 7) << LCD_DB7); //Bit 7 00241 00242 lcd_enable(); //enable pulse 00243 00244 #if USE_LCD_RW_PIN == TRUE 00245 LCD_PORT = LCD_PORT & ~(1 << LCD_RS) & ~(1 << LCD_RW) & ~(1 << LCD_EN) & ~(1 << LCD_DB4) & ~(1 << LCD_DB5) & ~(1 << LCD_DB6) & ~(1 << LCD_DB7); 00246 #else 00247 LCD_PORT = LCD_PORT & ~(1 << LCD_RS) & ~(1 << LCD_EN) & ~(1 << LCD_DB4) & ~(1 << LCD_DB5) & ~(1 << LCD_DB6) & ~(1 << LCD_DB7); 00248 #endif 00249 LCD_PORT = LCD_PORT | (1 << LCD_RS); 00250 LCD_PORT = LCD_PORT | ((data & 0x01) << LCD_DB4); //Bit 0 00251 LCD_PORT = LCD_PORT | (((data & 0x02) >> 1) << LCD_DB5); //Bit 1 00252 LCD_PORT = LCD_PORT | (((data & 0x04) >> 2) << LCD_DB6); //Bit 2 00253 LCD_PORT = LCD_PORT | (((data & 0x08) >> 3) << LCD_DB7); //Bit 3 00254 00255 lcd_enable(); //enable pulse 00256 _delay_us(DISPLAY_DATATRANSFER_TIME_US); //wait time for next data transfer (alternative: check for busy flag) 00257 00258 00259 00260 //check for ddram address and set new address 00261 #if AUTOWRAP_AROUND == TRUE 00262 00263 #if DISPLAY_RESOLUTION == HIGH_RES //5x11 Dots 00264 #if DISPLAY_NUMBER_ROWS == 2 00265 if (next_ddram_address == (DISPLAY_NUMBER_COLUMNS - 1)) { 00266 lcd_set_cursor(0, 0); //wrap around to first line 00267 } else { 00268 next_ddram_address++; //increase address pointer 00269 } 00270 #else 00271 if (next_ddram_address == (DISPLAY_NUMBER_COLUMNS + DISPLAY_NUMBER_COLUMNS - 1)) { 00272 lcd_set_cursor(0, 0); //wrap around to first line 00273 } else { 00274 next_ddram_address++; //increase address pointer 00275 } 00276 #endif 00277 #else 00278 #if DISPLAY_NUMBER_ROWS == 2 00279 if (next_ddram_address == DISPLAY_NUMBER_COLUMNS - 1) { 00280 lcd_set_cursor(0, 1); 00281 } else if (next_ddram_address == (DDRAM_OFFSET + DISPLAY_NUMBER_COLUMNS - 1)) { 00282 lcd_set_cursor(0, 0); 00283 } else { 00284 next_ddram_address++; //increase address pointer 00285 } 00286 #else 00287 if (next_ddram_address == DISPLAY_NUMBER_COLUMNS - 1) { 00288 lcd_set_cursor(0, 1); 00289 } else if (next_ddram_address == (DDRAM_OFFSET + DISPLAY_NUMBER_COLUMNS - 1)) { 00290 lcd_set_cursor(0, 2); 00291 } else if ((next_ddram_address == (DISPLAY_NUMBER_COLUMNS + DISPLAY_NUMBER_COLUMNS - 1))) { 00292 lcd_set_cursor(0, 3); 00293 } else if (next_ddram_address == (DDRAM_OFFSET + DISPLAY_NUMBER_COLUMNS + DISPLAY_NUMBER_COLUMNS - 1)) { 00294 lcd_set_cursor(0, 0); //wrap around to first line 00295 } else { 00296 next_ddram_address++; //increase address pointer 00297 } 00298 #endif 00299 #endif 00300 00301 #endif 00302 00303 } 00304 00305 00306 /****************************************************************************/ 00324 /****************************************************************************/ 00325 00326 void lcd_send_command(unsigned char command) { 00327 00328 #if USE_LCD_RW_PIN == TRUE 00329 LCD_PORT = LCD_PORT & ~(1 << LCD_RS) & ~(1 << LCD_RW) & ~(1 << LCD_EN) & ~(1 << LCD_DB4) & ~(1 << LCD_DB5) & ~(1 << LCD_DB6) & ~(1 << LCD_DB7); 00330 #else 00331 LCD_PORT = LCD_PORT & ~(1 << LCD_RS) & ~(1 << LCD_EN) & ~(1 << LCD_DB4) & ~(1 << LCD_DB5) & ~(1 << LCD_DB6) & ~(1 << LCD_DB7); 00332 #endif 00333 LCD_PORT = LCD_PORT | (((command & 0x10) >> 4) << LCD_DB4); //Bit 4 00334 LCD_PORT = LCD_PORT | (((command & 0x20) >> 5) << LCD_DB5); //Bit 5 00335 LCD_PORT = LCD_PORT | (((command & 0x40) >> 6) << LCD_DB6); //Bit 6 00336 LCD_PORT = LCD_PORT | (((command & 0x80) >> 7) << LCD_DB7); //Bit 7 00337 00338 lcd_enable(); 00339 00340 #if USE_LCD_RW_PIN == TRUE 00341 LCD_PORT = LCD_PORT & ~(1 << LCD_RS) & ~(1 << LCD_RW) & ~(1 << LCD_EN) & ~(1 << LCD_DB4) & ~(1 << LCD_DB5) & ~(1 << LCD_DB6) & ~(1 << LCD_DB7); 00342 #else 00343 LCD_PORT = LCD_PORT & ~(1 << LCD_RS) & ~(1 << LCD_EN) & ~(1 << LCD_DB4) & ~(1 << LCD_DB5) & ~(1 << LCD_DB6) & ~(1 << LCD_DB7); 00344 #endif 00345 LCD_PORT = LCD_PORT | ((command & 0x01) << LCD_DB4); //Bit 0 00346 LCD_PORT = LCD_PORT | (((command & 0x02) >> 1) << LCD_DB5); //Bit 1 00347 LCD_PORT = LCD_PORT | (((command & 0x04) >> 2) << LCD_DB6); //Bit 2 00348 LCD_PORT = LCD_PORT | (((command & 0x08) >> 3) << LCD_DB7); //Bit 3 00349 00350 lcd_enable(); 00351 _delay_us(DISPLAY_COMMANDTRANSFER_TIME_US); //wait time for next data transfer (alternative: check for busy flag) 00352 00353 } 00354 00355 00356 /****************************************************************************/ 00378 /****************************************************************************/ 00379 00380 void lcd_set_cursor(uint8_t column, uint8_t row) { 00381 00382 uint8_t address; 00383 00384 00385 //check for number of rows and columns 00386 if (column > DISPLAY_NUMBER_COLUMNS || row > DISPLAY_NUMBER_ROWS) { 00387 return; 00388 } 00389 00390 #if DISPLAY_RESOLUTION == HIGH_RES //5x11 Dots 00391 00392 //for LCDs up to 2 rows 00393 switch (row) { 00394 00395 case 1: //1. row 00396 address = SET_DDRAM_ADDRESS | column; //DB7=1 --> set DDRAM Address, DB0...DB6 --> Address 00397 break; 00398 00399 case 2: //2. row 00400 address = SET_DDRAM_ADDRESS | (DISPLAY_NUMBER_COLUMNS + column); //DB7=1 --> set DDRAM Address, DB0...DB6 --> Address 00401 break; 00402 00403 default: 00404 return; //do nothing if wrong row requested 00405 00406 } 00407 00408 #else //5x8 Dots 00409 00410 //for LCDs up to 4 rows 00411 switch (row) { 00412 00413 case 0: //1. row 00414 address = SET_DDRAM_ADDRESS | column; //DB7=1 --> set DDRAM Address, DB0...DB6 --> Address 00415 break; 00416 00417 case 1: //2. row 00418 address = SET_DDRAM_ADDRESS | (DDRAM_OFFSET + column); //DB7=1 --> set DDRAM Address, DB0...DB6 --> Address 00419 break; 00420 00421 case 2: //3. row 00422 address = SET_DDRAM_ADDRESS | (DISPLAY_NUMBER_COLUMNS + column); //DB7=1 --> set DDRAM Address, DB0...DB6 --> Address 00423 break; 00424 00425 case 3: //4. row 00426 address = SET_DDRAM_ADDRESS | (DDRAM_OFFSET + DISPLAY_NUMBER_COLUMNS + column); //DB7=1 --> set DDRAM Address, DB0...DB6 --> Address 00427 break; 00428 00429 default: 00430 return; //do nothing if wrong row requested 00431 } 00432 #endif 00433 00434 lcd_send_command(address); //send new address to LCD 00435 next_ddram_address = address - 0x80; //set next ddram address (and remove command bit) 00436 } 00437 00438 00439 /****************************************************************************/ 00462 /****************************************************************************/ 00463 00464 void lcd_send_string(const char *data, uint8_t flash_mem) { 00465 00466 char c; 00467 00468 if (flash_mem == TRUE) { 00469 00470 c = pgm_read_byte(data); 00471 while (c != '\0') { //until *s != '\0' --> means "string end sign" 00472 lcd_send_data(c); 00473 data++; 00474 c = pgm_read_byte(data); 00475 } 00476 00477 } else { 00478 00479 while (*data != '\0') { //until *s != '\0' --> means "string end sign" 00480 lcd_send_data(*data); 00481 data++; 00482 } 00483 00484 } 00485 } 00486 00487 00488 /****************************************************************************/ 00501 /****************************************************************************/ 00502 00503 void lcd_clear(void) { 00504 00505 lcd_send_command(CLEAR_DISPLAY); 00506 next_ddram_address = 0x00; //set next ddram address 00507 _delay_ms(DISPLAY_CLEAR_WAIT_TIME_MS); //wait time for clear command (at least 1,5 ms) 00508 00509 } 00510 00511 00512 /****************************************************************************/ 00525 /****************************************************************************/ 00526 00527 void lcd_cursor_home(void) { 00528 00529 lcd_send_command(CURSOR_HOME); 00530 next_ddram_address = 0x00; //set next ddram address 00531 _delay_ms(DISPLAY_CURSORHOME_WAIT_TIME_MS); //wait time for clear command (at least 1,5 ms) 00532 00533 } 00534 00535 00536 /****************************************************************************/ 00550 /****************************************************************************/ 00551 00552 void lcd_init(void) { 00553 00554 uint8_t tmp; 00555 00556 //wait for some time after Reset 00557 _delay_ms(DISPLAY_RESET_WAIT_TIME_MS); 00558 00559 //Function Set (8 Bit Mode) 00560 #if USE_LCD_RW_PIN == TRUE 00561 LCD_PORT = LCD_PORT & ~(1 << LCD_RS) & ~(1 << LCD_RW) & ~(1 << LCD_EN) & ~(1 << LCD_DB4) & ~(1 << LCD_DB5) & ~(1 << LCD_DB6) & ~(1 << LCD_DB7); 00562 #else 00563 LCD_PORT = LCD_PORT & ~(1 << LCD_RS) & ~(1 << LCD_EN) & ~(1 << LCD_DB4) & ~(1 << LCD_DB5) & ~(1 << LCD_DB6) & ~(1 << LCD_DB7); 00564 #endif 00565 LCD_PORT = LCD_PORT | (1 << LCD_DB5) | (1 << LCD_DB4); 00566 lcd_enable(); 00567 _delay_ms(DISPLAY_INIT_WAIT_TIME01_MS); 00568 00569 //Function Set (8 Bit Mode) 00570 #if USE_LCD_RW_PIN == TRUE 00571 LCD_PORT = LCD_PORT & ~(1 << LCD_RS) & ~(1 << LCD_RW) & ~(1 << LCD_EN) & ~(1 << LCD_DB4) & ~(1 << LCD_DB5) & ~(1 << LCD_DB6) & ~(1 << LCD_DB7); 00572 #else 00573 LCD_PORT = LCD_PORT & ~(1 << LCD_RS) & ~(1 << LCD_EN) & ~(1 << LCD_DB4) & ~(1 << LCD_DB5) & ~(1 << LCD_DB6) & ~(1 << LCD_DB7); 00574 #endif 00575 LCD_PORT = LCD_PORT | (1 << LCD_DB5) | (1 << LCD_DB4); 00576 lcd_enable(); 00577 _delay_ms(DISPLAY_INIT_WAIT_TIME02_MS); 00578 00579 //Function Set (8 Bit Mode) 00580 #if USE_LCD_RW_PIN == TRUE 00581 LCD_PORT = LCD_PORT & ~(1 << LCD_RS) & ~(1 << LCD_RW) & ~(1 << LCD_EN) & ~(1 << LCD_DB4) & ~(1 << LCD_DB5) & ~(1 << LCD_DB6) & ~(1 << LCD_DB7); 00582 #else 00583 LCD_PORT = LCD_PORT & ~(1 << LCD_RS) & ~(1 << LCD_EN) & ~(1 << LCD_DB4) & ~(1 << LCD_DB5) & ~(1 << LCD_DB6) & ~(1 << LCD_DB7); 00584 #endif 00585 LCD_PORT = LCD_PORT | (1 << LCD_DB5) | (1 << LCD_DB4); 00586 lcd_enable(); 00587 _delay_ms(DISPLAY_INIT_WAIT_TIME03_MS); 00588 00589 //Function Set (switch to 4 Bit Mode) 00590 #if USE_LCD_RW_PIN == TRUE 00591 LCD_PORT = LCD_PORT & ~(1 << LCD_RS) & ~(1 << LCD_RW) & ~(1 << LCD_EN) & ~(1 << LCD_DB4) & ~(1 << LCD_DB5) & ~(1 << LCD_DB6) & ~(1 << LCD_DB7); 00592 #else 00593 LCD_PORT = LCD_PORT & ~(1 << LCD_RS) & ~(1 << LCD_EN) & ~(1 << LCD_DB4) & ~(1 << LCD_DB5) & ~(1 << LCD_DB6) & ~(1 << LCD_DB7); 00594 #endif 00595 LCD_PORT = LCD_PORT | (1 << LCD_DB5); 00596 lcd_enable(); 00597 _delay_ms(DISPLAY_INIT_WAIT_TIME04_MS); 00598 00599 //Function Set (4 Bit) 00600 #if DISPLAY_RESOLUTION == HIGH_RES 00601 lcd_send_command(0x24); //1 Line and 5x11 dots 00602 #else 00603 lcd_send_command(0x28); //2 lines and 5x8 dots 00604 #endif 00605 _delay_ms(DISPLAY_INIT_WAIT_TIME_MS); 00606 00607 //Display Off 00608 lcd_send_command(0x08); 00609 _delay_ms(DISPLAY_INIT_WAIT_TIME_MS); 00610 00611 //Clear Display Command 00612 lcd_send_command(CLEAR_DISPLAY); 00613 _delay_ms(DISPLAY_CLEAR_WAIT_TIME_MS); 00614 00615 //Entry Mode (Increment, Shift Off) 00616 lcd_send_command(0x06); 00617 _delay_ms(DISPLAY_INIT_WAIT_TIME_MS); 00618 00619 //Display On 00620 tmp = 0x0c; 00621 #if DISPLAY_CURSOR == CURSOR_ON //Cursor On? 00622 tmp = tmp | 0x02; 00623 #endif 00624 #if DISPLAY_CURSOR_BLINK == BLINK_ON //Blinking On? 00625 tmp = tmp | 0x01; 00626 #endif 00627 lcd_send_command(tmp); 00628 _delay_ms(DISPLAY_INIT_WAIT_TIME_MS); 00629 00630 //Cursor Home Command 00631 lcd_send_command(CURSOR_HOME); 00632 _delay_ms(DISPLAY_CURSORHOME_WAIT_TIME_MS); 00633 00634 //reset LCD port 00635 #if USE_LCD_RW_PIN == TRUE 00636 LCD_PORT = LCD_PORT & ~(1 << LCD_RS) & ~(1 << LCD_RW) & ~(1 << LCD_EN) & ~(1 << LCD_DB4) & ~(1 << LCD_DB5) & ~(1 << LCD_DB6) & ~(1 << LCD_DB7); 00637 #else 00638 LCD_PORT = LCD_PORT & ~(1 << LCD_RS) & ~(1 << LCD_EN) & ~(1 << LCD_DB4) & ~(1 << LCD_DB5) & ~(1 << LCD_DB6) & ~(1 << LCD_DB7); 00639 #endif 00640 00641 00642 } 00643 00644 00645 /****************************************************************************/ 00661 /****************************************************************************/ 00662 00663 void lcd_pin_init(void) { 00664 00665 //initial values = 0 00666 #if USE_LCD_RW_PIN == TRUE 00667 LCD_PORT = LCD_PORT & ~(1 << LCD_RS) & ~(1 << LCD_RW) & ~(1 << LCD_EN) & ~(1 << LCD_DB4) & ~(1 << LCD_DB5) & ~(1 << LCD_DB6) & ~(1 << LCD_DB7); 00668 //set LCD Port Pins to Output ---> see "port_definitions.h" 00669 LCD_DDR = LCD_DDR | (1 << LCD_RS) | (1 << LCD_RW) | (1 << LCD_EN) | (1 << LCD_DB4) | (1 << LCD_DB5) | (1 << LCD_DB6) | (1 << LCD_DB7); 00670 #else 00671 LCD_PORT = LCD_PORT & ~(1 << LCD_RS) & ~(1 << LCD_EN) & ~(1 << LCD_DB4) & ~(1 << LCD_DB5) & ~(1 << LCD_DB6) & ~(1 << LCD_DB7); 00672 //set LCD Port Pins to Output ---> see "port_definitions.h" 00673 LCD_DDR = LCD_DDR | (1 << LCD_RS) | (1 << LCD_EN) | (1 << LCD_DB4) | (1 << LCD_DB5) | (1 << LCD_DB6) | (1 << LCD_DB7); 00674 #endif 00675 00676 00677 } 00678 00679 00680 00681 /****************************************************************************/ 00696 /****************************************************************************/ 00697 00698 void lcd_blpin_init(void) { 00699 00700 //initial values = 0 00701 LCDBL_PORT = LCD_PORT & ~(1 << LCDBL_CTRL); 00702 //set LCD Port Pins to Output ---> see "port_definitions.h" 00703 LCDBL_DDR = LCD_DDR | (1 << LCDBL_CTRL); 00704 00705 lcd_backlight_state = LCD_BL_OFF; 00706 00707 }