-
Notifications
You must be signed in to change notification settings - Fork 1
/
i2c_Flex_LCD_MRP_mod.h
231 lines (206 loc) · 7.03 KB
/
i2c_Flex_LCD_MRP_mod.h
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
//-----------------------------------------------------------------------------
// Title: i2c_Flex_LCD
// Description: Driver for common LCD with 1/2/3 or 4 rows by 1...20 columns
// using PCF8574T interface board with I2C protocol.
// Date: Nov-2013
// Ver.Rev.: 1.1
// Author: Hugo Silva ([email protected]) #Based on the routines of
// "20X4_LCD_I2C_DRIVER.h" from Pumrin S. and "lcd4_i2c.c" from XP8100
// MODIFICADA POR MOISES REYES PARA IMPRIMIR CARACTERES ESPECIALES CREADOS
//-----------------------------------------------------------------------------
//
// lcd_init() Must be called before any other function.
//
// lcd_putc(c) Will display c on the next position of the LCD.
//
// \f Clear LCD dispay
// \n Set write position on next lcd line
// \b LCD backspace
// lcd_gotoxy(x,y) Set write position on LCD (upper left is 1,1)
//
// lcd_backlight_led(ON)/lcd_backlight_led(OFF) = Turn ON/OFF LCD Backlight LED
//
//-----------------------------------------------------------------------------
// LCD pins D0-D3 are not used.
//-----------------------------------------------------------------------------
//
// Commment : Control of a compatible LCD (1...4 rows by 1...4 columns) from
// a bus I2C with an EXPANDER of I/O with connection I2C.
// The tests of these routines have been programmed using the IC
// Phillips PCF8574T. I've used 4 bits mode programming.
// The 8 bits mode programming is possible if you use 2 x PCF8574T.
// RW Pin is not being used.
//
// As defined in the following structure the pin connection is as follows:
//
// PCF8574P LCD
// ======== ======
// P0 RS
// P1 RW (Not used!)
// P2 Enable
// P3 Led Backlight
// P4 D4
// P5 D5
// P6 D6
// P7 D7
//
// The SCL and SDA pins should be pull-up resistor as shown below:
//
// +5v
// |
// <
// > 4.7K
// <
//To PIC | To i2c slave
//pin xx ------------------ SDA pin
//(SDA)
// +5v
// |
// <
// > 4.7K
// <
//To PIC | To i2c slave
//pin xx ------------------ SCL pin
//(SCL)
//
//To PIC To i2c slave
//Vss pin ----------------- Vss or ground pin
// |
// -----
// --- Ground
// -
//
// THIS DOCUMENT IS PROVIDED TO THE USER "AS IS"
//-----------------------------------------------------------------------------
#define LCD_ADDR 0x7E //I2C slave address for LCD module
#define lcd_total_rows 2 //Number of rows: 1,2,3 or 4
#define lcd_total_columns 16 //Number of columns: 1...16
#define RS 0b00000001 //P0 - PCF8574T Pin connected to RS
#define RW 0b00000010 //P1 - PCF8574T Pin connected to RW
#define ENABLE 0b00000100 //P2 - PCF8574T Pin connected to EN
#define LCD_BACKLIGHT 0b00001000 //P3 - PCF8574T Pin connected to BACKLIGHT LED
#define addr_row_one 0x00 //LCD RAM address for row 1
#define addr_row_two 0x40 //LCD RAM address for row 2
#define addr_row_three 0x14 //LCD RAM address for row 3
#define addr_row_four 0x54 //LCD RAM address for row 4
#define ON 1
#define OFF 0
#define NOT ~
#define data_shifted data<<4
int8 new_row_request=1, BACKLIGHT_LED=LCD_BACKLIGHT;
void lcd_backlight_led(byte bl)
{
if(bl){
BACKLIGHT_LED=LCD_BACKLIGHT;
}
else{
BACKLIGHT_LED=OFF;
}
}
void i2c_send_nibble(byte data, byte type)
{
switch (type)
{
case 0 :
i2c_write(data_shifted | BACKLIGHT_LED);
delay_cycles(1);
i2c_write(data_shifted | ENABLE | BACKLIGHT_LED );
delay_us(2);
i2c_write(data_shifted & NOT ENABLE | BACKLIGHT_LED);
break;
case 1 :
i2c_write(data_shifted | RS | BACKLIGHT_LED);
delay_cycles(1);
i2c_write(data_shifted | RS | ENABLE | BACKLIGHT_LED );
delay_us(2);
i2c_write(data_shifted | RS | BACKLIGHT_LED);
break;
}
}
void lcd_send_byte(byte data, byte type)
{
i2c_start();
i2c_write(LCD_ADDR);
i2c_send_nibble(data >> 4 , type);
i2c_send_nibble(data & 0xf , type);
i2c_stop();
}
void lcd_clear()
{
lcd_send_byte(0x01,0);
delay_ms(2);
new_row_request=1;
}
void lcd_init(void)
{
byte i;
byte CONST lcd_type=2; // 0=5x7, 1=5x10, 2=2 lines
byte CONST LCD_INIT_STRING[4] = {0x20 | (lcd_type << 2), 0xc, 1, 6}; // These bytes need to be sent to the LCD to start it up.
disable_interrupts(GLOBAL);
delay_ms(50); //LCD power up delay
i2c_start();
i2c_write(LCD_ADDR);
i2c_send_nibble(0x00,0);
delay_ms(15);
for (i=1;i<=3;++i)
{
i2c_send_nibble(0x03,0);
delay_ms(5);
}
i2c_send_nibble(0x02,0);
delay_ms(5);
i2c_stop();
for (i=0;i<=3;++i) {
lcd_send_byte(LCD_INIT_STRING[i],0);
delay_ms(5);
}
lcd_clear(); //Clear Display
enable_interrupts(GLOBAL);
}
void lcd_gotoxy( byte x, byte y)
{
byte row,column,row_addr,lcd_address;
static char data;
if (y>lcd_total_rows) row=lcd_total_rows; else row=y;
switch(row)
{
case 1: row_addr=addr_row_one; break;
case 2: row_addr=addr_row_two; break;
case 3: row_addr=addr_row_three; break;
case 4: row_addr=addr_row_four; break;
default: row_addr=addr_row_one; break;
}
if (x>lcd_total_columns) column=lcd_total_columns; else column=x;
lcd_address=(row_addr+(column-1));
lcd_send_byte(0x80|lcd_address,0);
}
//Display the character on LCD screen.
void LCD_PUTC(char in_data)
{
switch(in_data)
{
case '\f': lcd_clear(); break;
case '\n':
new_row_request++;
if (new_row_request>lcd_total_rows) new_row_request=1;
lcd_gotoxy(1, new_row_request);
break;
case '\b': lcd_send_byte(0x10,0); break;
default: lcd_send_byte(in_data,1); break;
}
}
/*Gacer uso de caracteres especiales https://www.youtube.com/watch?v=lH7zdISWp1U*/
void CGRAM(int n){
lcd_send_byte(n,1);
}
void CGRAM_x(int p){
lcd_send_byte(0x40+p*8,0);
}
void imprimir_caracter_especial(int ejex, int ejey, char caracter_especial[8], int pos_CGRAM)
{
int a = 0;
CGRAM_x(pos_CGRAM);
for(a=0;a<=7 ;a++) lcd_send_byte(caracter_especial[a],1);
lcd_gotoxy(ejex, ejey);
CGRAM(pos_CGRAM);
}