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 hd44780_lcd
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 <stdint.h>
47#include <stdbool.h>
48
49
50/**
51 * Configuration struct, to be instantiated and values assigned before passing
52 * it's address into and calling lcd_init().
53 * NOTE: 5x10 font cannot be used in conjunction with 2 line display. 4 bit
54 * mode takes at least twice as long as 8 bit mode to send each character, but
55 * frees up some GPIO pins.
56 */
57typedef struct
58{
59 bool eight_bit_mode; ///< true = 8 bit mode, false = 4 bit mode.
60 bool two_line_display; ///< true = 2 lines, false = 1 line.
61 bool five_by_ten_font; ///< true = 5x10 dots, false = 5x8 dots.
62 bool increment_counter; ///< true = increment, false = decrement.
63 bool display_shift; ///< true = display shift, false = cursor shift.
64 bool cursor_enable; ///< true = enabled, false = disabled.
65 bool blink_enable; ///< true = enabled, false = disabled.
67
68
69/**
70 * Initialisation routine (run once at startup).
71 * This function is to be called immediately after powerup of the display
72 * module. Instantiate the lcd_config_t object first then pass it's address
73 * into init_lcd() before using any other lcd functions.
74 * @param p_config is a pointer to the lcd_config_t object.
75 * @return Returns void.
76 */
77void init_lcd(lcd_config_t *p_config);
78
79
80/**
81 * Turn display off (config settings are retained). Side effect: cursor_enable
82 * and blink_enable variable states are written to the display during this
83 * command, so if they have been changed but lcd_reconfigure() was not called
84 * afterwards, this function will update those config bits on the display.
85 * @return Returns void.
86 */
87void lcd_display_off(void);
88
89
90/**
91 * Turn display on. Side effect: cursor_enable and blink_enable variable states
92 * are written to the display during this command, so if they have been changed
93 * but lcd_reconfigure() was not called afterwards, this function will update
94 * those config bits on the display.
95 * @return Returns void.
96 */
97void lcd_display_on(void);
98
99
100/**
101 * Prints a string of characters to the display. This function takes a string
102 * literal (constant) as it's parameter. Be sure to enclose text passed in with
103 * "" quotation marks. eg: "Hello, World!".
104 * @param str is a string literal.
105 * @return Returns void.
106 */
107void lcd_print_string(const char *str);
108
109
110/**
111 * Prints an integer variable. This function prints a signed integer value to
112 * the display, values from -32768 to 32767 are acceptable.
113 * @param number is a signed, fixed width integer.
114 * @return Returns void.
115 */
116void lcd_print_integer(int16_t number);
117
118
119/**
120 * Sets cursor location using x and y coordinates.
121 * Column is 0 for top row, 1 for bottom row (in 2 row mode). Row can be from
122 * 0 - 15, values outside of this range will be written to non visible spaces
123 * on the display.
124 * @param column is the x coordinate.
125 * @param row is the y coordinate.
126 * @return Returns void.
127 */
128void lcd_set_cursor(uint8_t column, uint8_t row);
129
130
131/**
132 * Writes space characters to all 32 sections of display (or 16 if in 1 line
133 * mode). This is much faster than the clear_display command, as that works by
134 * writing a space character to every single section of the display, even all
135 * the non-visible ones.
136 * @return Returns void.
137 */
138void lcd_fast_clear(void);
139
140
141/**
142 * Edits config settings on the display (lcd_config_t members must be changed
143 * first). data_length, display_lines, and font_size cannot be changed after
144 * init_lcd() is called.
145 * @return Returns void.
146 */
147void lcd_reconfigure(void);
148
149
150/**
151 * Sets DDRAM address 0 in address counter. Also returns display from being
152 * shifted to original position. DDRAM contents remain unchanged.
153 * @return Returns void.
154 */
155void lcd_return_home(void);
156
157
158/**
159 * Moves cursor left without changing DDRAM contents.
160 * @param distance is used to specify how many spaces to move.
161 * @return Returns void.
162 */
163void lcd_shift_cursor_left(uint8_t distance);
164
165
166/**
167 * Moves cursor right without changing DDRAM contents.
168 * @param distance is used to specify how many spaces to move.
169 * @return Returns void.
170 */
171void lcd_shift_cursor_right(uint8_t distance);
172
173
174/**
175 * Shifts display left without changing DDRAM contents. Optional delay between
176 * shifts for scrolling effect.
177 * @param distance is used to specify how many spaces to move.
178 * @param delay - pass in true for scrolling effect, false for no delay.
179 * @return Returns void.
180 */
181void lcd_shift_display_left(uint8_t distance, bool delay);
182
183
184/**
185 * Shifts display right without changing DDRAM contents. Optional delay between
186 * shifts for scrolling effect.
187 * @param distance is used to specify how many spaces to move.
188 * @param delay - pass in true for scrolling effect, false for no delay.
189 * @return Returns void.
190 */
191void lcd_shift_display_right(uint8_t distance, bool delay);
192
193
194#endif // HD_FOUR_FOUR_SEVEN_EIGHT_ZERO_LCD_DOT_H
195
196
197/*** 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