summaryrefslogtreecommitdiffstats
path: root/uc_str912/prj_test_lcd/src/lcd_lib_91x.c
blob: 7c01f532816a56eba49ab4e40cc0aceee2ed5624 (plain)
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
/******************** (C) COPYRIGHT 2007 PROPOX ********************************
* File Name          : lcd_lib_91x.c
* Author             : PROPOX Team
* Date First Issued  : 09/24/2007 : Version 1.0
* Description        : This file provides all the 2x16 LCD functions.
********************************************************************************
* History:
********************************************************************************
* THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
* AS A RESULT, PROPOX SHALL NOT BE HELD LIABLE FOR ANY DIRECT, INDIRECT OR
* CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
* CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*******************************************************************************/

/* Includes ------------------------------------------------------------------*/
#include "91x_lib.h"
#include "91x_gpio.h"
#include "91x_map.h"
#include "91x_scu.h"
#include "lcd_lib_91x.h"


/*******************************************************************************
* Function Name  : delay
* Description    : Delay (opoznienie)
*                  
* Input          : time - number of cycles
* Output         : None
* Return         : None
*******************************************************************************/
void delay(int time)
{
  while(time--) 
	asm volatile ("nop");
}

/*******************************************************************************
* Function Name  : LCDinit
* Description    : Inicialize LCD (Inicjalizacja LCD)
*                  (4 bit data, 2 lines, characters 5x7, blinking cursor on)
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void LCDinit(void)
{
  GPIO_Write(GPIO8, 0x00);
  delay(375000); //15ms
  //--------- Write 0x03 ----------- 
  D7_reset;
  D6_reset;
  D5_set;
  D4_set;
  E_set;
  delay(25000); //1ms
  E_reset;
  delay(125000); //5ms
  //--------- Write 0x03 -----------
  D7_reset;
  D6_reset;
  D5_set;
  D4_set;
  E_set;
  delay(25000); //1ms
  E_reset;
  delay(25000); //1ms
  //--------- Write 0x03 -----------
  D7_reset;
  D6_reset;
  D5_set;
  D4_set;
  E_set;
  delay(25000); //1ms
  E_reset;
  delay(25000); //1ms
  //--------- Enable Four Bit Mode ----------
  D7_reset;
  D6_reset;
  D5_set;
  D4_reset;
  E_set;
  delay(25000); //1ms
  E_reset;
  delay(25000); //1ms
  //---------- Set Interface Length ----------
  //Write 0x2 - 4 bits
  D7_reset;
  D6_reset;
  D5_set;
  D4_reset;
  E_set;
  delay(25000); //1ms
  E_reset;
  delay(25000); //1ms
  //Write 0x8 - 2 lines, 5x7
  D7_set;
  D6_reset;
  D5_reset;
  D4_reset;
  E_set;
  delay(25000); //1ms
  E_reset;
  delay(25000); //1ms
  //---------- Turn off the Display ----------
  //Write 0x0
  D7_reset;
  D6_reset;
  D5_reset;
  D4_reset;
  E_set;
  delay(25000); //1ms
  E_reset;
  delay(25000); //1ms
  //Write 0x8
  D7_set;
  D6_reset;
  D5_reset;
  D4_reset;
  E_set;
  delay(25000); //1ms
  E_reset;
  delay(25000); //1ms
  //------------ Clear the Display ----------- 
  //Write 0x0
  D7_reset;
  D6_reset;
  D5_reset;
  D4_reset;
  E_set;
  delay(25000); //1ms
  E_reset;
  delay(25000); //1ms
  //Write 0x1
  D7_reset;
  D6_reset;
  D5_reset;
  D4_set;
  E_set;
  delay(25000); //1ms
  E_reset;
  delay(25000); //1ms
  //-------- Set Cursor Move Direction -------- 
  //Write 0x0
  D7_reset;
  D6_reset;
  D5_reset;
  D4_reset;
  E_set;
  delay(25000); //1ms
  E_reset;
  delay(25000); //1ms
  //Write 0x6 - Increment the Cursor
  D7_reset;
  D6_set;
  D5_set;
  D4_reset;
  E_set;
  delay(25000); //1ms
  E_reset;
  delay(25000); //1ms
  //---------- Enable Display/Cursor ----------
  //Write 0x0
  D7_reset;
  D6_reset;
  D5_reset;
  D4_reset;
  E_set;
  delay(25000); //1ms
  E_reset;
  delay(25000); //1ms
  //Write 0xF - Display on, cursor on, blink on 
  D7_set;
  D6_set;
  D5_set;
  D4_set;
  E_set;
  delay(25000); //1ms
  E_reset;
  delay(25000); //1ms
}

/*******************************************************************************
* Function Name  : LCDsendChar
* Description    : Send Char to LCD (Wyslanie znaku na LCD)
*    
* Input          : ch - is a ascii code of character or char from char_code.h
* Output         : None
* Return         : None
*******************************************************************************/
void LCDsendChar(u8 ch)		
{ 
  //4 MSB bits
  //4 starsze bity
  GPIO_Write(GPIO8, (ch>>4) & 0x0f);
  RS_set;
  E_set; 
  delay(25000);
  E_reset;	
  delay(25000);
  //4 LSB bits
  //4 mlodsze bity
  GPIO_Write(GPIO8, ch & 0x0f);
  RS_set;
  E_set; 
  delay(25000);
  E_reset;
  delay(25000);
}

