forked from aostanin/avr-hd44780
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lcd.c
177 lines (136 loc) · 4.15 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
#include "lcd.h"
#include <stdarg.h>
#include <stdio.h>
#include <util/delay.h>
void lcd_send(uint8_t value, uint8_t mode);
void lcd_write_nibble(uint8_t nibble);
static uint8_t lcd_displayparams;
static char lcd_buffer[LCD_COL_COUNT + 1];
void lcd_command(uint8_t command) {
lcd_send(command, 0);
}
void lcd_write(uint8_t value) {
lcd_send(value, 1);
}
void lcd_send(uint8_t value, uint8_t mode) {
if (mode) {
LCD_PORT = LCD_PORT | (1 << LCD_RS);
} else {
LCD_PORT = LCD_PORT & ~(1 << LCD_RS);
}
LCD_PORT = LCD_PORT & ~(1 << LCD_RW);
lcd_write_nibble(value >> 4);
lcd_write_nibble(value);
}
void lcd_write_nibble(uint8_t nibble) {
LCD_PORT = (LCD_PORT & 0xff & ~(0x0f << LCD_D0)) | ((nibble & 0x0f) << LCD_D0);
LCD_PORT = LCD_PORT & ~(1 << LCD_EN);
LCD_PORT = LCD_PORT | (1 << LCD_EN);
LCD_PORT = LCD_PORT & ~(1 << LCD_EN);
_delay_ms(0.3); // If delay less than this value, the data is not correctly displayed
}
void lcd_init(void) {
// Configure pins as output
LCD_DDR = LCD_DDR
| (1 << LCD_RS)
| (1 << LCD_RW)
| (1 << LCD_EN)
| (1 << LCD_D0)
| (1 << LCD_D1)
| (1 << LCD_D2)
| (1 << LCD_D3);
// Wait for LCD to become ready (docs say 15ms+)
_delay_ms(15);
LCD_PORT = LCD_PORT
& ~(1 << LCD_EN)
& ~(1 << LCD_RS)
& ~(1 << LCD_RW);
_delay_ms(4.1);
lcd_write_nibble(0x03); // Switch to 4 bit mode
_delay_ms(4.1);
lcd_write_nibble(0x03); // 2nd time
_delay_ms(4.1);
lcd_write_nibble(0x03); // 3rd time
_delay_ms(4.1);
lcd_write_nibble(0x02); // Set 8-bit mode (?)
lcd_command(LCD_FUNCTIONSET | LCD_4BITMODE | LCD_2LINE | LCD_5x8DOTS);
lcd_displayparams = LCD_CURSOROFF | LCD_BLINKOFF;
lcd_command(LCD_DISPLAYCONTROL | lcd_displayparams);
}
void lcd_on(void) {
lcd_displayparams |= LCD_DISPLAYON;
lcd_command(LCD_DISPLAYCONTROL | lcd_displayparams);
}
void lcd_off(void) {
lcd_displayparams &= ~LCD_DISPLAYON;
lcd_command(LCD_DISPLAYCONTROL | lcd_displayparams);
}
void lcd_clear(void) {
lcd_command(LCD_CLEARDISPLAY);
_delay_ms(2);
}
void lcd_return_home(void) {
lcd_command(LCD_RETURNHOME);
_delay_ms(2);
}
void lcd_enable_blinking(void) {
lcd_displayparams |= LCD_BLINKON;
lcd_command(LCD_DISPLAYCONTROL | lcd_displayparams);
}
void lcd_disable_blinking(void) {
lcd_displayparams &= ~LCD_BLINKON;
lcd_command(LCD_DISPLAYCONTROL | lcd_displayparams);
}
void lcd_enable_cursor(void) {
lcd_displayparams |= LCD_CURSORON;
lcd_command(LCD_DISPLAYCONTROL | lcd_displayparams);
}
void lcd_disable_cursor(void) {
lcd_displayparams &= ~LCD_CURSORON;
lcd_command(LCD_DISPLAYCONTROL | lcd_displayparams);
}
void lcd_scroll_left(void) {
lcd_command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVELEFT);
}
void lcd_scroll_right(void) {
lcd_command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVERIGHT);
}
void lcd_set_left_to_right(void) {
lcd_displayparams |= LCD_ENTRYLEFT;
lcd_command(LCD_ENTRYMODESET | lcd_displayparams);
}
void lcd_set_right_to_left(void) {
lcd_displayparams &= ~LCD_ENTRYLEFT;
lcd_command(LCD_ENTRYMODESET | lcd_displayparams);
}
void lcd_enable_autoscroll(void) {
lcd_displayparams |= LCD_ENTRYSHIFTINCREMENT;
lcd_command(LCD_ENTRYMODESET | lcd_displayparams);
}
void lcd_disable_autoscroll(void) {
lcd_displayparams &= ~LCD_ENTRYSHIFTINCREMENT;
lcd_command(LCD_ENTRYMODESET | lcd_displayparams);
}
void lcd_create_char(uint8_t location, uint8_t *charmap) {
lcd_command(LCD_SETCGRAMADDR | ((location & 0x7) << 3));
for (int i = 0; i < 8; i++) {
lcd_write(charmap[i]);
}
lcd_command(LCD_SETDDRAMADDR);
}
void lcd_set_cursor(uint8_t col, uint8_t row) {
static uint8_t offsets[] = { 0x00, 0x40, 0x14, 0x54 };
lcd_command(LCD_SETDDRAMADDR | (col + offsets[row]));
}
void lcd_puts(char *string) {
for (char *it = string; *it; it++) {
lcd_write(*it);
}
}
void lcd_printf(char *format, ...) {
va_list args;
va_start(args, format);
vsnprintf(lcd_buffer, LCD_COL_COUNT + 1, format, args);
va_end(args);
lcd_puts(lcd_buffer);
}