-
Notifications
You must be signed in to change notification settings - Fork 9
/
LCD.c
189 lines (163 loc) · 4.52 KB
/
LCD.c
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
/* Driver for HD44780 Compatible LCD Modules
Copyright (C) 2014 Jesus Ruben Santa Anna Zamudio.
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/>.
Author website: http://www.geekfactory.mx
Author e-mail: ruben at geekfactory dot mx
*/
#include "LCD.h"
#define CONFIG_TIMING_MAIN_CLOCK 1000000
// Local variables
const uint8_t rowaddr[4] = {0x00, 0x40, 0x14, 0x54};
uint8_t lcdrows = 2;
uint8_t lcdcolumns = 16;
// Local copy of the Display on off control register
uint8_t dispctrl = 0x00;
uint8_t iomode = 0;
uint8_t lcd_init(void * iodata, uint8_t cols, uint8_t rows)
{
// Initialize IO pins
iomode = lcd_ioinit(iodata);
lcdrows = rows;
lcdcolumns = cols;
// Initial delay after power-up
delay_ms(100);
// Check if LCD interface is 8 or 4 bit mode
if (iomode == 4) {
// Begin LCD controller Initialization (HD44780 page 45-46)
// Put a loop here to send these three?
lcd_iowrite4(0x03);
delay_ms(5);
lcd_iowrite4(0x03);
delay_us(120);
lcd_iowrite4(0x03);
delay_us(120);
lcd_iowrite4(0x02);
delay_us(120);
lcd_command(E_FUNCTION_SET | BIT_DL_DATALENGTH_4 | BIT_N_DISP_LINES_2 | BIT_F_FONT_5_10);
delay_us(50);
} else if (iomode == 8) {
// Begin LCD controller Initialization (HD44780 page 45-46)
lcd_command(E_FUNCTION_SET);
delay_ms(5);
lcd_command(E_FUNCTION_SET);
delay_us(120);
lcd_command(E_FUNCTION_SET);
delay_us(120);
lcd_command(E_FUNCTION_SET | BIT_DL_DATALENGTH_8 | BIT_N_DISP_LINES_2 | BIT_F_FONT_5_10);
delay_us(50);
}
// Configure display after power up
lcd_command(E_DISPLAY_ON_OFF_CTRL | BIT_D_DISPLAY_OFF);
delay_us(50);
lcd_command(E_CLEAR_DISPLAY);
delay_ms(2);
lcd_command(E_ENTRY_MODE_SET | BIT_S_AUTOSCROLL_OFF | BIT_ID_INCREMENT_CURSOR);
delay_us(50);
return TRUE;
}
void lcd_clear()
{
lcd_command(E_CLEAR_DISPLAY);
delay_ms(2);
}
void lcd_home()
{
lcd_command(E_RETURN_HOME);
delay_ms(2);
}
void lcd_on()
{
dispctrl |= BIT_D_DISPLAY_ON;
lcd_command(E_DISPLAY_ON_OFF_CTRL | dispctrl);
delay_us(50);
}
void lcd_off()
{
dispctrl &= ~BIT_D_DISPLAY_ON;
lcd_command(E_DISPLAY_ON_OFF_CTRL | dispctrl);
delay_us(50);
}
void lcd_cursor(enum enLCDCursorModes mode)
{
dispctrl &= 0xFC;
dispctrl |= mode;
lcd_command(E_DISPLAY_ON_OFF_CTRL | dispctrl);
delay_us(50);
}
void lcd_cursor_left()
{
lcd_command(E_CURSOR_DISPLAY_SHIFT | BIT_SC_SHIFT_CURSOR | BIT_RL_SHIFT_LEFT);
delay_us(50);
}
void lcd_cursor_right()
{
lcd_command(E_CURSOR_DISPLAY_SHIFT | BIT_SC_SHIFT_CURSOR | BIT_RL_SHIFT_RIGHT);
delay_us(50);
}
void lcd_scroll_left()
{
lcd_command(E_CURSOR_DISPLAY_SHIFT | BIT_SC_SHIFT_DISPLAY | BIT_RL_SHIFT_LEFT);
delay_us(50);
}
void lcd_scroll_right()
{
lcd_command(E_CURSOR_DISPLAY_SHIFT | BIT_SC_SHIFT_DISPLAY | BIT_RL_SHIFT_RIGHT);
delay_us(50);
}
void lcd_autoscroll_on()
{
lcd_command(E_ENTRY_MODE_SET | BIT_S_AUTOSCROLL_ON | BIT_ID_INCREMENT_CURSOR);
delay_us(50);
}
void lcd_autoscroll_off()
{
lcd_command(E_ENTRY_MODE_SET | BIT_S_AUTOSCROLL_OFF | BIT_ID_INCREMENT_CURSOR);
delay_us(50);
}
void lcd_goto(uint8_t col, uint8_t row)
{
// Apply limits for Rows and Columns
if (row >= lcdrows)
row = lcdrows - 1;
if (col >= lcdcolumns)
col = lcdcolumns - 1;
lcd_command(E_SET_DDRAM_ADDR | (col + rowaddr[ row ]));
}
void lcd_send(uint8_t data, uint8_t rs)
{
// Write logic one to send characters or logic 0 to send a command
if (rs)
lcd_iohigh(E_RS_PIN);
else
lcd_iolow(E_RS_PIN);
// Clear the RW pin if used
lcd_iolow(E_RW_PIN);
if (iomode == 4) {
lcd_iowrite4(data >> 4);
lcd_iowrite4(data);
} else {
lcd_iowrite8(data);
}
}
void lcd_puts(const char * string)
{
while (*string != '\0')
lcd_write(*string++);
}
void lcd_create_char(uint8_t charnum, const uint8_t * chardata)
{
uint8_t i;
charnum &= 0x07;
lcd_command(E_SET_CGRAM_ADDR | (charnum << 3));
for (i = 0; i < 8; i++)
lcd_write(chardata[i]);
}