OpenCores
URL https://opencores.org/ocsvn/aemb/aemb/trunk

Subversion Repositories aemb

[/] [aemb/] [trunk/] [sw/] [vpio/] [vpioLcd.h] - Blame information for rev 197

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 197 sybreon
/* $Id: memtest.hh,v 1.8 2008-06-24 10:03:41 sybreon Exp $
2
**
3
** VIRTUAL PERIPHERAL I/O LIBRARY
4
** Copyright (C) 2009 Shawn Tan <shawn.tan@aeste.net>
5
**
6
** This file is part of AEMB.
7
**
8
** AEMB is free software: you can redistribute it and/or modify it
9
** under the terms of the GNU General Public License as published by
10
** the Free Software Foundation, either version 3 of the License, or
11
** (at your option) any later version.
12
**
13
** AEMB is distributed in the hope that it will be useful, but WITHOUT
14
** ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15
** or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
16
** License for more details.
17
**
18
** You should have received a copy of the GNU General Public License
19
** along with AEMB.  If not, see <http://www.gnu.org/licenses/>.
20
*/
21
 
22
/*!
23
  Character LCD Interface Library
24
  @file
25
 
26
  Interface library for standard HH44780/SED1278 character LCD
27
  drivers. This library assumes a 4-bit hardware interface mode to
28
  reduce the lines and GPIO width.
29
 
30
  Routines modified from AVRLib by Pascal Stang.
31
 */
32
 
33
#include "vpioGpio.h"
34
 
35
#ifndef VPIO_LCD_H
36
#define VPIO_LCD_H
37
 
38
#define LCD_NIB 1 // nibble mode
39
#define WAIT_STATE 1
40
 
41
// GPIO control lines - DO NOT MODIFY
42
#define LCD_D7 7 // D7 - P14
43
#define LCD_D6 6 // D6 - P13
44
#define LCD_D5 5 // D5 - P12
45
#define LCD_D4 4 // D4 - P11
46
#define LCD_EN 3 // EN - P6
47
#define LCD_RW 2 // RW - P5
48
#define LCD_RS 1 // RS - P4
49
#define LCD_HB 0 // HB - NC
50
 
51
// HD44780 LCD controller command set (do not modify these)
52
// writing:
53
#define LCD_CLR             0      // DB0: clear display
54
#define LCD_HOME            1      // DB1: return to home position
55
#define LCD_ENTRY_MODE      2      // DB2: set entry mode
56
#define LCD_ENTRY_INC       1      //   DB1: increment
57
#define LCD_ENTRY_SHIFT     0      //   DB2: shift
58
#define LCD_ON_CTRL         3      // DB3: turn lcd/cursor on
59
#define LCD_ON_DISPLAY      2      //   DB2: turn display on
60
#define LCD_ON_CURSOR       1      //   DB1: turn cursor on
61
#define LCD_ON_BLINK        0      //   DB0: blinking cursor
62
#define LCD_MOVE            4      // DB4: move cursor/display
63
#define LCD_MOVE_DISP       3      //   DB3: move display (0-> move cursor)
64
#define LCD_MOVE_RIGHT      2      //   DB2: move right (0-> left)
65
#define LCD_FUNCTION        5      // DB5: function set
66
#define LCD_FUNCTION_8BIT   4      //   DB4: set 8BIT mode (0->4BIT mode)
67
#define LCD_FUNCTION_2LINES 3      //   DB3: two lines (0->one line)
68
#define LCD_FUNCTION_10DOTS 2      //   DB2: 5x10 font (0->5x7 font)
69
#define LCD_CGRAM           6      // DB6: set CG RAM address
70
#define LCD_DDRAM           7      // DB7: set DD RAM address
71
// reading:
72
#define LCD_BUSY            7      // DB7: LCD is busy
73
 
