-
Notifications
You must be signed in to change notification settings - Fork 0
/
I2C.c
49 lines (39 loc) · 1.02 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
//
// I2C.c
// headmaster
//
// Created by Kevan Ahlquist on 11/3/12.
// Copyright (c) 2013 Tesla Works. MIT license.
//
#include "I2C.h"
#include <xc.h>
// =============================================================================
// Code
// =============================================================================
void I2CSend(char *buffer, int receiver);
void I2CSendByte(char data);
void I2CWaitForTransmission();
void I2CSend(char *buffer, int receiver)
{
int i = 0;
SSPCON2bits.SEN = 1; // Send Start bit
I2CWaitForTransmission();
I2CSendByte(receiver); // Send reciever address
// Send data for that reciever
for (i = 0; i < BYTES_PER_SLAVE; ++i)
{
I2CSendByte(buffer[BYTES_PER_SLAVE * receiver + i]);
}
SSPCON2bits.PEN = 1; // Send Stop bit
I2CWaitForTransmission();
}
void I2CSendByte(char data)
{
SSPBUF = data;
I2CWaitForTransmission();
}
void I2CWaitForTransmission(void)
{
while (!PIR1bits.SSPIF);
PIR1bits.SSPIF = 0;
}