Subversion Repositories svnkaklik

Rev

Rev 409 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log

Rev 409 Rev 410
1
 
1
 
2
/****************************************************************************
2
/****************************************************************************
3
 Title	:   C  file for the I2C FUNCTIONS library (i2c.c)
3
 Title	:   C  file for the I2C FUNCTIONS library (i2c.c)
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
#include <io.h>
12
#include <io.h>
13
#include "i2c.h"
13
#include "i2c.h"
14
 
14
 
15
#ifndef CONCAT1
15
#ifndef CONCAT1
16
#define CONCAT1(a, b) CONCAT2(a, b)
16
#define CONCAT1(a, b) CONCAT2(a, b)
17
#endif
17
#endif
18
 
18
 
19
#ifndef CONCAT2
19
#ifndef CONCAT2
20
#define CONCAT2(a, b) a ## b
20
#define CONCAT2(a, b) a ## b
21
#endif
21
#endif
22
 
22
 
23
 
23
 
24
/* Conversion of microseconds to the right value for the delay function */
24
/* Conversion of microseconds to the right value for the delay function */
25
#define I2C_DELAY	   ( (I2C_DELAY_TIME*(F_CPU/60000))/100 )  
25
#define I2C_DELAY	   ( (I2C_DELAY_TIME*(F_CPU/60000))/100 )  
26
#define I2C_TIMEOUT	   ( (I2C_TIMEOUT_TIME*(F_CPU/60000))/100 ) 
26
#define I2C_TIMEOUT	   ( (I2C_TIMEOUT_TIME*(F_CPU/60000))/100 ) 
27
 
27
 
28
/* Register name forming */
28
/* Register name forming */
29
#define I2C_SDA_OUT_REG   CONCAT1(PORT, I2C_SDA_PORT)
29
#define I2C_SDA_OUT_REG   CONCAT1(PORT, I2C_SDA_PORT)
30
#define I2C_SCL_OUT_REG   CONCAT1(PORT, I2C_SCL_PORT)
30
#define I2C_SCL_OUT_REG   CONCAT1(PORT, I2C_SCL_PORT)
31
#define I2C_SDA_DDR_REG   CONCAT1(DDR, I2C_SDA_PORT)
31
#define I2C_SDA_DDR_REG   CONCAT1(DDR, I2C_SDA_PORT)
32
#define I2C_SCL_DDR_REG   CONCAT1(DDR, I2C_SCL_PORT)
32
#define I2C_SCL_DDR_REG   CONCAT1(DDR, I2C_SCL_PORT)
33
#define I2C_SDA_PIN_REG   CONCAT1(PIN, I2C_SDA_PORT)
33
#define I2C_SDA_PIN_REG   CONCAT1(PIN, I2C_SDA_PORT)
34
#define I2C_SCL_PIN_REG   CONCAT1(PIN, I2C_SCL_PORT)
34
#define I2C_SCL_PIN_REG   CONCAT1(PIN, I2C_SCL_PORT)
35
 
35
 
36
/* Conversion of microseconds to the right value for the delay function */
36
/* Conversion of microseconds to the right value for the delay function */
37
#define I2C_DELAY	  ( (I2C_DELAY_TIME*(F_CPU/60000))/100 )  
37
#define I2C_DELAY	  ( (I2C_DELAY_TIME*(F_CPU/60000))/100 )  
38
#define I2C_TIMEOUT	  ( (I2C_TIMEOUT_TIME*(F_CPU/60000))/100 ) 
38
#define I2C_TIMEOUT	  ( (I2C_TIMEOUT_TIME*(F_CPU/60000))/100 ) 
39
 
39
 
40
/* Pin states */
40
/* Pin states */
41
#define SCL_1() 	  cbi(I2C_SCL_DDR_REG, SCL_PIN)
41
#define SCL_1() 	  cbi(I2C_SCL_DDR_REG, SCL_PIN)
42
#define SCL_0() 	  sbi(I2C_SCL_DDR_REG, SCL_PIN)
42
#define SCL_0() 	  sbi(I2C_SCL_DDR_REG, SCL_PIN)
43
#define SDA_1() 	  cbi(I2C_SDA_DDR_REG, SDA_PIN)
43
#define SDA_1() 	  cbi(I2C_SDA_DDR_REG, SDA_PIN)
44
#define SDA_0() 	  sbi(I2C_SDA_DDR_REG, SDA_PIN)
44
#define SDA_0() 	  sbi(I2C_SDA_DDR_REG, SDA_PIN)
45
 
