-
Notifications
You must be signed in to change notification settings - Fork 0
/
i2c.c
232 lines (210 loc) · 3.95 KB
/
i2c.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
#include "i2c.h"
#include <xc.h>
#include "i2c_customization.h"
#include <libpic30.h>
#define Slave 0x7C //3E for CiZ (0x7C shifted over 1 bit) //3C for BiZ (0x78 shifted over 1 bit)
#define Comsend 0x00
#define Datasend 0x40
#define Line2 0xC0
//internal for ic2_poll
char bit_in(void);
void i2c_start()
{
if (SDA_dir == 1) SDA_dir = 0;
if (SCL_dir == 1) SCL_dir = 0;
SDA = 1; //nach Reset ist der Bus Low, er muss aber High sein!!!
SCL = 1; //nach Reset ist der Bus Low, er muss aber High sein!!!
SDA = 0;
__delay_us(5);
SCL = 0;
__delay_us(5);
}
void i2c_stop()
{
SDA = 0;
__delay_us(5);
SCL = 1;
__delay_us(5);
SDA = 1;
__delay_us(5);
}
void i2c_send(uint8_t b)
{
uint8_t mask;
mask = 0x80;
do
{
if(b & mask)
SDA = 1;
else
SDA = 0;
SCL = 1;
__delay_us(5);
SCL = 0;
__delay_us(5);
mask = mask/2;
}
while(mask > 0);
SDA = 1;
SCL = 1;
__delay_us(5);
SCL = 0;
__delay_us(5);
}
unsigned char i2c_read(uint8_t ack)
{
uint8_t mask,daten;
daten = 0;
mask = 0x80;
SDA = 1;
do
{
SDA_dir = 0; // Port wird von Ausgang auf Eingang umgeschalten
SCL = 1;
__delay_us(5);
if(SDA_Read ==1)daten = daten | mask;
SCL = 0;
__delay_us(5);
mask = mask/2;
SDA_dir = 1; // Port wird wieder Ausgang
}
while(mask > 0);
__delay_us(5);
SDA = ack;
__delay_us(5);
SCL = 1;
__delay_us(5);
SCL = 0;
__delay_us(5);
return daten;
}
uint8_t i2c_poll(char addr)
{
uint8_t ack = 0;
uint8_t mask;
i2c_start();
mask = 0x80;
do
{
if(addr & mask) SDA = 1;
else SDA = 0;
SCL = 1;
__delay_us(5);
SCL = 0;
__delay_us(5);
mask = mask/2;
}
while(mask > 0);
ack = bit_in();
i2c_stop();
return ack;
}
char bit_in(void)
{
char ret = 0;
SCL = 0; // ensure SCL is low
SDA_dir = 1; // configure SDA as an input
SCL = 1; // bring SCL high to begin transfer
int i = 0;
while(i<10000 && ret==0)
{
ret = SDA_Read;
i++;
__delay_us(1);
}
SCL = 0; // bring SCL low again.
SDA_dir = 0; // configure SDA as output again
return ret;
}
void Show(char *text)
{
int n, d;
d=0x00;
i2c_start();
i2c_send(Slave);
i2c_send(Datasend);
for(n=0;n<16;n++){
i2c_send(*text);
++text;
}
i2c_stop();
}
void nextline(void)
{
i2c_start();
i2c_send(Slave);
i2c_send(Comsend);
i2c_send(Line2);
i2c_stop();
}
void CGRAM (void)
{
i2c_start();
i2c_send(Slave);
i2c_send(Comsend);
i2c_send(0x38); //go to instructino table 0
i2c_send(0x40); //Set CGRAM address to 0x00
i2c_stop();
__delay_ms(10);
i2c_start();
i2c_send(Slave);
i2c_send(Datasend);
i2c_send(0x00); //write to first CGRAM address
i2c_send(0x1E);
i2c_send(0x18);
i2c_send(0x14);
i2c_send(0x12);
i2c_send(0x01);
i2c_send(0x00);
i2c_send(0x00); //8 bytes per character
//continue writing to remaining CGRAM if desired
i2c_stop();
}
/****************************************************
* Initialization For ST7032i *
*****************************************************/
void init_LCD()
{
//i2c_poll(Slave);
i2c_start();
i2c_send(Slave);
i2c_send(Comsend);
i2c_send(0x38);
__delay_ms(1);
i2c_send(0x39);
__delay_ms(1);
i2c_send(0x14); // Internalosc
i2c_send(0x70);
i2c_send(0x5E);
i2c_send(0x6D);
i2c_send(0x0C); // Displayon
i2c_send(0x01); // Clear
i2c_send(0x06); // Entrymode
__delay_ms(1);
i2c_stop();
/*
i2c_start();
i2c_send(Slave);
i2c_send(Comsend);
i2c_send(0x38);
__delay_ms(10);
i2c_send(0x39);
__delay_ms(10);
i2c_send(0x14);
i2c_send(0x78);
i2c_send(0x5E);
i2c_send(0x6D);
i2c_send(0x0C);
i2c_send(0x01);
i2c_send(0x06);
__delay_ms(10);
i2c_stop();
CGRAM(); //define CGRAM
i2c_start();
i2c_send(Slave);
i2c_send(Comsend);
i2c_send(0x39);
i2c_send(0x01); //go back Home
i2c_stop();
__delay_ms(10);*/
}