-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlcd.h
245 lines (208 loc) · 4.46 KB
/
lcd.h
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#define LCD_DATA (*(volatile unsigned char *)0x8000)
#define LCD_CONTROL (*(volatile unsigned char *)0x8001)
static char lcd_con;
#define LCD_ENABLE_ON (LCD_CONTROL = (lcd_con|=0x04))
#define LCD_ENABLE_OFF (LCD_CONTROL = (lcd_con&=0x0b))
#define LCD_R_W_ON (LCD_CONTROL = (lcd_con|=0x02))
#define LCD_R_W_OFF (LCD_CONTROL = (lcd_con&=0x0d))
#define LCD_RS_ON (LCD_CONTROL = (lcd_con|=0x01))
#define LCD_RS_OFF (LCD_CONTROL = (lcd_con&=0x0e))
//////////////////////////////////////////// interface //////////////////////////////////////////////
// 주의: 딜레이 넉넉히 넣어 주지 않으면 맛이 간다
// 실질적으로 쓰는 함수들 나열함
void lcdInit(void);
void lcdClear(void);
void lcd_gotoxy(unsigned char x, unsigned char y);
void lcd_putn1(unsigned int number);
void lcd_putn2(unsigned int number);
void lcd_putn3(unsigned int number);
void lcd_putn4(unsigned int number);
void lcd_putn5(unsigned int number);
void lcd_putn6(unsigned int number);
//////////////////////////////////////////// --------- //////////////////////////////////////////////
/* private function */
void lcdDelay(unsigned int d);
void lcdClear(void);
void lcdRegWrite(unsigned char reg);
void lcdDelayLong(unsigned int d);
/* public function. interface */
void lcdInit(void);
void lcdClear(void);
void lcd_putch(unsigned char reg);
void putcharInt(unsigned char reg);
void putcharHex(unsigned char reg);
void putString(char str[]);
void secondRow(void);
void lcd_gotoxy(unsigned char x, unsigned char y);
void lcd_puts(char lcd_l, char *s) {
lcd_gotoxy(1,lcd_l);
for(;*s;){
lcd_putch(*s);
s++;
}
}
void lcd_putss(char *s) {
for(;*s;){
lcd_putch(*s);
s++;
}
}
void secondRow(void) {
unsigned char i;
for(i=0; i<24; i++){
lcd_putch(0);
lcdDelay(30);
}
}
void putString(char str[]) {
unsigned char i=0;
while(str[i]){
if(i==16){
secondRow();
}
lcd_putch(str[i]);
lcdDelay(30);
i++;
}
}
//-사용자- 커서 위치 변경(x,y)
// ---> X
// |
// |
// v Y
void lcd_gotoxy(unsigned char x, unsigned char y) {
switch(y) {
case 1:
lcdRegWrite(0x80+x-1);
lcdDelay(30);
break;
case 2 :
lcdRegWrite(0xc0+x-1);
lcdDelay(30);
break;
}
}
void lcdInit(void) {
unsigned char i, lcd_reg[6]={0x38, 0x0c, 0x06};
LCD_ENABLE_OFF;
LCD_R_W_ON;
LCD_RS_ON;
lcdDelay(200);
for(i=0; i<3; i++) {
lcdRegWrite(lcd_reg[i]);
//lcdDelay(200);
}
lcdClear();
}
void lcdClear(void) {
lcdRegWrite(0x01);
lcdDelay(500);
}
void putcharInt(unsigned char reg) {
unsigned char temp;
if(reg>99){
temp = reg / 100;
lcd_putch( temp + 0x30 );
reg = reg - temp*100;
temp = reg / 10;
lcd_putch( temp + 0x30 );
reg = reg - temp*10;
lcd_putch( reg + 0x30 );
}else if(reg>9){
lcd_putch(' ');
temp = reg / 10;
lcd_putch( temp + 0x30 );
reg = reg - temp*10;
lcd_putch( reg + 0x30 );
}else{
lcd_putch(' ');
lcd_putch(' ');
lcd_putch( reg + 0x30 );
}
}
void putcharHex(unsigned char reg) {
unsigned char temp;
temp = reg;
temp>>=4;
if(temp<10){
lcd_putch(temp + 0x30);
}else{
lcd_putch(temp + 0x57);
}
temp = reg & 0x0f;
if(temp<10){
lcd_putch(temp + 0x30);
}else{
lcd_putch(temp + 0x57);
}
}
void lcd_putch(unsigned char reg) {
LCD_R_W_OFF;
LCD_RS_ON;
lcdDelay(5);
LCD_ENABLE_ON;
LCD_DATA = reg;
lcdDelay(10);
LCD_ENABLE_OFF;
lcdDelay(5);
}
void lcd_putn1(unsigned int number) {
number%=10;
lcd_putch(number+'0');
}
void lcd_putn2(unsigned int number) {
number%=100;
lcd_putch(number/10+'0');
number%=10;
lcd_putch(number+'0');
}
void lcd_puth2(unsigned int number) {
number%=100;
lcd_putch((number/16)+'0');
// number%=10;
// lcd_putch(hex_char[number&0x0f]);
lcd_putch((number&0x0f)+'0');
}
//-사용자- 3자리 숫자 출력 예)012
void lcd_putn3(unsigned int number) {
number%=1000;
lcd_putch(number/100+'0');
number%=100;
lcd_putch(number/10+'0');
number%=10;
lcd_putch(number+'0');
}
void lcd_putn4(unsigned int number) {
lcd_putn1(number/1000);
lcd_putn3(number);
}
void lcd_putn5(unsigned int number) {
lcd_putn2(number/1000);
lcd_putn3(number);
}
//-사용자- 6자리 숫자 출력 예)001234
void lcd_putn6(unsigned int number) {
lcd_putn3(number/1000);
lcd_putn3(number);
}
void lcdRegWrite(unsigned char reg) {
LCD_R_W_OFF;
LCD_RS_OFF;
lcdDelay(5);
LCD_ENABLE_ON;
LCD_DATA = reg;
lcdDelay(10);
LCD_ENABLE_OFF;
lcdDelay(5);
}
void lcdDelay(unsigned int d) {
volatile int di;
while(d--)
for(di=0; di<2; di++);
}
void lcdDelayLong(unsigned int d) {
unsigned char i, j, k;
for(i=0; i<80; i++)
for(j=0; j<50; j++)
for(k=0; k<d; k++);
}