-
Notifications
You must be signed in to change notification settings - Fork 1
/
lcd.c
387 lines (280 loc) · 8.23 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
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/******************************************************************************
eXtreme Electronics xAPI(TM)
----------------------------
xAPI is a Powerful but easy to use C library to program the xBoard(TM)
series of AVR development board. The library has support for commonly use tasks
like:-
*LCD interfacing
*MUXED 7 segment displays.
*Remote Control
*Serial Communication
*DC Motor Controls with Speed Control
*Analog Measurement for Sensor Interface.
*Temperature Measurement.
*I2C Communication.
*EEPROM Interface
*Real Time Clock (RTC Interface)
The APIs are highly documented and easy to use even by a beginner.
For More Info Log On to
www.eXtremeElectronics.co.in
Copyright 2008-2010 eXtreme Electronics India
LCD Core
----------
This module is used for interfacing with Standard Alpha Numeric LCD Modules.
For More information please see supplied tutorials and videos.
NOTICE
--------
NO PART OF THIS WORK CAN BE COPIED, DISTRIBUTED OR PUBLISHED WITHOUT A
WRITTEN PERMISSION FROM EXTREME ELECTRONICS INDIA. THE LIBRARY, NOR ANY PART
OF IT CAN BE USED IN COMMERCIAL APPLICATIONS. IT IS INTENDED TO BE USED FOR
HOBBY, LEARNING AND EDUCATIONAL PURPOSE ONLY. IF YOU WANT TO USE THEM IN
COMMERCIAL APPLICATION PLEASE WRITE TO THE AUTHOR.
WRITTEN BY:
AVINASH GUPTA
me@avinashgupta.com
*******************************************************************************/
#include <avr/io.h>
#include <inttypes.h>
#include <util/delay.h>
#include "lcd.h"
//Custom Charset support
#include "custom_char.h"
#define LCD_DATA_PORT PORT(LCD_DATA)
#define LCD_E_PORT PORT(LCD_E)
#define LCD_RS_PORT PORT(LCD_RS)
#define LCD_RW_PORT PORT(LCD_RW)
#define LCD_DATA_DDR DDR(LCD_DATA)
#define LCD_E_DDR DDR(LCD_E)
#define LCD_RS_DDR DDR(LCD_RS)
#define LCD_RW_DDR DDR(LCD_RW)
#define LCD_DATA_PIN PIN(LCD_DATA)
#define SET_E() (LCD_E_PORT|=(1<<LCD_E_POS))
#define SET_RS() (LCD_RS_PORT|=(1<<LCD_RS_POS))
#define SET_RW() (LCD_RW_PORT|=(1<<LCD_RW_POS))
#define CLEAR_E() (LCD_E_PORT&=(~(1<<LCD_E_POS)))
#define CLEAR_RS() (LCD_RS_PORT&=(~(1<<LCD_RS_POS)))
#define CLEAR_RW() (LCD_RW_PORT&=(~(1<<LCD_RW_POS)))
#ifdef LCD_TYPE_162
#define LCD_TYPE_204
#endif
#ifdef LCD_TYPE_202
#define LCD_TYPE_204
#endif
void LCDByte(uint8_t c,uint8_t isdata)
{
//Sends a byte to the LCD in 4bit mode
//cmd=0 for data
//cmd=1 for command
//NOTE: THIS FUNCTION RETURS ONLY WHEN LCD HAS PROCESSED THE COMMAND
uint8_t hn,ln; //Nibbles
uint8_t temp;
hn=c>>4;
ln=(c & 0x0F);
if(isdata==0)
CLEAR_RS();
else
SET_RS();
_delay_us(0.500); //tAS
SET_E();
//Send high nibble
temp=(LCD_DATA_PORT & (~(0X0F<<LCD_DATA_POS)))|((hn<<LCD_DATA_POS));
LCD_DATA_PORT=temp;
_delay_us(1); //tEH
//Now data lines are stable pull E low for transmission
CLEAR_E();
_delay_us(1);
//Send the lower nibble
SET_E();
temp=(LCD_DATA_PORT & (~(0X0F<<LCD_DATA_POS)))|((ln<<LCD_DATA_POS));
LCD_DATA_PORT=temp;
_delay_us(1); //tEH
//SEND
CLEAR_E();
_delay_us(1); //tEL
LCDBusyLoop();
}
void LCDBusyLoop()
{
//This function waits till lcd is BUSY
uint8_t busy,status=0x00,temp;
//Change Port to input type because we are reading data
LCD_DATA_DDR&=(~(0x0f<<LCD_DATA_POS));
//change LCD mode
SET_RW(); //Read mode
CLEAR_RS(); //Read status
//Let the RW/RS lines stabilize
_delay_us(0.5); //tAS
do
{
SET_E();
//Wait tDA for data to become available
_delay_us(0.5);
status=(LCD_DATA_PIN>>LCD_DATA_POS);
status=status<<4;
_delay_us(0.5);
//Pull E low
CLEAR_E();
_delay_us(1); //tEL
SET_E();
_delay_us(0.5);
temp=(LCD_DATA_PIN>>LCD_DATA_POS);
temp&=0x0F;
status=status|temp;
busy=status & 0b10000000;
_delay_us(0.5);
CLEAR_E();
_delay_us(1); //tEL
}while(busy);
CLEAR_RW(); //write mode
//Change Port to output
LCD_DATA_DDR|=(0x0F<<LCD_DATA_POS);
}
void LCDInit(uint8_t style)
{
/*****************************************************************
This function Initializes the lcd module
must be called before calling lcd related functions
Arguments:
style = LS_BLINK,LS_ULINE(can be "OR"ed for combination)
LS_BLINK :The cursor is blinking type
LS_ULINE :Cursor is "underline" type else "block" type
*****************************************************************/
//After power on Wait for LCD to Initialize
_delay_ms(30);
//Set IO Ports
LCD_DATA_DDR|=(0x0F<<LCD_DATA_POS);
LCD_E_DDR|=(1<<LCD_E_POS);
LCD_RS_DDR|=(1<<LCD_RS_POS);
LCD_RW_DDR|=(1<<LCD_RW_POS);
LCD_DATA_PORT&=(~(0x0F<<LCD_DATA_POS));
CLEAR_E();
CLEAR_RW();
CLEAR_RS();
//Set 4-bit mode
_delay_us(0.3); //tAS
SET_E();
LCD_DATA_PORT|=((0b00000010)<<LCD_DATA_POS); //[B] To transfer 0b00100000 i was using LCD_DATA_PORT|=0b00100000
_delay_us(1);
CLEAR_E();
_delay_us(1);
//Wait for LCD to execute the Functionset Command
LCDBusyLoop(); //[B] Forgot this delay
//Now the LCD is in 4-bit mode
LCDCmd(0b00001100|style); //Display On
LCDCmd(0b00101000); //function set 4-bit,2 line 5x7 dot format
/* Custom Char */
LCDCmd(0b01000000);
uint8_t __i;
for(__i=0;__i<sizeof(__cgram);__i++)
LCDData(__cgram[__i]);
LCDGotoXY(0,0);
}
void LCDWriteString(const char *msg)
{
/*****************************************************************
This function Writes a given string to lcd at the current cursor
location.
Arguments:
msg: a null terminated string to print
Their are 8 custom char in the LCD they can be defined using
"LCD Custom Character Builder" PC Software.
You can print custom character using the % symbol. For example
to print custom char number 0 (which is a degree symbol), you
need to write
LCDWriteString("Temp is 30%0C");
^^
|----> %0 will be replaced by
custom char 0.
So it will be printed like.
Temp is 30°C
In the same way you can insert any syblom numbered 0-7
*****************************************************************/
while(*msg!='\0')
{
//Custom Char Support
if(*msg=='%')
{
msg++;
int8_t cc=*msg-'0';
if(cc>=0 && cc<=7)
{
LCDData(cc);
}
else
{
LCDData('%');
LCDData(*msg);
}
}
else
{
LCDData(*msg);
}
msg++;
}
}
void LCDWriteInt(int val,unsigned int field_length)
{
/***************************************************************
This function writes a integer type value to LCD module
Arguments:
1)int val : Value to print
2)unsigned int field_length :total length of field in which the value is printed
must be between 1-5 if it is -1 the field length is no of digits in the val
****************************************************************/
char str[5]={0,0,0,0,0};
int i=4,j=0;
while(val)
{
str[i]=val%10;
val=val/10;
i--;
}
if(field_length==-1)
while(str[j]==0) j++;
else
j=5-field_length;
if(val<0) LCDData('-');
for(i=j;i<5;i++)
{
LCDData(48+str[i]);
}
}
void LCDGotoXY(uint8_t x,uint8_t y)
{
if(x>=20) return;
#ifdef LCD_TYPE_204
switch(y)
{
case 0:
break;
case 1:
x|=0b01000000;
break;
case 2:
x+=0x14;
break;
case 3:
x+=0x54;
break;
}
#endif
#ifdef LCD_TYPE_164
switch(y)
{
case 0:
break;
case 1:
x|=0b01000000;
break;
case 2:
x+=0x10;
break;
case 3:
x+=0x50;
break;
}
#endif
x|=0b10000000;
LCDCmd(x);
}