-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlcdifTest.c
378 lines (339 loc) · 13.7 KB
/
lcdifTest.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/*******************************************************************************
*
* LCD INTERFACE MODULE TEST PROGRAM
*
*******************************************************************************/
/*******************************************************************************
*
* Tests the LCD Interface module's functionality
*
* Filename : lcdifTest.c
* Version : V0.01
* Programmer(s) : Stuart Cording aka CODINGHEAD
*
********************************************************************************
* Note(s) :
* V0.01 - First cut
*******************************************************************************/
/*******************************************************************************
* Commenting notes
* ???? Question(s) regarding implementation or design specification.
* $$$$ Future function that needs to be implemented.
* @@@@ Old code to leave as-is because ....
* #### Technical issue not (satisfactorily) resolved.
*******************************************************************************/
/*******************************************************************************
*
* LCD INTERFACE MODULE TEST PROGRAM
*
*******************************************************************************/
/*******************************************************************************
* INCLUDE FILES
*******************************************************************************/
/******************************************/
/* HI-TECH PICC / PICC-Lite compiler */
/******************************************/
#if defined(__PICC__) || defined(__PICCLITE__)
// PIC16 processor
#include <pic.h>
/******************************************/
/* HI-TECH PICC-18 compiler */
/******************************************/
#elif defined(__PICC18__)
// PIC18 processor
#include <pic18.h>
/******************************************/
/* HI-TECH dsPICC compiler */
/******************************************/
#elif defined(__DSPICC__)
// dsPIC or PIC24 processor
#include <dspic.h>
/******************************************/
/* HI-TECH C for PIC32 */
/******************************************/
#elif defined(__PICC32__)
#include <pic32.h>
/******************************************/
/* Microchip C18 compiler */
/******************************************/
#elif defined(__18CXX)
// PIC18 processor
#include <p18cxxx.h>
#include "..\lcdif_module\lcdif_c18.h"
#include "..\lcdif_module\pbif_c18.h"
#if defined(__18F4520)
#include "p18f4520configbits.h"
#else
#error Chosen PIC18 device is not supported
#endif
/******************************************/
/* Microchip C30 compiler */
/******************************************/
#elif defined(__PIC24F__)
// PIC24F processor
#include <p24Fxxxx.h>
/******************************************/
/* Microchip C30 compiler */
/******************************************/
#elif defined(__PIC24H__)
// PIC24H processor
#include <p24Hxxxx.h>
/******************************************/
/* Microchip C30 compiler */
/******************************************/
#elif defined(__dsPIC33F__)
// dsPIC33F processor
#include <p33Fxxxx.h>
/******************************************/
/* Microchip C30 compiler */
/******************************************/
#elif defined(__dsPIC30F__)
// dsPIC30F processor
#include <p30fxxxx.h>
/******************************************/
/* Microchip C32 compiler */
/******************************************/
#elif defined(__PIC32MX__)
#include <p32xxxx.h>
#include <plib.h>
#pragma config POSCMOD=XT, FNOSC=PRIPLL
#pragma config FPLLIDIV=DIV_2, FPLLMUL=MUL_20, FPLLODIV=DIV_1
#pragma config FPBDIV=DIV_2, FWDTEN=OFF, CP=OFF, BWP=OFF
#else
#error Unknown processor or compiler.
#endif
//#include "..\hd44780_module\hd44780.h"
/* #### Note: these defines need to */
/* later moved into the compiler */
/* specific defines above */
/* #### Note: need config bits! */
/* later moved into the compiler */
/* specific defines above */
/*******************************************************************************
* LOCAL DEFINES
*******************************************************************************/
#define WAITMS 91
/*******************************************************************************
* LOCAL CONSTANTS
*******************************************************************************/
/*******************************************************************************
* LOCAL DATA TYPES
*******************************************************************************/
/*******************************************************************************
* LOCAL TABLES
*******************************************************************************/
/*******************************************************************************
* LOCAL GLOBAL VARIABLES
*******************************************************************************/
/*******************************************************************************
* LOCAL FUNCTION PROTOTYPES
*******************************************************************************/
static void wait(unsigned int count);
/*******************************************************************************
* LOCAL CONFIGURATION ERRORS
*******************************************************************************/
#if !defined(PICDEM2PLUS_RED) && !defined(PICDEM2PLUS_GREEN)
#error No board specified! Please add one of the following preprocessor macros:\
- PICDEM2PLUS_RED - for old PICDEM 2 Plus Red Board \
- PICDEM2PLUS_GREEN - for newer PICDEM 2 Plus Green ROHS Board \
~\
For C18 select Project->Build Options...->Project, "MPLAB C18" tab and\
then in "Proprocessor Macros" click "Add..." button.\
~
#endif
/*******************************************************************************
* main()
*
* Description:
* Main application code
*
* See also:
*
* Arguments:
* void
*
* Returns:
* void
*
* Callers: C start-up code
*
* Notes :
*
*******************************************************************************/
#if defined(__18CXX)
void main (void)
#else
int main(void)
#endif
{
LCDIFNUM lcdIfOneNum;
LCDIFNUM lcdIfTwoNum;
HLCDIF hLcdIfOne;
HLCDIF hLcdIfTwo;
PBIFOBJ pbIf;
LCDIFOBJ lcdIfOneObj;
LCDIFOBJ lcdIfTwoObj;
unsigned char readData=0;
unsigned char readAddress=0;
/* Make RW, RS and E lines outputs */
#if defined(__18CXX)
/* Clear LATD register (functions as */
/* data lines on both boards too) */
LATD = 0;
#if defined (PICDEM2PLUS_GREEN)
/* On GREEN PICDEM 2 Plus these are: */
/* RD5 = RW */
/* RD4 = RS */
/* RD6 = E */
TRISDbits.TRISD5 = 0;
TRISDbits.TRISD4 = 0;
TRISDbits.TRISD6 = 0;
#elif defined(PICDEM2PLUS_RED)
/* On RED PICDEM 2 Plus these are: */
/* RA2 = RW */
/* RA3 = RS */
/* RA1 = E */
/* Make PORTA bits digital too and */
/* clear LATA register */
ADCON1 = 0x0F;
LATA = 0;
TRISAbits.TRISA2 = 0;
TRISAbits.TRISA3 = 0;
TRISAbits.TRISA1 = 0;
#endif
#endif
/* Init module */
lcdifInit();
/* Fill parallel bus interface struct */
#if defined(PICDEM2PLUS_GREEN)
pbIf.RW_LAT = &LATD;
pbIf.RW_BIT = (1 << 5);
pbIf.RS_LAT = &LATD;
pbIf.RS_BIT = (1 << 4);
#elif defined(PICDEM2PLUS_RED)
pbIf.RW_LAT = &LATA;
pbIf.RW_BIT = (1 << 2);
pbIf.RS_LAT = &LATA;
pbIf.RS_BIT = (1 << 3);
#endif
pbIf.DATA_LAT = &LATD;
pbIf.DATA_PORT = &PORTD;
pbIf.DATA_TRIS = &TRISD;
pbIf.DATA_MASK = 0x0F;
/* Fill lcd interface struct */
#if defined(__18CXX)
#if defined(PICDEM2PLUS_GREEN)
lcdIfOneObj.E_LAT = &LATD;
lcdIfOneObj.E_BIT = (1 << 6);
#elif defined(PICDEM2PLUS_RED)
lcdIfOneObj.E_LAT = &LATA;
lcdIfOneObj.E_BIT = (1 << 1);
#endif
#endif
lcdIfOneObj.pbIfObject = &pbIf;
lcdIfOneNum = lcdifCreate(&lcdIfOneObj);
hLcdIfOne = lcdifOpen(lcdIfOneNum);
#if defined(__18CXX)
/* Turn on the LCD display (uses RD7) */
#if defined(PICDEM2PLUS_GREEN)
TRISDbits.TRISD7 = 0;
LATDbits.LATD7 = 1;
#endif
#endif
lcdifGetPb(hLcdIfOne);
wait(3640); /* About 40ms */
lcdif4BitFunctionSet(hLcdIfOne, 0x02);
wait(374);
lcdif4BitFunctionSet(hLcdIfOne, 0x02);
wait(10);
lcdif4BitFunctionSet(hLcdIfOne, 0x02);
wait(4);
/* Set up 4-bit bus, 2 line display */
/* and 5 x 10 dot font */
lcdifWriteInstruction(hLcdIfOne, 0x2C);
wait(4); /* Clear display */
lcdifWriteInstruction(hLcdIfOne, 0x01);
wait(139); /* Set Display ON, Cursor ON, Blink */
/* ON */
lcdifWriteInstruction(hLcdIfOne, 0x0F);
wait(4); /* Write some text */
lcdifWriteData(hLcdIfOne, 'H');
wait(4);
lcdifWriteData(hLcdIfOne, 'e');
wait(4);
lcdifWriteData(hLcdIfOne, 'l');
wait(4);
lcdifWriteData(hLcdIfOne, 'l');
wait(4);
lcdifWriteData(hLcdIfOne, 'o');
wait(4);
lcdifWriteData(hLcdIfOne, '!');
wait(4);
lcdifWriteData(hLcdIfOne, ' ');
wait(4);
lcdifWriteData(hLcdIfOne, 0xC0);
wait(4);
lcdifWriteData(hLcdIfOne, 0xC1);
wait(4);
lcdifWriteData(hLcdIfOne, 0x00);
wait(4);
lcdifWriteInstruction(hLcdIfOne, 0x80);
wait(4);
lcdifReadData(hLcdIfOne, &readData);
wait(4);
lcdifReadData(hLcdIfOne, &readData);
wait(4);
lcdifWriteInstruction(hLcdIfOne, 0x40);
wait(4);
lcdifWriteData(hLcdIfOne, 0x01);
wait(4);
lcdifWriteData(hLcdIfOne, 0x03);
wait(4);
lcdifWriteData(hLcdIfOne, 0x07);
wait(4);
lcdifWriteData(hLcdIfOne, 0x0F);
wait(4);
lcdifWriteData(hLcdIfOne, 0x1F);
wait(4);
lcdifWriteData(hLcdIfOne, 0x3F);
wait(4);
lcdifReadAddress(hLcdIfOne, &readAddress);
wait(4);
lcdifWriteInstruction(hLcdIfOne, 0x80);
wait(4);
lcdifWriteData(hLcdIfOne, 'B');
wait(4);
lcdifReadAddress(hLcdIfOne, &readAddress);
wait(4);
lcdifWriteInstruction(hLcdIfOne, 0xE4);
wait(4);
lcdifReadAddress(hLcdIfOne, &readAddress);
wait(4);
lcdifWriteData(hLcdIfOne, 'H');
wait(4);
lcdifWriteInstruction(hLcdIfOne, 0xC6);
wait(4);
lcdifReadAddress(hLcdIfOne, &readAddress);
wait(4);
lcdifWriteData(hLcdIfOne, 'H');
wait(4);
lcdIfOneNum = lcdifClose(hLcdIfOne);
lcdifDestroy(lcdIfOneNum);
lcdifDeinit();
/* Congratulations! If we stop here all tests passed */
while(1);
}
void wait(unsigned int count)
{
/* count of 1 is approx 10.5us */
volatile unsigned int x;
for(x = 0; x < count; x++)
{
Nop();
}
}
/*******************************************************************************
*
* LCD INTERFACE MODULE TEST PROGRAM END
*
*******************************************************************************/