1 |
|
1 |
|
2 |
/****************************************************************************
|
2 |
/****************************************************************************
|
3 |
Title : C include file for the I2C FUNCTIONS library (i2c.h)
|
3 |
Title : C include file for the I2C FUNCTIONS library (i2c.h)
|
4 |
Author: Chris efstathiou hendrix@otenet.gr
|
4 |
Author: Chris efstathiou hendrix@otenet.gr
|
5 |
Date: 13/Jul/2002
|
5 |
Date: 13/Jul/2002
|
6 |
Software: AVR-GCC with AVR-AS
|
6 |
Software: AVR-GCC with AVR-AS
|
7 |
Target: any AVR device
|
7 |
Target: any AVR device
|
8 |
Comments: This software is FREE.
|
8 |
Comments: This software is FREE.
|
9 |
|
9 |
|
10 |
*****************************************************************************/
|
10 |
*****************************************************************************/
|
11 |
|
11 |
|
12 |
|
12 |
|
13 |
#ifndef I2C_H
|
13 |
#ifndef I2C_H
|
14 |
#define I2C_H 1
|
14 |
#define I2C_H 1
|
15 |
|
15 |
|
16 |
/*##############################################################################################*/
|
16 |
/*##############################################################################################*/
|
17 |
/* START OF CONFIGURATION BLOCK */
|
17 |
/* START OF CONFIGURATION BLOCK */
|
18 |
/*##############################################################################################*/
|
18 |
/*##############################################################################################*/
|
19 |
|
19 |
|
20 |
#ifndef F_CPU
|
20 |
#ifndef F_CPU
|
21 |
#define F_CPU 3686400L /* The cpu clock frequency in Hertz (used to calculate time)*/
|
21 |
#define F_CPU 3686400L /* The cpu clock frequency in Hertz (used to calculate time)*/
|
22 |
#endif
|
22 |
#endif
|
23 |
|
23 |
|
24 |
#define I2C_SDA_PORT D /* The SDA port. Use capital letter (A,B,C,D... etc.) */
|
24 |
#define I2C_SDA_PORT D /* The SDA port. Use capital letter (A,B,C,D... etc.) */
|
25 |
#define SDA_PIN 4 /* The SDA port pin */
|
25 |
#define SDA_PIN 4 /* The SDA port pin */
|
26 |
|
26 |
|
27 |
#define I2C_SCL_PORT D /* The SCL port. Use capital letter (A,B,C,D... etc.) */
|
27 |
#define I2C_SCL_PORT D /* The SCL port. Use capital letter (A,B,C,D... etc.) */
|
28 |
#define SCL_PIN 5 /* The SCL port pin */
|
28 |
#define SCL_PIN 5 /* The SCL port pin */
|
29 |
/*
|
29 |
/*
|
30 |
The I2C_DELAY_TIME normally is 5 microseconds for a 100 Khz I2C bus ( (1/100000)/2 ).
|
30 |
The I2C_DELAY_TIME normally is 5 microseconds for a 100 Khz I2C bus ( (1/100000)/2 ).
|
31 |
but due to bus capacitance and device responce time in my application i use 50 microseconds.
|
31 |
but due to bus capacitance and device responce time in my application i use 50 microseconds.
|
32 |
The I2C_TIMEOUT_TIME is set to whatever is the maximum time you think your device will take
|
32 |
The I2C_TIMEOUT_TIME is set to whatever is the maximum time you think your device will take
|
33 |
to perform the requested task. After that time the i2c_transmit function will return
|
33 |
to perform the requested task. After that time the i2c_transmit function will return
|
34 |
a "I2C_ERROR_DEVICE_NOT_RESPONDING" or "I2C_ERROR_DEVICE_BUSY" error code.
|
34 |
a "I2C_ERROR_DEVICE_NOT_RESPONDING" or "I2C_ERROR_DEVICE_BUSY" error code.
|
35 |
*/
|
35 |
*/
|
36 |
#define I2C_DELAY_TIME 50 /* in microseconds (max over 1 second) */
|
36 |
#define I2C_DELAY_TIME 50 /* in microseconds (max over 1 second) */
|
37 |
#define I2C_TIMEOUT_TIME 1000 /* in microseconds (max over 1 second) */
|
37 |
#define I2C_TIMEOUT_TIME 1000 /* in microseconds (max over 1 second) */
|
38 |
|
38 |
|
39 |
/*##############################################################################################*/
|
39 |
/*##############################################################################################*/
|
40 |
/* END OF CONFIGURATION BLOCK */
|
40 |
/* END OF CONFIGURATION BLOCK */
|
41 |
/*##############################################################################################*/
|
41 |
/*##############################################################################################*/
|
42 |
|
42 |
|
43 |
/* Keyword definitions */
|
43 |
/* Keyword definitions */
|
44 |
|
44 |
|
45 |
#define I2C_READ 1
|
45 |
#define I2C_READ 1
|
46 |
#define I2C_WRITE 0
|
46 |
#define I2C_WRITE 0
|
47 |
|
47 |
|
48 |
#define I2C_QUIT 0
|
48 |
#define I2C_QUIT 0
|
49 |
#define I2C_CONTINUE 1
|
49 |
#define I2C_CONTINUE 1
|
50 |
|
50 |
|
51 |
#define I2C_NO_ERROR 0
|
51 |
#define I2C_NO_ERROR 0
|
52 |
#define I2C_ERROR_DEVICE_BUSY 1
|
52 |
#define I2C_ERROR_DEVICE_BUSY 1
|
53 |
#define I2C_ERROR_DEVICE_NOT_RESPONDING 2
|
53 |
#define I2C_ERROR_DEVICE_NOT_RESPONDING 2
|
54 |
|
54 |
|
55 |
|
55 |
|
56 |
/* Function Declaration */
|
56 |
/* Function Declaration */
|
57 |
|
57 |
|
58 |
extern void i2c_init(void);
|
58 |
extern void i2c_init(void);
|
59 |
extern void i2c_start(void);
|
59 |
extern void i2c_start(void);
|
60 |
extern void i2c_stop(void);
|
60 |
extern void i2c_stop(void);
|
61 |
extern unsigned char i2c_transmit(unsigned char data);
|
61 |
extern unsigned char i2c_transmit(unsigned char data);
|
62 |
extern unsigned char i2c_receive(unsigned char ack);
|
62 |
extern unsigned char i2c_receive(unsigned char ack);
|
63 |
|
63 |
|
64 |
|
64 |
|
65 |
|
65 |
|
66 |
/* Macro definitions */
|
66 |
/* Macro definitions */
|
67 |
|
67 |
|
68 |
#define I2C_START(ADDRESS) { i2c_start(); i2c_transmit(ADDRESS); }
|
68 |
#define I2C_START(ADDRESS) { i2c_start(); i2c_transmit(ADDRESS); }
|
69 |
#define I2C_START_TX(ADDRESS) I2C_START(ADDRESS)
|
69 |
#define I2C_START_TX(ADDRESS) I2C_START(ADDRESS)
|
70 |
#define I2C_START_RX(ADDRESS) I2C_START(ADDRESS | I2C_READ)
|
70 |
#define I2C_START_RX(ADDRESS) I2C_START(ADDRESS | I2C_READ)
|
71 |
|
71 |
|
72 |
#endif /* #ifndef I2C_H */
|
72 |
#endif /* #ifndef I2C_H */
|
73 |
/*######################################################################################################*/
|
73 |
/*######################################################################################################*/
|
74 |
/* T H E E N D */
|
74 |
/* T H E E N D */
|
75 |
/*######################################################################################################*/
|
75 |
/*######################################################################################################*/
|
76 |
|
76 |
|