74
// Default LCD setup
75
// this default setup is loaded on LCD initialization
76
#define LCD_FDEF_1           (0<<LCD_FUNCTION_8BIT)
77
#define LCD_FDEF_2           (1<<LCD_FUNCTION_2LINES)
78
#define LCD_FUNCTION_DEFAULT ((1<<LCD_FUNCTION) | LCD_FDEF_1 | LCD_FDEF_2)
79
#define LCD_MODE_DEFAULT     ((1<<LCD_ENTRY_MODE) | (1<<LCD_ENTRY_INC))
80
 
81
#define LCD_OUTPUT  0xFF
82
#define LCD_INPUT   0x0F
83
 
84
typedef struct gpioRegs lcdRegs;
85
 
86
// Low-level I/O functions
87
void lcdReset(lcdRegs* lcd);
88
void lcdWaitBusy(lcdRegs* lcd);
89
void lcdPutControl(lcdRegs* lcd, char ctrl);
90
char lcdGetControl(lcdRegs* lcd);
91
void lcdPutData(lcdRegs* lcd, char data);
92
char lcdGetData(lcdRegs* lcd);
93
void lcdDelay(int ms);
94
//void lcdSetTris(lcdRegs* lcd, gpioTrisType dir);
95
 
96
// High-level application functions
97
void lcdInit(lcdRegs* lcd);
98
void lcdHome(lcdRegs* lcd);
99
void lcdClear(lcdRegs* lcd);
100
 
101
 
102
/*!
103
  Reset the LCD hardware.
104
 
105
  @param lcd LCD GPIO port.
106
 */
107
 
108
void lcdReset(lcdRegs* lcd)
109
{
110
  // set GPIO to output
111
  gpioSetTris(lcd, LCD_OUTPUT);
112
 
113
  // clear lines
114
  /*
115
  gpioClrBit(lcd, LCD_RS);
116
  gpioClrBit(lcd, LCD_RW);
117
  gpioClrBit(lcd, LCD_EN);
118
  */
119
  gpioPutData(lcd, 0x00);
120
 
121
  // set lines
122
  /*
123
  gpioSetBit(lcd, LCD_RS);
124
  gpioSetBit(lcd, LCD_RW);
125
  gpioSetBit(lcd, LCD_EN);
126
  */
127
  gpioPutData(lcd, 0xFF);
128
 
129
  // set GPIO to input
130
  gpioSetTris(lcd, LCD_INPUT);
131
}
132
 
133
/*!
134
  Blocking wait for LCD busy signal.
135
 
136
  This function blocks the LCD port while the busy signal is asserted
137
  by the LCD.
138
 
139
  @param lcd LCD GPIO port.
140
 */
141
void lcdWaitBusy(lcdRegs* lcd)
142
{
143
  int i;
144
  gpioSetTris(lcd, LCD_INPUT); // set INPUT
145
  //gpioClrBit(lcd, LCD_RS); // select CONTROL
146
  //gpioSetBit(lcd, LCD_RW); // set to READ
147
  //gpioSetBit(lcd, LCD_EN);
148
  gpioPutData(lcd, (1<<LCD_RW) | (1<<LCD_EN)); // EN|RD|CT
149
  lcdDelay(WAIT_STATE);
150
  while (gpioGetBit(lcd, LCD_BUSY))
151
    {
152
      for (i = 2; i>0; --i)
153
        {
154
          /*
155
            gpioClrBit(lcd, LCD_EN);
156
            lcdDelay(WAIT_STATE);
157
            gpioSetBit(lcd, LCD_EN);
158
            lcdDelay(WAIT_STATE);
159
          */
160
          gpioPutData(lcd, (1<<LCD_RW) );
161
          lcdDelay(WAIT_STATE);
162
          gpioPutData(lcd, (1<<LCD_RW) | (1<<LCD_EN) );
163
          lcdDelay(WAIT_STATE);
164
        }
165
    }
166
 
167
  gpioPutData(lcd, (1<<LCD_RW) );
168
  //gpioClrBit(lcd, LCD_EN);
169
  //gpioTogBit(lcd, LCD_HB);
170
}
171
 
