-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathI2C_BB.c
87 lines (64 loc) · 1.22 KB
/
I2C_BB.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
#include "I2C_BB.h"
#include <p18f66j60.h>
void I2C_ST(void) {
SDA_HIGH();
SCL_HIGH();
SDA_LOW();
SCL_LOW();
}
void I2C_SP(void) {
SDA_LOW();
SCL_HIGH();
SDA_HIGH();
}
char I2C_OUT(unsigned char data) {
char bit_temp;
SCL_LOW();
for (bit_temp = 0; bit_temp < 8; bit_temp++) {
if (data & 0x80)
SDA_HIGH();
else
SDA_LOW();
SCL_HIGH();
data <<= 1;
SCL_LOW();
}
SCL_HIGH();
SDA_HIGH();
bit_temp = SDA_IN; // possible ACK bit
SCL_LOW();
SDA_LOW();
return bit_temp;
}
unsigned char I2C_IN(char NACK) {
char bit_temp;
unsigned char data;
SDA_HIGH();
for (bit_temp = 0; bit_temp < 8; bit_temp++) {
data <<= 1;
SCL_HIGH();
if (SDA_IN)
data |= 1;
SCL_LOW();
}
if (NACK == 1)
SDA_LOW();
else
SDA_HIGH();
SCL_HIGH();
SCL_LOW();
SDA_LOW();
return data;
}
void delay(unsigned int time) {
unsigned int t;
for (t = 0; t < time; t++) {
_asm nop _endasm
}
}
void delay2(unsigned int time) {
unsigned int t;
for (t = 0; t < time; t++) {
Delay10KTCYx(1);
}
}