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