172
/*!
173
  Write a control byte to the LCD.
174
 
175
  This function writes a byte to the LCD control register.
176
  @param lcd LCD GPIO port.
177
  @param ctrl Control byte to write
178
 */
179
void lcdPutControl(lcdRegs* lcd, char ctrl)
180
{
181
  lcdWaitBusy(lcd); // wait until free
182
  //gpioClrBit(lcd, LCD_RS); // select CONTROL
183
  //gpioClrBit(lcd, LCD_RW); // set to WRITE
184
  gpioPutData(lcd, (1<<LCD_HB));
185
  gpioSetTris(lcd, LCD_OUTPUT); // Set OUTPUT
186
 
187
  gpioPutData(lcd, (gpioGetData(lcd) & 0x0F) | (ctrl & 0xF0) );
188
  gpioSetBit(lcd, LCD_EN);
189
  lcdDelay(WAIT_STATE);
190
  gpioClrBit(lcd, LCD_EN);
191
  lcdDelay(WAIT_STATE);
192
  gpioPutData(lcd, (gpioGetData(lcd) & 0x0F) | (ctrl << 4) );
193
  gpioSetBit(lcd, LCD_EN);
194
  lcdDelay(WAIT_STATE);
195
  gpioClrBit(lcd, LCD_EN);
196
  gpioSetTris(lcd, LCD_INPUT); // set INPUT
197
}
198
 
199
/*!
200
  Read a control byte from the LCD.
201
 
202
  This function reads a control byte from the LCD control register.
203
  @param lcd LCD GPIO port.
204
  @return control register value.
205
 */
206
 
207
char lcdGetControl(lcdRegs* lcd)
208
{
209
  char b;
210
  lcdWaitBusy(lcd); // wait until free
211
  gpioSetTris(lcd, LCD_INPUT); // Set INPUT
212
  //gpioClrBit(lcd, LCD_RS); // select CONTROL
213
  //gpioSetBit(lcd, LCD_RW); // set to READ
214
  //gpioSetBit(lcd, LCD_EN); 
215
  gpioPutData(lcd, (1<<LCD_RW) | (1<<LCD_EN) );
216
  lcdDelay(WAIT_STATE);
217
 
218
  b = gpioGetData(lcd) & 0xF0; // read upper nib
219
  gpioClrBit(lcd, LCD_EN);
220
  lcdDelay(WAIT_STATE);
221
  gpioSetBit(lcd, LCD_EN);
222
  lcdDelay(WAIT_STATE);
223
  gpioClrBit(lcd, LCD_EN);
224
  b |= (gpioGetData(lcd) >> 4); // read lower nib
225
  gpioSetTris(lcd, LCD_INPUT); // set INPUT
226
  return b;
227
}
228
 
229
/*!
230
  Write a data byte to the LCD.
231
 
232
  This function writes a byte to the LCD data register.
233
  @param lcd LCD GPIO port.
234
  @param ctrl Control byte to write
235
 */
236
void lcdPutData(lcdRegs* lcd, char data)
237
{
238
  lcdWaitBusy(lcd); // wait until free
239
  //gpioSetBit(lcd, LCD_RS); // select DATA
240
  //gpioClrBit(lcd, LCD_RW); // set to WRITE
241
  gpioPutData(lcd, (1<<LCD_RS) );
242
  gpioSetTris(lcd, LCD_OUTPUT); // Set OUTPUT
243
 
244
  gpioPutData(lcd, (gpioGetData(lcd) & 0x0F) | (data & 0xF0) );
245
  gpioSetBit(lcd, LCD_EN);
246
  lcdDelay(WAIT_STATE);
247
  gpioClrBit(lcd, LCD_EN);
248
  lcdDelay(WAIT_STATE);
249
  gpioPutData(lcd, (gpioGetData(lcd) & 0x0F) | (data << 4) );
250
  gpioSetBit(lcd, LCD_EN);
251
  lcdDelay(WAIT_STATE);
252
  gpioClrBit(lcd, LCD_EN);
253
  //gpioPutData(lcd, (1<<LCD_RS) );
254
  gpioSetTris(lcd, LCD_INPUT); // set INPUT
255
}
256
 
