-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathi2c.c
79 lines (67 loc) · 1016 Bytes
/
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
#include <xc.h>
#include "i2c.h"
void init_i2c(void)
{
/* Set SCL and SDA pins as inputs */
TRISC3 = 1;
TRISC4 = 1;
/* Set I2C master mode */
SSPCON1 = 0x28;
SSPADD = 0x31;
/* Use I2C levels, worked also with '0' */
CKE = 0;
/* Disable slew rate control worked also with '0' */
SMP = 1;
/* Clear SSPIF interrupt flag */
SSPIF = 0;
/* Clear bus collision flag */
BCLIF = 0;
}
void i2c_idle(void)
{
while (!SSPIF);
SSPIF = 0;
}
void i2c_ack(void)
{
if (ACKSTAT)
{
/* Do debug print here if required */
}
}
void i2c_start(void)
{
SEN = 1;
i2c_idle();
}
void i2c_stop(void)
{
PEN = 1;
i2c_idle();
}
void i2c_rep_start(void)
{
RSEN = 1;
i2c_idle();
}
void i2c_write(unsigned char data)
{
SSPBUF = data;
i2c_idle();
}
void i2c_rx_mode(void)
{
RCEN = 1;
i2c_idle();
}
void i2c_no_ack(void)
{
ACKDT = 1;
ACKEN = 1;
}
unsigned char i2c_read(void)
{
i2c_rx_mode();
//i2c_no_ack();
return SSPBUF;
}