OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [freertos-6.1.1/] [Demo/] [dsPIC_MPLAB/] [lcd.c] - Blame information for rev 800

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 591 jeremybenn
/*
2
    FreeRTOS V6.1.1 - Copyright (C) 2011 Real Time Engineers Ltd.
3
 
4
    ***************************************************************************
5
    *                                                                         *
6
    * If you are:                                                             *
7
    *                                                                         *
8
    *    + New to FreeRTOS,                                                   *
9
    *    + Wanting to learn FreeRTOS or multitasking in general quickly       *
10
    *    + Looking for basic training,                                        *
11
    *    + Wanting to improve your FreeRTOS skills and productivity           *
12
    *                                                                         *
13
    * then take a look at the FreeRTOS books - available as PDF or paperback  *
14
    *                                                                         *
15
    *        "Using the FreeRTOS Real Time Kernel - a Practical Guide"        *
16
    *                  http://www.FreeRTOS.org/Documentation                  *
17
    *                                                                         *
18
    * A pdf reference manual is also available.  Both are usually delivered   *
19
    * to your inbox within 20 minutes to two hours when purchased between 8am *
20
    * and 8pm GMT (although please allow up to 24 hours in case of            *
21
    * exceptional circumstances).  Thank you for your support!                *
22
    *                                                                         *
23
    ***************************************************************************
24
 
25
    This file is part of the FreeRTOS distribution.
26
 
27
    FreeRTOS is free software; you can redistribute it and/or modify it under
28
    the terms of the GNU General Public License (version 2) as published by the
29
    Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
30
    ***NOTE*** The exception to the GPL is included to allow you to distribute
31
    a combined work that includes FreeRTOS without being obliged to provide the
32
    source code for proprietary components outside of the FreeRTOS kernel.
33
    FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
34
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
35
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
36
    more details. You should have received a copy of the GNU General Public
37
    License and the FreeRTOS license exception along with FreeRTOS; if not it
38
    can be viewed here: http://www.freertos.org/a00114.html and also obtained
39
    by writing to Richard Barry, contact details for whom are available on the
40
    FreeRTOS WEB site.
41
 
42
    1 tab == 4 spaces!
43
 
44
    http://www.FreeRTOS.org - Documentation, latest information, license and
45
    contact details.
46
 
47
    http://www.SafeRTOS.com - A version that is certified for use in safety
48
    critical systems.
49
 
50
    http://www.OpenRTOS.com - Commercial support, development, porting,
51
    licensing and training services.
52
*/
53
 
54
/* Scheduler includes. */
55
#include "FreeRTOS.h"
56
#include "task.h"
57
#include "queue.h"
58
 
59
/* Demo includes. */
60
#include "lcd.h"
61
 
62
/*
63
 * The LCD is written to by more than one task so is controlled by this
64
 * 'gatekeeper' task.  This is the only task that is actually permitted to
65
 * access the LCD directly.  Other tasks wanting to display a message send
66
 * the message to the gatekeeper.
67
 */
68
static void vLCDTask( void *pvParameters );
69
 
70
/*
71
 * Setup the peripherals required to communicate with the LCD.
72
 */
73
static void prvSetupLCD( void );
74
 
75
/*
76
 * Move to the first (0) or second (1) row of the LCD.
77
 */
78
static void prvLCDGotoRow( unsigned portSHORT usRow );
79
 
80
/*
81
 * Write a string of text to the LCD.
82
 */
83
static void prvLCDPutString( portCHAR *pcString );
84
 
85
/*
86
 * Clear the LCD.
87
 */
88
static void prvLCDClear( void );
89
 
90
/*-----------------------------------------------------------*/
91
 
92
/* Brief delay to permit the LCD to catch up with commands. */
93
#define lcdVERY_SHORT_DELAY     ( 1 )
94
#define lcdSHORT_DELAY          ( 4 / portTICK_RATE_MS )
95
#define lcdLONG_DELAY           ( 15 / portTICK_RATE_MS )
96
 
97
/* LCD commands. */
98
#define lcdCLEAR                        ( 0x01 )
99
#define lcdHOME                         ( 0x02 )
100
#define lcdLINE2                        ( 0xc0 )
101
 
