2 FreeRTOS V5.4.0 - Copyright (C) 2003-2009 Richard Barry.
\r
4 This file is part of the FreeRTOS distribution.
\r
6 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
7 the terms of the GNU General Public License (version 2) as published by the
\r
8 Free Software Foundation and modified by the FreeRTOS exception.
\r
9 **NOTE** The exception to the GPL is included to allow you to distribute a
\r
10 combined work that includes FreeRTOS without being obliged to provide the
\r
11 source code for proprietary components outside of the FreeRTOS kernel.
\r
12 Alternative commercial license and support terms are also available upon
\r
13 request. See the licensing section of http://www.FreeRTOS.org for full
\r
16 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
\r
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
21 You should have received a copy of the GNU General Public License along
\r
22 with FreeRTOS; if not, write to the Free Software Foundation, Inc., 59
\r
23 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
\r
26 ***************************************************************************
\r
28 * Looking for a quick start? Then check out the FreeRTOS eBook! *
\r
29 * See http://www.FreeRTOS.org/Documentation for details *
\r
31 ***************************************************************************
\r
35 Please ensure to read the configuration and relevant port sections of the
\r
36 online documentation.
\r
38 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
41 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
44 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
45 licensing and training services.
\r
48 /* Scheduler includes. */
\r
49 #include "FreeRTOS.h"
\r
53 /* Demo includes. */
\r
57 * The LCD is written to by more than one task so is controlled by this
\r
58 * 'gatekeeper' task. This is the only task that is actually permitted to
\r
59 * access the LCD directly. Other tasks wanting to display a message send
\r
60 * the message to the gatekeeper.
\r
62 static void vLCDTask( void *pvParameters );
\r
65 * Setup the peripherals required to communicate with the LCD.
\r
67 static void prvSetupLCD( void );
\r
70 * Move to the first (0) or second (1) row of the LCD.
\r
72 static void prvLCDGotoRow( unsigned portSHORT usRow );
\r
75 * Write a string of text to the LCD.
\r
77 static void prvLCDPutString( portCHAR *pcString );
\r
82 static void prvLCDClear( void );
\r
84 /*-----------------------------------------------------------*/
\r
86 /* Brief delay to permit the LCD to catch up with commands. */
\r
87 #define lcdVERY_SHORT_DELAY ( 1 )
\r
88 #define lcdSHORT_DELAY ( 4 / portTICK_RATE_MS )
\r
89 #define lcdLONG_DELAY ( 15 / portTICK_RATE_MS )
\r
92 #define lcdCLEAR ( 0x01 )
\r
93 #define lcdHOME ( 0x02 )
\r
94 #define lcdLINE2 ( 0xc0 )
\r
96 /* SFR that seems to be missing from the standard header files. */
\r
97 #define PMAEN *( ( unsigned short * ) 0x60c )
\r
99 /* LCD R/W signal. */
\r
100 #define lcdRW LATDbits.LATD5
\r
102 /* LCD lcdRS signal. */
\r
103 #define lcdRS LATBbits.LATB15
\r
105 /* LCD lcdE signal . */
\r
106 #define lcdE LATDbits.LATD4
\r
108 /* Control signal pin direction. */
\r
109 #define RW_TRIS TRISDbits.TRISD5
\r
110 #define RS_TRIS TRISBbits.TRISB15
\r
111 #define E_TRIS TRISDbits.TRISD4
\r
113 /* Port for LCD data */
\r
114 #define lcdDATA LATE
\r
115 #define lcdDATAPORT PORTE
\r
117 /* I/O setup for data Port. */
\r
118 #define TRISDATA TRISE
\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
124 /* The queue used to send messages to the LCD task. */
\r
125 xQueueHandle xLCDQueue;
\r
127 static void prvLCDCommand( portCHAR cCommand );
\r
128 static void prvLCDData( portCHAR cChar );
\r
130 /*-----------------------------------------------------------*/
\r
132 xQueueHandle xStartLCDTask( void )
\r
134 /* Create the queue used by the LCD task. Messages for display on the LCD
\r
135 are received via this queue. */
\r
136 xLCDQueue = xQueueCreate( lcdQUEUE_SIZE, sizeof( xLCDMessage ) );
\r
138 /* Start the task that will write to the LCD. The LCD hardware is
\r
139 initialised from within the task itself so delays can be used. */
\r
140 xTaskCreate( vLCDTask, ( signed portCHAR * ) "LCD", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 1, NULL );
\r
144 /*-----------------------------------------------------------*/
\r
146 static void prvLCDGotoRow( unsigned portSHORT usRow )
\r
150 prvLCDCommand( lcdHOME );
\r
154 prvLCDCommand( lcdLINE2 );
\r
157 /*-----------------------------------------------------------*/
\r
159 static void prvLCDCommand( portCHAR cCommand )
\r
161 /* Prepare RD0 - RD7. */
\r
162 lcdDATA &= 0xFF00;
\r
164 /* Command byte to lcd. */
\r
165 lcdDATA |= cCommand;
\r
167 /* Ensure lcdRW is 0. */
\r
171 /* Toggle lcdE line. */
\r
173 vTaskDelay( lcdVERY_SHORT_DELAY );
\r
176 vTaskDelay( lcdSHORT_DELAY );
\r
178 /*-----------------------------------------------------------*/
\r
180 static void prvLCDData( portCHAR cChar )
\r
182 /* ensure lcdRW is 0. */
\r
185 /* Assert register select to 1. */
\r
188 /* Prepare RD0 - RD7. */
\r
189 lcdDATA &= 0xFF00;
\r
191 /* Data byte to lcd. */
\r
198 /* Toggle lcdE signal. */
\r
201 /* Negate register select to 0. */
\r
204 vTaskDelay( lcdVERY_SHORT_DELAY );
\r
206 /*-----------------------------------------------------------*/
\r
208 static void prvLCDPutString( portCHAR *pcString )
\r
210 /* Write out each character with appropriate delay between each. */
\r
213 prvLCDData( *pcString );
\r
215 vTaskDelay( lcdSHORT_DELAY );
\r
218 /*-----------------------------------------------------------*/
\r
220 static void prvLCDClear( void )
\r
222 prvLCDCommand( lcdCLEAR );
\r
224 /*-----------------------------------------------------------*/
\r
226 static void prvSetupLCD( void )
\r
228 /* Wait for proper power up. */
\r
229 vTaskDelay( lcdLONG_DELAY );
\r
231 /* Set initial states for the data and control pins */
\r
234 /* R/W state set low. */
\r
237 /* lcdRS state set low. */
\r
240 /* lcdE state set low. */
\r
243 /* Set data and control pins to outputs */
\r
246 /* lcdRW pin set as output. */
\r
249 /* lcdRS pin set as output. */
\r
252 /* lcdE pin set as output. */
\r
255 /* 1st LCD initialization sequence */
\r
263 /* Toggle lcdE signal. */
\r
266 vTaskDelay( lcdSHORT_DELAY );
\r
267 vTaskDelay( lcdSHORT_DELAY );
\r
268 vTaskDelay( lcdSHORT_DELAY );
\r
270 /* 2nd LCD initialization sequence */
\r
278 /* Toggle lcdE signal. */
\r
281 vTaskDelay( lcdSHORT_DELAY );
\r
283 /* 3rd LCD initialization sequence */
\r
291 /* Toggle lcdE signal. */
\r
294 vTaskDelay( lcdSHORT_DELAY );
\r
297 /* Function set. */
\r
298 prvLCDCommand( 0x38 );
\r
300 /* Display on/off control, cursor blink off (0x0C). */
\r
301 prvLCDCommand( 0x0C );
\r
303 /* Entry mode set (0x06). */
\r
304 prvLCDCommand( 0x06 );
\r
306 prvLCDCommand( lcdCLEAR );
\r
308 /*-----------------------------------------------------------*/
\r
310 static void vLCDTask( void *pvParameters )
\r
312 xLCDMessage xMessage;
\r
313 unsigned portSHORT usRow = 0;
\r
315 /* Initialise the hardware. This uses delays so must not be called prior
\r
316 to the scheduler being started. */
\r
319 /* Welcome message. */
\r
320 prvLCDPutString( "www.FreeRTOS.org" );
\r
324 /* Wait for a message to arrive that requires displaying. */
\r
325 while( xQueueReceive( xLCDQueue, &xMessage, portMAX_DELAY ) != pdPASS );
\r
327 /* Clear the current display value. */
\r
330 /* Switch rows each time so we can see that the display is still being
\r
332 prvLCDGotoRow( usRow & 0x01 );
\r
334 prvLCDPutString( xMessage.pcMessage );
\r
336 /* Delay the requested amount of time to ensure the text just written
\r
337 to the LCD is not overwritten. */
\r
338 vTaskDelay( xMessage.xMinDisplayTime );
\r