aboutsummaryrefslogtreecommitdiff
path: root/3rd-party/LCDHD44780.h
blob: 8e2e95367d05db760e49d1cc50999fdd4e1a820a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*##############################################################*/
/* Author: Marcus Nasarek                                       */
/* File:   LCDH44780.h                                          */
/* License:                                                     */
/*                                                              */
/*  This program is free software; you can redistribute it      */
/*  and/or modify it under the terms of the GNU General         */
/*  Public License as published by the Free Software            */
/*  Foundation; either version 3 of the License, or             */
/*  (at your option) any later version.                         */
/*                                                              */
/*  This program is distributed in the hope that it will        */
/*  be useful, but WITHOUT ANY WARRANTY; without even the       */
/*  implied warranty of MERCHANTABILITY or                      */
/*  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General       */
/*  Public License for more details.                            */
/*                                                              */
/*  You should have received a copy of the GNU General          */
/*  Public License along with this program; if not,             */
/*  see <http://www.gnu.org/licenses/>.                         */
/*                                                              */
/*##############################################################*/

// Thanks to Matt Hawkins (http://www.raspberrypi-spy.co.uk)
// The wiring for the LCD is as follows:
// 1 : GND
// 2 : 5V
// 3 : Contrast (0-5V)*
// 4 : RS (Register Select)
// 5 : R/W (Read Write)       - GROUND THIS PIN
// 6 : Enable or Strobe
// 7 : Data Bit 0             - NOT USED
// 8 : Data Bit 1             - NOT USED
// 9 : Data Bit 2             - NOT USED
// 10: Data Bit 3             - NOT USED
// 11: Data Bit 4
// 12: Data Bit 5
// 13: Data Bit 6
// 14: Data Bit 7
// 15: LCD Backlight +5V**
// 16: LCD Backlight GND

#ifndef _LCDHD44780_H_
#define _LCDHD44780_H_

#include <stdlib.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <bcm2835.h>

// Define GPIO pin to LCD pin mapping
#define LCD_RS 21
#define LCD_E  20
#define LCD_D4 26
#define LCD_D5 19
#define LCD_D6 13
#define LCD_D7 6

// Define some device constants
// Maximum characters per line
#define LCD_WIDTH 16
#define LCD_DATA HIGH
#define LCD_CMD LOW

// Function set -
// Sets interface data length
// (DL), number of display lines
// (N), and character font (F).
// RS R/W DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0
// 0  0   0   0   1   DL   N   F   -   -
// DL = 0 --> 4 bit data length (1 = 8 bit)
// N = 1 --> 2 line display (0 = 1 line)
// F = 0 --> 5x8 dot character font
// F = 1 --> 5x10 dot character font
// 0x28 =
// 0  0   0   0   1   0    1  0   0   0
#define LCD_FUNCTION_SET 0x20
#define DATA_LENGTH      0x10
#define TWO_LINES        0x08
#define BIG_FONT_SET     0x04



// Display control flags
// Sets entire display (D) on/off,
// cursor on/off (C), and
// blinking of cursor position
// character (B).
// RS R/W DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0
// 0  0   0   0   0   0   1   D   C   B
// D = 1 --> display on
// C = 0 --> cursor off
// B = 0 --> blinking off
#define LCD_CONTROL 0x08 // display control
#define DISPLAY_ON  0x04
#define DISPLAY_OFF 0x00
#define CURSOR_ON	0x02
#define BLINKING_ON 0x01

// Entry mode set -
// Sets cursor move direction
// and specifies display shift.
// These operations are
// performed during data write
// and read.
// Execution time 37us
// RS R/W DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0
// 0  0   0   0   0   0   0   1   I/D S
// I/D = increment/decrement address pointer
// I/D = 1 --> increment
// S = 0 --> no shift
#define LCD_ENTRY_MODE 0x04
#define ADDR_INCREMENT 0x02
#define LINE_SHIFT		 0x01

// Cursor or display shift -
// Moves the cursor and shifts
// display without chaning
// DDRAM contents.
// Execution time 37us
// RS R/W DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0
// 0  0   0   0   0   1   S/C R/L -   -
// S/C = cursor/display shift
// S/C = 0 --> cursor shift
// R/L = shift direction
// R/L = 1 --> shift to right
#define CURSOR_DISPLAY_SHIFT 0x10
#define CURSOR_SHIFT         0x00
#define DISPLAY_SHIFT        0x08
#define SHIFT_RIGHT          0x04
#define SHIFT_LEFT           0x00


// LCD DDRAM address for the 1st line
// bits DB0..DB6 --> 0x00..0x4F
// DB7 = 1 --> offset = 0x80
// RS R/W DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0
// 0  0   1   ADD ADD ADD ADD ADD ADD ADD
// 0x80 =
// 0  0   1   0   0   0   0   0   0   0
#define LCD_LINE_1 0x80
// LCD DDRAM address for the 2nd line
// 2 lines with equal # of addresses
// DB7 = 1 --> offset = 0x80 + (0x00..0x4F)/2 = 0xC0
#define LCD_LINE_2 0xC0

// Timing constants in us
#define E_DELAY 1         // us
#define E_CMD_PULSE 5000  // us
#define E_DATA_PULSE 5000 // us
#define C_DELAY 1         // us
#define R_EXEC_TIME 50    // us
#define GPIO_RISING 10    // ms

#define MAX_WAIT 100

void init_gpio(void);

void init_lcd(void);

void lcd_function_set(uint8_t control_flags);

void lcd_control(uint8_t control_flags);

void lcd_entry_mode(uint8_t control_flags);

void lcd_shift_control(uint8_t control_flags);

void clear_lcd(void);

void set_cursor(uint8_t addr);

void display_off(void);

void show_cursor(bool on);

void cursor_blinking(bool on);

void shift_cursor(uint8_t direction);

void shift_display(uint8_t direction);

void return_home(void);

void write_to_lcd(uint8_t byte, uint8_t mode);

void write_string(char *str);

uint8_t read_from_lcd(uint8_t mode);

void gpio_reset(void);

#endif