lcd.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. * 4-bit HD44780 LCD support
  3. *
  4. * Copyright (C) 2007-2016 Michael Buesch <m@bues.ch>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include "lcd.h"
  17. #include <avr/io.h>
  18. #include <util/delay.h>
  19. #include <string.h>
  20. #include <stdio.h>
  21. #define LCD_NR_CHARS (LCD_NR_LINES * LCD_NR_COLUMNS)
  22. #define LCD_BUFFER_SIZE LCD_NR_CHARS
  23. static uint8_t lcd_buffer[LCD_BUFFER_SIZE];
  24. uint8_t lcd_cursor_pos;
  25. /** lcd_enable_pulse - Send an E-pulse */
  26. static void lcd_enable_pulse(void)
  27. {
  28. LCD_PORT = (uint8_t)(LCD_PORT | LCD_PIN_E);
  29. __asm__ __volatile__("nop\n\t"
  30. "nop\n\t"
  31. : : : "memory");
  32. LCD_PORT = (uint8_t)(LCD_PORT & ~LCD_PIN_E);
  33. }
  34. /** lcd_write - Write one byte to the LCD. */
  35. static void lcd_write(uint8_t data)
  36. {
  37. LCD_PORT = (uint8_t)((LCD_PORT & ~(0xF << LCD_DATA_SHIFT)) |
  38. (((data & 0xF0) >> 4) << LCD_DATA_SHIFT));
  39. lcd_enable_pulse();
  40. LCD_PORT = (uint8_t)((LCD_PORT & ~(0xF << LCD_DATA_SHIFT)) |
  41. ((data & 0x0F) << LCD_DATA_SHIFT));
  42. lcd_enable_pulse();
  43. _delay_us(50);
  44. }
  45. /** lcd_data - Send data to the LCD. */
  46. static void lcd_data(uint8_t data)
  47. {
  48. LCD_PORT = (uint8_t)(LCD_PORT | LCD_PIN_RS);
  49. lcd_write(data);
  50. }
  51. /** lcd_command - Send command to the LCD. */
  52. static void lcd_command(uint8_t command)
  53. {
  54. LCD_PORT = (uint8_t)(LCD_PORT & ~LCD_PIN_RS);
  55. lcd_write(command);
  56. }
  57. /** lcd_cmd_clear - Clear LCD and return cursor to home position. */
  58. static void lcd_cmd_clear(void)
  59. {
  60. lcd_command(0x01);
  61. _delay_ms(2);
  62. }
  63. /** lcd_cmd_home - Move cursor to home position. */
  64. static void lcd_cmd_home(void)
  65. {
  66. lcd_command(0x02);
  67. _delay_ms(2);
  68. }
  69. /** lcd_cmd_entrymode - Set entry mode.
  70. * @cursor_inc: 1 = increment cursor, 0 = decrement cursor.
  71. * @display_shift: 1 = shift display, 0 = don't shift display.
  72. */
  73. static void lcd_cmd_entrymode(uint8_t cursor_inc,
  74. uint8_t display_shift)
  75. {
  76. lcd_command((uint8_t)(0x04u |
  77. (cursor_inc ? 0x02u : 0x00u) |
  78. (display_shift ? 0x01u : 0x00u)));
  79. }
  80. /** lcd_cmd_dispctl - Display control.
  81. * @display_on: 1 = turn display on, 0 = turn display off.
  82. * @cursor_on: 1 = turn cursor on, 0 = turn cursor off.
  83. * @cursor_blink: 1 = blink cursor, 0 = don't blink cursor.
  84. */
  85. void lcd_cmd_dispctl(uint8_t display_on,
  86. uint8_t cursor_on,
  87. uint8_t cursor_blink)
  88. {
  89. lcd_command((uint8_t)(0x08u |
  90. (display_on ? 0x04u : 0x00u) |
  91. (cursor_on ? 0x02u : 0x00u) |
  92. (cursor_blink ? 0x01u : 0x00u)));
  93. }
  94. /** lcd_cmd_shiftctl - Display shift control.
  95. * @shift_display: 1 = move display, 0 = move cursor.
  96. * @shift_right: 1 = right direction, 0 = left direction.
  97. */
  98. static void lcd_cmd_shiftctl(uint8_t shift_display,
  99. uint8_t shift_right)
  100. {
  101. lcd_command((uint8_t)(0x10u |
  102. (shift_display ? 0x08u : 0x00u) |
  103. (shift_right ? 0x04u : 0x00u)));
  104. }
  105. /** lcd_cmd_funcset - Set basic display function.
  106. * @eight_bit: 1 = 8bit mode, 0 = 4bit mode.
  107. * @two_lines: 1 = two rows, 0 = one row.
  108. * @font_5x10: 1 = 5x10 font, 0 = 5x8 font.
  109. */
  110. static void lcd_cmd_funcset(uint8_t eight_bit,
  111. uint8_t two_lines,
  112. uint8_t font_5x10)
  113. {
  114. lcd_command((uint8_t)(0x20u |
  115. (eight_bit ? 0x10u : 0x00u) |
  116. (two_lines ? 0x08u : 0x00u) |
  117. (font_5x10 ? 0x04u : 0x00u)));
  118. }
  119. /** lcd_cmd_cgram_addr_set - Move CGRAM address pointer.
  120. * @address: The address to move to.
  121. */
  122. static void lcd_cmd_cgram_addr_set(uint8_t address)
  123. {
  124. lcd_command((uint8_t)(0x40u | (address & 0x3Fu)));
  125. }
  126. /** lcd_cmd_cursor - Move cursor (DDRAM address).
  127. * @line: Line number. 0 - 1.
  128. * @column: Column number. 0 - 15.
  129. */
  130. void lcd_cmd_cursor(uint8_t line, uint8_t column)
  131. {
  132. lcd_command((uint8_t)(0x80u |
  133. ((line & (LCD_NR_LINES - 1u)) << 6u) |
  134. (column & (LCD_NR_COLUMNS - 1u))));
  135. }
  136. /** lcd_clear_buffer - Clear the software buffer. */
  137. void lcd_clear_buffer(void)
  138. {
  139. memset(lcd_buffer, ' ', LCD_BUFFER_SIZE);
  140. lcd_cursor_pos = 0;
  141. }
  142. /** lcd_commit - Write the software buffer to the display. */
  143. void lcd_commit(void)
  144. {
  145. uint8_t line, col;
  146. const uint8_t *buf = lcd_buffer;
  147. for (line = 0; line < LCD_NR_LINES; line++) {
  148. lcd_cmd_cursor(line, 0);
  149. for (col = 0; col < LCD_NR_COLUMNS; col++)
  150. lcd_data(*buf++);
  151. }
  152. lcd_cmd_cursor(lcd_getline(), lcd_getcolumn());
  153. }
  154. /** lcd_put_char - Put one character into software buffer. */
  155. void lcd_put_char(char c)
  156. {
  157. uint8_t line, column;
  158. if (c == '\r') {
  159. lcd_cursor(lcd_getline(), 0);
  160. } else if (c == '\n') {
  161. line = (uint8_t)(lcd_getline() + 1u);
  162. lcd_cursor(line & (LCD_NR_LINES - 1u), 0);
  163. } else {
  164. lcd_buffer[lcd_cursor_pos] = (uint8_t)c;
  165. column = (lcd_getcolumn() + 1u) & (LCD_NR_COLUMNS - 1u);
  166. lcd_cursor(lcd_getline(), column);
  167. }
  168. }
  169. static int lcd_stream_putchar(char c, FILE *unused)
  170. {
  171. lcd_put_char(c);
  172. return 0;
  173. }
  174. static FILE lcd_fstream = FDEV_SETUP_STREAM(lcd_stream_putchar, NULL,
  175. _FDEV_SETUP_WRITE);
  176. void _lcd_printf(const char PROGPTR *fmt, ...)
  177. {
  178. va_list args;
  179. va_start(args, fmt);
  180. vfprintf_P(&lcd_fstream, fmt, args);
  181. va_end(args);
  182. }
  183. void lcd_put_pstr(const char PROGPTR *str)
  184. {
  185. uint8_t c;
  186. for ( ; ; str++) {
  187. c = pgm_read_byte(str);
  188. if (c == '\0')
  189. break;
  190. lcd_put_char((char)c);
  191. }
  192. }
  193. /** lcd_upload_char - Upload a user defined character to CGRAM.
  194. * @char_code: The character code to use.
  195. * @char_tab: The character bitmap. The bitmap has got one byte
  196. * per character pixel row. The upper 3 bits of each byte
  197. * are unused. For 5x10 char-pixel displays, the char_tab
  198. * is 10 bytes long. For 5x8 char-pixel displays, it's 8 bytes.
  199. */
  200. void lcd_upload_char(uint8_t char_code,
  201. const uint8_t PROGPTR *char_tab)
  202. {
  203. uint8_t i, c, address;
  204. address = (uint8_t)(char_code << (LCD_FONT_5x10 ? 4u : 3u));
  205. for (i = 0; i < (LCD_FONT_5x10 ? 10u : 8u); i++) {
  206. lcd_cmd_cgram_addr_set(address);
  207. c = pgm_read_byte(char_tab);
  208. lcd_data(c);
  209. address++;
  210. char_tab++;
  211. }
  212. lcd_cmd_cursor(lcd_getline(), lcd_getcolumn());
  213. }
  214. /** lcd_init - Initialize the LCD. */
  215. void lcd_init(void)
  216. {
  217. uint8_t i;
  218. LCD_DDR = (uint8_t)(LCD_DDR | (0xFu << LCD_DATA_SHIFT) |
  219. LCD_PIN_E | LCD_PIN_RS);
  220. /* Force it into 8-bit mode first */
  221. LCD_PORT = (uint8_t)(LCD_PORT & ~(LCD_PIN_E | LCD_PIN_RS |
  222. (0xFu << LCD_DATA_SHIFT)));
  223. LCD_PORT = (uint8_t)(LCD_PORT | (0x03u << LCD_DATA_SHIFT));
  224. long_delay_ms(200);
  225. for (i = 3; i; i--) {
  226. lcd_enable_pulse();
  227. _delay_ms(5);
  228. }
  229. /* We're in a known state. Enable 4-bit mode. */
  230. LCD_PORT = (uint8_t)((LCD_PORT & ~(0xFu << LCD_DATA_SHIFT)) |
  231. (0x02u << LCD_DATA_SHIFT));
  232. lcd_enable_pulse();
  233. _delay_ms(10);
  234. lcd_cmd_funcset(0,
  235. (LCD_NR_LINES > 1) ? 1 : 0,
  236. LCD_FONT_5x10 ? 1 : 0);
  237. lcd_cmd_dispctl(0, 0, 0);
  238. lcd_cmd_clear();
  239. lcd_cmd_entrymode(1, 0);
  240. lcd_cmd_shiftctl(0, 0);
  241. lcd_cmd_dispctl(1, 0, 0);
  242. lcd_cmd_home();
  243. lcd_cursor_pos = 0;
  244. lcd_clear_buffer();
  245. lcd_commit();
  246. }