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