1 /*****************************************************************************
\r
6 * Function : Routines for LCD
\r
7 * Designer : K. Sterckx
\r
8 * Creation date : 22/01/2007
\r
10 * Processor : LPC2368
\r
14 * based on example code from NXP
\r
16 ************************************************************************
\r
18 * This code is used to place text on the LCD.
\r
20 ************************************************************************/
\r
22 #include <targets/LPC2368.h>
\r
23 #include "portlcd.h"
\r
24 #include "FreeRTOS.h"
\r
27 /* Please note, on old MCB2300 board, the LCD_E bit is p1.30, on the new board
\r
28 it's p1.31, please check the schematic carefully, and change LCD_CTRL and LCD_E
\r
29 accordingly if you have a different board. */
\r
31 /* LCD IO definitions */
\r
32 #define LCD_E 0x80000000 /* Enable control pin */
\r
33 #define LCD_RW 0x20000000 /* Read/Write control pin */
\r
34 #define LCD_RS 0x10000000 /* Data/Instruction control */
\r
35 #define LCD_CTRL 0xB0000000 /* Control lines mask */
\r
36 #define LCD_DATA 0x0F000000 /* Data lines mask */
\r
38 /* Local variables */
\r
39 static unsigned int lcd_ptr;
\r
41 /* 8 user defined characters to be loaded into CGRAM (used for bargraph) */
\r
42 static const unsigned char UserFont[8][8] = {
\r
43 { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
\r
44 { 0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10 },
\r
45 { 0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18 },
\r
46 { 0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C },
\r
47 { 0x1E,0x1E,0x1E,0x1E,0x1E,0x1E,0x1E,0x1E },
\r
48 { 0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F },
\r
49 { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
\r
50 { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }
\r
53 /* Local Function Prototypes */
\r
54 static void lcd_write( unsigned int c );
\r
55 static void lcd_write_4bit( unsigned int c );
\r
56 static unsigned int lcd_read_stat( void );
\r
57 static void lcd_write_cmd( unsigned int c );
\r
58 static void lcd_write_data( unsigned int d );
\r
59 static void lcd_wait_busy( void );
\r
62 /******************************************************************************
\r
63 ** Function name: lcd_write_4bit
\r
67 ** parameters: four bits to write
\r
68 ** Returned value: None
\r
70 ******************************************************************************/
\r
71 static void lcd_write_4bit(unsigned int c)
\r
73 /* Write a 4-bit command to LCD controller. */
\r
74 FIO1DIR |= LCD_DATA | LCD_CTRL;
\r
75 FIO1CLR = LCD_RW | LCD_DATA;
\r
76 FIO1SET = (c & 0xF) << 24;
\r
84 /******************************************************************************
\r
85 ** Function name: lcd_write
\r
89 ** parameters: word to write
\r
90 ** Returned value: None
\r
92 ******************************************************************************/
\r
93 static void lcd_write(unsigned int c)
\r
95 /* Write data/command to LCD controller. */
\r
96 lcd_write_4bit (c >> 4);
\r
101 /******************************************************************************
\r
102 ** Function name: lcd_read_stat
\r
106 ** parameters: None
\r
107 ** Returned value: status
\r
109 ******************************************************************************/
\r
110 static unsigned int lcd_read_stat(void)
\r
112 /* Read status of LCD controller (ST7066) */
\r
115 FIO1DIR &= ~LCD_DATA;
\r
121 stat = (FIO1PIN >> 20) & 0xF0;
\r
126 stat |= (FIO1PIN >> 24) & 0xF;
\r
131 /******************************************************************************
\r
132 ** Function name: lcd_wait_busy
\r
136 ** parameters: None
\r
137 ** Returned value: None
\r
139 ******************************************************************************/
\r
140 static void lcd_wait_busy(void)
\r
142 /* Wait until LCD controller (ST7066) is busy. */
\r
147 stat = lcd_read_stat();
\r
149 while (stat & 0x80); /* Wait for busy flag */
\r
154 /******************************************************************************
\r
155 ** Function name: lcd_write_cmd
\r
159 ** parameters: command word
\r
160 ** Returned value: None
\r
162 ******************************************************************************/
\r
163 static void lcd_write_cmd(unsigned int c)
\r
165 /* Write command to LCD controller. */
\r
172 /******************************************************************************
\r
173 ** Function name: lcd_write_data
\r
177 ** parameters: data word
\r
178 ** Returned value: None
\r
180 ******************************************************************************/
\r
181 static void lcd_write_data(unsigned int d)
\r
183 /* Write data to LCD controller. */
\r
190 /******************************************************************************
\r
191 ** Function name: LCD_init
\r
195 ** parameters: None
\r
196 ** Returned value: None
\r
198 ******************************************************************************/
\r
199 void LCD_init(void)
\r
201 /* Initialize the ST7066 LCD controller to 4-bit mode. */
\r
202 PINSEL3 = 0x00000000;
\r
204 SCS |= 0x00000001;/* set GPIOx to use Fast I/O */
\r
206 FIO1DIR |= LCD_CTRL | LCD_DATA;
\r
207 FIO1CLR = LCD_RW | LCD_RS | LCD_DATA;
\r
209 lcd_write_4bit(0x3); /* Select 4-bit interface */
\r
211 lcd_write_4bit(0x3);
\r
213 lcd_write_4bit(0x3);
\r
214 lcd_write_4bit(0x2);
\r
216 lcd_write_cmd(0x28); /* 2 lines, 5x8 character matrix */
\r
217 lcd_write_cmd(0x0e); /* Display ctrl:Disp/Curs/Blnk=ON */
\r
218 lcd_write_cmd(0x06); /* Entry mode: Move right, no shift */
\r
220 LCD_load( (unsigned char *)&UserFont, sizeof (UserFont) );
\r
225 /******************************************************************************
\r
226 ** Function name: LCD_load
\r
230 ** parameters: pointer to the buffer and counter
\r
231 ** Returned value: None
\r
233 ******************************************************************************/
\r
234 void LCD_load(unsigned char *fp, unsigned int cnt)
\r
236 /* Load user-specific characters into CGRAM */
\r
239 lcd_write_cmd( 0x40 ); /* Set CGRAM address counter to 0 */
\r
240 for (i = 0; i < cnt; i++, fp++)
\r
242 lcd_write_data( *fp );
\r
247 /******************************************************************************
\r
248 ** Function name: LCD_gotoxy
\r
252 ** parameters: pixel X and Y
\r
253 ** Returned value: None
\r
255 ******************************************************************************/
\r
256 void LCD_gotoxy(unsigned int x, unsigned int y)
\r
258 /* Set cursor position on LCD display. Left corner: 1,1, right: 16,2 */
\r
266 lcd_write_cmd (c | 0x80);
\r
267 lcd_ptr = y*16 + x;
\r
271 /******************************************************************************
\r
272 ** Function name: LCD_cls
\r
276 ** parameters: None
\r
277 ** Returned value: None
\r
279 ******************************************************************************/
\r
282 /* Clear LCD display, move cursor to home position. */
\r
283 lcd_write_cmd (0x01);
\r
288 /******************************************************************************
\r
289 ** Function name: LCD_cur_off
\r
293 ** parameters: None
\r
294 ** Returned value: None
\r
296 ******************************************************************************/
\r
297 void LCD_cur_off(void)
\r
299 /* Switch off LCD cursor. */
\r
300 lcd_write_cmd(0x0c);
\r
305 /******************************************************************************
\r
306 ** Function name: LCD_on
\r
310 ** parameters: None
\r
311 ** Returned value: None
\r
313 ******************************************************************************/
\r
316 /* Switch on LCD and enable cursor. */
\r
317 lcd_write_cmd (0x0e);
\r
321 /******************************************************************************
\r
322 ** Function name: LCD_putc
\r
326 ** parameters: unsigned char character
\r
327 ** Returned value: None
\r
329 ******************************************************************************/
\r
330 void LCD_putc(unsigned char c)
\r
332 /* Print a character to LCD at current cursor position. */
\r
333 if (lcd_ptr == 16)
\r
335 lcd_write_cmd (0xc0);
\r
342 /******************************************************************************
\r
343 ** Function name: LCD_puts
\r
347 ** parameters: pointer to the buffer
\r
348 ** Returned value: None
\r
350 ******************************************************************************/
\r
351 void LCD_puts(unsigned char *sp)
\r
353 /* Print a string to LCD display. */
\r
361 /******************************************************************************
\r
362 ** Function name: LCD_bargraph
\r
366 ** parameters: value and size
\r
367 ** Returned value: None
\r
369 ******************************************************************************/
\r
370 void LCD_bargraph(unsigned int val, unsigned int size)
\r
372 /* Print a bargraph to LCD display. */
\r
373 /* - val: value 0..100 % */
\r
374 /* - size: size of bargraph 1..16 */
\r
377 val = val * size / 20; /* Display matrix 5 x 8 pixels */
\r
378 for (i = 0; i < size; i++)
\r