Real Time Clock Module with I2C Interface  0.3
⇒ back to Project Page
 All Data Structures Files Functions Variables Typedefs Macros Pages
lcd.c
Go to the documentation of this file.
1 /****************************************************************************/
39 /******************************************
40  \internal
41  Include files
42 *******************************************/
43 
44 #include <avr/io.h>
45 #include <util/delay.h>
46 #include <avr/pgmspace.h>
47 
48 #include "port_definitions.h"
49 #include "lcd.h"
50 
51 
52 
53 
54 
55 /******************************************
56  \internal
57  Global variables
58 *******************************************/
59 
62 
63 
64 
65 
66 
67 /****************************************************************************/
85 /****************************************************************************/
86 
87 void lcd_set_backlight(uint8_t control) {
88 
89 
90  switch (control) {
91 
92  case LCD_BL_SWITCH_OFF:
93  LCDBL_PORT = LCDBL_PORT & ~(1 << LCDBL_CTRL); //switch off backlight
95  break;
96 
97  case LCD_BL_SWITCH_ON:
98  LCDBL_PORT = LCDBL_PORT | (1 << LCDBL_CTRL); //switch on backlight
100  break;
101 
102  case LCD_BL_ALTERNATE:
103 
105  LCDBL_PORT = LCDBL_PORT | (1 << LCDBL_CTRL); //switch on backlight
107  } else {
108  LCDBL_PORT = LCDBL_PORT & ~(1 << LCDBL_CTRL); //switch off backlight
110  }
111  break;
112 
113  //unknown control value --> switch on the backlight
114  default:
115  LCDBL_PORT = LCDBL_PORT | (1 << LCDBL_CTRL);
117 
118  }
119 
120 }
121 
122 
123 /****************************************************************************/
137 /****************************************************************************/
138 
139 void lcd_enable(void) {
140 
141  //raise Enable
142  LCD_PORT = LCD_PORT | (1 << LCD_EN);
143  _delay_us(DISPLAY_ENABLE_TIME_US); //Enable Pulse Width (at least 140 ns)
144 
145  //set Enable to 0
146  LCD_PORT = LCD_PORT & ~(1 << LCD_EN);
147  _delay_us(DISPLAY_ADDRESS_HOLD_TIME_US); //Adress Hold Time (at least 10 ns)
148 
149 }
150 
151 
152 /****************************************************************************/
175 /****************************************************************************/
176 
177 void lcd_send_data(unsigned char data) {
178 
179 
180 //check for newline
181 
182 #if PROCESS_NEWLINE == TRUE
183 
184 #if DISPLAY_RESOLUTION == HIGH_RES
185 #if DISPLAY_NUMBER_ROWS == 2
186  if (data == '\n') {
187  lcd_set_cursor(0, 0); //wrap around to first line
188  return;
189  }
190 #else
191  if (data == '\n') {
193  lcd_set_cursor(0, 1);
194  } else {
195  lcd_set_cursor(0, 0); //wrap around to first line
196  }
197  return;
198  }
199 #endif
200 #else
201 #if DISPLAY_NUMBER_ROWS == 2
202  if (data == '\n') {
204  lcd_set_cursor(0, 1);
206  lcd_set_cursor(0, 0); //wrap around to first line
207  }
208  return;
209  }
210 #else
211  if (data == '\n') {
213  lcd_set_cursor(0, 1);
215  lcd_set_cursor(0, 3);
217  lcd_set_cursor(0, 2);
219  lcd_set_cursor(0, 0); //wrap around to first line
220  }
221  return;
222  }
223 #endif
224 #endif
225 
226 #endif
227 
228 
229  //send data byte
230 #if USE_LCD_RW_PIN == TRUE
231  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);
232 #else
233  LCD_PORT = LCD_PORT & ~(1 << LCD_RS) & ~(1 << LCD_EN) & ~(1 << LCD_DB4) & ~(1 << LCD_DB5) & ~(1 << LCD_DB6) & ~(1 << LCD_DB7);
234 #endif
235  LCD_PORT = LCD_PORT | (1 << LCD_RS); //raise RS for writing data into LCD RAM
236  LCD_PORT = LCD_PORT | (((data & 0x10) >> 4) << LCD_DB4); //Bit 4
237  LCD_PORT = LCD_PORT | (((data & 0x20) >> 5) << LCD_DB5); //Bit 5
238  LCD_PORT = LCD_PORT | (((data & 0x40) >> 6) << LCD_DB6); //Bit 6
239  LCD_PORT = LCD_PORT | (((data & 0x80) >> 7) << LCD_DB7); //Bit 7
240 
241  lcd_enable(); //enable pulse
242 
243 #if USE_LCD_RW_PIN == TRUE
244  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);
245 #else
246  LCD_PORT = LCD_PORT & ~(1 << LCD_RS) & ~(1 << LCD_EN) & ~(1 << LCD_DB4) & ~(1 << LCD_DB5) & ~(1 << LCD_DB6) & ~(1 << LCD_DB7);
247 #endif
248  LCD_PORT = LCD_PORT | (1 << LCD_RS);
249  LCD_PORT = LCD_PORT | ((data & 0x01) << LCD_DB4); //Bit 0
250  LCD_PORT = LCD_PORT | (((data & 0x02) >> 1) << LCD_DB5); //Bit 1
251  LCD_PORT = LCD_PORT | (((data & 0x04) >> 2) << LCD_DB6); //Bit 2
252  LCD_PORT = LCD_PORT | (((data & 0x08) >> 3) << LCD_DB7); //Bit 3
253 
254  lcd_enable(); //enable pulse
255  _delay_us(DISPLAY_DATATRANSFER_TIME_US); //wait time for next data transfer (alternative: check for busy flag)
256 
257 
258 
259  //check for ddram address and set new address
260 #if AUTOWRAP_AROUND == TRUE
261 
262 #if DISPLAY_RESOLUTION == HIGH_RES //5x11 Dots
263 #if DISPLAY_NUMBER_ROWS == 2
265  lcd_set_cursor(0, 0); //wrap around to first line
266  } else {
267  next_ddram_address++; //increase address pointer
268  }
269 #else
271  lcd_set_cursor(0, 0); //wrap around to first line
272  } else {
273  next_ddram_address++; //increase address pointer
274  }
275 #endif
276 #else
277 #if DISPLAY_NUMBER_ROWS == 2
279  lcd_set_cursor(0, 1);
281  lcd_set_cursor(0, 0);
282  } else {
283  next_ddram_address++; //increase address pointer
284  }
285 #else
287  lcd_set_cursor(0, 1);
289  lcd_set_cursor(0, 2);
291  lcd_set_cursor(0, 3);
293  lcd_set_cursor(0, 0); //wrap around to first line
294  } else {
295  next_ddram_address++; //increase address pointer
296  }
297 #endif
298 #endif
299 
300 #endif
301 
302 }
303 
304 
305 /****************************************************************************/
323 /****************************************************************************/
324 
325 void lcd_send_command(unsigned char command) {
326 
327 #if USE_LCD_RW_PIN == TRUE
328  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);
329 #else
330  LCD_PORT = LCD_PORT & ~(1 << LCD_RS) & ~(1 << LCD_EN) & ~(1 << LCD_DB4) & ~(1 << LCD_DB5) & ~(1 << LCD_DB6) & ~(1 << LCD_DB7);
331 #endif
332  LCD_PORT = LCD_PORT | (((command & 0x10) >> 4) << LCD_DB4); //Bit 4
333  LCD_PORT = LCD_PORT | (((command & 0x20) >> 5) << LCD_DB5); //Bit 5
334  LCD_PORT = LCD_PORT | (((command & 0x40) >> 6) << LCD_DB6); //Bit 6
335  LCD_PORT = LCD_PORT | (((command & 0x80) >> 7) << LCD_DB7); //Bit 7
336 
337  lcd_enable();
338 
339 #if USE_LCD_RW_PIN == TRUE
340  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);
341 #else
342  LCD_PORT = LCD_PORT & ~(1 << LCD_RS) & ~(1 << LCD_EN) & ~(1 << LCD_DB4) & ~(1 << LCD_DB5) & ~(1 << LCD_DB6) & ~(1 << LCD_DB7);
343 #endif
344  LCD_PORT = LCD_PORT | ((command & 0x01) << LCD_DB4); //Bit 0
345  LCD_PORT = LCD_PORT | (((command & 0x02) >> 1) << LCD_DB5); //Bit 1
346  LCD_PORT = LCD_PORT | (((command & 0x04) >> 2) << LCD_DB6); //Bit 2
347  LCD_PORT = LCD_PORT | (((command & 0x08) >> 3) << LCD_DB7); //Bit 3
348 
349  lcd_enable();
350  _delay_us(DISPLAY_COMMANDTRANSFER_TIME_US); //wait time for next data transfer (alternative: check for busy flag)
351 
352 }
353 
354 
355 /****************************************************************************/
377 /****************************************************************************/
378 
379 void lcd_set_cursor(uint8_t column, uint8_t row) {
380 
381  uint8_t address;
382 
383 
384  //check for number of rows and columns
385  if (column > DISPLAY_NUMBER_COLUMNS || row > DISPLAY_NUMBER_ROWS) {
386  return;
387  }
388 
389 #if DISPLAY_RESOLUTION == HIGH_RES //5x11 Dots
390 
391  //for LCDs up to 2 rows
392  switch (row) {
393 
394  case 1: //1. row
395  address = SET_DDRAM_ADDRESS | column; //DB7=1 --> set DDRAM Address, DB0...DB6 --> Address
396  break;
397 
398  case 2: //2. row
399  address = SET_DDRAM_ADDRESS | (DISPLAY_NUMBER_COLUMNS + column); //DB7=1 --> set DDRAM Address, DB0...DB6 --> Address
400  break;
401 
402  default:
403  return; //do nothing if wrong row requested
404 
405  }
406 
407 #else //5x8 Dots
408 
409  //for LCDs up to 4 rows
410  switch (row) {
411 
412  case 0: //1. row
413  address = SET_DDRAM_ADDRESS | column; //DB7=1 --> set DDRAM Address, DB0...DB6 --> Address
414  break;
415 
416  case 1: //2. row
417  address = SET_DDRAM_ADDRESS | (DDRAM_OFFSET + column); //DB7=1 --> set DDRAM Address, DB0...DB6 --> Address
418  break;
419 
420  case 2: //3. row
421  address = SET_DDRAM_ADDRESS | (DISPLAY_NUMBER_COLUMNS + column); //DB7=1 --> set DDRAM Address, DB0...DB6 --> Address
422  break;
423 
424  case 3: //4. row
425  address = SET_DDRAM_ADDRESS | (DDRAM_OFFSET + DISPLAY_NUMBER_COLUMNS + column); //DB7=1 --> set DDRAM Address, DB0...DB6 --> Address
426  break;
427 
428  default:
429  return; //do nothing if wrong row requested
430  }
431 #endif
432 
433  lcd_send_command(address); //send new address to LCD
434  next_ddram_address = address - 0x80; //set next ddram address (and remove command bit)
435 }
436 
437 
438 /****************************************************************************/
461 /****************************************************************************/
462 
463 void lcd_send_string(const char *data, uint8_t flash_mem) {
464 
465  char c;
466 
467  if (flash_mem == TRUE) {
468 
469  c = pgm_read_byte(data);
470  while (c != '\0') { //until *s != '\0' --> means "string end sign"
471  lcd_send_data(c);
472  data++;
473  c = pgm_read_byte(data);
474  }
475 
476  } else {
477 
478  while (*data != '\0') { //until *s != '\0' --> means "string end sign"
479  lcd_send_data(*data);
480  data++;
481  }
482 
483  }
484 }
485 
486 
487 /****************************************************************************/
500 /****************************************************************************/
501 
502 void lcd_clear(void) {
503 
505  next_ddram_address = 0x00; //set next ddram address
506  _delay_ms(DISPLAY_CLEAR_WAIT_TIME_MS); //wait time for clear command (at least 1,5 ms)
507 
508 }
509 
510 
511 /****************************************************************************/
524 /****************************************************************************/
525 
526 void lcd_cursor_home(void) {
527 
529  next_ddram_address = 0x00; //set next ddram address
530  _delay_ms(DISPLAY_CURSORHOME_WAIT_TIME_MS); //wait time for clear command (at least 1,5 ms)
531 
532 }
533 
534 
535 /****************************************************************************/
549 /****************************************************************************/
550 
551 void lcd_init(void) {
552 
553  uint8_t tmp;
554 
555  //wait for some time after Reset
556  _delay_ms(DISPLAY_RESET_WAIT_TIME_MS);
557 
558  //Function Set (8 Bit Mode)
559 #if USE_LCD_RW_PIN == TRUE
560  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);
561 #else
562  LCD_PORT = LCD_PORT & ~(1 << LCD_RS) & ~(1 << LCD_EN) & ~(1 << LCD_DB4) & ~(1 << LCD_DB5) & ~(1 << LCD_DB6) & ~(1 << LCD_DB7);
563 #endif
564  LCD_PORT = LCD_PORT | (1 << LCD_DB5) | (1 << LCD_DB4);
565  lcd_enable();
566  _delay_ms(DISPLAY_INIT_WAIT_TIME01_MS);
567 
568  //Function Set (8 Bit Mode)
569 #if USE_LCD_RW_PIN == TRUE
570  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);
571 #else
572  LCD_PORT = LCD_PORT & ~(1 << LCD_RS) & ~(1 << LCD_EN) & ~(1 << LCD_DB4) & ~(1 << LCD_DB5) & ~(1 << LCD_DB6) & ~(1 << LCD_DB7);
573 #endif
574  LCD_PORT = LCD_PORT | (1 << LCD_DB5) | (1 << LCD_DB4);
575  lcd_enable();
576  _delay_ms(DISPLAY_INIT_WAIT_TIME02_MS);
577 
578  //Function Set (8 Bit Mode)
579 #if USE_LCD_RW_PIN == TRUE
580  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);
581 #else
582  LCD_PORT = LCD_PORT & ~(1 << LCD_RS) & ~(1 << LCD_EN) & ~(1 << LCD_DB4) & ~(1 << LCD_DB5) & ~(1 << LCD_DB6) & ~(1 << LCD_DB7);
583 #endif
584  LCD_PORT = LCD_PORT | (1 << LCD_DB5) | (1 << LCD_DB4);
585  lcd_enable();
586  _delay_ms(DISPLAY_INIT_WAIT_TIME03_MS);
587 
588  //Function Set (switch to 4 Bit Mode)
589 #if USE_LCD_RW_PIN == TRUE
590  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);
591 #else
592  LCD_PORT = LCD_PORT & ~(1 << LCD_RS) & ~(1 << LCD_EN) & ~(1 << LCD_DB4) & ~(1 << LCD_DB5) & ~(1 << LCD_DB6) & ~(1 << LCD_DB7);
593 #endif
594  LCD_PORT = LCD_PORT | (1 << LCD_DB5);
595  lcd_enable();
596  _delay_ms(DISPLAY_INIT_WAIT_TIME04_MS);
597 
598 
599  //Function Set (4 Bit)
600 #if DISPLAY_RESOLUTION == HIGH_RES
601  lcd_send_command(0x24); //1 Line and 5x11 dots
602 #else
603  lcd_send_command(0x28); //2 lines and 5x8 dots
604 #endif
605  _delay_ms(DISPLAY_INIT_WAIT_TIME_MS);
606 
607  //Display Off
608  lcd_send_command(0x08);
609  _delay_ms(DISPLAY_INIT_WAIT_TIME_MS);
610 
611  //Clear Display Command
613  _delay_ms(DISPLAY_CLEAR_WAIT_TIME_MS);
614 
615  //Entry Mode (Increment, Shift Off)
616  lcd_send_command(0x06);
617  _delay_ms(DISPLAY_INIT_WAIT_TIME_MS);
618 
619  //Display On
620  tmp = 0x0c;
621 #if DISPLAY_CURSOR == CURSOR_ON //Cursor On?
622  tmp = tmp | 0x02;
623 #endif
624 #if DISPLAY_CURSOR_BLINK == BLINK_ON //Blinking On?
625  tmp = tmp | 0x01;
626 #endif
627  lcd_send_command(tmp);
628  _delay_ms(DISPLAY_INIT_WAIT_TIME_MS);
629 
630  //Cursor Home Command
633 
634  //reset LCD port
635 #if USE_LCD_RW_PIN == TRUE
636  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);
637 #else
638  LCD_PORT = LCD_PORT & ~(1 << LCD_RS) & ~(1 << LCD_EN) & ~(1 << LCD_DB4) & ~(1 << LCD_DB5) & ~(1 << LCD_DB6) & ~(1 << LCD_DB7);
639 #endif
640 
641 
642 }
643 
644 
645 /****************************************************************************/
661 /****************************************************************************/
662 
663 void lcd_pin_init(void) {
664 
665  //initial values = 0
666 #if USE_LCD_RW_PIN == TRUE
667  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);
668  //set LCD Port Pins to Output ---> see "port_definitions.h"
669  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);
670 #else
671  LCD_PORT = LCD_PORT & ~(1 << LCD_RS) & ~(1 << LCD_EN) & ~(1 << LCD_DB4) & ~(1 << LCD_DB5) & ~(1 << LCD_DB6) & ~(1 << LCD_DB7);
672  //set LCD Port Pins to Output ---> see "port_definitions.h"
673  LCD_DDR = LCD_DDR | (1 << LCD_RS) | (1 << LCD_EN) | (1 << LCD_DB4) | (1 << LCD_DB5) | (1 << LCD_DB6) | (1 << LCD_DB7);
674 #endif
675 
676 
677 }
678 
679 
680 
681 /****************************************************************************/
696 /****************************************************************************/
697 
698 void lcd_blpin_init(void) {
699 
700  //initial values = 0
701  LCDBL_PORT = LCDBL_PORT & ~(1 << LCDBL_CTRL);
702  //set LCD Port Pins to Output ---> see "port_definitions.h"
703  LCDBL_DDR = LCDBL_DDR | (1 << LCDBL_CTRL);
704 
706 
707 }