2 FreeRTOS.org V4.7.0 - 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 a version that has been certified for use
\r
32 in safety critical systems, plus commercial licensing, development and
\r
34 ***************************************************************************
\r
37 /* Scheduler includes. */
\r
38 #include "FreeRTOS.h"
\r
42 /* Demo includes. */
\r
46 * The LCD is written to by more than one task so is controlled by this
\r
47 * 'gatekeeper' task. This is the only task that is actually permitted to
\r
48 * access the LCD directly. Other tasks wanting to display a message send
\r
49 * the message to the gatekeeper.
\r
51 static void vLCDTask( void *pvParameters );
\r
54 * Setup the peripherals required to communicate with the LCD.
\r
56 static void prvSetupLCD( void );
\r
59 * Move to the first (0) or second (1) row of the LCD.
\r
61 static void prvLCDGotoRow( unsigned portSHORT usRow );
\r
64 * Write a string of text to the LCD.
\r
66 static void prvLCDPutString( portCHAR *pcString );
\r
71 static void prvLCDClear( void );
\r
73 /*-----------------------------------------------------------*/
\r
75 /* Brief delay to permit the LCD to catch up with commands. */
\r
76 #define lcdVERY_SHORT_DELAY ( 1 )
\r
77 #define lcdSHORT_DELAY ( 4 / portTICK_RATE_MS )
\r
78 #define lcdLONG_DELAY ( 15 / portTICK_RATE_MS )
\r
81 #define lcdCLEAR ( 0x01 )
\r
82 #define lcdHOME ( 0x02 )
\r
83 #define lcdLINE2 ( 0xc0 )
\r
85 /* SFR that seems to be missing from the standard header files. */
\r
86 #define PMAEN *( ( unsigned short * ) 0x60c )
\r
88 /* LCD R/W signal. */
\r
89 #define lcdRW LATDbits.LATD5
\r
91 /* LCD lcdRS signal. */
\r
92 #define lcdRS LATBbits.LATB15
\r
94 /* LCD lcdE signal . */
\r
95 #define lcdE LATDbits.LATD4
\r
97 /* Control signal pin direction. */
\r
98 #define RW_TRIS TRISDbits.TRISD5
\r
99 #define RS_TRIS TRISBbits.TRISB15
\r
100 #define E_TRIS TRISDbits.TRISD4
\r
102 /* Port for LCD data */
\r
103 #define lcdDATA LATE
\r
104 #define lcdDATAPORT PORTE
\r
106 /* I/O setup for data Port. */
\r
107 #define TRISDATA TRISE
\r
109 /* The length of the queue used to send messages to the LCD gatekeeper task. */
\r
110 #define lcdQUEUE_SIZE 3
\r
111 /*-----------------------------------------------------------*/
\r
113 /* The queue used to send messages to the LCD task. */
\r
114 xQueueHandle xLCDQueue;
\r
116 static void prvLCDCommand( portCHAR cCommand );
\r
117 static void prvLCDData( portCHAR cChar );
\r
119 /*-----------------------------------------------------------*/
\r
121 xQueueHandle xStartLCDTask( void )
\r
123 /* Create the queue used by the LCD task. Messages for display on the LCD
\r
124 are received via this queue. */
\r
125 xLCDQueue = xQueueCreate( lcdQUEUE_SIZE, sizeof( xLCDMessage ) );
\r
127 /* Start the task that will write to the LCD. The LCD hardware is
\r
128 initialised from within the task itself so delays can be used. */
\r
129 xTaskCreate( vLCDTask, ( signed portCHAR * ) "LCD", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 1, NULL );
\r
133 /*-----------------------------------------------------------*/
\r
135 static void prvLCDGotoRow( unsigned portSHORT usRow )
\r
139 prvLCDCommand( lcdHOME );
\r
143 prvLCDCommand( lcdLINE2 );
\r
146 /*-----------------------------------------------------------*/
\r
148 static void prvLCDCommand( portCHAR cCommand )
\r
150 /* Prepare RD0 - RD7. */
\r
151 lcdDATA &= 0xFF00;
\r
153 /* Command byte to lcd. */
\r
154 lcdDATA |= cCommand;
\r
156 /* Ensure lcdRW is 0. */
\r
160 /* Toggle lcdE line. */
\r
162 vTaskDelay( lcdVERY_SHORT_DELAY );
\r
165 vTaskDelay( lcdSHORT_DELAY );
\r
167 /*-----------------------------------------------------------*/
\r
169 static void prvLCDData( portCHAR cChar )
\r
171 /* ensure lcdRW is 0. */
\r
174 /* Assert register select to 1. */
\r
177 /* Prepare RD0 - RD7. */
\r
178 lcdDATA &= 0xFF00;
\r
180 /* Data byte to lcd. */
\r
187 /* Toggle lcdE signal. */
\r
190 /* Negate register select to 0. */
\r
193 vTaskDelay( lcdVERY_SHORT_DELAY );
\r
195 /*-----------------------------------------------------------*/
\r
197 static void prvLCDPutString( portCHAR *pcString )
\r
199 /* Write out each character with appropriate delay between each. */
\r
202 prvLCDData( *pcString );
\r
204 vTaskDelay( lcdSHORT_DELAY );
\r
207 /*-----------------------------------------------------------*/
\r
209 static void prvLCDClear( void )
\r
211 prvLCDCommand( lcdCLEAR );
\r
213 /*-----------------------------------------------------------*/
\r
215 static void prvSetupLCD( void )
\r
217 /* Wait for proper power up. */
\r
218 vTaskDelay( lcdLONG_DELAY );
\r
220 /* Set initial states for the data and control pins */
\r
223 /* R/W state set low. */
\r
226 /* lcdRS state set low. */
\r
229 /* lcdE state set low. */
\r
232 /* Set data and control pins to outputs */
\r
235 /* lcdRW pin set as output. */
\r
238 /* lcdRS pin set as output. */
\r
241 /* lcdE pin set as output. */
\r
244 /* 1st LCD initialization sequence */
\r
252 /* Toggle lcdE signal. */
\r
255 vTaskDelay( lcdSHORT_DELAY );
\r
256 vTaskDelay( lcdSHORT_DELAY );
\r
257 vTaskDelay( lcdSHORT_DELAY );
\r
259 /* 2nd LCD initialization sequence */
\r
267 /* Toggle lcdE signal. */
\r
270 vTaskDelay( lcdSHORT_DELAY );
\r
272 /* 3rd LCD initialization sequence */
\r
280 /* Toggle lcdE signal. */
\r
283 vTaskDelay( lcdSHORT_DELAY );
\r
286 /* Function set. */
\r
287 prvLCDCommand( 0x38 );
\r
289 /* Display on/off control, cursor blink off (0x0C). */
\r
290 prvLCDCommand( 0x0C );
\r
292 /* Entry mode set (0x06). */
\r
293 prvLCDCommand( 0x06 );
\r
295 prvLCDCommand( lcdCLEAR );
\r
297 /*-----------------------------------------------------------*/
\r
299 static void vLCDTask( void *pvParameters )
\r
301 xLCDMessage xMessage;
\r
302 unsigned portSHORT usRow = 0;
\r
304 /* Initialise the hardware. This uses delays so must not be called prior
\r
305 to the scheduler being started. */
\r
308 /* Welcome message. */
\r
309 prvLCDPutString( "www.FreeRTOS.org" );
\r
313 /* Wait for a message to arrive that requires displaying. */
\r
314 while( xQueueReceive( xLCDQueue, &xMessage, portMAX_DELAY ) != pdPASS );
\r
316 /* Clear the current display value. */
\r
319 /* Switch rows each time so we can see that the display is still being
\r
321 prvLCDGotoRow( usRow & 0x01 );
\r
323 prvLCDPutString( xMessage.pcMessage );
\r
325 /* Delay the requested amount of time to ensure the text just written
\r
326 to the LCD is not overwritten. */
\r
327 vTaskDelay( xMessage.xMinDisplayTime );
\r