-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLCD_4bit.c
308 lines (235 loc) · 11.2 KB
/
LCD_4bit.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
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/******************************************************************************/
/* LCD.c: Functions for 2 line 16 character Text LCD, with 4-bit interface */
/******************************************************************************/
/* This file is part of the uVision/ARM development tools. */
/* Copyright (c) 2005-2006 Keil Software. All rights reserved. */
/* This software may only be used under the terms of a valid, current, */
/* end user licence from KEIL for a compatible version of KEIL software */
/* development tools. Nothing else gives you the right to use this software. */
/******************************************************************************/
#include <LPC23xx.H>
#include <stdlib.h> /* LPC23xx definitions */
/*********************** Hardware specific configuration **********************/
/*------------------------- Speed dependant settings -------------------------*/
/* If processor works on high frequency delay has to be increased, it can be
increased by factor 2^N by this constant */
#define DELAY_2N 0
/*------------------------- Text LCD size definitions ------------------------*/
#define LineLen 16 /* Width (in characters) */
#define NumLines 2 /* Hight (in lines) */
/*-------------------- LCD interface hardware definitions --------------------*/
/* PINS:
- DB4 = P1.24
- DB5 = P1.25
- DB6 = P1.26
- DB7 = P1.27
- E = P1.31 (for V1 P1.30)
- RW = P1.29
- RS = P1.28 */
#define MCB2300_V1 /* First version of MCB2300 */
#define PIN_E 0x80000000
#define PIN_RW 0x20000000
#define PIN_RS 0x10000000
#define PINS_CTRL 0xB0000000
#define PINS_DATA 0x0F000000
#ifdef MCB2300_V1
#undef PIN_E
#define PIN_E 0xC0000000
#undef PINS_CTRL
#define PINS_CTRL 0xF0000000
#endif
/* pin E setting to 0 or 1 */
#define LCD_E(x) ((x) ? (IOSET1 = PIN_E) : (IOCLR1 = PIN_E) );
/* pin RW setting to 0 or 1 */
#define LCD_RW(x) ((x) ? (IOSET1 = PIN_RW) : (IOCLR1 = PIN_RW));
/* pin RS setting to 0 or 1 */
#define LCD_RS(x) ((x) ? (IOSET1 = PIN_RS) : (IOCLR1 = PIN_RS));
/* Reading DATA pins */
#define LCD_DATA_IN ((IOPIN1 >> 24) & 0xF)
/* Writing value to DATA pins */
#define LCD_DATA_OUT(x) IOCLR1 = PINS_DATA; IOSET1 = (x & 0xF) << 24;
/* Setting all pins to output mode */
#define LCD_ALL_DIR_OUT IODIR1 |= PINS_CTRL | PINS_DATA;
/* Setting DATA pins to input mode */
#define LCD_DATA_DIR_IN IODIR1 &= ~PINS_DATA;
/* Setting DATA pins to output mode */
#define LCD_DATA_DIR_OUT IODIR1 |= PINS_DATA;
/******************************************************************************/
/* 8 user defined characters to be loaded into CGRAM (used for bargraph) */
const unsigned char UserFont[8][8] = {
{ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
{ 0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10 },
{ 0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18 },
{ 0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C },
{ 0x1E,0x1E,0x1E,0x1E,0x1E,0x1E,0x1E,0x1E },
{ 0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F },
{ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
{ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }
};
/************************ Global function definitions *************************/
/*******************************************************************************
* Delay in while loop cycles *
* Parameter: cnt: number of while cycles to delay *
* Return: *
*******************************************************************************/
static void delay (int cnt)
{
cnt <<= DELAY_2N;
while (cnt--);
}
/*******************************************************************************
* Read status of LCD controller *
* Parameter: none *
* Return: Status byte contains busy flag and address pointer *
*******************************************************************************/
static unsigned char lcd_read_status (void)
{
unsigned char status;
LCD_DATA_DIR_IN
LCD_RS(0)
LCD_RW(1)
delay(10);
LCD_E(1)
delay(10);
status = LCD_DATA_IN << 4;
LCD_E(0)
delay(10);
LCD_E(1)
delay(10);
status |= LCD_DATA_IN;
LCD_E(0)
LCD_DATA_DIR_OUT
return (status);
}
/*******************************************************************************
* Wait until LCD controller busy flag is 0 *
* Parameter: *
* Return: Status byte of LCD controller (busy + address) *
*******************************************************************************/
static unsigned char wait_while_busy (void)
{
unsigned char status;
do {
status = lcd_read_status();
} while (status & 0x80); /* Wait for busy flag */
return (status);
}
/*******************************************************************************
* Write 4-bits to LCD controller *
* Parameter: c: command to be written *
* Return: *
*******************************************************************************/
void lcd_write_4bit (unsigned char c)
{
LCD_RW(0)
LCD_E(1)
LCD_DATA_OUT(c&0x0F)
delay(10);
LCD_E(0)
delay(10);
}
/*******************************************************************************
* Write command to LCD controller *
* Parameter: c: command to be written *
* Return: *
*******************************************************************************/
void lcd_write_cmd (unsigned char c)
{
wait_while_busy();
LCD_RS(0)
lcd_write_4bit (c>>4);
lcd_write_4bit (c);
}
/*******************************************************************************
* Write data to LCD controller *
* Parameter: c: data to be written *
* Return: *
*******************************************************************************/
static void lcd_write_data (unsigned char c)
{
wait_while_busy();
LCD_RS(1)
lcd_write_4bit (c>>4);
lcd_write_4bit (c);
}
/*******************************************************************************
* Print Character to current cursor position *
* Parameter: c: character to be printed *
* Return: *
*******************************************************************************/
void lcd_putchar (char c)
{
lcd_write_data (c);
}
/*******************************************************************************
* Initialize the LCD controller *
* Parameter: *
* Return: *
*******************************************************************************/
void lcd_init (void)
{
int i;
unsigned char const *p;
/* Set all pins for LCD as outputs */
LCD_ALL_DIR_OUT
delay (15000);
LCD_RS(0)
lcd_write_4bit (0x3); /* Select 4-bit interface */
delay (4100);
lcd_write_4bit (0x3);
delay (100);
lcd_write_4bit (0x3);
lcd_write_4bit (0x2);
lcd_write_cmd (0x28); /* 2 lines, 5x8 character matrix */
lcd_write_cmd (0x0C); /* Display ctrl:Disp=ON,Curs/Blnk=OFF */
lcd_write_cmd (0x06); /* Entry mode: Move right, no shift */
/* Load user-specific characters into CGRAM */
lcd_write_cmd(0x40); /* Set CGRAM address counter to 0 */
p = &UserFont[0][0];
for (i = 0; i < sizeof(UserFont); i++, p++)
lcd_putchar (*p);
lcd_write_cmd(0x80); /* Set DDRAM address counter to 0 */
}
/*******************************************************************************
* Set cursor position on LCD display *
* Parameter: column: column position *
* line: line position *
* Return: *
*******************************************************************************/
void set_cursor (unsigned char column, unsigned char line)
{
unsigned char address;
address = (line * 40) + column;
address = 0x80 + (address & 0x7F);
lcd_write_cmd(address); /* Set DDRAM address counter to 0 */
}
/*******************************************************************************
* Clear the LCD display *
* Parameter: *
* Return: *
*******************************************************************************/
void lcd_clear (void)
{
lcd_write_cmd(0x01); /* Display clear */
set_cursor (0, 0);
}
/*******************************************************************************
* Print sting to LCD display *
* Parameter: string: pointer to output string *
* Return: *
*******************************************************************************/
void lcd_print (char *string)
{
while (*string) {
lcd_putchar (*string++);
}
}
/******************************************************************************/
/*void lcd_string (char data[34])
{
int i =0;
while (data[i]) {
lcd_putchar (data[i]);
i++;
}
}*/