/*******************************************************************************
* Function Name  : LCDsendCommand
* Description    : Send Command to LCD (Wyslanie rozkazu do LCD)
*    
* Input          : cmd - is a ascii code of command
* Output         : None
* Return         : None
*******************************************************************************/
void LCDsendCommand(u8 cmd)		
{ 
  //4 MSB bits
  //4 starsze bity
  GPIO_Write(GPIO8, (cmd>>4) & 0x0f);
  RS_reset;
  E_set; 
  delay(25000);
  E_reset;	
  delay(25000);
  //4 LSB bits
  //4 mlodsze bity
  GPIO_Write(GPIO8, cmd & 0x0f);
  RS_reset;
  E_set; 
  delay(25000);
  E_reset;
  delay(25000);
}

/*******************************************************************************
* Function Name  : LCDclr
* Description    : Clear LCD (Czyszczenie LCD)
*    
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void LCDclr(void)				
{
  LCDsendCommand(0x01);
}

/*******************************************************************************
* Function Name  : LCDhome
* Description    : LCD cursor home (Przesuniecie kursora na pozycje poczatkowa)
*    
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void LCDhome(void)			
{
  LCDsendCommand(0x02);
}

/*******************************************************************************
* Function Name  : LCDstring
* Description    : Outputs string to LCD (Wyswietl ciag znakow na LCD)
*    
* Input          : data - pointer to start of table, nBytes - nuber of bytes to send
* Output         : None
* Return         : None
*******************************************************************************/
void LCDstring(u8* data, u8 nBytes)	
{
  u8 count;
  //check to make sure we have a good pointer
  //sprawdzenie zgodnosci wskaznika
  if (!data) return;

  //print data
  //wyswietl znaki
  for(count=0; count<nBytes; count++)
  {
    LCDsendChar(data[count]);
  }
}

/*******************************************************************************
* Function Name  : LCDGotoXY
* Description    : Cursor to X Y position (Kursor na pozycje XY)
*    
* Input          : row - x position, column - y position
* Output         : None
* Return         : None
*******************************************************************************/
void LCDGotoXY(u8 row, u8 column)	
{
#define LCD_DDRAM             7	

#define LCD_LINE0_DDRAMADDR   0x00
#define LCD_LINE1_DDRAMADDR   0x40
#define LCD_LINE2_DDRAMADDR   0x14
#define LCD_LINE3_DDRAMADDR   0x54

	u8 DDRAMAddr;
	//remap lines into proper order
        //wyznaczenie adresu polozenia
	switch(column)
	{
	case 0:  DDRAMAddr = LCD_LINE0_DDRAMADDR + row; break;
	case 1:  DDRAMAddr = LCD_LINE1_DDRAMADDR + row; break;
	case 2:  DDRAMAddr = LCD_LINE2_DDRAMADDR + row; break;
	case 3:  DDRAMAddr = LCD_LINE3_DDRAMADDR + row; break;
	default: DDRAMAddr = LCD_LINE0_DDRAMADDR + row;
	}
	//set data address
        //wyslanie adresu
	LCDsendCommand(1<<LCD_DDRAM | DDRAMAddr);	
} 

/*******************************************************************************
* Function Name  : LCDshiftLeft
* Description    : Scrol n of characters Right (Przsuniecie znakow o n w prawo)
*    
* Input          : right - number of characters
* Output         : None
* Return         : None
*******************************************************************************/
void LCDshiftLeft(u8 right)	
{
u8 count;
  for (count=0;count<right;count++)
  {
    LCDsendCommand(0x1E);
  }
}

/*******************************************************************************
* Function Name  : LCDshiftRight
* Description    : Scrol n of characters Left (Przsuniecie znakow o n w lewo)
*    
* Input          : left - number of characters
* Output         : None
* Return         : None
*******************************************************************************/
void LCDshiftRight(u8 left)	
{
u8 count;
  for (count=0;count<left;count++)
  {
    LCDsendCommand(0x18);
  }
}

/*******************************************************************************
* Function Name  : LCDcursorOn
* Description    : Displays LCD cursor (Kursor aktywny)
*    
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void LCDcursorOn(void) 
{
  LCDsendCommand(0x0E);
}

/*******************************************************************************
* Function Name  : LCDcursorOnBlink
* Description    : Displays LCD blinking cursor (Migajacy kursor aktywny)
*    
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void LCDcursorOnBlink(void)	
{
  LCDsendCommand(0x0F);
}

/*******************************************************************************
* Function Name  : LCDcursorOFF
* Description    : Turns OFF cursor (Kursor wylaczony)
*    
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void LCDcursorOFF(void)	
{
  LCDsendCommand(0x0C);
}

/*******************************************************************************
* Function Name  : LCDblank
* Description    : Blanks LCD (LCD nieaktywny)
*    
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void LCDblank(void)		
{
  LCDsendCommand(0x08);
}

/*******************************************************************************
* Function Name  : LCDvisible
* Description    : Shows LCD (LCD aktywny)
*    
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void LCDvisible(void)		
{
  LCDsendCommand(0x0C);
}

/*******************************************************************************
* Function Name  : LCDcursorLeft
* Description    : Moves cursor by n poisitions left (Kursor n pozycji w lewo)
*    
* Input          : left - number of positions
* Output         : None
* Return         : None
*******************************************************************************/
void LCDcursorLeft(u8 left)	
{
u8 count;
  for (count=0;count<left;count++)
  {
    LCDsendCommand(0x10);
  }
}

/*******************************************************************************
* Function Name  : LCDcursorRight
* Description    : Moves cursor by n poisitions right (Kursor n pozycji w prawo)
*    
* Input          : right - number of positions
* Output         : None
* Return         : None
*******************************************************************************/
void LCDcursorRight(u8 right)	
{
u8 count;
  for (count=0;count<right;count++)
  {
    LCDsendCommand(0x14);
  }
}

/******************* (C) COPYRIGHT 2007 PROPOX *****END OF FILE****/
OpenPOWER on IntegriCloud