-
Notifications
You must be signed in to change notification settings - Fork 0
/
i2c.c
114 lines (98 loc) · 1.87 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
#include "i2c.h"
#include <xc.h>
void I2CIdleWait(void)
{
while (I2C1CON & 0x1F);
while (I2C1STATbits.TRSTAT);
}
void I2CStart(void)
{
I2CIdleWait();
I2C1CONbits.SEN = 1;
while (I2C1CONbits.SEN);
I2CDebug("Start");
}
void I2CStop(void)
{
I2CIdleWait();
I2C1CONbits.PEN = 1;
while(I2C1CONbits.PEN);
I2CDebug("Stop");
}
void I2CRestart(void)
{
I2CIdleWait();
I2C1CONbits.RSEN = 1;
while(I2C1CONbits.RSEN);
I2CDebug("Restart");
}
void I2CAck(void)
{
I2CIdleWait();
I2C1CONbits.ACKDT = 0;
I2C1CONbits.ACKEN = 1;
while(I2C1CONbits.ACKEN);
I2CDebug("Ack");
}
void I2CAckWait(void)
{
I2CDebug("Ack Wait");
while (I2C1STATbits.TRSTAT & I2C1STATbits.ACKSTAT);
}
void I2CNackWait(void)
{
I2CDebug("Nack Wait");
while (I2C1STATbits.TRSTAT & !I2C1STATbits.ACKSTAT);
}
void I2CNack(void) // Acknowledge Data bit
{
I2CIdleWait();
I2C1CONbits.ACKDT = 1;
I2C1CONbits.ACKEN = 1;
while(I2C1CONbits.ACKEN);
I2CDebug("Nack");
}
void I2CInit(void)
{
I2C1BRG = 34;
I2C1CONbits.I2CEN = 1;
I2C1CONbits.DISSLW = 1;
I2CDebug("I2C Init");
}
void I2CBusIdleWait(void)
{
while (I2C1STATbits.TRSTAT);
}
byte I2CReadByte(void)
{
byte read = 0;
I2C1CONbits.RCEN = 1;
while (I2C1CONbits.RCEN & !I2C1STATbits.RBF) ;
if (I2C1STATbits.BCL)
{
DEBUG_WRITE("R: Bus Coll.");
while (1) { }
}
if (I2C1STATbits.I2COV)
{
DEBUG_WRITE("Overflow in RCV");
while (1) { }
}
I2CIdleWait();
read = (byte)(I2C1RCV & 0xFF);
I2CDebug("R: 0x%02x", read);
return read;
}
void I2CWriteByte(byte byte)
{
if (I2C1STATbits.BCL)
{
DEBUG_WRITE("W: Bus Coll.");
while (1) { }
}
I2C1TRN = byte;
while (I2C1STATbits.TBF);
I2CIdleWait();
I2CAckWait();
I2CDebug("W: 0x%02x", byte);
}