]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/PIC24_MPLAB/lcd.c
Prepare for V7.3.0 release.
[freertos] / FreeRTOS / Demo / PIC24_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 lcdSHORT_DELAY          3\r
109 \r
110 /* SFR that seems to be missing from the standard header files. */\r
111 #define PMAEN                           *( ( unsigned short * ) 0x60c )\r
112 \r
113 /* LCD commands. */\r
114 #define lcdDEFAULT_FUNCTION     0x3c\r
115 #define lcdDISPLAY_CONTROL      0x0c\r
116 #define lcdCLEAR_DISPLAY        0x01\r
117 #define lcdENTRY_MODE           0x06\r
118 \r
119 /* The length of the queue used to send messages to the LCD gatekeeper task. */\r
120 #define lcdQUEUE_SIZE           3\r
121 /*-----------------------------------------------------------*/\r
122 \r
123 /* The queue used to send messages to the LCD task. */\r
124 xQueueHandle xLCDQueue;\r
125 \r
126 \r
127 /*-----------------------------------------------------------*/\r
128 \r
129 xQueueHandle xStartLCDTask( void )\r
130 {\r
131         /* Create the queue used by the LCD task.  Messages for display on the LCD\r
132         are received via this queue. */\r
133         xLCDQueue = xQueueCreate( lcdQUEUE_SIZE, sizeof( xLCDMessage ) );\r
134 \r
135         /* Start the task that will write to the LCD.  The LCD hardware is\r
136         initialised from within the task itself so delays can be used. */\r
137         xTaskCreate( vLCDTask, ( signed portCHAR * ) "LCD", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 1, NULL );\r
138 \r
139         return xLCDQueue;\r
140 }\r
141 /*-----------------------------------------------------------*/\r
142 \r
143 static void prvLCDGotoRow( unsigned portSHORT usRow )\r
144 {\r
145         if( usRow == 0 )\r
146         {\r
147                 PMADDR = 0x0000;\r
148                 PMDIN1 = 0x02;\r
149         }\r
150         else\r
151         {\r
152                 PMADDR = 0x0000;\r
153                 PMDIN1 = 0xc0;\r
154         }\r
155 \r
156         vTaskDelay( lcdSHORT_DELAY );\r
157 }\r
158 /*-----------------------------------------------------------*/\r
159 \r
160 static void prvLCDPutString( portCHAR *pcString )\r
161 {\r
162         /* Write out each character with appropriate delay between each. */\r
163         while( *pcString )\r
164         {\r
165                 PMADDR = 0x0001;\r
166                 PMDIN1 = *pcString;\r
167                 pcString++;\r
168                 vTaskDelay( lcdSHORT_DELAY );\r
169         }\r
170 }\r
171 /*-----------------------------------------------------------*/\r
172 \r
173 static void prvLCDClear( void )\r
174 {\r
175         /* Clear the display. */\r
176         PMADDR = 0x0000;\r
177         PMDIN1 = lcdCLEAR_DISPLAY;\r
178         vTaskDelay( lcdSHORT_DELAY );   \r
179 }\r
180 /*-----------------------------------------------------------*/\r
181 \r
182 static void prvSetupLCD( void )\r
183 {\r
184         /* Setup the PMP. */\r
185         PMCON = 0x83BF;\r
186         PMMODE = 0x3FF;\r
187         PMAEN = 1;\r
188         PMADDR = 0x0000;\r
189         vTaskDelay( lcdSHORT_DELAY );\r
190 \r
191         /* Set the default function. */\r
192         PMDIN1 = lcdDEFAULT_FUNCTION;\r
193         vTaskDelay( lcdSHORT_DELAY );\r
194 \r
195         /* Set the display control. */\r
196         PMDIN1 = lcdDISPLAY_CONTROL;\r
197         vTaskDelay( lcdSHORT_DELAY );\r
198 \r
199         /* Clear the display. */\r
200         PMDIN1 = lcdCLEAR_DISPLAY;\r
201         vTaskDelay( lcdSHORT_DELAY );\r
202 \r
203         /* Set the entry mode. */\r
204         PMDIN1 = lcdENTRY_MODE;\r
205         vTaskDelay( lcdSHORT_DELAY );\r
206 }\r
207 /*-----------------------------------------------------------*/\r
208 \r
209 static void vLCDTask( void *pvParameters )\r
210 {\r
211 xLCDMessage xMessage;\r
212 unsigned portSHORT usRow = 0;\r
213 \r
214         /* Initialise the hardware.  This uses delays so must not be called prior\r
215         to the scheduler being started. */\r
216         prvSetupLCD();\r
217 \r
218         /* Welcome message. */\r
219         prvLCDPutString( "www.FreeRTOS.org" );\r
220 \r
221         for( ;; )\r
222         {\r
223                 /* Wait for a message to arrive that requires displaying. */\r
224                 while( xQueueReceive( xLCDQueue, &xMessage, portMAX_DELAY ) != pdPASS );\r
225 \r
226                 /* Clear the current display value. */\r
227                 prvLCDClear();\r
228 \r
229                 /* Switch rows each time so we can see that the display is still being\r
230                 updated. */\r
231                 prvLCDGotoRow( usRow & 0x01 );\r
232                 usRow++;\r
233                 prvLCDPutString( xMessage.pcMessage );\r
234 \r
235                 /* Delay the requested amount of time to ensure the text just written \r
236                 to the LCD is not overwritten. */\r
237                 vTaskDelay( xMessage.xMinDisplayTime );         \r
238         }\r
239 }\r
240 \r
241 \r
242 \r
243 \r