-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlcd.c
175 lines (130 loc) · 2.87 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
/*
* lcd.c
*
* Created: 2011-11-29 17:01:14
* Author: pn264889
*/
#include "lcd.h"
#include <avr/delay.h>
#include "commons.h"
void lcdSend4Bits(uint8_t data)
{
SBI(LCD_E_PORT, LCD_OE);
SBI(LCD_DATA_PORT, LCD_D4);
SBI(LCD_DATA_PORT, LCD_D5);
SBI(LCD_DATA_PORT, LCD_D6);
SBI(LCD_DATA_PORT, LCD_D7);
if (!CHECK(data, 4))
CBI(LCD_DATA_PORT, LCD_D4);
if (!CHECK(data, 5))
CBI(LCD_DATA_PORT, LCD_D5);
if (!CHECK(data, 6))
CBI(LCD_DATA_PORT, LCD_D6);
if (!CHECK(data, 7))
CBI(LCD_DATA_PORT, LCD_D7);
_delay_loop_1(1); // 3+ cycles
CBI(LCD_E_PORT, LCD_OE);
}
void lcdSend4BitsMode(uint8_t data)
{
lcdSend4Bits(data);
SWAP(data);
lcdSend4Bits(data);
_delay_us(40);
}
void lcdInit()
{
// based on http://www.mimuw.edu.pl/~marpe/mikrokontrolery/w5_klawisze_lcd.pdf
// slide no. 26
// set input/output direction
SBI(LCD_DATA_DDR, LCD_D4);
SBI(LCD_DATA_DDR, LCD_D5);
SBI(LCD_DATA_DDR, LCD_D6);
SBI(LCD_DATA_DDR, LCD_D7);
SBI(LCD_E_DDR, LCD_OE);
SBI(LCD_RS_DDR, LCD_RS);
CBI(LCD_RS_PORT, LCD_RS);
CBI(LCD_E_PORT, LCD_OE);
_delay_ms(40);
SBI(LCD_E_PORT, LCD_OE);
SBI(LCD_DATA_PORT, LCD_D4);
SBI(LCD_DATA_PORT, LCD_D5);
CBI(LCD_DATA_PORT, LCD_D6);
CBI(LCD_DATA_PORT, LCD_D7);
_delay_loop_1(1); // 3+ cycles
CBI(LCD_E_PORT, LCD_OE);
_delay_ms(4.1);
SBI(LCD_E_PORT, LCD_OE);
_delay_loop_1(1); // 3+ cycles
CBI(LCD_E_PORT, LCD_OE);
_delay_us(100);
SBI(LCD_E_PORT, LCD_OE);
_delay_loop_1(1); // 3+ cycles
CBI(LCD_E_PORT, LCD_OE);
_delay_us(100);
SBI(LCD_E_PORT, LCD_OE);
CBI(LCD_DATA_PORT, LCD_D4);
_delay_loop_1(1); // 3+ cycles
CBI(LCD_E_PORT, LCD_OE);
_delay_us(100);
lcdInstr(0x28); //0b00101000
lcdInstr(0x06); //0b00000110
lcdInstr(0x0f); //0b00001111
lcdInstr(0x01); //0b00000001
_delay_ms(2);
}
//move cursor to pos position (7 bit)
void lcdGoTo(uint8_t pos)
{
lcdInstr(pos | (1<<7));
}
void lcdClear(void)
{
lcdInstr(0x01); //0b00000001
_delay_ms(3);
}
//send 8bit data to the LCD using R16 as argument
void lcdData(uint8_t data)
{
SBI(LCD_RS_PORT, LCD_RS);
lcdSend4BitsMode(data);
}
void lcdString(char* str)
{
while (*str != 0)
{
lcdData(*str);
++str;
}
}
void lcdInt(int16_t i)
{
if (i < 0)
{
lcdString("-");
i = -i;
}
lcdUint(i);
}
void lcdUint(uint16_t i)
{
int8_t log2 = 0;
int16_t tmp = i;
while (tmp > 15)
{
tmp >>= 4;
log2 += 4;
}
while (log2 >= 0)
{
int16_t div = i >> log2;
lcdData(div + (div > 9 ? 'A'- 10: '0'));
i -= div << log2;
log2 -= 4;
}
}
void lcdInstr(uint8_t instr)
{
CBI(LCD_RS_PORT, LCD_RS);
lcdSend4BitsMode(instr);
}