45
 
46
#define RELEASE_I2C_BUS() { SCL_1(); SDA_1(); }
46
#define RELEASE_I2C_BUS() { SCL_1(); SDA_1(); }
47
 
47
 
48
/*#################################################################################################*/
48
/*#################################################################################################*/
49
 
49
 
50
static void delay(unsigned long us)
50
static void delay(unsigned long us)
51
{
51
{
52
 
52
 
53
   while ( us ) { us--; }  /* 6 cpu cycles per loop */
53
   while ( us ) { us--; }  /* 6 cpu cycles per loop */
54
 
54
 
55
}
55
}
56
/*#################################################################################################*/
56
/*#################################################################################################*/
57
 
57
 
58
void i2c_init(void)
58
void i2c_init(void)
59
{
59
{
60
	cbi(I2C_SDA_OUT_REG, SDA_PIN);
60
	cbi(I2C_SDA_OUT_REG, SDA_PIN);
61
	cbi(I2C_SCL_OUT_REG, SCL_PIN);
61
	cbi(I2C_SCL_OUT_REG, SCL_PIN);
62
	RELEASE_I2C_BUS();
62
	RELEASE_I2C_BUS();
63
	delay(I2C_TIMEOUT);
63
	delay(I2C_TIMEOUT);
64
	i2c_start();
64
	i2c_start();
65
	delay(I2C_TIMEOUT);
65
	delay(I2C_TIMEOUT);
66
	i2c_stop();
66
	i2c_stop();
67
	delay(I2C_TIMEOUT);
67
	delay(I2C_TIMEOUT);
68
 
68
 
69
 
69
 
70
return;
70
return;
71
}
71
}
72
/*#################################################################################################*/
72
/*#################################################################################################*/
73
 
73
 
74
void i2c_start(void)
74
void i2c_start(void)
75
{
75
{
76
	RELEASE_I2C_BUS();
76
	RELEASE_I2C_BUS();
77
	delay(I2C_DELAY);
77
	delay(I2C_DELAY);
78
	SDA_0();
78
	SDA_0();
79
	delay(I2C_DELAY);
79
	delay(I2C_DELAY);
80
	SCL_0();
80
	SCL_0();
81
	delay(I2C_DELAY);
81
	delay(I2C_DELAY);
82
 
82
 
83
return;
83
return;
84
}
84
}
85
/*#################################################################################################*/
85
/*#################################################################################################*/
86
 
86
 
87
void i2c_stop(void)
87
void i2c_stop(void)
88
{
88
{
89
	SDA_0();
89
	SDA_0();
90
	SCL_1();
90
	SCL_1();
91
	delay(I2C_DELAY);
91
	delay(I2C_DELAY);
92
	SDA_1();
92
	SDA_1();
93
	delay(I2C_DELAY);
93
	delay(I2C_DELAY);
94
	SCL_0();
94
	SCL_0();
95
	delay(I2C_DELAY);
95
	delay(I2C_DELAY);
96
 
96
 
97
return;
97
return;
98
}
98
}
99
/*#################################################################################################*/
99
/*#################################################################################################*/
100
 
100
 
