-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathi2c_writ.c
66 lines (57 loc) · 2.12 KB
/
i2c_writ.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
#include <p18cxxx.h>
#include "i2c.h"
/********************************************************************
* Function Name: WriteI2C *
* Return Value: Status byte for WCOL detection. *
* Parameters: Single data byte for I2C bus. *
* Description: This routine writes a single byte to the *
* I2C bus. *
********************************************************************/
#if defined (I2C_V1)
signed char WriteI2C( unsigned char data_out )
{
SSPBUF = data_out; // write single byte to SSPBUF
if ( SSPCON1bits.WCOL ) // test if write collision occurred
return ( -1 ); // if WCOL bit is set return negative #
else
{
if( ((SSPCON1&0x0F)!=0x08) && ((SSPCON1&0x0F)!=0x0B) ) //Slave mode only
{
SSPCON1bits.CKP = 1; // release clock line
while ( !PIR1bits.SSPIF ); // wait until ninth clock pulse received
if ( ( !SSPSTATbits.R_W ) && ( !SSPSTATbits.BF ) )// if R/W=0 and BF=0, NOT ACK was received
{
return ( -2 ); //return NACK
}
else
{
return ( 0 ); //return ACK
}
}
else if( ((SSPCON1&0x0F)==0x08) || ((SSPCON1&0x0F)==0x0B) ) //master mode only
{
while( SSPSTATbits.BF ); // wait until write cycle is complete
IdleI2C(); // ensure module is idle
if ( SSPCON2bits.ACKSTAT ) // test for ACK condition received
return ( -2 ); // return NACK
else return ( 0 ); //return ACK
}
}
}
#endif
#if defined (I2C_V4)
signed char WriteI2C( unsigned char data_out )
{
SSPBUF = data_out; // write single byte to SSPBUF
if ( SSPCONbits.WCOL ) // test if write collision occurred
return ( -1 ); // if WCOL bit is set return negative #
else
{
while( SSPSTATbits.BF ); // wait until write cycle is complete
// if ( PIR1bits.SSPIF ) // test for ACK condition received
// return ( -2 );
// else
return ( 0 ); // if WCOL bit is not set return non-negative #
}
}
#endif