2 FreeRTOS.org V4.3.1 - Copyright (C) 2003-2007 Richard Barry.
\r
4 This file is part of the FreeRTOS.org distribution.
\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
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
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
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
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
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
36 /* Scheduler includes. */
\r
37 #include "FreeRTOS.h"
\r
41 /* Demo includes. */
\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
50 static void vLCDTask( void *pvParameters );
\r
53 * Setup the peripherals required to communicate with the LCD.
\r
55 static void prvSetupLCD( void );
\r
58 * Move to the first (0) or second (1) row of the LCD.
\r
60 static void prvLCDGotoRow( unsigned portSHORT usRow );
\r
63 * Write a string of text to the LCD.
\r
65 static void prvLCDPutString( portCHAR *pcString );
\r
70 static void prvLCDClear( void );
\r
72 /*-----------------------------------------------------------*/
\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
80 #define lcdCLEAR ( 0x01 )
\r
81 #define lcdHOME ( 0x02 )
\r
82 #define lcdLINE2 ( 0xc0 )
\r
84 /* SFR that seems to be missing from the standard header files. */
\r
85 #define PMAEN *( ( unsigned short * ) 0x60c )
\r
87 /* LCD R/W signal. */
\r
88 #define lcdRW LATDbits.LATD5
\r
90 /* LCD lcdRS signal. */
\r
91 #define lcdRS LATBbits.LATB15
\r
93 /* LCD lcdE signal . */
\r
94 #define lcdE LATDbits.LATD4
\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
101 /* Port for LCD data */
\r
102 #define lcdDATA LATE
\r
103 #define lcdDATAPORT PORTE
\r
105 /* I/O setup for data Port. */
\r
106 #define TRISDATA TRISE
\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
112 /* The queue used to send messages to the LCD task. */
\r
113 xQueueHandle xLCDQueue;
\r
115 static void prvLCDCommand( portCHAR cCommand );
\r
116 static void prvLCDData( portCHAR cChar );
\r
118 /*-----------------------------------------------------------*/
\r
120 xQueueHandle xStartLCDTask( void )
\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
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
132 /*-----------------------------------------------------------*/
\r
134 static void prvLCDGotoRow( unsigned portSHORT usRow )
\r
138 prvLCDCommand( lcdHOME );
\r
142 prvLCDCommand( lcdLINE2 );
\r
145 /*-----------------------------------------------------------*/
\r
147 static void prvLCDCommand( portCHAR cCommand )
\r
149 /* Prepare RD0 - RD7. */
\r
150 lcdDATA &= 0xFF00;
\r
152 /* Command byte to lcd. */
\r
153 lcdDATA |= cCommand;
\r
155 /* Ensure lcdRW is 0. */
\r
159 /* Toggle lcdE line. */
\r
161 vTaskDelay( lcdVERY_SHORT_DELAY );
\r
164 vTaskDelay( lcdSHORT_DELAY );
\r
166 /*-----------------------------------------------------------*/
\r
168 static void prvLCDData( portCHAR cChar )
\r
170 /* ensure lcdRW is 0. */
\r
173 /* Assert register select to 1. */
\r
176 /* Prepare RD0 - RD7. */
\r
177 lcdDATA &= 0xFF00;
\r
179 /* Data byte to lcd. */
\r
186 /* Toggle lcdE signal. */
\r
189 /* Negate register select to 0. */
\r
192 vTaskDelay( lcdVERY_SHORT_DELAY );
\r
194 /*-----------------------------------------------------------*/
\r
196 static void prvLCDPutString( portCHAR *pcString )
\r
198 /* Write out each character with appropriate delay between each. */
\r
201 prvLCDData( *pcString );
\r
203 vTaskDelay( lcdSHORT_DELAY );
\r
206 /*-----------------------------------------------------------*/
\r
208 static void prvLCDClear( void )
\r
210 prvLCDCommand( lcdCLEAR );
\r
212 /*-----------------------------------------------------------*/
\r
214 static void prvSetupLCD( void )
\r
216 /* Wait for proper power up. */
\r
217 vTaskDelay( lcdLONG_DELAY );
\r
219 /* Set initial states for the data and control pins */
\r
222 /* R/W state set low. */
\r
225 /* lcdRS state set low. */
\r
228 /* lcdE state set low. */
\r
231 /* Set data and control pins to outputs */
\r
234 /* lcdRW pin set as output. */
\r
237 /* lcdRS pin set as output. */
\r
240 /* lcdE pin set as output. */
\r
243 /* 1st LCD initialization sequence */
\r
251 /* Toggle lcdE signal. */
\r
254 vTaskDelay( lcdSHORT_DELAY );
\r
255 vTaskDelay( lcdSHORT_DELAY );
\r
256 vTaskDelay( lcdSHORT_DELAY );
\r
258 /* 2nd LCD initialization sequence */
\r
266 /* Toggle lcdE signal. */
\r
269 vTaskDelay( lcdSHORT_DELAY );
\r
271 /* 3rd LCD initialization sequence */
\r
279 /* Toggle lcdE signal. */
\r
282 vTaskDelay( lcdSHORT_DELAY );
\r
285 /* Function set. */
\r
286 prvLCDCommand( 0x38 );
\r
288 /* Display on/off control, cursor blink off (0x0C). */
\r
289 prvLCDCommand( 0x0C );
\r
291 /* Entry mode set (0x06). */
\r
292 prvLCDCommand( 0x06 );
\r
294 prvLCDCommand( lcdCLEAR );
\r
296 /*-----------------------------------------------------------*/
\r
298 static void vLCDTask( void *pvParameters )
\r
300 xLCDMessage xMessage;
\r
301 unsigned portSHORT usRow = 0;
\r
303 /* Initialise the hardware. This uses delays so must not be called prior
\r
304 to the scheduler being started. */
\r
307 /* Welcome message. */
\r
308 prvLCDPutString( "www.FreeRTOS.org" );
\r
312 /* Wait for a message to arrive that requires displaying. */
\r
313 while( xQueueReceive( xLCDQueue, &xMessage, portMAX_DELAY ) != pdPASS );
\r
315 /* Clear the current display value. */
\r
318 /* Switch rows each time so we can see that the display is still being
\r
320 prvLCDGotoRow( usRow & 0x01 );
\r
322 prvLCDPutString( xMessage.pcMessage );
\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