]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/PIC24_MPLAB/lcd.c
0a5c2c651b9f43a1807cf777b5de41ee0b28c190
[freertos] / FreeRTOS / Demo / PIC24_MPLAB / lcd.c
1 /*\r
2     FreeRTOS V8.2.0 - Copyright (C) 2015 Real Time Engineers Ltd.\r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     This file is part of the FreeRTOS distribution.\r
8 \r
9     FreeRTOS is free software; you can redistribute it and/or modify it under\r
10     the terms of the GNU General Public License (version 2) as published by the\r
11     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
12 \r
13         ***************************************************************************\r
14     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
15     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
16     >>!   obliged to provide the source code for proprietary components     !<<\r
17     >>!   outside of the FreeRTOS kernel.                                   !<<\r
18         ***************************************************************************\r
19 \r
20     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
21     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
22     FOR A PARTICULAR PURPOSE.  Full license text is available on the following\r
23     link: http://www.freertos.org/a00114.html\r
24 \r
25     ***************************************************************************\r
26      *                                                                       *\r
27      *    FreeRTOS provides completely free yet professionally developed,    *\r
28      *    robust, strictly quality controlled, supported, and cross          *\r
29      *    platform software that is more than just the market leader, it     *\r
30      *    is the industry's de facto standard.                               *\r
31      *                                                                       *\r
32      *    Help yourself get started quickly while simultaneously helping     *\r
33      *    to support the FreeRTOS project by purchasing a FreeRTOS           *\r
34      *    tutorial book, reference manual, or both:                          *\r
35      *    http://www.FreeRTOS.org/Documentation                              *\r
36      *                                                                       *\r
37     ***************************************************************************\r
38 \r
39     http://www.FreeRTOS.org/FAQHelp.html - Having a problem?  Start by reading\r
40         the FAQ page "My application does not run, what could be wrong?".  Have you\r
41         defined configASSERT()?\r
42 \r
43         http://www.FreeRTOS.org/support - In return for receiving this top quality\r
44         embedded software for free we request you assist our global community by\r
45         participating in the support forum.\r
46 \r
47         http://www.FreeRTOS.org/training - Investing in training allows your team to\r
48         be as productive as possible as early as possible.  Now you can receive\r
49         FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers\r
50         Ltd, and the world's leading authority on the world's leading RTOS.\r
51 \r
52     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
53     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
54     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
55 \r
56     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.\r
57     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.\r
58 \r
59     http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High\r
60     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
61     licenses offer ticketed support, indemnification and commercial middleware.\r
62 \r
63     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
64     engineered and independently SIL3 certified version for use in safety and\r
65     mission critical applications that require provable dependability.\r
66 \r
67     1 tab == 4 spaces!\r
68 */\r
69 \r
70 /* Scheduler includes. */\r
71 #include "FreeRTOS.h"\r
72 #include "task.h"\r
73 #include "queue.h"\r
74 \r
75 /* Demo includes. */\r
76 #include "lcd.h"\r
77 \r
78 /*\r
79  * The LCD is written to by more than one task so is controlled by this\r
80  * 'gatekeeper' task.  This is the only task that is actually permitted to\r
81  * access the LCD directly.  Other tasks wanting to display a message send\r
82  * the message to the gatekeeper.\r
83  */\r
84 static void vLCDTask( void *pvParameters );\r
85 \r
86 /*\r
87  * Setup the peripherals required to communicate with the LCD.\r
88  */\r
89 static void prvSetupLCD( void );\r
90 \r
91 /* \r
92  * Move to the first (0) or second (1) row of the LCD. \r
93  */\r
94 static void prvLCDGotoRow( unsigned short usRow );\r
95 \r
96 /* \r
97  * Write a string of text to the LCD. \r
98  */\r
99 static void prvLCDPutString( char *pcString );\r
100 \r
101 /* \r
102  * Clear the LCD. \r
103  */\r
104 static void prvLCDClear( void );\r
105 \r
106 /*-----------------------------------------------------------*/\r
107 \r
108 /* Brief delay to permit the LCD to catch up with commands. */\r
109 #define lcdSHORT_DELAY          3\r
110 \r
111 /* SFR that seems to be missing from the standard header files. */\r
112 #define PMAEN                           *( ( unsigned short * ) 0x60c )\r
113 \r
114 /* LCD commands. */\r
115 #define lcdDEFAULT_FUNCTION     0x3c\r
116 #define lcdDISPLAY_CONTROL      0x0c\r
117 #define lcdCLEAR_DISPLAY        0x01\r
118 #define lcdENTRY_MODE           0x06\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 QueueHandle_t xLCDQueue;\r
126 \r
127 \r
128 /*-----------------------------------------------------------*/\r
129 \r
130 QueueHandle_t xStartLCDTask( void )\r
131 {\r
132         /* Create the queue used by the LCD task.  Messages for display on the LCD\r
133         are received via this queue. */\r
134         xLCDQueue = xQueueCreate( lcdQUEUE_SIZE, sizeof( xLCDMessage ) );\r
135 \r
136         /* Start the task that will write to the LCD.  The LCD hardware is\r
137         initialised from within the task itself so delays can be used. */\r
138         xTaskCreate( vLCDTask, "LCD", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 1, NULL );\r
139 \r
140         return xLCDQueue;\r
141 }\r
142 /*-----------------------------------------------------------*/\r
143 \r
144 static void prvLCDGotoRow( unsigned short usRow )\r
145 {\r
146         if( usRow == 0 )\r
147         {\r
148                 PMADDR = 0x0000;\r
149                 PMDIN1 = 0x02;\r
150         }\r
151         else\r
152         {\r
153                 PMADDR = 0x0000;\r
154                 PMDIN1 = 0xc0;\r
155         }\r
156 \r
157         vTaskDelay( lcdSHORT_DELAY );\r
158 }\r
159 /*-----------------------------------------------------------*/\r
160 \r
161 static void prvLCDPutString( char *pcString )\r
162 {\r
163         /* Write out each character with appropriate delay between each. */\r
164         while( *pcString )\r
165         {\r
166                 PMADDR = 0x0001;\r
167                 PMDIN1 = *pcString;\r
168                 pcString++;\r
169                 vTaskDelay( lcdSHORT_DELAY );\r
170         }\r
171 }\r
172 /*-----------------------------------------------------------*/\r
173 \r
174 static void prvLCDClear( void )\r
175 {\r
176         /* Clear the display. */\r
177         PMADDR = 0x0000;\r
178         PMDIN1 = lcdCLEAR_DISPLAY;\r
179         vTaskDelay( lcdSHORT_DELAY );   \r
180 }\r
181 /*-----------------------------------------------------------*/\r
182 \r
183 static void prvSetupLCD( void )\r
184 {\r
185         /* Setup the PMP. */\r
186         PMCON = 0x83BF;\r
187         PMMODE = 0x3FF;\r
188         PMAEN = 1;\r
189         PMADDR = 0x0000;\r
190         vTaskDelay( lcdSHORT_DELAY );\r
191 \r
192         /* Set the default function. */\r
193         PMDIN1 = lcdDEFAULT_FUNCTION;\r
194         vTaskDelay( lcdSHORT_DELAY );\r
195 \r
196         /* Set the display control. */\r
197         PMDIN1 = lcdDISPLAY_CONTROL;\r
198         vTaskDelay( lcdSHORT_DELAY );\r
199 \r
200         /* Clear the display. */\r
201         PMDIN1 = lcdCLEAR_DISPLAY;\r
202         vTaskDelay( lcdSHORT_DELAY );\r
203 \r
204         /* Set the entry mode. */\r
205         PMDIN1 = lcdENTRY_MODE;\r
206         vTaskDelay( lcdSHORT_DELAY );\r
207 }\r
208 /*-----------------------------------------------------------*/\r
209 \r
210 static void vLCDTask( void *pvParameters )\r
211 {\r
212 xLCDMessage xMessage;\r
213 unsigned short usRow = 0;\r
214 \r
215         /* Remove compiler warnigns. */\r
216         ( void ) pvParameters;\r
217 \r
218         /* Initialise the hardware.  This uses delays so must not be called prior\r
219         to the scheduler being started. */\r
220         prvSetupLCD();\r
221 \r
222         /* Welcome message. */\r
223         prvLCDPutString( "www.FreeRTOS.org" );\r
224 \r
225         for( ;; )\r
226         {\r
227                 /* Wait for a message to arrive that requires displaying. */\r
228                 while( xQueueReceive( xLCDQueue, &xMessage, portMAX_DELAY ) != pdPASS );\r
229 \r
230                 /* Clear the current display value. */\r
231                 prvLCDClear();\r
232 \r
233                 /* Switch rows each time so we can see that the display is still being\r
234                 updated. */\r
235                 prvLCDGotoRow( usRow & 0x01 );\r
236                 usRow++;\r
237                 prvLCDPutString( xMessage.pcMessage );\r
238 \r
239                 /* Delay the requested amount of time to ensure the text just written \r
240                 to the LCD is not overwritten. */\r
241                 vTaskDelay( xMessage.xMinDisplayTime );         \r
242         }\r
243 }\r
244 \r
245 \r
246 \r
247 \r