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