]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/dsPIC_MPLAB/lcd.c
3a4c48009e42a2fe0813bd104303ca6b162495e8
[freertos] / FreeRTOS / Demo / dsPIC_MPLAB / lcd.c
1 /*\r
2     FreeRTOS V7.2.0 - Copyright (C) 2012 Real Time Engineers Ltd.\r
3         \r
4 \r
5     ***************************************************************************\r
6      *                                                                       *\r
7      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
8      *    Complete, revised, and edited pdf reference manuals are also       *\r
9      *    available.                                                         *\r
10      *                                                                       *\r
11      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
12      *    ensuring you get running as quickly as possible and with an        *\r
13      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
14      *    the FreeRTOS project to continue with its mission of providing     *\r
15      *    professional grade, cross platform, de facto standard solutions    *\r
16      *    for microcontrollers - completely free of charge!                  *\r
17      *                                                                       *\r
18      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
19      *                                                                       *\r
20      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
21      *                                                                       *\r
22     ***************************************************************************\r
23 \r
24 \r
25     This file is part of the FreeRTOS distribution.\r
26 \r
27     FreeRTOS is free software; you can redistribute it and/or modify it under\r
28     the terms of the GNU General Public License (version 2) as published by the\r
29     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
30     >>>NOTE<<< The modification to the GPL is included to allow you to\r
31     distribute a combined work that includes FreeRTOS without being obliged to\r
32     provide the source code for proprietary components outside of the FreeRTOS\r
33     kernel.  FreeRTOS is distributed in the hope that it will be useful, but\r
34     WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
35     or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
36     more details. You should have received a copy of the GNU General Public\r
37     License and the FreeRTOS license exception along with FreeRTOS; if not it\r
38     can be viewed here: http://www.freertos.org/a00114.html and also obtained\r
39     by writing to Richard Barry, contact details for whom are available on the\r
40     FreeRTOS WEB site.\r
41 \r
42     1 tab == 4 spaces!\r
43     \r
44     ***************************************************************************\r
45      *                                                                       *\r
46      *    Having a problem?  Start by reading the FAQ "My application does   *\r
47      *    not run, what could be wrong?                                      *\r
48      *                                                                       *\r
49      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
50      *                                                                       *\r
51     ***************************************************************************\r
52 \r
53     \r
54     http://www.FreeRTOS.org - Documentation, training, latest information, \r
55     license and contact details.\r
56     \r
57     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
58     including FreeRTOS+Trace - an indispensable productivity tool.\r
59 \r
60     Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell \r
61     the code with commercial support, indemnification, and middleware, under \r
62     the OpenRTOS brand: http://www.OpenRTOS.com.  High Integrity Systems also\r
63     provide a safety engineered and independently SIL3 certified version under \r
64     the SafeRTOS brand: http://www.SafeRTOS.com.\r
65 */\r
66 \r
67 /* Scheduler includes. */\r
68 #include "FreeRTOS.h"\r
69 #include "task.h"\r
70 #include "queue.h"\r
71 \r
72 /* Demo includes. */\r
73 #include "lcd.h"\r
74 \r
75 /*\r
76  * The LCD is written to by more than one task so is controlled by this\r
77  * 'gatekeeper' task.  This is the only task that is actually permitted to\r
78  * access the LCD directly.  Other tasks wanting to display a message send\r
79  * the message to the gatekeeper.\r
80  */\r
81 static void vLCDTask( void *pvParameters );\r
82 \r
83 /*\r
84  * Setup the peripherals required to communicate with the LCD.\r
85  */\r
86 static void prvSetupLCD( void );\r
87 \r
88 /* \r
89  * Move to the first (0) or second (1) row of the LCD. \r
90  */\r
91 static void prvLCDGotoRow( unsigned portSHORT usRow );\r
92 \r
93 /* \r
94  * Write a string of text to the LCD. \r
95  */\r
96 static void prvLCDPutString( portCHAR *pcString );\r
97 \r
98 /* \r
99  * Clear the LCD. \r
100  */\r
101 static void prvLCDClear( void );\r
102 \r
103 /*-----------------------------------------------------------*/\r
104 \r
105 /* Brief delay to permit the LCD to catch up with commands. */\r
106 #define lcdVERY_SHORT_DELAY     ( 1 )\r
107 #define lcdSHORT_DELAY          ( 4 / portTICK_RATE_MS )\r
108 #define lcdLONG_DELAY           ( 15 / portTICK_RATE_MS )\r
109 \r
110 /* LCD commands. */\r
111 #define lcdCLEAR                        ( 0x01 )\r
112 #define lcdHOME                         ( 0x02 )\r
113 #define lcdLINE2                        ( 0xc0 )\r
114 \r
115 /* SFR that seems to be missing from the standard header files. */\r
116 #define PMAEN                           *( ( unsigned short * ) 0x60c )\r
117 \r
118 /* LCD R/W signal. */\r
119 #define  lcdRW  LATDbits.LATD5       \r
120 \r
121 /* LCD lcdRS signal. */\r
122 #define  lcdRS  LATBbits.LATB15      \r
123 \r
124 /* LCD lcdE signal . */\r
125 #define  lcdE   LATDbits.LATD4       \r
126 \r
127 /* Control signal pin direction. */\r
128 #define  RW_TRIS        TRISDbits.TRISD5 \r
129 #define  RS_TRIS        TRISBbits.TRISB15\r
130 #define  E_TRIS         TRISDbits.TRISD4\r
131 \r
132 /* Port for LCD data */\r
133 #define  lcdDATA      LATE           \r
134 #define  lcdDATAPORT  PORTE\r
135 \r
136 /* I/O setup for data Port. */\r
137 #define  TRISDATA  TRISE          \r
138 \r
139 /* The length of the queue used to send messages to the LCD gatekeeper task. */\r
140 #define lcdQUEUE_SIZE           3\r
141 /*-----------------------------------------------------------*/\r
142 \r
143 /* The queue used to send messages to the LCD task. */\r
144 xQueueHandle xLCDQueue;\r
145 \r
146 static void prvLCDCommand( portCHAR cCommand );\r
147 static void prvLCDData( portCHAR cChar );\r
148 \r
149 /*-----------------------------------------------------------*/\r
150 \r
151 xQueueHandle xStartLCDTask( void )\r
152 {\r
153         /* Create the queue used by the LCD task.  Messages for display on the LCD\r
154         are received via this queue. */\r
155         xLCDQueue = xQueueCreate( lcdQUEUE_SIZE, sizeof( xLCDMessage ) );\r
156 \r
157         /* Start the task that will write to the LCD.  The LCD hardware is\r
158         initialised from within the task itself so delays can be used. */\r
159         xTaskCreate( vLCDTask, ( signed portCHAR * ) "LCD", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 1, NULL );\r
160 \r
161         return xLCDQueue;\r
162 }\r
163 /*-----------------------------------------------------------*/\r
164 \r
165 static void prvLCDGotoRow( unsigned portSHORT usRow )\r
166 {\r
167         if( usRow == 0 )\r
168         {\r
169                 prvLCDCommand( lcdHOME );\r
170         }\r
171         else\r
172         {\r
173                 prvLCDCommand( lcdLINE2 );\r
174         }\r
175 }\r
176 /*-----------------------------------------------------------*/\r
177 \r
178 static void prvLCDCommand( portCHAR cCommand ) \r
179 {\r
180         /* Prepare RD0 - RD7. */\r
181         lcdDATA &= 0xFF00;               \r
182 \r
183         /* Command byte to lcd. */\r
184     lcdDATA |= cCommand;                  \r
185 \r
186         /* Ensure lcdRW is 0. */\r
187         lcdRW = 0;                       \r
188     lcdRS = 0;\r
189 \r
190         /* Toggle lcdE line. */\r
191     lcdE = 1;                        \r
192     vTaskDelay( lcdVERY_SHORT_DELAY );\r
193     lcdE = 0;\r
194 \r
195         vTaskDelay( lcdSHORT_DELAY );\r
196 }\r
197 /*-----------------------------------------------------------*/\r
198 \r
199 static void prvLCDData( portCHAR cChar )\r
200 {\r
201         /* ensure lcdRW is 0. */\r
202         lcdRW = 0;                                       \r
203 \r
204         /* Assert register select to 1. */\r
205     lcdRS = 1;                       \r
206 \r
207         /* Prepare RD0 - RD7. */\r
208         lcdDATA &= 0xFF00;               \r
209 \r
210         /* Data byte to lcd. */\r
211     lcdDATA |= cChar;                 \r
212     lcdE = 1;                           \r
213         Nop();\r
214     Nop();\r
215     Nop();\r
216 \r
217         /* Toggle lcdE signal. */\r
218     lcdE = 0;                       \r
219 \r
220         /* Negate register select to 0. */\r
221     lcdRS = 0;                      \r
222 \r
223         vTaskDelay( lcdVERY_SHORT_DELAY );\r
224 }\r
225 /*-----------------------------------------------------------*/\r
226 \r
227 static void prvLCDPutString( portCHAR *pcString )\r
228 {\r
229         /* Write out each character with appropriate delay between each. */\r
230         while( *pcString )\r
231         {\r
232                 prvLCDData( *pcString );\r
233                 pcString++;\r
234                 vTaskDelay( lcdSHORT_DELAY );\r
235         }\r
236 }\r
237 /*-----------------------------------------------------------*/\r
238 \r
239 static void prvLCDClear( void )\r
240 {\r
241         prvLCDCommand( lcdCLEAR );\r
242 }\r
243 /*-----------------------------------------------------------*/\r
244 \r
245 static void prvSetupLCD( void )\r
246 {\r
247         /* Wait for proper power up. */\r
248         vTaskDelay( lcdLONG_DELAY );\r
249                         \r
250         /* Set initial states for the data and control pins */\r
251         LATE &= 0xFF00; \r
252 \r
253         /* R/W state set low. */\r
254     lcdRW = 0;                       \r
255 \r
256         /* lcdRS state set low. */\r
257         lcdRS = 0;                       \r
258 \r
259         /* lcdE state set low. */\r
260         lcdE = 0;                        \r
261 \r
262         /* Set data and control pins to outputs */\r
263         TRISE &= 0xFF00;\r
264 \r
265         /* lcdRW pin set as output. */\r
266         RW_TRIS = 0;                  \r
267 \r
268         /* lcdRS pin set as output. */\r
269         RS_TRIS = 0;                  \r
270 \r
271         /* lcdE pin set as output. */\r
272         E_TRIS = 0;                   \r
273 \r
274         /* 1st LCD initialization sequence */\r
275         lcdDATA &= 0xFF00;\r
276     lcdDATA |= 0x0038;\r
277     lcdE = 1;   \r
278     Nop();\r
279     Nop();\r
280     Nop();\r
281 \r
282         /* Toggle lcdE signal. */\r
283     lcdE = 0;                        \r
284 \r
285         vTaskDelay( lcdSHORT_DELAY );\r
286         vTaskDelay( lcdSHORT_DELAY );\r
287         vTaskDelay( lcdSHORT_DELAY );\r
288       \r
289         /* 2nd LCD initialization sequence */\r
290         lcdDATA &= 0xFF00;\r
291     lcdDATA |= 0x0038;\r
292     lcdE = 1;   \r
293     Nop();\r
294     Nop();\r
295     Nop();      \r
296 \r
297         /* Toggle lcdE signal. */\r
298     lcdE = 0;                        \r
299 \r
300     vTaskDelay( lcdSHORT_DELAY );\r
301 \r
302         /* 3rd LCD initialization sequence */\r
303         lcdDATA &= 0xFF00;\r
304     lcdDATA |= 0x0038;\r
305     lcdE = 1;           \r
306     Nop();\r
307     Nop();\r
308     Nop();      \r
309 \r
310         /* Toggle lcdE signal. */\r
311     lcdE = 0;                        \r
312 \r
313         vTaskDelay( lcdSHORT_DELAY );\r
314 \r
315 \r
316         /* Function set. */\r
317     prvLCDCommand( 0x38 );              \r
318 \r
319         /* Display on/off control, cursor blink off (0x0C). */\r
320     prvLCDCommand( 0x0C );              \r
321 \r
322         /* Entry mode set (0x06). */\r
323     prvLCDCommand( 0x06 );              \r
324 \r
325         prvLCDCommand( lcdCLEAR );        \r
326 }\r
327 /*-----------------------------------------------------------*/\r
328 \r
329 static void vLCDTask( void *pvParameters )\r
330 {\r
331 xLCDMessage xMessage;\r
332 unsigned portSHORT usRow = 0;\r
333 \r
334         /* Initialise the hardware.  This uses delays so must not be called prior\r
335         to the scheduler being started. */\r
336         prvSetupLCD();\r
337 \r
338         /* Welcome message. */\r
339         prvLCDPutString( "www.FreeRTOS.org" );\r
340 \r
341         for( ;; )\r
342         {\r
343                 /* Wait for a message to arrive that requires displaying. */\r
344                 while( xQueueReceive( xLCDQueue, &xMessage, portMAX_DELAY ) != pdPASS );\r
345 \r
346                 /* Clear the current display value. */\r
347                 prvLCDClear();\r
348 \r
349                 /* Switch rows each time so we can see that the display is still being\r
350                 updated. */\r
351                 prvLCDGotoRow( usRow & 0x01 );\r
352                 usRow++;\r
353                 prvLCDPutString( xMessage.pcMessage );\r
354 \r
355                 /* Delay the requested amount of time to ensure the text just written \r
356                 to the LCD is not overwritten. */\r
357                 vTaskDelay( xMessage.xMinDisplayTime );         \r
358         }\r
359 }\r
360 \r
361 \r
362 \r
363 \r