-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathLCD-PIC16.c
152 lines (135 loc) · 3.37 KB
/
LCD-PIC16.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
/* Driver for HD44780 Compatible LCD Modules
Copyright (C) 2014 Jesus Ruben Santa Anna Zamudio.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Author website: http://www.geekfactory.mx
Author e-mail: ruben at geekfactory dot mx
*/
/**
* This file implements the functions that handle the IO port access for the
* LCD display driver. The functions on this file can be implemented on a more
* optimized way to save some program memory and execution time, but this is a
* generic implementation.
*
* With this code, you can select any pin for any function at compile time:
* For example, you can place LCD data bus lines on pins of different IO ports
* at arbitrary positions.
*/
#include "LCD.h"
#include "LCD-PIC16.h"
/**
* CHECK IF LOW BITS ON DATA BUS ARE DEFINED IN LCD-PIC16.H ARE DEFINED, IF NOT
* WE´RE DRIVING THE LCD USING 4 BIT BUS
*/
#if defined( LCD_PIN_D0 ) && defined( LCD_PIN_D1 ) && defined( LCD_PIN_D2 ) && defined( LCD_PIN_D3 )
#define LCD_IO8
#else
#define LCD_IO4
#endif
uint8_t lcd_ioinit(void * iodata)
{
uint8_t i = 0;
// Configure control lines as outputs
LCD_TRIS_E = 0;
LCD_TRIS_RS = 0;
#if defined(LCD_PIN_RW)
LCD_TRIS_WR = 0;
#endif
// Configure bus data lines as outputs
#if defined(LCD_IO4)
LCD_TRIS_D4 = 0;
LCD_TRIS_D5 = 0;
LCD_TRIS_D6 = 0;
LCD_TRIS_D7 = 0;
#elif defined(LCD_IO8)
LCD_TRIS_D0 = 0;
LCD_TRIS_D1 = 0;
LCD_TRIS_D2 = 0;
LCD_TRIS_D3 = 0;
LCD_TRIS_D4 = 0;
LCD_TRIS_D5 = 0;
LCD_TRIS_D6 = 0;
LCD_TRIS_D7 = 0;
#endif
// Set all the pins to "low" state
for (i = 0; i < 11; i++)
lcd_ioset(i, FALSE);
// Return bus lenght
#if defined(LCD_IO4)
return 4;
#else
return 8;
#endif
}
void lcd_ioset(enum enLCDControlPins pin, uint8_t out)
{
switch (pin) {
#if defined( LCD_IO8 )
case E_D0_PIN:
LCD_PIN_D0 = out;
break;
case E_D1_PIN:
LCD_PIN_D1 = out;
break;
case E_D2_PIN:
LCD_PIN_D2 = out;
break;
case E_D3_PIN:
LCD_PIN_D3 = out;
break;
#endif
case E_D4_PIN:
LCD_PIN_D4 = out;
break;
case E_D5_PIN:
LCD_PIN_D5 = out;
break;
case E_D6_PIN:
LCD_PIN_D6 = out;
break;
case E_D7_PIN:
LCD_PIN_D7 = out;
break;
case E_RS_PIN:
LCD_PIN_RS = out;
break;
case E_EN_PIN:
LCD_PIN_E = out;
break;
case E_RW_PIN:
#if defined(LCD_PIN_RW)
LCD_PIN_WR = 0;
#endif
break;
}
}
void lcd_iowrite4(uint8_t data)
{
#if defined(LCD_IO4)
uint8_t i;
for (i = 4; i < 8; i++)
lcd_ioset(i, (data & (1 << i - 4)) ? TRUE : FALSE);
lcd_iohigh(E_EN_PIN);
delay_us(10);
lcd_iolow(E_EN_PIN);
#endif
}
void lcd_iowrite8(uint8_t data)
{
#if defined(LCD_IO8)
uint8_t i;
for (i = 0; i < 8; i++)
lcd_ioset(i, (data & (1 << i)) ? TRUE : FALSE);
lcd_iohigh(E_EN_PIN);
delay_us(10);
lcd_iolow(E_EN_PIN);
#endif
}