257
/*!
258
  Read a data byte from the LCD.
259
 
260
  This function reads a control byte from the LCD control register.
261
  @param lcd LCD GPIO port.
262
  @return data register value.
263
 */
264
 
265
char lcdGetData(lcdRegs* lcd)
266
{
267
  char b;
268
  lcdWaitBusy(lcd); // wait until free
269
  gpioSetTris(lcd, LCD_INPUT); // Set INPUT
270
  gpioSetBit(lcd, LCD_RS); // select DATA
271
  gpioSetBit(lcd, LCD_RW); // set to READ
272
  gpioSetBit(lcd, LCD_EN);
273
  lcdDelay(WAIT_STATE);
274
  b = gpioGetData(lcd) & 0xF0; // read upper nib
275
  gpioClrBit(lcd, LCD_EN);
276
  lcdDelay(WAIT_STATE);
277
  gpioSetBit(lcd, LCD_EN);
278
  lcdDelay(WAIT_STATE);
279
  gpioClrBit(lcd, LCD_EN);
280
  b |= (gpioGetData(lcd) >> 4); // read lower nib
281
  gpioSetTris(lcd, LCD_INPUT); // set INPUT
282
  return b;
283
}
284
 
285
/*!
286
  Initialisation Routine.
287
 
288
  This function brings the LCD through the standard initialisation
289
  process. It sets the LCD mode to default entry modes, clears the
290
  screen and cursors to home.
291
 
292
  @param lcd LCD GPIO port.
293
 */
294
 
295
void lcdInit(lcdRegs* lcd)
296
{
297
  lcdReset(lcd); // reset HW
298
 
299
  lcdPutControl(lcd, LCD_FUNCTION_DEFAULT);
300
 
301
  lcdPutControl(lcd, 1 << LCD_CLR);
302
  lcdDelay(0x00008000); // delay 30ms+
303
 
304
  lcdPutControl(lcd, 1 << LCD_ENTRY_MODE | 1 << LCD_ENTRY_INC);
305
  lcdPutControl(lcd, 1 << LCD_ON_CTRL | 1 << LCD_ON_DISPLAY);
306
 
307
  lcdPutControl(lcd, 1 << LCD_HOME);
308
  lcdPutControl(lcd, 1 << LCD_DDRAM | 0x00);
309
}
310
 
311
/*!
312
  Arbitrary delay routine.
313
 
314
  This function does a NOP loop for an arbitrary number of cycles. The
315
  cycles are not calibrated to any clock but is assumed to run on a
316
  nominal 100MHz clock.
317
 
318
  @param us Assume number of micro-seconds (us).
319
 */
320
 
321
inline
322
void lcdDelay(int us)
323
{
324
  int i;
325
  //for (i = 0; i < (ms << 3); ++i) { asm volatile ("nop"); } // do NOP
326
  for (i = us << 4; i > 0; --i) { asm volatile ("nop"); } // do NOP
327
}
328
 
329
/*!
330
  Returns the LCD cursor to home.
331
 
332
  @param lcd LCD GPIO port.
333
 */
334
void lcdHome(lcdRegs* lcd)
335
{
336
  lcdPutControl(lcd, 1<<LCD_HOME);
337
}
338
 
339
/*!
340
  Clear the LCD screen.
341
 
342
  @param lcd LCD GPIO port.
343
 */
344
void lcdClear(lcdRegs* lcd)
345
{
346
  lcdPutControl(lcd, 1<<LCD_CLR);
347
}
348
 
349
#endif

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.