102
/* SFR that seems to be missing from the standard header files. */
103
#define PMAEN                           *( ( unsigned short * ) 0x60c )
104
 
105
/* LCD R/W signal. */
106
#define  lcdRW  LATDbits.LATD5       
107
 
108
/* LCD lcdRS signal. */
109
#define  lcdRS  LATBbits.LATB15      
110
 
111
/* LCD lcdE signal . */
112
#define  lcdE   LATDbits.LATD4       
113
 
114
/* Control signal pin direction. */
115
#define  RW_TRIS        TRISDbits.TRISD5 
116
#define  RS_TRIS        TRISBbits.TRISB15
117
#define  E_TRIS         TRISDbits.TRISD4
118
 
119
/* Port for LCD data */
120
#define  lcdDATA      LATE           
121
#define  lcdDATAPORT  PORTE
122
 
123
/* I/O setup for data Port. */
124
#define  TRISDATA  TRISE          
125
 
126
/* The length of the queue used to send messages to the LCD gatekeeper task. */
127
#define lcdQUEUE_SIZE           3
128
/*-----------------------------------------------------------*/
129
 
130
/* The queue used to send messages to the LCD task. */
131
xQueueHandle xLCDQueue;
132
 
133
static void prvLCDCommand( portCHAR cCommand );
134
static void prvLCDData( portCHAR cChar );
135
 
136
/*-----------------------------------------------------------*/
137
 
138
xQueueHandle xStartLCDTask( void )
139
{
140
        /* Create the queue used by the LCD task.  Messages for display on the LCD
141
        are received via this queue. */
142
        xLCDQueue = xQueueCreate( lcdQUEUE_SIZE, sizeof( xLCDMessage ) );
143
 
144
        /* Start the task that will write to the LCD.  The LCD hardware is
145
        initialised from within the task itself so delays can be used. */
146
        xTaskCreate( vLCDTask, ( signed portCHAR * ) "LCD", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 1, NULL );
147
 
148
        return xLCDQueue;
149
}
150
/*-----------------------------------------------------------*/
151
 
152
static void prvLCDGotoRow( unsigned portSHORT usRow )
153
{
154
        if( usRow == 0 )
155
        {
156
                prvLCDCommand( lcdHOME );
157
        }
158
        else
159
        {
160
                prvLCDCommand( lcdLINE2 );
161
        }
162
}
163
/*-----------------------------------------------------------*/
164
 
165
static void prvLCDCommand( portCHAR cCommand )
166
{
167
        /* Prepare RD0 - RD7. */
168
        lcdDATA &= 0xFF00;
169
 
170
        /* Command byte to lcd. */
171
    lcdDATA |= cCommand;
172
 
173
        /* Ensure lcdRW is 0. */
174
        lcdRW = 0;
175
    lcdRS = 0;
176
 
177
        /* Toggle lcdE line. */
178
    lcdE = 1;
179
    vTaskDelay( lcdVERY_SHORT_DELAY );
180
    lcdE = 0;
181
 
182
        vTaskDelay( lcdSHORT_DELAY );
183
}
184
/*-----------------------------------------------------------*/
185
 
186
static void prvLCDData( portCHAR cChar )
187
{
188
        /* ensure lcdRW is 0. */
189
        lcdRW = 0;
190
 
191
        /* Assert register select to 1. */
192
    lcdRS = 1;
193
 
194
        /* Prepare RD0 - RD7. */
195
        lcdDATA &= 0xFF00;
196
 
197
        /* Data byte to lcd. */
198
    lcdDATA |= cChar;
199
    lcdE = 1;
200
        Nop();
201
    Nop();
202
    Nop();
203
 
204
        /* Toggle lcdE signal. */
205
    lcdE = 0;
206
 
207
        /* Negate register select to 0. */
208
    lcdRS = 0;
209
 
210
        vTaskDelay( lcdVERY_SHORT_DELAY );
211
}
212
/*-----------------------------------------------------------*/
213
 
214
static void prvLCDPutString( portCHAR *pcString )
215
{
216
        /* Write out each character with appropriate delay between each. */
217
        while( *pcString )
218
        {
219
                prvLCDData( *pcString );
220
                pcString++;
221
                vTaskDelay( lcdSHORT_DELAY );
222
        }
223
}
224
/*-----------------------------------------------------------*/
225
 
