1 |
torben |
2 |
#ifndef _I2C_H_ |
2 |
|
|
#define _I2C_H_ |
3 |
|
|
|
4 |
|
|
/* |
5 |
|
|
* SDA (data) and SCL (clock) bits |
6 |
|
|
* |
7 |
|
|
* Special note!!! |
8 |
|
|
* |
9 |
|
|
* If the clock and data lines are in the same port, you will need |
10 |
|
|
* to beware of the Read/Modify/Write issue in the PIC - since |
11 |
|
|
* a bit set or clear on any one bit in a port will read and write |
12 |
|
|
* back all other bits, any bits configured as input which |
13 |
|
|
*/ |
14 |
|
|
|
15 |
|
|
|
16 |
|
|
/* Uncomment the next line to use the PIC's SSP Module*/ |
17 |
|
|
#define I2C_MODULE 1 |
18 |
|
|
|
19 |
|
|
#ifdef I2C_MODULE |
20 |
|
|
/* I2C module uses PORT C */ |
21 |
|
|
#define SCL RC3 /* clock on port C bit 2 */ |
22 |
|
|
#define SCL_DIR TRISC3 |
23 |
|
|
#define SDA RC4 /* data on port C bit 1 */ |
24 |
|
|
#define SDA_DIR TRISC4 |
25 |
|
|
#define I2CTRIS TRISC |
26 |
|
|
#define MASTER_MODE 0B1011 /* I2C firmware controlled Master Mode (slave idle) */ |
27 |
|
|
#define SSPMode(val) SSPCON1 &=0xF0; SSPCON1 |=(val & 0xf) |
28 |
|
|
|
29 |
|
|
#else |
30 |
|
|
/* Change port as required - defaults to port b */ |
31 |
|
|
#define SCL RB2 /* clock on port B bit 2 */ |
32 |
|
|
#define SCL_DIR TRISB2 |
33 |
|
|
|
34 |
|
|
#define SDA RB1 /* data on port B bit 1 */ |
35 |
|
|
#define SDA_DIR TRISB1 |
36 |
|
|
#define I2CTRIS TRISB |
37 |
|
|
|
38 |
|
|
#endif |
39 |
|
|
|
40 |
|
|
#define M_SDA_INP 0x02 |
41 |
|
|
#define M_SDA_OUT 0xFD |
42 |
|
|
#define M_SCL_INP 0x04 |
43 |
|
|
#define M_SCL_OUT 0xFB |
44 |
|
|
|
45 |
|
|
#define I2C_INPUT 1 /* data direction input */ |
46 |
|
|
#define I2C_OUTPUT 0 /* data direction output */ |
47 |
|
|
|
48 |
|
|
#define I2C_READ 0x01 /* read bit used with address */ |
49 |
|
|
#define I2C_WRITE 0x00 /* write bit used with address */ |
50 |
|
|
|
51 |
|
|
#define FALSE 0 |
52 |
|
|
#define TRUE !FALSE |
53 |
|
|
|
54 |
|
|
#define I2C_ERROR (-1) |
55 |
|
|
#define I2C_LAST FALSE /* SendAck: no more bytes to send */ |
56 |
|
|
#define I2C_MORE TRUE /* SendAck: more bytes to send */ |
57 |
|
|
|
58 |
|
|
#define i2c_Start() i2c_Restart() |
59 |
|
|
#define i2c_WriteTo(address) i2c_Open((address), I2C_WRITE) |
60 |
|
|
#define i2c_ReadFrom(address) i2c_Open((address), I2C_READ) |
61 |
|
|
|
62 |
|
|
#ifdef I2C_MODULE |
63 |
|
|
#define SCL_HIGH() SCL_DIR = I2C_INPUT |
64 |
|
|
#define SCL_LOW() SCL_DIR = I2C_OUTPUT |
65 |
|
|
#define SDA_HIGH() SDA_DIR = I2C_INPUT |
66 |
|
|
#define SDA_LOW() SDA_DIR = I2C_OUTPUT |
67 |
|
|
#else |
68 |
|
|
#define SCL_HIGH() SCL = 1; SCL_DIR = I2C_OUTPUT |
69 |
|
|
#define SCL_LOW() SCL = 0; SCL_DIR = I2C_OUTPUT |
70 |
|
|
#define SDA_HIGH() SDA = 1; SDA_DIR = I2C_OUTPUT |
71 |
|
|
#define SDA_LOW() SDA = 0; SDA_DIR = I2C_OUTPUT |
72 |
|
|
#endif |
73 |
|
|
|
74 |
|
|
/* |
75 |
|
|
* Timings for the i2c bus. Times are rounded up to the nearest |
76 |
|
|
* micro second. |
77 |
|
|
*/ |
78 |
|
|
|
79 |
|
|
#define I2C_TM_BUS_FREE 5 |
80 |
|
|
#define I2C_TM_START_SU 5 |
81 |
|
|
#define I2C_TM_START_HD 4 |
82 |
|
|
#define I2C_TM_SCL_LOW 5 |
83 |
|
|
#define I2C_TM_SCL_HIGH 4 |
84 |
|
|
#define I2C_TM_DATA_SU 1 |
85 |
|
|
#define I2C_TM_DATA_HD 0 |
86 |
|
|
#define I2C_TM_SCL_TO_DATA 4 /* SCL low to data valid */ |
87 |
|
|
#define I2C_TM_STOP_SU 4 |
88 |
|
|
#define I2C_TM_SCL_TMO 10 /* clock time out */ |
89 |
|
|
|
90 |
|
|
extern signed char i2c_ReadAcknowledge(void); |
91 |
|
|
extern unsigned char i2c_SendAddress(unsigned char, unsigned char); |
92 |
|
|
extern unsigned char i2c_SendByte(unsigned char); |
93 |
|
|
extern int i2c_ReadByte(void); |
94 |
|
|
extern void i2c_Restart(void); |
95 |
|
|
extern void i2c_Stop(void); |
96 |
|
|
extern void i2c_SendAcknowledge(unsigned char); |
97 |
|
|
extern signed char i2c_PutByte(unsigned char); |
98 |
|
|
extern int i2c_GetByte(unsigned char); |
99 |
|
|
extern unsigned char i2c_Open(unsigned char, unsigned char); |
100 |
|
|
extern unsigned char i2c_GetString(unsigned char *, unsigned char); |
101 |
|
|
extern int i2c_PutString(const unsigned char *, unsigned char); |
102 |
|
|
extern unsigned char i2c_WaitForSCL(void); |
103 |
|
|
extern void i2c_Free(void); |
104 |
|
|
#endif /* _I2C_H_ */ |