]> git.sur5r.net Git - freertos/blob - Demo/PIC24_MPLAB/lcd.c
Update to V4.3.0 as described in http://www.FreeRTOS.org/History.txt
[freertos] / Demo / PIC24_MPLAB / lcd.c
1 /*\r
2         FreeRTOS.org V4.3.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 lcdSHORT_DELAY          3\r
76 \r
77 /* SFR that seems to be missing from the standard header files. */\r
78 #define PMAEN                           *( ( unsigned short * ) 0x60c )\r
79 \r
80 /* LCD commands. */\r
81 #define lcdDEFAULT_FUNCTION     0x3c\r
82 #define lcdDISPLAY_CONTROL      0x0c\r
83 #define lcdCLEAR_DISPLAY        0x01\r
84 #define lcdENTRY_MODE           0x06\r
85 \r
86 /* The length of the queue used to send messages to the LCD gatekeeper task. */\r
87 #define lcdQUEUE_SIZE           3\r
88 /*-----------------------------------------------------------*/\r
89 \r
90 /* The queue used to send messages to the LCD task. */\r
91 xQueueHandle xLCDQueue;\r
92 \r
93 \r
94 /*-----------------------------------------------------------*/\r
95 \r
96 xQueueHandle xStartLCDTask( void )\r
97 {\r
98         /* Create the queue used by the LCD task.  Messages for display on the LCD\r
99         are received via this queue. */\r
100         xLCDQueue = xQueueCreate( lcdQUEUE_SIZE, sizeof( xLCDMessage ) );\r
101 \r
102         /* Start the task that will write to the LCD.  The LCD hardware is\r
103         initialised from within the task itself so delays can be used. */\r
104         xTaskCreate( vLCDTask, ( signed portCHAR * ) "LCD", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 1, NULL );\r
105 \r
106         return xLCDQueue;\r
107 }\r
108 /*-----------------------------------------------------------*/\r
109 \r
110 static void prvLCDGotoRow( unsigned portSHORT usRow )\r
111 {\r
112         if( usRow == 0 )\r
113         {\r
114                 PMADDR = 0x0000;\r
115                 PMDIN1 = 0x02;\r
116         }\r
117         else\r
118         {\r
119                 PMADDR = 0x0000;\r
120                 PMDIN1 = 0xc0;\r
121         }\r
122 \r
123         vTaskDelay( lcdSHORT_DELAY );\r
124 }\r
125 /*-----------------------------------------------------------*/\r
126 \r
127 static void prvLCDPutString( portCHAR *pcString )\r
128 {\r
129         /* Write out each character with appropriate delay between each. */\r
130         while( *pcString )\r
131         {\r
132                 PMADDR = 0x0001;\r
133                 PMDIN1 = *pcString;\r
134                 pcString++;\r
135                 vTaskDelay( lcdSHORT_DELAY );\r
136         }\r
137 }\r
138 /*-----------------------------------------------------------*/\r
139 \r
140 static void prvLCDClear( void )\r
141 {\r
142         /* Clear the display. */\r
143         PMADDR = 0x0000;\r
144         PMDIN1 = lcdCLEAR_DISPLAY;\r
145         vTaskDelay( lcdSHORT_DELAY );   \r
146 }\r
147 /*-----------------------------------------------------------*/\r
148 \r
149 static void prvSetupLCD( void )\r
150 {\r
151         /* Setup the PMP. */\r
152         PMCON = 0x83BF;\r
153         PMMODE = 0x3FF;\r
154         PMAEN = 1;\r
155         PMADDR = 0x0000;\r
156         vTaskDelay( lcdSHORT_DELAY );\r
157 \r
158         /* Set the default function. */\r
159         PMDIN1 = lcdDEFAULT_FUNCTION;\r
160         vTaskDelay( lcdSHORT_DELAY );\r
161 \r
162         /* Set the display control. */\r
163         PMDIN1 = lcdDISPLAY_CONTROL;\r
164         vTaskDelay( lcdSHORT_DELAY );\r
165 \r
166         /* Clear the display. */\r
167         PMDIN1 = lcdCLEAR_DISPLAY;\r
168         vTaskDelay( lcdSHORT_DELAY );\r
169 \r
170         /* Set the entry mode. */\r
171         PMDIN1 = lcdENTRY_MODE;\r
172         vTaskDelay( lcdSHORT_DELAY );\r
173 }\r
174 /*-----------------------------------------------------------*/\r
175 \r
176 static void vLCDTask( void *pvParameters )\r
177 {\r
178 xLCDMessage xMessage;\r
179 unsigned portSHORT usRow = 0;\r
180 \r
181         /* Initialise the hardware.  This uses delays so must not be called prior\r
182         to the scheduler being started. */\r
183         prvSetupLCD();\r
184 \r
185         /* Welcome message. */\r
186         prvLCDPutString( "www.FreeRTOS.org" );\r
187 \r
188         for( ;; )\r
189         {\r
190                 /* Wait for a message to arrive that requires displaying. */\r
191                 while( xQueueReceive( xLCDQueue, &xMessage, portMAX_DELAY ) != pdPASS );\r
192 \r
193                 /* Clear the current display value. */\r
194                 prvLCDClear();\r
195 \r
196                 /* Switch rows each time so we can see that the display is still being\r
197                 updated. */\r
198                 prvLCDGotoRow( usRow & 0x01 );\r
199                 usRow++;\r
200                 prvLCDPutString( xMessage.pcMessage );\r
201 \r
202                 /* Delay the requested amount of time to ensure the text just written \r
203                 to the LCD is not overwritten. */\r
204                 vTaskDelay( xMessage.xMinDisplayTime );         \r
205         }\r
206 }\r
207 \r
208 \r
209 \r
210 \r