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