-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlcdif_c30.c
1312 lines (1249 loc) · 57.7 KB
/
lcdif_c30.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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************
*
* LCD INTERFACE MODULE FOR C30 COMPILER AND PIC24/dsPIC
*
*******************************************************************************/
/*******************************************************************************
*
* This module is used to provide the interface for an HD44780 LCD display, based
* upon the bit-banged GPIO based parallel bus provided the "pbif_<compiler>.h"
* module
*
* Filename : lcdif_c30.c
* Version : V0.01
* Programmer(s) : Stuart Cording aka. CODINGHEAD
*
********************************************************************************
* Note(s) :
* V0.01 - First cut
*
*******************************************************************************/
/*******************************************************************************
*
* LCDIFC30 MODULE
*
*******************************************************************************/
/*******************************************************************************
* INCLUDE FILES
*******************************************************************************/
#include "lcdif_c30.h"
#include "pbif_c30.h"
/*******************************************************************************
* LOCAL DEFINES
*******************************************************************************/
/*******************************************************************************
* Summary:
* Used to indicate that the LCD interface object is open and in use
*******************************************************************************/
#define LCDIF_OPEN (0x01 << 7)
/*******************************************************************************
* Summary:
* Used to indicate the parallel bus width is 4 bits
*******************************************************************************/
#define LCDIF_PBWIDTH4BITS (0x01 << 6)
/*******************************************************************************
* Summary:
* Used to indicate that this LCD interface currently owns the peripheral bus
*******************************************************************************/
#define LCDIF_OWNPB (0x01 << 5)
/*******************************************************************************
* Summary:
* Used to mask the bitShiftData information in flags (lowest three bits)
*******************************************************************************/
#define LCDIF_SHIFTDATAMASK (0x07)
/*******************************************************************************
* Summary:
* Used to indicate that the parallel bus is not busy when the LCD interface
* object is initialised in lcdifCreate()
* #### ToDo: If this module becomes a generic module for all controllers,
* PBIF_NOT_BUSY should be 0x00 for PIC24, dsPIC and PIC32 as they
* will use an atomic bit test & set to indicate the bus is in use and
* not a subtraction as is used for the PIC18
*******************************************************************************/
#if defined(__18CXX)
#define PBIF_NOT_BUSY 0x01;
#else
#define PBIF_NOT_BUSY 0x00;
#endif
/*******************************************************************************
* Summary:
* Used to indicate that the LCD interface is in use from another task
*******************************************************************************/
#define LCDIF_BUSY 0;
/*******************************************************************************
* Summary:
* Used to indicate that the LCD interface call was successful
*******************************************************************************/
#define LCDIF_SUCCESS 1;
/*******************************************************************************
* LOCAL CONSTANTS
*******************************************************************************/
/*******************************************************************************
* LOCAL DATA TYPES
*******************************************************************************/
/*******************************************************************************
* LOCAL TABLES
*******************************************************************************/
/*******************************************************************************
* LOCAL GLOBAL VARIABLES
*******************************************************************************/
/*******************************************************************************
* Summary:
* Local pointer to a linked list of LCD intrface objects
*******************************************************************************/
static LCDIFOBJ * startOfLcdIfObjs;
/*******************************************************************************
* Summary:
* Used to note how many LCD interface objects are active. Each bit in this
* variable relates to one active object.
*******************************************************************************/
static unsigned int activeLcdIfObjects;
/*******************************************************************************
* LOCAL FUNCTION PROTOTYPES
*******************************************************************************/
/* These functions are implemented in */
/* assembly code and hence are */
/* defined as extern */
extern unsigned char pbifGetBusMutex(unsigned int * pbIfFlag);
extern void pbifReturnBusMutex(unsigned int * pbIfFlag);
/*******************************************************************************
* LOCAL CONFIGURATION ERRORS
*******************************************************************************/
#if !defined(__C30)
#error This module requires the use of the C30 compiler.
#error If you wish to use this code with another platform, search in the
#error directory where you found this file for a possible port.
#error This file is currently saved here: __FILE__
#endif
/*******************************************************************************
* lcdifInit()
*
* Summary:
* Initialises the LCDIFC18 module for first use
*
* See also:
* lcdifDeinit()
*
* Arguments:
* None
*
* Returns:
* void
*
* Callers:
* Main application code
*
* Notes :
* None
*******************************************************************************/
void lcdifInit(void)
{
/* Initialise the local pointer to */
/* the list of LCDIF objects */
startOfLcdIfObjs = (LCDIFOBJ *) 0;
/* Currently no active LCDIF objects */
activeLcdIfObjects = 0;
}
/*******************************************************************************
* lcdifDeinit()
*
* Summary:
* Deinitialises the LCDIFC18 module for first use
*
* See also:
* lcdifInit()
*
* Arguments:
* None
*
* Returns:
* void
*
* Callers:
* Main application code
*
* Notes :
* None
*******************************************************************************/
void lcdifDeinit(void)
{
/* Initialise the local pointer to */
/* the list of LCDIF objects */
startOfLcdIfObjs = (LCDIFOBJ *) 0;
/* Currently no active LCDIF objects */
activeLcdIfObjects = 0;
}
/*******************************************************************************
* lcdifCreate()
*
* Summary:
* Creates an LCD interface for use by this module
*
* See also:
* lcdifDestroy()
*
* Arguments:
* lcdIfObj - lcdif object to insert in a list of lcdif objects
*
* Returns:
* - 1 to MAX_LCFIFS - number the LCD interface has been assigned if it was
* possible to allocate it
* - 0 - if the LCD interface allocation failed
*
* Callers:
* Main application code
*
* Notes :
* 1. lcdifCreate() must have been called prior to calling this function
*******************************************************************************/
LCDIFNUM lcdifCreate(LCDIFOBJ * const lcdIfObj)
{
LCDIFOBJ * localLcdIfObj; /* Stores local copy of pointer to */
/* linked list so we can insert next */
/* object in at the top */
unsigned int interfaceNumber = 0x0001;
/* Used to calculate the interface */
/* number to return to caller */
unsigned char bitTest;
unsigned char bitCount;
/* Used to note bus width */
unsigned char busWidth;
/* Used to note how many bits to */
/* shift bus data */
unsigned char busDataShift;
/* Check we got an object to point to */
if(lcdIfObj != (LCDIFOBJ *) 0)
{
/* Check that there is some useful */
/* information in the object that was */
/* passed */
/* First, do we have a E_LAT? */
if (lcdIfObj->E_LAT == (volatile unsigned int *) 0)
{
goto cannot_create_if;
}
/* Do we have only one E_BIT? */
for (bitTest = 0x01, bitCount = 0; bitTest != 0x00; bitTest <<= 1)
{
if (lcdIfObj->E_BIT & bitTest)
{
bitCount++;
}
}
if (bitCount != 1)
{
goto cannot_create_if;
}
/* Do we have a PBIFOBJ object? */
if (lcdIfObj->pbIfObject == (PBIFOBJ *) 0)
{
goto cannot_create_if;
}
/* Do we have anything in the */
/* PBIFOBJ object? */
/* Do we have only one RW_BIT? */
for (bitTest = 0x01, bitCount = 0; bitTest != 0x00; bitTest <<= 1)
{
if (lcdIfObj->pbIfObject->RW_BIT & bitTest)
{
bitCount++;
}
}
if (bitCount != 1)
{
goto cannot_create_if;
}
/* Do we have only one RS_BIT? */
for (bitTest = 0x01, bitCount = 0; bitTest != 0x00; bitTest <<= 1)
{
if (lcdIfObj->pbIfObject->RW_BIT & bitTest)
{
bitCount++;
}
}
if (bitCount != 1)
{
goto cannot_create_if;
}
/* Do we have only four or eight */
/* DATA_MASK bits */
for (bitTest = 0x01, bitCount = 0; bitTest != 0x00; bitTest <<= 1)
{
if (lcdIfObj->pbIfObject->DATA_MASK & bitTest)
{
bitCount++;
}
}
if (bitCount == 4)
{
busWidth = bitCount;
/* Calculate number of bits to shift */
/* data for 4-bit bus */
for (bitTest = 0x01, busDataShift = 0; bitTest != 0x00;
bitTest <<= 1)
{
if (!(lcdIfObj->pbIfObject->DATA_MASK & bitTest))
{
busDataShift++;
}
else
{
break;
}
}
}
else if (bitCount == 8)
{
busWidth = bitCount;
}
else
{
goto cannot_create_if;
}
/* Do we have a RW_LAT? */
if (lcdIfObj->pbIfObject->RW_LAT == (volatile unsigned int *) 0)
{
goto cannot_create_if;
}
/* Do we have a RS_LAT? */
if (lcdIfObj->pbIfObject->RS_LAT == (volatile unsigned int *) 0)
{
goto cannot_create_if;
}
/* Do we have a DATA_PORT? */
if (lcdIfObj->pbIfObject->DATA_PORT == (volatile unsigned int *) 0)
{
goto cannot_create_if;
}
/* Do we have a DATA_LAT? */
if (lcdIfObj->pbIfObject->DATA_LAT == (volatile unsigned int *) 0)
{
goto cannot_create_if;
}
/* Do we have a DATA_TRIS? */
if (lcdIfObj->pbIfObject->DATA_TRIS == (volatile unsigned int *) 0)
{
goto cannot_create_if;
}
/* If we got here the object contains */
/* valid data we can work with */
/* If there are no active objects in */
/* the list put this object at the */
/* top */
if (activeLcdIfObjects == 0 && startOfLcdIfObjs == (LCDIFOBJ *) 0)
{
startOfLcdIfObjs = lcdIfObj;
startOfLcdIfObjs->nextLcdIfObj = (LCDIFOBJ *) 0;
/* Assign the interface number */
activeLcdIfObjects |= interfaceNumber;
startOfLcdIfObjs->lcdIfNum = interfaceNumber;
/* Clear the object's flags */
startOfLcdIfObjs->lcdIfFlags = 0;
/* Note parallel bus width - if it is */
/* not 4 it must be 8 */
/* Also note amount to shift data to */
/* use on the bus */
if (busWidth == 4)
{
startOfLcdIfObjs->lcdIfFlags |= LCDIF_PBWIDTH4BITS;
startOfLcdIfObjs->lcdIfFlags |= (busDataShift &
LCDIF_SHIFTDATAMASK);
}
/* Note that the parallel bus is not */
/* in use */
startOfLcdIfObjs->pbIfObject->mutex = PBIF_NOT_BUSY;
return startOfLcdIfObjs->lcdIfNum;
}
/* Otherwise, if we haven't allocated */
/* all the LCD interface objects we */
/* can support, insert another */
/* #### Need to mask this to 16-bits */
else if (activeLcdIfObjects != 0xFFFF)
{
/* Insert this object at start of the */
/* list */
localLcdIfObj = startOfLcdIfObjs;
startOfLcdIfObjs = lcdIfObj;
startOfLcdIfObjs->nextLcdIfObj = localLcdIfObj;
/* Find a free LCD interface number */
/* for this interface */
for (interfaceNumber = 0x0002; interfaceNumber == 0x0000;
interfaceNumber <<= 1)
{
if (!activeLcdIfObjects & interfaceNumber)
{
/* Assign the interface number */
activeLcdIfObjects |= interfaceNumber;
startOfLcdIfObjs->lcdIfNum = interfaceNumber;
/* Clear the object's flags */
startOfLcdIfObjs->lcdIfFlags = 0;
return startOfLcdIfObjs->lcdIfNum;
}
}
}
}
cannot_create_if:
/* Couldn't create interface */
return 0;
}
/*******************************************************************************
* lcdifDestroy()
*
* Summary:
* Destroys a previously created LCD interface object
*
* See also:
* lcdifCreate()
*
* Arguments:
* lcdIfNumber - number of the LCD interface object to destroy
*
* Returns:
* - 1 - LCD interface was successfully destroyed
* - 0 - couldn't detroy requested LCD interface - probably still open
*
* Callers:
* Main application code
*
* Notes :
* 1. lcdifCreate() must have been called prior to calling this function
*******************************************************************************/
unsigned char lcdifDestroy(LCDIFNUM lcdIfNumber)
{
LCDIFOBJ * localLcdIfObj; /* Stores local copy of pointer to */
/* linked list so we can insert next */
/* object in at the top */
LCDIFOBJ * tempLcdIfObj; /* Temporary object pointer so we can */
/* remove an object from the list */
unsigned int interfaceNumber; /* Used to calculate the interface */
/* number to return to caller */
/* Check that there are created */
/* objects */
if(startOfLcdIfObjs != (LCDIFOBJ *) 0)
{
/* Search through the list to find */
/* this interface */
localLcdIfObj = startOfLcdIfObjs;
/* If this is the first object in the */
/* list and the object is not open, */
/* simply remove it */
if (startOfLcdIfObjs->lcdIfNum == lcdIfNumber &&
!(localLcdIfObj->lcdIfFlags & LCDIF_OPEN))
{
startOfLcdIfObjs = startOfLcdIfObjs->nextLcdIfObj;
/* Also note that we have one less */
/* active LCD interface */
activeLcdIfObjects &= ~lcdIfNumber;
return 1;
}
/* Otherwise if this object is still */
/* open, return that is can't be */
/* destroyed */
else if (startOfLcdIfObjs->lcdIfNum == lcdIfNumber &&
(localLcdIfObj->lcdIfFlags & LCDIF_OPEN))
{
return 0;
}
/* Otherwise parse through list of */
/* objects until we find the object */
/* or find no object with that number */
do
{
/* Save pointer to currect object in */
/* case next object will be removed */
tempLcdIfObj = localLcdIfObj;
/* Ensure that this object actually */
/* points to another object and not */
/* to NULL */
if(localLcdIfObj->nextLcdIfObj != (LCDIFOBJ *) 0)
{
/* Point to that next object in */
/* list... */
localLcdIfObj = localLcdIfObj->nextLcdIfObj;
/* ...and see if it is the */
/* interface we are searching for and */
/* that the interface is not open */
if (localLcdIfObj->lcdIfNum == lcdIfNumber &&
!(localLcdIfObj->lcdIfFlags && LCDIF_OPEN))
{
/* If so, remove this item from list */
/* by pointing previous object to */
/* where this object points */
tempLcdIfObj->nextLcdIfObj = localLcdIfObj->nextLcdIfObj;
/* Also note that we have one less */
/* active LCD interface */
activeLcdIfObjects &= ~lcdIfNumber;
return 1;
}
}
} while(localLcdIfObj->nextLcdIfObj != (LCDIFOBJ *) 0);
}
/* Couldn't destroy interface */
return 0;
}
/*******************************************************************************
* lcdifOpen()
*
* Summary:
* Opens an LCD interface for use by caller and initialises an HLCDIF
* handle to it
*
* See also:
* lcdifClose()
*
* Arguments:
* lcdIfNumber - number of an existing LCD interface object to use
*
* Returns:
* - NULL - if LCD interface couldn't be opened
* - handle - if LCD interface was opened properly
*
* Callers:
* User application
*
* Notes :
* 1. Caller must have created (lcdifCreate) at least one LCD interface object
* before calling this function
*******************************************************************************/
HLCDIF lcdifOpen(LCDIFNUM lcdIfNumber)
{
LCDIFOBJ * localLcdIfObj; /* Stores local copy of pointer to */
/* linked list so we can insert next */
/* object in at the top */
/* Check that there are created */
/* objects */
if(startOfLcdIfObjs != (LCDIFOBJ *) 0)
{
/* Check that a LCDIFNUM object */
/* exists with lcdIfNumber */
localLcdIfObj = startOfLcdIfObjs;
do
{
if (localLcdIfObj->lcdIfNum == lcdIfNumber)
{
/* Check buffer is not already open */
if (!(localLcdIfObj->lcdIfFlags & LCDIF_OPEN))
{
/* Note that it is now in use */
localLcdIfObj->lcdIfFlags |= LCDIF_OPEN;
/* Return handle to it */
return localLcdIfObj;
}
/* If buffer is already open, return */
/* NULL handle pointer */
else
{
return (LCDIFOBJ *) 0;
}
}
else
{
localLcdIfObj = localLcdIfObj->nextLcdIfObj;
}
} while (localLcdIfObj != (LCDIFOBJ *) 0);
}
/* Return handle to NULL otherwise */
return (LCDIFOBJ *) 0;
}
/*******************************************************************************
* lcdifClose()
*
* Summary:
* Closes an LCD interface and releases the handle to it
*
* See also:
* lcdifOpen()
*
* Arguments:
* hLcdIf - handle to the open buffer
*
* Returns:
* - >0 - number of LCD interface object if it was was open
* - 0 - if the LCD interface was not open
*
* Callers:
* User application
*
* Notes :
* 1. Caller must have 'created' at least one LCD interface object before
* calling this function
*
*******************************************************************************/
LCDIFNUM lcdifClose(HLCDIF const hLcdIf)
{
/* Check that there are created */
/* objects */
if(startOfLcdIfObjs != (LCDIFOBJ *) 0)
{
/* Check LCD interface is actually */
/* open */
if (hLcdIf->lcdIfFlags & LCDIF_OPEN)
{
/* Note that this LCD interface */
/* object is closed */
hLcdIf->lcdIfFlags &= ~LCDIF_OPEN;
/* Return LCD interface object's */
/* interface number */
return hLcdIf->lcdIfNum;
}
}
/* Otherwise return 0 to say that */
/* buffer object wasn't open */
return (LCDIFNUM) 0;
}
/*******************************************************************************
* lcdifGetPb()
*
* Summary:
* Attempts to aquire the peripheral bus for use by the LCD interface module
*
* See also:
* lcdifReturnPb()
*
* Arguments:
* hLcdIf - handle to the open LCD interface
*
* Returns:
* - 1 - this LCD interface (hLcdIf) owns the peripheral bus
* - 0 - the peripheral bus is currently in use by another task
*
* Callers:
* User application
*
* Notes :
* 1. Caller must have 'created' at least one LCD interface object before
* calling this function
*
*******************************************************************************/
unsigned char lcdifGetPb(HLCDIF const hLcdIf)
{
/* Attempt to get the peripheral bus */
/* for this hLcdIf object */
if (pbifGetBusMutex(&hLcdIf->pbIfObject->mutex))
{
/* If we were successful, note the */
/* fact in the LCD interface's flags */
/* variable */
hLcdIf->lcdIfFlags |= LCDIF_OWNPB;
return 1;
}
else
{
/* If we failed in our attempt, */
/* return 0 */
return 0;
}
}
/*******************************************************************************
* lcdifReturnPb()
*
* Summary:
* Returns the peripheral bus for use by other modules
*
* See also:
* lcdifGetPb()
*
* Arguments:
* hLcdIf - handle to the open LCD interface
*
* Returns:
* void
*
* Callers:
* User application
*
* Notes :
* 1. Caller must have 'created' at least one LCD interface object before
* calling this function
*
*******************************************************************************/
void lcdifReturnPb(HLCDIF const hLcdIf)
{
/* Return the peripheral bus */
pbifReturnBusMutex(&hLcdIf->pbIfObject->mutex);
/* Note this in the LCD interface's */
/* flags variable */
hLcdIf->lcdIfFlags &= ~LCDIF_OWNPB;
}
/*******************************************************************************
* lcdifWriteData()
*
* Summary:
* Writes data to the LCD interface
*
* See also:
* lcdifReadData()
*
* Arguments:
* hLcdIf - handle to the open LCD interface
* data - data to write to the LCD interface
*
* Returns:
* - LCDIF_BUSY - if the LCD parallel bus is in use
* - LCDIF_SUCCESS - if the LCD write completed
*
* Callers:
* User application
*
* Notes :
* 1. Caller must have 'created' at least one LCD interface object before
* calling this function
* 2. In the lcdifReadxxxx and lcdifWritexxxx functions, no assumptions are made
* about the state of the pins. The functions use the pins as desired and then
* leave them in that state. For this write function that also means that the
* PORT pins being used for data output are left as outputs. The only rule is
* to leave the ENABLE pin low when finished
* 3. You must have called lcdifGetPb() successfully before calling this function
*******************************************************************************/
unsigned char lcdifWriteData(HLCDIF const hLcdIf, unsigned char data)
{
unsigned char tempData;
/* Check if we own the peripheral bus */
if (hLcdIf->lcdIfFlags && LCDIF_OWNPB)
{
/* If it is a 4-bit bus, use a 4-bit */
/* access */
if (hLcdIf->lcdIfFlags & LCDIF_PBWIDTH4BITS)
{
/* The following is a 4-bit write */
/* data sequence as per HD44780 */
/* datasheets */
/* Clear RW pin */
*hLcdIf->pbIfObject->RW_LAT &= ~hLcdIf->pbIfObject->RW_BIT;
/* Set RS pin */
*hLcdIf->pbIfObject->RS_LAT |= hLcdIf->pbIfObject->RS_BIT;
/* Get high nibble of data */
tempData = data >> 4;
/* Get data in correct position for */
/* 4-bit bus */
tempData <<= startOfLcdIfObjs->lcdIfFlags & LCDIF_SHIFTDATAMASK;
/* Clear data pins */
*hLcdIf->pbIfObject->DATA_LAT &= ~hLcdIf->pbIfObject->DATA_MASK;
/* Set desired data pins */
*hLcdIf->pbIfObject->DATA_LAT |= tempData;
/* Set data pins to outputs */
*hLcdIf->pbIfObject->DATA_TRIS &= ~hLcdIf->pbIfObject->DATA_MASK;
/* Set E pin */
*hLcdIf->E_LAT |= hLcdIf->E_BIT;
/* Clear E pin */
*hLcdIf->E_LAT &= ~hLcdIf->E_BIT;
/* Clear data pins */
*hLcdIf->pbIfObject->DATA_LAT &= ~hLcdIf->pbIfObject->DATA_MASK;
/* Get low nibble of data */
tempData = data & 0x0F;
/* Get data in correct position for */
/* 4-bit bus */
tempData <<= startOfLcdIfObjs->lcdIfFlags & LCDIF_SHIFTDATAMASK;
/* Set desired data pins */
*hLcdIf->pbIfObject->DATA_LAT |= tempData;
/* Set E pin */
*hLcdIf->E_LAT |= hLcdIf->E_BIT;
/* Clear E pin */
*hLcdIf->E_LAT &= ~hLcdIf->E_BIT;
/* Write data process finished */
}
else
{
/* The following is an 8-bit write */
/* data sequence as per HD44780 */
/* datasheets */
/* Clear RW pin */
*hLcdIf->pbIfObject->RW_LAT &= ~hLcdIf->pbIfObject->RW_BIT;
/* Set RS pin */
*hLcdIf->pbIfObject->RS_LAT |= hLcdIf->pbIfObject->RS_BIT;
/* Write data to pins */
*hLcdIf->pbIfObject->DATA_LAT = data;
/* Set data pins to outputs */
*hLcdIf->pbIfObject->DATA_TRIS = 0x00;
/* Set E pin */
*hLcdIf->E_LAT |= hLcdIf->E_BIT;
/* Clear E pin */
*hLcdIf->E_LAT &= ~hLcdIf->E_BIT;
/* Write data process finished */
}
/* Inform caller that write succeeded */
return LCDIF_SUCCESS;
}
/* Inform caller that bus is busy */
return LCDIF_BUSY;
}
/*******************************************************************************
* lcdifReadData()
*
* Summary:
* Reads data from the LCD interface. The data read will be the contents of a
* CGRAM address if the previous instruction set a CGRAM address. If the
* previous instruction set a DDRAM address, the DDRAM address will be read
*
* See also:
* lcdifWriteData()
*
* Arguments:
* hLcdIf - handle to the open LCD interface
* * data - variable in which to store read data
*
* Returns:
* - LCDIF_BUSY - if the LCD parallel bus is in use
* - LCDIF_SUCCESS - if the LCD write completed
*
* Callers:
* User application
*
* Notes :
* 1. Caller must have 'created' at least one LCD interface object before
* calling this function
* 2. In the lcdifReadxxxx and lcdifWritexxxx functions, no assumptions are made
* about the state of the pins. The functions use the pins as desired and then
* leave them in that state. For this write function that also means that the
* PORT pins being used for data output are left as outputs. The only rule is
* to leave the ENABLE pin low when finished
* 3. You must have called lcdifGetPb() successfully before calling this function
*******************************************************************************/
unsigned char lcdifReadData(HLCDIF const hLcdIf, unsigned char * const data)
{
unsigned char tempData;
unsigned char tempData2;
/* Check if we own the peripheral bus */
if (hLcdIf->lcdIfFlags && LCDIF_OWNPB)
{
/* If it is a 4-bit bus, use a 4-bit */
/* access */
if (hLcdIf->lcdIfFlags & LCDIF_PBWIDTH4BITS)
{
/* The following is a 4-bit read */
/* data sequence as per HD44780 */
/* datasheets */
/* Set RW pin */
*hLcdIf->pbIfObject->RW_LAT |= hLcdIf->pbIfObject->RW_BIT;
/* Set RS pin */
*hLcdIf->pbIfObject->RS_LAT |= hLcdIf->pbIfObject->RS_BIT;
/* Set data pins to inputs */
*hLcdIf->pbIfObject->DATA_TRIS |= hLcdIf->pbIfObject->DATA_MASK;
/* Set E pin */
*hLcdIf->E_LAT |= hLcdIf->E_BIT;
/* Read high nibble of data and shift */
/* into low four bytes of tempData */
tempData = (*hLcdIf->pbIfObject->DATA_PORT &
hLcdIf->pbIfObject->DATA_MASK);
tempData >>= (startOfLcdIfObjs->lcdIfFlags & LCDIF_SHIFTDATAMASK);
/* Clear E pin */
*hLcdIf->E_LAT &= ~hLcdIf->E_BIT;
/* Shift data into high nibble of */
/* tempData */
tempData <<= 4;
/* Set E pin */
*hLcdIf->E_LAT |= hLcdIf->E_BIT;
/* Read low nibble of data and add */
/* into low four bytes of tempData */
tempData2 = (*hLcdIf->pbIfObject->DATA_PORT&
hLcdIf->pbIfObject->DATA_MASK);
tempData2 >>= (startOfLcdIfObjs->lcdIfFlags & LCDIF_SHIFTDATAMASK);
/* Formulate whole data byte */
tempData += tempData2;
/* Give value read back to caller */
* data = tempData;
/* Clear E pin */
*hLcdIf->E_LAT &= ~hLcdIf->E_BIT;
/* Read data process finished */
}
else
{
/* The following is an 8-bit read */
/* data sequence as per HD44780 */
/* datasheets */
/* Set RW pin */
*hLcdIf->pbIfObject->RW_LAT |= hLcdIf->pbIfObject->RW_BIT;
/* Set RS pin */
*hLcdIf->pbIfObject->RS_LAT |= hLcdIf->pbIfObject->RS_BIT;
/* Set data pins to inputs */
*hLcdIf->pbIfObject->DATA_TRIS = 0xFF;
/* Set E pin */
*hLcdIf->E_LAT |= hLcdIf->E_BIT;
/* Read high nibble of data and shift */
/* into low four bytes of tempData */
tempData = *hLcdIf->pbIfObject->DATA_PORT;
/* Clear E pin */
*hLcdIf->E_LAT &= ~hLcdIf->E_BIT;
/* Give value read back to caller */
* data = tempData;
/* Read data process finished */
}
/* Inform caller that write succeeded */
return LCDIF_SUCCESS;
}
/* Inform caller that bus is busy */
return LCDIF_BUSY;
}
/*******************************************************************************
* lcdifWriteInstruction()
*
* Summary:
* Writes an instruction to the LCD interface
*
* See also:
* lcdifReadAddress()
*
* Arguments:
* hLcdIf - handle to the open LCD interface
* instruction - instruction to write to the LCD interface
*
* Returns:
* - LCDIF_BUSY - if the LCD parallel bus is in use
* - LCDIF_SUCCESS - if the LCD write completed
*
* Callers:
* User application
*
* Notes :
* 1. Caller must have 'created' at least one LCD interface object before
* calling this function
* 2. In the lcdifReadxxxx and lcdifWritexxxx functions, no assumptions are made
* about the state of the pins. The functions use the pins as desired and then
* leave them in that state. For this write function that also means that the
* PORT pins being used for data output are left as outputs. The only rule is
* to leave the ENABLE pin low when finished
* 3. You must have called lcdifGetPb() successfully before calling this function
*******************************************************************************/
unsigned char lcdifWriteInstruction(HLCDIF const hLcdIf,
unsigned char instruction)
{
unsigned char tempInstruction;
/* Check if we own the peripheral bus */
if (hLcdIf->lcdIfFlags && LCDIF_OWNPB)
{
/* If it is a 4-bit bus, use a 4-bit */
/* access */
if (hLcdIf->lcdIfFlags & LCDIF_PBWIDTH4BITS)
{
/* The following is a 4-bit write */
/* data sequence as per HD44780 */
/* datasheets */
/* Clear RW pin */
*hLcdIf->pbIfObject->RW_LAT &= ~hLcdIf->pbIfObject->RW_BIT;
/* Clear RS pin */
*hLcdIf->pbIfObject->RS_LAT &= ~hLcdIf->pbIfObject->RS_BIT;
/* Get high nibble of instruction */
tempInstruction = instruction >> 4;
/* Get data in correct position for */
/* 4-bit bus */
tempInstruction <<= startOfLcdIfObjs->lcdIfFlags &
LCDIF_SHIFTDATAMASK;
/* Clear data pins */
*hLcdIf->pbIfObject->DATA_LAT &= ~hLcdIf->pbIfObject->DATA_MASK;
/* Set desired data pins */
*hLcdIf->pbIfObject->DATA_LAT |= tempInstruction;
/* Set data pins to outputs */
*hLcdIf->pbIfObject->DATA_TRIS &= ~hLcdIf->pbIfObject->DATA_MASK;
/* Set E pin */
*hLcdIf->E_LAT |= hLcdIf->E_BIT;
/* Clear E pin */
*hLcdIf->E_LAT &= ~hLcdIf->E_BIT;
/* Clear data pins */
*hLcdIf->pbIfObject->DATA_LAT &= ~hLcdIf->pbIfObject->DATA_MASK;
/* Get low nibble of instruction */
tempInstruction = instruction & 0x0F;
/* Get instruction in correct */
/* position for 4-bit bus */
tempInstruction <<= startOfLcdIfObjs->lcdIfFlags &
LCDIF_SHIFTDATAMASK;
/* Set desired data pins */