226
static void prvLCDClear( void )
227
{
228
        prvLCDCommand( lcdCLEAR );
229
}
230
/*-----------------------------------------------------------*/
231
 
232
static void prvSetupLCD( void )
233
{
234
        /* Wait for proper power up. */
235
        vTaskDelay( lcdLONG_DELAY );
236
 
237
        /* Set initial states for the data and control pins */
238
        LATE &= 0xFF00;
239
 
240
        /* R/W state set low. */
241
    lcdRW = 0;
242
 
243
        /* lcdRS state set low. */
244
        lcdRS = 0;
245
 
246
        /* lcdE state set low. */
247
        lcdE = 0;
248
 
249
        /* Set data and control pins to outputs */
250
        TRISE &= 0xFF00;
251
 
252
        /* lcdRW pin set as output. */
253
        RW_TRIS = 0;
254
 
255
        /* lcdRS pin set as output. */
256
        RS_TRIS = 0;
257
 
258
        /* lcdE pin set as output. */
259
        E_TRIS = 0;
260
 
261
        /* 1st LCD initialization sequence */
262
        lcdDATA &= 0xFF00;
263
    lcdDATA |= 0x0038;
264
    lcdE = 1;
265
    Nop();
266
    Nop();
267
    Nop();
268
 
269
        /* Toggle lcdE signal. */
270
    lcdE = 0;
271
 
272
        vTaskDelay( lcdSHORT_DELAY );
273
        vTaskDelay( lcdSHORT_DELAY );
274
        vTaskDelay( lcdSHORT_DELAY );
275
 
276
        /* 2nd LCD initialization sequence */
277
        lcdDATA &= 0xFF00;
278
    lcdDATA |= 0x0038;
279
    lcdE = 1;
280
    Nop();
281
    Nop();
282
    Nop();
283
 
284
        /* Toggle lcdE signal. */
285
    lcdE = 0;
286
 
287
    vTaskDelay( lcdSHORT_DELAY );
288
 
289
        /* 3rd LCD initialization sequence */
290
        lcdDATA &= 0xFF00;
291
    lcdDATA |= 0x0038;
292
    lcdE = 1;
293
    Nop();
294
    Nop();
295
    Nop();
296
 
297
        /* Toggle lcdE signal. */
298
    lcdE = 0;
299
 
300
        vTaskDelay( lcdSHORT_DELAY );
301
 
302
 
303
        /* Function set. */
304
    prvLCDCommand( 0x38 );
305
 
306
        /* Display on/off control, cursor blink off (0x0C). */
307
    prvLCDCommand( 0x0C );
308
 
309
        /* Entry mode set (0x06). */
310
    prvLCDCommand( 0x06 );
311
 
312
        prvLCDCommand( lcdCLEAR );
313
}
314
/*-----------------------------------------------------------*/
315
 
316
static void vLCDTask( void *pvParameters )
317
{
318
xLCDMessage xMessage;
319
unsigned portSHORT usRow = 0;
320
 
321
        /* Initialise the hardware.  This uses delays so must not be called prior
322
        to the scheduler being started. */
323
        prvSetupLCD();
324
 
325
        /* Welcome message. */
326
        prvLCDPutString( "www.FreeRTOS.org" );
327
 
328
        for( ;; )
329
        {
330
                /* Wait for a message to arrive that requires displaying. */
331
                while( xQueueReceive( xLCDQueue, &xMessage, portMAX_DELAY ) != pdPASS );
332
 
333
                /* Clear the current display value. */
334
                prvLCDClear();
335
 
336
                /* Switch rows each time so we can see that the display is still being
337
                updated. */
338
                prvLCDGotoRow( usRow & 0x01 );
339
                usRow++;
340
                prvLCDPutString( xMessage.pcMessage );
341
 
342
                /* Delay the requested amount of time to ensure the text just written
343
                to the LCD is not overwritten. */
344
                vTaskDelay( xMessage.xMinDisplayTime );
345
        }
346
}
347
 
348
 
349
 
350
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.