101
unsigned char i2c_transmit(unsigned char data)
101
unsigned char i2c_transmit(unsigned char data)
102
{
102
{
103
register unsigned char bit=0;
103
register unsigned char bit=0;
104
 
104
 
105
	for(bit=0; bit<=7; bit++)
105
	for(bit=0; bit<=7; bit++)
106
	  {
106
	  {
107
	      if( data & 0x80 ) { SDA_1(); } else { SDA_0(); }
107
	      if( data & 0x80 ) { SDA_1(); } else { SDA_0(); }
108
	      SCL_1();
108
	      SCL_1();
109
	      delay(I2C_DELAY);
109
	      delay(I2C_DELAY);
110
	      SCL_0();
110
	      SCL_0();
111
	      delay(I2C_DELAY);
111
	      delay(I2C_DELAY);
112
	      data = (data<<1);
112
	      data = (data<<1);
113
	  }
113
	  }
114
	/* Look for AKNOWLEDGE */
114
	/* Look for AKNOWLEDGE */
115
	RELEASE_I2C_BUS();
115
	RELEASE_I2C_BUS();
116
	delay(I2C_DELAY);
116
	delay(I2C_DELAY);
117
 
117
 
118
 
118
 
119
	if( bit_is_clear(I2C_SDA_PIN_REG, SDA_PIN) )
119
	if( bit_is_clear(I2C_SDA_PIN_REG, SDA_PIN) )
120
	 {
120
	 {
121
	     SCL_0();
121
	     SCL_0();
122
	     delay(I2C_DELAY);
122
	     delay(I2C_DELAY);
123
	 }
123
	 }
124
	else{
124
	else{
125
		 delay(I2C_TIMEOUT);
125
		 delay(I2C_TIMEOUT);
126
		 if( bit_is_clear(I2C_SDA_PIN_REG, SDA_PIN) )
126
		 if( bit_is_clear(I2C_SDA_PIN_REG, SDA_PIN) )
127
		  {
127
		  {
128
		     SCL_0();
128
		     SCL_0();
129
		     delay(I2C_DELAY);
129
		     delay(I2C_DELAY);
130
		  }
130
		  }
131
		 else { return(I2C_ERROR_DEVICE_NOT_RESPONDING); }
131
		 else { return(I2C_ERROR_DEVICE_NOT_RESPONDING); }
132
	    }
132
	    }
133
 
133
 
134
 
134
 
135
	if( bit_is_clear(I2C_SDA_PIN_REG, SDA_PIN) ) 
135
	if( bit_is_clear(I2C_SDA_PIN_REG, SDA_PIN) ) 
136
	 { 
136
	 { 
137
	       delay(I2C_TIMEOUT);
137
	       delay(I2C_TIMEOUT);
138
	       if( bit_is_clear(I2C_SDA_PIN_REG, SDA_PIN) ) { return(I2C_ERROR_DEVICE_BUSY); }
138
	       if( bit_is_clear(I2C_SDA_PIN_REG, SDA_PIN) ) { return(I2C_ERROR_DEVICE_BUSY); }
139
	 }   
139
	 }   
140
 
140
 
141
 
141
 
142
return(I2C_NO_ERROR);	  
142
return(I2C_NO_ERROR);	  
143
}
143
}
144
/*#################################################################################################*/
144
/*#################################################################################################*/
145
 
145
 
146
unsigned char i2c_receive(unsigned char ack)
146
unsigned char i2c_receive(unsigned char ack)
147
{
147
{
148
register unsigned char bit=0, data=0;
148
register unsigned char bit=0, data=0;
149
 
149
 
150
	SDA_1();
150
	SDA_1();
151
	for(bit=0; bit<=7; bit++)
151
	for(bit=0; bit<=7; bit++)
152
	  {
152
	  {
153
	      SCL_1();
153
	      SCL_1();
154
	      delay(I2C_DELAY);
154
	      delay(I2C_DELAY);
155
	      data = (data<<1);
155
	      data = (data<<1);
156
	      if( bit_is_set(I2C_SDA_PIN_REG, SDA_PIN) ) { data++; }
156
	      if( bit_is_set(I2C_SDA_PIN_REG, SDA_PIN) ) { data++; }
157
	      SCL_0();
157
	      SCL_0();
158
	      delay(I2C_DELAY);
158
	      delay(I2C_DELAY);
159
	  }
159
	  }
160
	
160
	
161
	/* if CONTINUE then send AKNOWLEDGE else if QUIT do not send AKNOWLEDGE (send Nack) */	     
161
	/* if CONTINUE then send AKNOWLEDGE else if QUIT do not send AKNOWLEDGE (send Nack) */	     
162
	if(ack==I2C_CONTINUE) { SDA_0(); }  else { SDA_1(); }
162
	if(ack==I2C_CONTINUE) { SDA_0(); }  else { SDA_1(); }
163
	SCL_1();
163
	SCL_1();
164
	delay(I2C_DELAY);
164
	delay(I2C_DELAY);
165
	SCL_0();
165
	SCL_0();
166
	delay(I2C_DELAY);
166
	delay(I2C_DELAY);
167
 
167
 
168
return data;
168
return data;
169
}
169
}
170
/*#################################################################################################*/
170
/*#################################################################################################*/
171
 
171
 
172
 
172