Repository navigation
Expand file tree
/
Copy pathi2c2.c
More file actions
169 lines (149 loc) · 2.41 KB
/
Copy pathi2c2.c
File metadata and controls
169 lines (149 loc) · 2.41 KB
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
#include "fubsd.h"
#include <xc.h>
#include <plib.h>
#include "i2c2.h"
void delay1(int time)
{
unsigned int j;
while(time--)
for(j=0;j<48;j++);
}
void delay2()
{
unsigned char j;
for(j=0;j<4;j++);
}
char sda_setup(void)
{
SDA_HIGH;
delay1(5);
return isSDA;
}
//IIC start
void I2C_Start(void)
{
SDA_HIGH;
delay2();
SCL_HIGH;
delay1(5);
SDA_LOW;
delay1(5);
SCL_LOW;
delay2();
}
//IIC stop
void I2C_Stop(void)
{
SDA_LOW;
delay2();
SCL_HIGH;
delay1(5);
SDA_HIGH;
delay1(5);
SCL_LOW;
delay2();
}
//Wait for an answer signal
unsigned char I2C_Wait_Ack(void)
{ unsigned char errtime=0;
SDA_HIGH;
delay1(1);
SCL_HIGH;
delay1(1);
while(isSDA == 1)
{
errtime++;
if(errtime>250)
{
I2C_Stop();
return 1;
}
}
SCL_LOW;
delay2();
}
//Acknowledge
void I2C_Ack(void)
{ SCL_LOW;
SDA_LOW;
delay1(2);
SCL_HIGH;
delay1(2);
SCL_LOW;
delay2();
}
//NO Acknowledge
void I2C_NAck(void)
{ SCL_LOW;
SDA_HIGH;
delay1(2);
SCL_HIGH;
delay1(2);
SCL_LOW;
delay2();
}
void I2C_Send_Byte(unsigned char data)
{ unsigned char t;
for(t=0;t<8;t++)
{
SCL_LOW;
if((data & 0x80) > 0)
SDA_HIGH;
else
SDA_LOW;
data <<=1;
delay1(2);
SCL_HIGH;
delay1(2);
SCL_LOW;
delay1(2);
}
}
unsigned char I2C_Read_Byte(unsigned char ack)
{
unsigned char t,receive=0;
SCL_LOW;
SDA_HIGH;
for(t = 0; t < 8; t++)
{
delay2();
SCL_HIGH;
delay2();
receive <<= 1;
if(isSDA == 1)
receive |= 0x01;
delay1(2);
SCL_LOW;
delay1(2);
}
if (ack) I2C_NAck();//NO Acknowledge
else I2C_Ack(); //Send Acknowledge
return receive;
}
void I2C_Wr_Reg(unsigned char WRADD, unsigned char RegIndex,unsigned char RegValue1)
{
I2C_Start();
I2C_Send_Byte(WRADD);
I2C_Wait_Ack();
I2C_Send_Byte(RegIndex);
I2C_Wait_Ack();
I2C_Send_Byte(RegValue1);
I2C_Wait_Ack();
I2C_Stop();
delay1(100);
}
unsigned char I2C_Read_Reg(unsigned char WRADD, unsigned char RegIndex)
{
unsigned char receive=0;
I2C_Start();
I2C_Send_Byte(WRADD);
I2C_Wait_Ack();
I2C_Send_Byte(RegIndex);
I2C_Wait_Ack();
I2C_Start();
I2C_Send_Byte((WRADD|1));
I2C_Wait_Ack();
receive=I2C_Read_Byte(1);
I2C_Stop();
return receive;
}