SLS AVR Lib 0.1a
 
Loading...
Searching...
No Matches
lcd_hd44780_pin.h
Go to the documentation of this file.
1// ---------------------------------------------------------------------------+
2// This file is part of SLS AVR Library
3// https://github.com/SimonLitt/sls-avr-lib
4// ---------------------------------------------------------------------------+
5// Copyright (C) 2025 Simon Litt <simon@1itt.net> https://coding.1itt.net,
6// https://github.com/SimonLitt
7//
8// This program is free software: you can redistribute it and/or modify it
9// under the terms of the GNU General Public License as published by the Free
10// Software Foundation, version 3.
11//
12// This program is distributed in the hope that it will be useful, but WITHOUT
13// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15// more details.
16//
17// You should have received a copy of the GNU General Public License along
18// with this program. If not, see <https://www.gnu.org/licenses/>.
19// ---------------------------------------------------------------------------+
30#ifndef SLS_AVR_LCD_HD44780_PIN_H_
31#define SLS_AVR_LCD_HD44780_PIN_H_
32
33#include <stdbool.h>
34#include <stdint.h>
35
36#include <sls-avr/avr.h>
37#include <sls-lcd/dm_hd44780.h>
38
39#ifndef LCD_HD44780_PIN_SINGLE_SOME_CODE
40# define LCD_HD44780_PIN_SINGLE_SOME_CODE 0
41#endif // BTN_FAST_SOME_CODE
42
43#if LCD_HD44780_PIN_MULTI_MODE
44
45typedef uint8_t lcd_addr_t;
46typedef struct {
47 uint8_t flags;
48 uint8_t row_cout;
49 uint8_t col_cout;
50
51 ddr_t data_ddr;
52 port_t data_port;
53 pin_t data_pin;
54 uint8_t data_shift; // For 4-bit IDL
55 uint8_t port_mask;
56
57 port_t rs_port;
58 pin_bit_t rs_pin;
59
60 port_t e_port;
61 pin_bit_t e_pin;
62
63 //port_t e2_port; // LCD 40x4
64 //pin_bit_t e2_pin; // LCD 40x4
65
66 port_t rw_port;
67 pin_bit_t rw_pin;
68
69 lcd_addr_t row_1_addr;
70 lcd_addr_t row_2_addr;
71 lcd_addr_t row_3_addr;
72 lcd_addr_t row_4_addr;
73} lcd_info_struct;
74
75typedef volatile lcd_info_struct lcd_info_t;
76
77# define __LCD_MULTIMODE_ONLY_INFO_ARG(_n) const lcd_info_t *const _ ## _n
78# define __LCD_MULTIMODE_ONLY_INFO_ARG_WITH_COMMA(_n) __LCD_MULTIMODE_ONLY_INFO_ARG(_n),
79
80#else
81# define LCD_HD44780_PIN_MULTI_MODE 0
82
83# ifndef LCD_HD44780_PIN_DISPLAY_TYPE
84# define LCD_HD44780_PIN_DISPLAY_TYPE
85# error "LCD_HD44780_PIN_DISPLAY_TYPE should be specified"
86# endif
87
88# ifndef LCD_HD44780_PIN_DATA_PORT
89# error "LCD_HD44780_PIN_DATA_PORT should be specified"
90# endif
91
92# if !LCD_HD44780_PIN_IDL_8BIT
93# ifndef LCD_HD44780_PIN_DATA_FIRST_PIN
94# error "For 4-bit IDL the LCD_HD44780_PIN_DATA_FIRST_PIN should be specified!"
95# endif
96# endif
97
98# if (!defined(LCD_HD44780_PIN_RS_PORT)) || (!defined(LCD_HD44780_PIN_RS_PIN))
99# error "LCD_HD44780_PIN_RS_PORT and LCD_HD44780_PIN_RS_PIN should be specified!"
100# endif
101
102# if (!defined(LCD_HD44780_PIN_E_PORT)) || (!defined(LCD_HD44780_PIN_E_PIN))
103# error "LCD_HD44780_PIN_E_PORT and LCD_HD44780_PIN_E_PIN should be specified!"
104# endif
105
106# if defined(LCD_HD44780_PIN_RW_PORT) && defined(LCD_HD44780_PIN_RW_PIN)
107# define LCD_HD44780_PIN_ALLOW_RW 1
108# endif
110# if LCD_HD44780_PIN_SINGLE_SOME_CODE
111# define lcd_info_t byte_t
112# else
113# define lcd_info_t void
114# endif // LCD_HD44780_PIN_SINGLE_SOME_CODE
115# define __LCD_MULTIMODE_ONLY_INFO_ARG(_n) void
116# define __LCD_MULTIMODE_ONLY_INFO_ARG_WITH_COMMA(_n)
118#endif // LCD_HD44780_PIN_MULTI_MODE
119
120#if __DOXYGEN__
122typedef volatile lcd_info_struct lcd_info_t;
123#endif // __DOXYGEN__
124
125// It's possible to pass a single port pointer, such as PINA, and receive DDRA and PORTA as a pointer offset. However, multi-screen mode is an exception, mostly for testing, so we'll leave it as is.
127typedef struct {
128 uint8_t flags;
129 #if LCD_HD44780_PIN_MULTI_MODE || __DOXYGEN__
131
135 uint8_t data_shift;
136
140
144
148 #endif // LCD_HD44780_PIN_MULTI_MODE
149} lcd_init_t;
150
151// ---------------------------------------------------------------------------+
152// Init flags
153// ---------------------------------------------------------------------------+
155#define __HD44780_INIT_IDL_BIT 0
156#define __HD44780_INIT_READ_BIT 1
157#define __HD44780_INIT_MOV_DIR_BIT 2
158#define __HD44780_INIT_SHIFT_BIT 3
159#define __HD44780_INIT_BLINKING_BIT 4
160#define __HD44780_INIT_CURSOR_BIT 5
161#define __HD44780_INIT_FONT_BIT 6
162#define __HD44780_INIT_DISP_BIT 7
164
165#define HD44780_INIT_IDL_4BIT 0x00
166#define HD44780_INIT_IDL_8BIT (_BV(__HD44780_INIT_IDL_BIT))
167
168#define HD44780_INIT_READ_OFF 0x00
169#define HD44780_INIT_READ_ON (_BV(__HD44780_INIT_READ_BIT))
170
171#define HD44780_INIT_MOV_DIR_DEC 0x00
172#define HD44780_INIT_MOV_DIR_INC (_BV(__HD44780_INIT_MOV_DIR_BIT))
173
174#define HD44780_INIT_SHIFT_OFF 0x00
175#define HD44780_INIT_SHIFT_ON (_BV(__HD44780_INIT_SHIFT_BIT))
176
177#define HD44780_INIT_BLINKING_OFF 0x00
178#define HD44780_INIT_BLINKING_ON (_BV(__HD44780_INIT_BLINKING_BIT))
179
180#define HD44780_INIT_CURSOR_OFF 0x00
181#define HD44780_INIT_CURSOR_ON (_BV(__HD44780_INIT_CURSOR_BIT))
182
183#define HD44780_INIT_FONT_NORMAL 0x00
184#define HD44780_INIT_FONT_BIG (_BV(__HD44780_INIT_FONT_BIT))
185
186#define HD44780_INIT_DISP_OFF 0x00
187#define HD44780_INIT_DISP_ON (_BV(__HD44780_INIT_DISP_BIT))
188
190#define __HD44780_CONF_IDL_BIT 0
191#define __HD44780_CONF_READ_BIT 1
192
193#define _HD44780_CONF_IDL_4BIT 0x00 // 4-bit interface data length.
194#define _HD44780_CONF_IDL_8BIT (_BV(__HD44780_CONF_IDL_BIT)) // 8-bit interface data length.
195
196#define _HD44780_CONF_READ_OFF 0x00 // Read disable.
197#define _HD44780_CONF_READ_ON (_BV(__HD44780_CONF_READ_BIT)) // Read enable.
198
199#define _HD44780_HALF_DATA_MASK 0x0F // 4-bit interface default mask
201
202#if LCD_HD44780_PIN_MULTI_MODE || __DOXYGEN__
211lcd_info_t lcd_init(const lcd_init_t *const config);
212
219void lcd_clear(const lcd_info_t *const info);
220
228void lcd_home(const lcd_info_t *const info, const uint8_t flags);
229
237void lcd_entry_mode(const lcd_info_t *const info, const uint8_t flags);
238
246void lcd_display_ctrl(const lcd_info_t *const info, const uint8_t flags);
247
255void lcd_cursor(const lcd_info_t *const info, const uint8_t flags);
256
264void lcd_func_set(const lcd_info_t *const info, const uint8_t flags);
265
273void lcd_cgr_adr(const lcd_info_t *const info, const uint8_t flags);
274
282void lcd_ddr_adr(const lcd_info_t *const info, const uint8_t flags);
283
292void lcd_set_pos(const lcd_info_t *const info, const lcd_line_t line, const uint8_t pos);
293
301void lcd_byte(const lcd_info_t *const info, const byte_t ch);
302
312void lcd_line(const lcd_info_t *const info, const char str[], const lcd_line_t line, const uint8_t start_pos);
313
322void lcd_print(const lcd_info_t *const info, const char str[]);
323
331void lcd_refresh_ml(const lcd_info_t *const info, const char str[]);
332
342void lcd_custom_char(const lcd_info_t *const info, const byte_t char_pos, const byte_t custom_char[8]);
343
352
360byte_t lcd_read_data(const lcd_info_t *const info);
361#endif // LCD_HD44780_PIN_MULTI_MODE
362
363#if !LCD_HD44780_PIN_MULTI_MODE
364# if !LCD_HD44780_PIN_SINGLE_SOME_CODE
370void lcd_init(const lcd_init_t *const config);
371# else
372lcd_info_t lcd_init(const lcd_init_t *const config);
373# endif // LCD_HD44780_PIN_SINGLE_SOME_CODE
374
375# if LCD_HD44780_PIN_SINGLE_SOME_CODE
376 #error "TODO: implement"
377 void _sc_lcd_clear(void);
378 void _sc_lcd_home(const uint8_t flags);
379 void _sc_lcd_entry_mode(const uint8_t flags);
380 void _sc_lcd_display_ctrl(const uint8_t flags);
381 void _sc_lcd_cursor(const uint8_t flags);
382 void _sc_lcd_func_set(const uint8_t flags);
383 void _sc_lcd_cgr_adr(const uint8_t flags);
384 void _sc_lcd_ddr_adr(const uint8_t flags);
385 void _sc_lcd_set_pos(const lcd_line_t line, const uint8_t pos);
386 void _sc_lcd_byte(const byte_t ch);
387 void _sc_lcd_line(const char str[], const lcd_line_t line, const uint8_t start_pos);
388 void _sc_lcd_print(const char str[]);
389 void _sc_lcd_refresh_ml(const char str[]);
390 void _sc_lcd_custom_char(const byte_t char_pos, const byte_t custom_char[8]);
391 byte_t _sc_lcd_read_busy_and_addr();
392 byte_t _sc_lcd_read_data();
393# define lcd_clear(_i) _sc_lcd_clear()
394# define lcd_home(_i, flags) _sc_lcd_home((flags))
395# define lcd_entry_mode(_i, flags) _sc_lcd_entry_mode((flags))
396# define lcd_display_ctrl(_i, flags) _sc_lcd_entry_mode((flags))
397# define lcd_cursor(_i, flags) _sc_lcd_entry_mode((flags))
398# define lcd_func_set(_i, flags) _sc_lcd_entry_mode((flags))
399# define lcd_cgr_adr(_i, flags) _sc_lcd_entry_mode((flags))
400# define lcd_ddr_adr(_i, flags) _sc_lcd_entry_mode((flags))
401# define lcd_set_pos(_i, line, pos) _sc_lcd_set_pos((line), (pos))
402# define lcd_byte(_i, ch) _sc_lcd_byte((ch))
403# define lcd_line(_i, str, line, sp) _sc_lcd_line((str), (line), (sp))
404# define lcd_print(_i, str) _sc_lcd_print((str))
405# define lcd_refresh_ml(_i, str) _sc_lcd_refresh_ml((str))
406# define lcd_custom_char(_i, pos, s) _sc_lcd_custom_char((pos), (s))
407# define lcd_read_busy_and_addr(_i) _sc_lcd_read_busy_and_addr()
408# define lcd_read_data(_i) _sc_lcd_read_data()
409# else
413void lcd_clear(void);
414
419void lcd_home(const uint8_t flags);
420
425void lcd_entry_mode(const uint8_t flags);
426
431void lcd_display_ctrl(const uint8_t flags);
432
437void lcd_cursor(const uint8_t flags);
438
443void lcd_func_set(const uint8_t flags);
444
449void lcd_cgr_adr(const uint8_t flags);
450
455void lcd_ddr_adr(const uint8_t flags);
456
462void lcd_set_pos(const lcd_line_t line, const uint8_t pos);
463
468void lcd_byte(const byte_t ch);
469
476void lcd_line(const char str[], const lcd_line_t line, const uint8_t start_pos);
477
483void lcd_print(const char str[]);
484
489void lcd_refresh_ml(const char str[]);
490
491# if LCD_HD44780_PIN_ALLOW_RW || __DOXYGEN__
497
503# endif
504# endif // LCD_HD44780_PIN_SINGLE_SOME_CODE
505#endif // LCD_HD44780_PIN_MULTI_MODE
506
507#endif // SLS_AVR_LCD_HD44780_PIN_H_
AVR lib.
uint8_t byte_t
8-bit type
Definition defs.h:64
byte_t pin_bit_t
The type for specifying the pin bit.
Definition defs.h:91
volatile byte_t *const ddr_t
Type for passing a reference to a direct register.
Definition defs.h:93
volatile byte_t *const port_t
Type for passing a reference to a port register.
Definition defs.h:94
volatile byte_t *const pin_t
Type for passing a reference to a pin register.
Definition defs.h:95
The HD44780 compatible dot-matrix liquid crystal display lib.
lcd_line_t
LCD row.
Definition dm_hd44780.h:277
lcd_display_t
LCD display type.
Definition dm_hd44780.h:297
void lcd_byte(const lcd_info_t *const info, const byte_t ch)
Outputs a symbol.
void lcd_func_set(const lcd_info_t *const info, const uint8_t flags)
Sets options.
void lcd_entry_mode(const lcd_info_t *const info, const uint8_t flags)
Sets cursor move direction and specifies display shift.
void lcd_ddr_adr(const lcd_info_t *const info, const uint8_t flags)
Sets DDRAM address.
void lcd_custom_char(const lcd_info_t *const info, const byte_t char_pos, const byte_t custom_char[8])
Creates a custom symbol.
volatile lcd_info_struct lcd_info_t
Information to distinguish a specific display from several connected ones.
Definition lcd_hd44780_pin.h:122
void lcd_cursor(const lcd_info_t *const info, const uint8_t flags)
Moves the cursor or shifts the display.
void lcd_line(const lcd_info_t *const info, const char str[], const lcd_line_t line, const uint8_t start_pos)
Outputs a string on entry line.
void lcd_cgr_adr(const lcd_info_t *const info, const uint8_t flags)
Sets CGRAM address.
byte_t lcd_read_data()
Reads data from CGRAM or from DDRAM.
void lcd_display_ctrl(const lcd_info_t *const info, const uint8_t flags)
Sets diplay options.
void lcd_print(const lcd_info_t *const info, const char str[])
Outputs a string to buffer.
void lcd_clear(void)
Clears the display.
void lcd_set_pos(const lcd_info_t *const info, const lcd_line_t line, const uint8_t pos)
Sets DDRAM address to a given position.
byte_t lcd_read_busy_and_addr()
Waits until display is buisy and returns address counter contents.
lcd_info_t lcd_init(const lcd_init_t *const config)
Initializes the display.
void lcd_home(const lcd_info_t *const info, const uint8_t flags)
Sets DDRAM address 0 in a ddress counter.
void lcd_refresh_ml(const lcd_info_t *const info, const char str[])
Outputs a string on all lines.
Initialization information.
Definition lcd_hd44780_pin.h:127
ddr_t rs_ddr
RS DDRx register.
Definition lcd_hd44780_pin.h:137
pin_bit_t e_pin
E(enable) pin bit.
Definition lcd_hd44780_pin.h:143
port_t rw_port
RW PORTx register. This is only necessary if reading is enabled.
Definition lcd_hd44780_pin.h:146
port_t e_port
E(enable) PORTx register.
Definition lcd_hd44780_pin.h:142
pin_t data_pin
This is only necessary if reading is enabled.
Definition lcd_hd44780_pin.h:134
pin_bit_t rs_pin
RS pin bit.
Definition lcd_hd44780_pin.h:139
ddr_t e_ddr
E(enable) DDRx register.
Definition lcd_hd44780_pin.h:141
ddr_t rw_ddr
RW DDRx register. This is only necessary if reading is enabled.
Definition lcd_hd44780_pin.h:145
uint8_t data_shift
It is only necessary for 4-bit IDL.
Definition lcd_hd44780_pin.h:135
port_t data_port
Data PORTx register.
Definition lcd_hd44780_pin.h:133
uint8_t flags
Config flags.
Definition lcd_hd44780_pin.h:128
lcd_display_t display_type
Display type, see lcd_display_t.
Definition lcd_hd44780_pin.h:130
ddr_t data_ddr
Data DDRx register.
Definition lcd_hd44780_pin.h:132
pin_bit_t rw_pin
RW pin bit. This is only necessary if reading is enabled.
Definition lcd_hd44780_pin.h:147
port_t rs_port
RS PORTx register.
Definition lcd_hd44780_pin.h:138