-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathi2c.c
More file actions
109 lines (97 loc) · 3.94 KB
/
i2c.c
File metadata and controls
109 lines (97 loc) · 3.94 KB
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
#include <xc.h>
#include "i2c.h"
/********************************************************************
* Function Name: OpenI2C *
* Return Value: void *
* Parameters: SSP peripheral setup bytes *
* Description: This function sets up the SSP module on a *
* PIC18CXXX device for use with a Microchip I2C *
* EEPROM device or I2C bus device. *
********************************************************************/
void OpenI2C( unsigned char sync_mode, unsigned char slew )
{
// SSPSTAT &= 0x3F; // power on state
// SSPCON1 = 0x00; // power on state
// SSPCON2 = 0x00; // power on state
// SSPCON1 |= sync_mode; // select serial mode
// SSPSTAT |= slew; // slew rate on/off
// I2C_SCL = 1;
// I2C_SDA = 1;
// SSPCON1 |= SSPENB; // enable synchronous serial port
}
/********************************************************************
* Function Name: ReadI2C *
* Return Value: contents of SSPBUF register *
* Parameters: void *
* Description: Read single byte from I2C bus. *
********************************************************************/
unsigned char ReadI2C( void )
{
if( ((SSP1CON1&0x0F)==0x08) || ((SSP1CON1&0x0F)==0x0B) ) //master mode only
SSP1CON2bits.RCEN = 1; // enable master for 1 byte reception
while ( !SSP1STATbits.BF ); // wait until byte received
return ( SSP1BUF ); // return with read byte
}
/********************************************************************
* 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. *
********************************************************************/
signed char WriteI2C( unsigned char data_out )
{
SSP1BUF = data_out; // write single byte to SSPBUF
if ( SSP1CON1bits.WCOL ) // test if write collision occurred
return ( -1 ); // if WCOL bit is set return negative #
else
{
if( ((SSP1CON1&0x0F)!=0x08) && ((SSP1CON1&0x0F)!=0x0B) ) //Slave mode only
{
SSP1CON1bits.CKP = 1; // release clock line
while ( !PIR3bits.SSP1IF ); // wait until ninth clock pulse received
if ( ( !SSP1STATbits.R_W ) && ( !SSP1STATbits.BF ) )// if R/W=0 and BF=0, NOT ACK was received
{
return ( -2 ); //return NACK
}
else
{
return ( 0 ); //return ACK
}
}
else if( ((SSP1CON1&0x0F)==0x08) || ((SSP1CON1&0x0F)==0x0B) ) //master mode only
{
while( SSP1STATbits.BF ); // wait until write cycle is complete
IdleI2C(); // ensure module is idle
if ( SSP1CON2bits.ACKSTAT ) // test for ACK condition received
return ( -2 ); // return NACK
else return ( 0 ); //return ACK
}
}
}
//function to get the temperature from the sensor
unsigned char tsttc (void)
{
unsigned char value;
do{
IdleI2C();
StartI2C(); IdleI2C();
WriteI2C(0x9a | 0x00); IdleI2C();
WriteI2C(0x01); IdleI2C();
RestartI2C(); IdleI2C();
WriteI2C(0x9a | 0x01); IdleI2C();
value = ReadI2C(); IdleI2C();
NotAckI2C(); IdleI2C();
StopI2C();
} while (!(value & 0x40));
IdleI2C();
StartI2C(); IdleI2C();
WriteI2C(0x9a | 0x00); IdleI2C();
WriteI2C(0x00); IdleI2C();
RestartI2C(); IdleI2C();
WriteI2C(0x9a | 0x01); IdleI2C();
value = ReadI2C(); IdleI2C();
NotAckI2C(); IdleI2C();
StopI2C();
return value;
}