Subversion Repositories HomeAutomation

Rev

Details | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
363 johboh 1
/*********************************************************************
2
*
3
*       LCD Access Routines
4
*
5
*********************************************************************
6
* FileName:        LCDBlocking.c
7
* Dependencies:    Compiler.h
8
* Processor:       PIC24F
9
* Complier:        MCC30 v2.01 or higher
10
* Company:         Microchip Technology, Inc.
11
*
12
* Software License Agreement
13
*
14
 * This software is owned by Microchip Technology Inc. ("Microchip")
15
 * and is supplied to you for use exclusively as described in the
16
 * associated software agreement.  This software is protected by
17
 * software and other intellectual property laws.  Any use in
18
 * violation of the software license may subject the user to criminal
19
 * sanctions as well as civil liability.  Copyright 2006 Microchip
20
 * Technology Inc.  All rights reserved.
21
 *
22
 * This software is provided "AS IS."  MICROCHIP DISCLAIMS ALL
23
 * WARRANTIES, EXPRESS, IMPLIED, STATUTORY OR OTHERWISE, NOT LIMITED
24
 * TO MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND
25
 * INFRINGEMENT.  Microchip shall in no event be liable for special,
26
 * incidental, or consequential damages.
27
*
28
*
29
* Author               Date         Comment
30
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
31
* Howard Schlunder     4/03/06      Original
32
* Howard Schlunder     4/12/06      Changed from using PMP to LCDWrite()
33
* Howard Schlunder     8/10/06      Fixed a delay being too short
34
*                                   when CLOCK_FREQ was a smaller
35
*                                   value, added FOUR_BIT_MODE
36
********************************************************************/
37
 
38
#include "..\Include\StackTsk.h"
39
#include "..\Include\Compiler.h"
40
#include "..\Include\Delay.h"
41
 
42
// Do not include this source file if there is no LCD on the 
43
// target board
44
#ifdef USE_LCD
45
 
46
#if defined(PICDEMNET)
47
#define FOUR_BIT_MODE
48
#endif
49
 
50
// LCDText is a 32 byte shadow of the LCD text.  Write to it and 
51
// then call LCDUpdate() to copy the string into the LCD module.
52
BYTE LCDText[16*2+1];
53
 
54
/******************************************************************************
55
 * Function:        static void LCDWrite(BYTE RS, BYTE Data)
56
 *
57
 * PreCondition:    None
58
 *
59
 * Input:           RS - Register Select - 1:RAM, 0:Config registers
60
 *                  Data - 8 bits of data to write
61
 *
62
 * Output:          None
63
 *
64
 * Side Effects:    None
65
 *
66
 * Overview:        Controls the Port I/O pins to cause an LCD write
67
 *
68
 * Note:            None
69
 *****************************************************************************/
70
static void LCDWrite(BYTE RS, BYTE Data)
71
{
72
    LCD_DATA_TRIS = 0x00;
73
    LCD_RS_TRIS = 0;
74
    LCD_RD_WR_TRIS = 0;
75
    LCD_RD_WR_IO = 0;
76
    LCD_RS_IO = RS;
77
 
78
#if defined(FOUR_BIT_MODE)
79
    LCD_DATA_IO = Data>>4;
80
    Nop();                  // Wait Data setup time (min 40ns)
81
    Nop();
82
    LCD_E_IO = 1;
83
    Nop();                  // Wait E Pulse width time (min 230ns)
84
    Nop();
85
    Nop();
86
    Nop();
87
    Nop();
88
    Nop();
89
    Nop();
90
    Nop();
91
    Nop();
92
    LCD_E_IO = 0;
93
#endif
94
 
95
    LCD_DATA_IO = Data;
96
    Nop();                  // Wait Data setup time (min 40ns)
97
    Nop();
98
    LCD_E_IO = 1;
99
    Nop();                  // Wait E Pulse width time (min 230ns)
100
    Nop();
101
    Nop();
102
    Nop();
103
    Nop();
104
    Nop();
105
    Nop();
106
    Nop();
107
    Nop();
108
    LCD_E_IO = 0;
109
 
110
    LCD_DATA_TRIS = 0xFF;
111
    LCD_RS_TRIS = 1;
112
    LCD_RD_WR_TRIS = 1;
113
}
114
 
115
 
