-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathi2c2writ.c
43 lines (40 loc) · 1.6 KB
/
i2c2writ.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
#include <p18cxxx.h>
#include "i2c.h"
/********************************************************************
* Function Name: WriteI2C2 *
* Return Value: Status byte for WCOL detection. *
* Parameters: Single data byte for I2C2 bus. *
* Description: This routine writes a single byte to the *
* I2C2 bus. *
********************************************************************/
#if defined (I2C_V3) || defined (I2C_V6) || defined (I2C_V6_1)
signed char WriteI2C2( unsigned char data_out )
{
SSP2BUF = data_out; // write single byte to SSP2BUF
if ( SSP2CON1bits.WCOL ) // test if write collision occurred
return ( -1 ); // if WCOL bit is set return negative #
else
{
if( ((SSP2CON1&0x0F)!=0x08) && ((SSP2CON1&0x0F)!=0x0B) ) //slave mode only
{
#if defined (I2C_SFR_V1)
if ( ( !SSP2STATbits.R_NOT_W ) && ( !SSP2STATbits.BF ) )
#else
if ( ( !SSP2STATbits.R_W ) && ( !SSP2STATbits.BF ) )// if R/W=0 and BF=0, NOT ACK was received
#endif
{
return ( -2 ); // return NACK
}
else return(0); // return ACK
}
else if( ((SSP2CON1&0x0F)==0x08) || ((SSP2CON1&0x0F)==0x0B) ) //master mode only
{
while( SSP2STATbits.BF ); // wait until write cycle is complete
IdleI2C2(); // ensure module is idle
if ( SSP2CON2bits.ACKSTAT ) // test for ACK condition received
return ( -2 ); //return NACK
else return ( 0 ); //return ACK
}
}
}
#endif