-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathlcd.c
176 lines (142 loc) · 3.1 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
#include <avr/io.h>
#include <stdio.h>
#include <avr/eeprom.h>
#include <avr/interrupt.h>
#include <avr/eeprom.h>
#include "23nbfm.h"
#include <util/delay.h>
// internal function prototypes
void lcdCmduN(char c); // wm
void lcdCmd(char c);
void lcdData(char c);
void lcdInit()
{
#ifdef LCD_20x4 // wm
_delay_ms(20); // min. 15ms
lcdCmduN(0x30);
_delay_ms(10); // min. 4.1ms
lcdCmduN(0x30);
_delay_ms(1); // > 100us
lcdCmduN(0x30);
_delay_ms(1);
lcdCmduN(0x20);
_delay_ms(1);
lcdCmd(0x28); // NF00, N=1 2 Lines, F=0 5x8 Dots
_delay_ms(1);
lcdCmd(0x08); // Function set, interface 4 bits long
_delay_ms(1);
lcdCmd(0x01); // Clear Display
_delay_ms(1);
lcdCmd(0x06); // Entry mode set
_delay_ms(1);
lcdCmd(0x0C); // 0000 1DCB, D=1 Display on, C=0 no Cursor, B=0 no blinking
_delay_ms(1);
#else
// 4 bits mode, 2 lines
lcdCmd(0x20);
_delay_ms(10);
lcdCmd(0x20);
_delay_ms(10);
// cursor shift right, text no shift
lcdCmd(0x18);
_delay_ms(20);
// display on, no cursor, no blink
lcdCmd(0x0c);
_delay_ms(20);
// shift mode
lcdCmd(0x06);
_delay_ms(20);
// home
lcdCmd(0x02);
_delay_ms(20);
// clear display
lcdCmd(0x01);
_delay_ms(20);
#endif
}
void lcdCmduN(char c) // wm use only for LcdInit()
{
char t;
t = c & 0xf0;
PORTB = t;
sbi(PORTB, LCD_E);
_delay_us(2);
cbi(PORTB, LCD_E);
_delay_us(200);
}
void lcdCmd(char c)
{
char t;
t = PINB & 0x0c; // wm PB2 and PB3
t |= c & 0xf0; // wm don't clear PB2 and PB3!
PORTB = t;
sbi(PORTB, LCD_E);
_delay_us(2);
cbi(PORTB, LCD_E);
_delay_us(200);
c <<= 4;
c &= 0xf0;
c |= PINB & 0x0c; // wm don't clear PB2 and PB3!
PORTB = c;
sbi(PORTB, LCD_E);
_delay_us(2);
cbi(PORTB, LCD_E);
_delay_us(200);
}
void lcdData(char c)
{
char t;
t = PINB & 0x0c; // wm PB2 and PB3
t |= c & 0xf0; // wm don't clear PB2 and PB3!
t |= (1<<LCD_RS);
PORTB = t;
sbi(PORTB, LCD_E);
_delay_us(2);
cbi(PORTB, LCD_E);
_delay_us(200);
c <<= 4;
c &= 0xf0;
c |= PINB & 0x0c; // wm don't clear PB2 and PB3!
c |= (1<<LCD_RS);
PORTB = c;
sbi(PORTB, LCD_E);
_delay_us(2);
cbi(PORTB, LCD_E);
_delay_us(200);
}
void lcdCursor(int col, int row)
#ifdef LCD_20x4 // wm
{
int row_offsets[] = {0x00, 0x40, 0x14, 0x54}; // 00, 64, 20, 84
if ( row >= 4 ) {
row = 4-1; // we count rows starting with 0
}
lcdCmd(0x80 | (col + row_offsets[row])); // SETDDRAMADDR 0x80
_delay_us(200);
}
#else
{
if (row==0) lcdCmd(0x80 + col);
else lcdCmd(0xc0 + col);
_delay_us(200);
}
#endif
void lcdClear()
{
// clear display
lcdCmd(0x01);
_delay_ms(20);
#ifdef LCD_20x4 // wm
lcdCmd(0x06); // Entry mode set
_delay_ms(1);
#endif
}
void lcdChar(char c)
{
lcdData(c);
}
void lcdStr(char *s)
{
while (*s)
lcdData(*s++);
}