116
/******************************************************************************
117
 * Function:        void LCDInit(void)
118
 *
119
 * PreCondition:    None
120
 *
121
 * Input:           None
122
 *
123
 * Output:          None
124
 *
125
 * Side Effects:    None
126
 *
127
 * Overview:        LCDText[] is blanked, port I/O pin TRIS registers are
128
 *                  configured, and the LCD is placed in the default state
129
 *
130
 * Note:            None
131
 *****************************************************************************/
132
void LCDInit(void)
133
{
134
    BYTE i;
135
 
136
    for(i = 0; i < sizeof(LCDText)-1; i++)
137
    {
138
        LCDText[i] = ' ';
139
    }
140
    LCDText[sizeof(LCDText)-1] = 0;
141
 
142
    // Setup the I/O pins
143
    LCD_E_IO = 0;
144
    LCD_RD_WR_IO = 0;
145
 
146
    LCD_DATA_TRIS = 0x00;
147
    LCD_RD_WR_TRIS = 0;
148
    LCD_RS_TRIS = 0;
149
    LCD_E_TRIS = 0;
150
 
151
 
152
    // Wait the required time for the LCD to reset
153
    DelayMs(30);
154
 
155
    // Set the default function
156
#if defined(FOUR_BIT_MODE)
157
    LCDWrite(0, 0b00101000);
158
#else
159
    LCDWrite(0, 0b00111000);
160
#endif
161
    Delay10us(5);
162
 
163
    // Set the display control
164
    LCDWrite(0, 0b00001100);
165
    Delay10us(5);
166
 
167
    // Clear the display
168
    LCDWrite(0, 0b00000001);
169
    Delay10us(153);
170
 
171
    // Set the entry mode
172
    LCDWrite(0, 0b00000110);
173
    Delay10us(5);
174
}
175
 
176
 
177
/******************************************************************************
178
 * Function:        void LCDUpdate(void)
179
 *
180
 * PreCondition:    LCDInit() must have been called once
181
 *
182
 * Input:           LCDText[]
183
 *
184
 * Output:          None
185
 *
186
 * Side Effects:    None
187
 *
188
 * Overview:        Copies the contents of the local LCDText[] array into the
189
 *                  LCD's internal display buffer.  Null terminators in
190
 *                  LCDText[] terminate the current line, so strings may be
191
 *                  printed directly to LCDText[].
192
 *
193
 * Note:            None
194
 *****************************************************************************/
195
void LCDUpdate(void)
196
{
197
    BYTE i, j;
198
 
199
    // Go home
200
    LCDWrite(0, 0b00000010);
201
    DelayMs(1);
202
    Delay10us(53);
203
 
204
    // Output first line
205
    for(i = 0; i < 16; i++)
206
    {
207
        // Erase the rest of the line if a null char is 
208
        // encountered (good for printing strings directly)
209
        if(LCDText[i] == 0)
210
        {
211
            for(j=i; j < 16; j++)
212
            {
213
                LCDText[j] = ' ';
214
            }
215
        }
216
        LCDWrite(1, LCDText[i]);
217
        Delay10us(5);
218
    }
219
 
220
    // Set the address to the second line
221
    LCDWrite(0, 0xC0);
222
    Delay10us(5);
223
 
224
    // Output second line
225
    for(i = 16; i < 32; i++)
226
    {
227
        // Erase the rest of the line if a null char is 
228
        // encountered (good for printing strings directly)
229
        if(LCDText[i] == 0)
230
        {
231
            for(j=i; j < 32; j++)
232
            {
233
                LCDText[j] = ' ';
234
            }
235
        }
236
        LCDWrite(1, LCDText[i]);
237
        Delay10us(5);
238
    }
239
}
240
 
241
/******************************************************************************
242
 * Function:        void LCDErase(void)
243
 *
244
 * PreCondition:    LCDInit() must have been called once
245
 *
246
 * Input:           None
247
 *
248
 * Output:          None
249
 *
250
 * Side Effects:    None
251
 *
252
 * Overview:        Clears LCDText[] and the LCD's internal display buffer
253
 *
254
 * Note:            None
255
 *****************************************************************************/
256
void LCDErase(void)
257
{
258
    // Clear display
259
    LCDWrite(0, 0b00000001);
260
    Delay10us(153);
261
 
262
    // Clear local copy
263
    memset(LCDText, ' ', 32);
264
}
265
 
266
#endif  //#ifdef USE_LCD