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