AVRly - AVR Development Resources
hd44780_lcd.h
Go to the documentation of this file.
1/******************************************************************************
2 @copyright Copyright © 2022 by Jason Duffy.
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 SOFTWARE.
21******************************************************************************/
22
23/**
24 * @file hd44780_lcd.h
25 * @ingroup digital_clock
26 * @author Jason Duffy
27 * @date 1st March 2022
28 * @brief Driver for the HD44780 based 16x2 liquid crystal display.
29 * @bug No known bugs.
30 *
31 * This file provides the basic low-level functionality for the ubiquitous 16x2
32 * display. Please note that it uses long blocking waits in the initialisation
33 * routine (100ms), and short blocking waits in other utility functions
34 * (2uS - 2mS). This was done to simplify the code as the busy flag read
35 * introduced pitfalls. This driver currently only allows for a single display
36 * to be used, but this might be improved on at a later date.
37 *
38 * This driver was written using the datasheet for the HITACHI HD44780U LCD
39 * driver chip, which can be found at the link below.
40 * @see http://www.datasheet-pdf.com/PDF/HD44780U-Datasheet-Hitachi-1109874
41 */
42
43#ifndef HD_FOUR_FOUR_SEVEN_EIGHT_ZERO_LCD_DOT_H
44#define HD_FOUR_FOUR_SEVEN_EIGHT_ZERO_LCD_DOT_H
45
46#include <stdbool.h>
47
48
49/**
50 * Configuration struct, to be instantiated and values assigned before passing
51 * it's address into and calling lcd_init().
52 * NOTE: 5x10 font cannot be used in conjunction with 2 line display. 4 bit
53 * mode takes at least twice as long as 8 bit mode to send each character, but
54 * frees up some GPIO pins.
55 */
56typedef struct
57{
58 bool eight_bit_mode; ///< true = 8 bit mode, false = 4 bit mode.
59 bool two_line_display; ///< true = 2 lines, false = 1 line.
60 bool five_by_ten_font; ///< true = 5x10 dots, false = 5x8 dots.
61 bool increment_counter; ///< true = increment, false = decrement.
62 bool display_shift; ///< true = display shift, false = cursor shift.
63 bool cursor_enable; ///< true = enabled, false = disabled.
64 bool blink_enable; ///< true = enabled, false = disabled.
66
67
68/**
69 * Initialisation routine (run once at startup).
70 * This function is to be called immediately after powerup of the display
71 * module. Instantiate the lcd_config_t object first then pass it's address
72 * into init_lcd() before using any other lcd functions.
73 * @param p_config is a pointer to the lcd_config_t object.
74 * @return Returns void.
75 */
76void init_lcd(lcd_config_t *p_config);
77
78
79/**
80 * Turn display off (config settings are retained). Side effect: cursor_enable
81 * and blink_enable variable states are written to the display during this
82 * command, so if they have been changed but lcd_reconfigure() was not called
83 * afterwards, this function will update those config bits on the display.
84 * @return Returns void.
85 */
86void lcd_display_off(void);
87
88
89/**
90 * Turn display on. Side effect: cursor_enable and blink_enable variable states
91 * are written to the display during this command, so if they have been changed
92 * but lcd_reconfigure() was not called afterwards, this function will update
93 * those config bits on the display.
94 * @return Returns void.
95 */
96void lcd_display_on(void);
97
98
99/**
100 * Prints a string of characters to the display. This function takes a string
101 * literal (constant) as it's parameter. Be sure to enclose text passed in with
102 * "" quotation marks. eg: "Hello, World!".
103 * @param str is a string literal.
104 * @return Returns void.
105 */
106void lcd_print_string(const char *str);
107
108
109/**
110 * Prints an integer variable. This function prints a signed integer value to
111 * the display, values from -32768 to 32767 are acceptable.
112 * @param number is a signed, fixed width integer.
113 * @return Returns void.
114 */
115void lcd_print_integer(int16_t number);
116
117
118/**
119 * Sets cursor location using x and y coordinates.
120 * Column is 0 for top row, 1 for bottom row (in 2 row mode). Row can be from
121 * 0 - 15, values outside of this range will be written to non visible spaces
122 * on the display.
123 * @param column is the x coordinate.
124 * @param row is the y coordinate.
125 * @return Returns void.
126 */
127void lcd_set_cursor(uint8_t column, uint8_t row);
128
129
130/**
131 * Writes space characters to all 32 sections of display (or 16 if in 1 line
132 * mode). This is much faster than the clear_display command, as that works by
133 * writing a space character to every single section of the display, even all
134 * the non-visible ones.
135 * @return Returns void.
136 */
137void lcd_fast_clear(void);
138
139
140/**
141 * Edits config settings on the display (lcd_config_t members must be changed
142 * first). data_length, display_lines, and font_size cannot be changed after
143 * init_lcd() is called.
144 * @return Returns void.
145 */
146void lcd_reconfigure(void);
147
148
149/**
150 * Sets DDRAM address 0 in address counter. Also returns display from being
151 * shifted to original position. DDRAM contents remain unchanged.
152 * @return Returns void.
153 */
154void lcd_return_home(void);
155
156
157/**
158 * Moves cursor left without changing DDRAM contents.
159 * @param distance is used to specify how many spaces to move.
160 * @return Returns void.
161 */
162void lcd_shift_cursor_left(uint8_t distance);
163
164
165/**
166 * Moves cursor right without changing DDRAM contents.
167 * @param distance is used to specify how many spaces to move.
168 * @return Returns void.
169 */
170void lcd_shift_cursor_right(uint8_t distance);
171
172
173/**
174 * Shifts display left without changing DDRAM contents. Optional delay between
175 * shifts for scrolling effect.
176 * @param distance is used to specify how many spaces to move.
177 * @param delay - pass in true for scrolling effect, false for no delay.
178 * @return Returns void.
179 */
180void lcd_shift_display_left(uint8_t distance, bool delay);
181
182
183/**
184 * Shifts display right without changing DDRAM contents. Optional delay between
185 * shifts for scrolling effect.
186 * @param distance is used to specify how many spaces to move.
187 * @param delay - pass in true for scrolling effect, false for no delay.
188 * @return Returns void.
189 */
190void lcd_shift_display_right(uint8_t distance, bool delay);
191
192
193#endif // HD_FOUR_FOUR_SEVEN_EIGHT_ZERO_LCD_DOT_H
194
195/*** end of file ***/
void lcd_shift_cursor_left(uint8_t distance)
Moves cursor left without changing DDRAM contents.
Definition: hd44780_lcd.c:319
void lcd_print_string(const char *str)
Prints a string of characters to the display.
Definition: hd44780_lcd.c:205
void lcd_display_off(void)
Turn display off (config settings are retained).
Definition: hd44780_lcd.c:178
void lcd_print_integer(int16_t number)
Prints an integer variable.
Definition: hd44780_lcd.c:217
void lcd_shift_display_right(uint8_t distance, bool delay)
Shifts display right without changing DDRAM contents.
Definition: hd44780_lcd.c:370
void init_lcd(lcd_config_t *p_config)
Initialisation routine (run once at startup).
Definition: hd44780_lcd.c:105
void lcd_display_on(void)
Turn display on.
Definition: hd44780_lcd.c:191
void lcd_fast_clear(void)
Writes space characters to all 32 sections of display (or 16 if in 1 line mode).
Definition: hd44780_lcd.c:270
void lcd_shift_display_left(uint8_t distance, bool delay)
Shifts display left without changing DDRAM contents.
Definition: hd44780_lcd.c:350
void lcd_reconfigure(void)
Edits config settings on the display (lcd_config_t members must be changed first).
Definition: hd44780_lcd.c:292
void lcd_return_home(void)
Sets DDRAM address 0 in address counter.
Definition: hd44780_lcd.c:309
void lcd_shift_cursor_right(uint8_t distance)
Moves cursor right without changing DDRAM contents.
Definition: hd44780_lcd.c:334
void lcd_set_cursor(uint8_t column, uint8_t row)
Sets cursor location using x and y coordinates.
Definition: hd44780_lcd.c:247
Configuration struct, to be instantiated and values assigned before passing it's address into and cal...
Definition: hd44780_lcd.h:57
bool display_shift
true = display shift, false = cursor shift.
Definition: hd44780_lcd.h:62
bool cursor_enable
true = enabled, false = disabled.
Definition: hd44780_lcd.h:63
bool eight_bit_mode
true = 8 bit mode, false = 4 bit mode.
Definition: hd44780_lcd.h:58
bool increment_counter
true = increment, false = decrement.
Definition: hd44780_lcd.h:61
bool blink_enable
true = enabled, false = disabled.
Definition: hd44780_lcd.h:64
bool five_by_ten_font
true = 5x10 dots, false = 5x8 dots.
Definition: hd44780_lcd.h:60
bool two_line_display
true = 2 lines, false = 1 line.
Definition: hd44780_lcd.h:59