?lang_form? ?lang_select? ?lang_submit? ?lang_endform?
{HEADER END}
{BLAME START}

library

?curdirlinks? -

Blame information for rev 6

Line No. Rev Author Line
1 6 kaklik <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2 <html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
3 <title>Procyon AVRlib: i2csw.c Source File</title>
4 <link href="dox.css" rel="stylesheet" type="text/css">
5 </head><body>
6 <!-- Generated by Doxygen 1.4.2 -->
7 <div class="qindex"><a class="qindex" href="main.html">Main&nbsp;Page</a> | <a class="qindex" href="modules.html">Modules</a> | <a class="qindex" href="annotated.html">Data&nbsp;Structures</a> | <a class="qindex" href="dirs.html">Directories</a> | <a class="qindex" href="files.html">File&nbsp;List</a> | <a class="qindex" href="functions.html">Data&nbsp;Fields</a> | <a class="qindex" href="globals.html">Globals</a> | <a class="qindex" href="pages.html">Related&nbsp;Pages</a></div>
8 <h1>i2csw.c</h1><a href="i2csw_8c.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment">00001 <span class="comment">/*! \file i2csw.c \brief Software I2C interface using port pins. */</span>
9 00002 <span class="comment">//*****************************************************************************</span>
10 00003 <span class="comment">//</span>
11 00004 <span class="comment">// File Name : 'i2csw.c'</span>
12 00005 <span class="comment">// Title : Software I2C interface using port pins</span>
13 00006 <span class="comment">// Author : Pascal Stang</span>
14 00007 <span class="comment">// Created : 11/22/2000</span>
15 00008 <span class="comment">// Revised : 5/2/2002</span>
16 00009 <span class="comment">// Version : 1.1</span>
17 00010 <span class="comment">// Target MCU : Atmel AVR series</span>
18 00011 <span class="comment">// Editor Tabs : 4</span>
19 00012 <span class="comment">//</span>
20 00013 <span class="comment">// This code is distributed under the GNU Public License</span>
21 00014 <span class="comment">// which can be found at http://www.gnu.org/licenses/gpl.txt</span>
22 00015 <span class="comment">//</span>
23 00016 <span class="comment">//*****************************************************************************</span>
24 00017
25 00018 <span class="preprocessor">#include &lt;avr/io.h&gt;</span>
26 00019
27 00020 <span class="preprocessor">#include "<a class="code" href="i2csw_8h.html">i2csw.h</a>"</span>
28 00021
29 00022 <span class="comment">// Standard I2C bit rates are:</span>
30 00023 <span class="comment">// 100KHz for slow speed</span>
31 00024 <span class="comment">// 400KHz for high speed</span>
32 00025
33 00026 <span class="comment">//#define QDEL delay(5) // i2c quarter-bit delay</span>
34 00027 <span class="comment">//#define HDEL delay(10) // i2c half-bit delay</span>
35 00028
36 00029 <span class="comment">// i2c quarter-bit delay</span>
37 00030 <span class="preprocessor">#define QDEL asm volatile("nop"); asm volatile("nop"); asm volatile("nop"); asm volatile("nop"); asm volatile("nop");</span>
38 00031 <span class="preprocessor"></span><span class="comment">// i2c half-bit delay</span>
39 00032 <span class="preprocessor">#define HDEL asm volatile("nop"); asm volatile("nop"); asm volatile("nop"); asm volatile("nop"); asm volatile("nop"); asm volatile("nop"); asm volatile("nop"); asm volatile("nop"); asm volatile("nop"); asm volatile("nop");</span>
40 00033 <span class="preprocessor"></span>
41 00034 <span class="preprocessor">#define I2C_SDL_LO cbi( SDAPORT, SDA)</span>
42 00035 <span class="preprocessor"></span><span class="preprocessor">#define I2C_SDL_HI sbi( SDAPORT, SDA)</span>
43 00036 <span class="preprocessor"></span>
44 00037 <span class="preprocessor">#define I2C_SCL_LO cbi( SCLPORT, SCL); </span>
45 00038 <span class="preprocessor"></span><span class="preprocessor">#define I2C_SCL_HI sbi( SCLPORT, SCL); </span>
46 00039 <span class="preprocessor"></span>
47 00040 <span class="preprocessor">#define I2C_SCL_TOGGLE HDEL; I2C_SCL_HI; HDEL; I2C_SCL_LO;</span>
48 00041 <span class="preprocessor"></span><span class="preprocessor">#define I2C_START I2C_SDL_LO; QDEL; I2C_SCL_LO; </span>
49 00042 <span class="preprocessor"></span><span class="preprocessor">#define I2C_STOP HDEL; I2C_SCL_HI; QDEL; I2C_SDL_HI; HDEL;</span>
50 00043 <span class="preprocessor"></span>
51 00044 <span class="comment">/*</span>
52 00045 <span class="comment">void i2ct(void)</span>
53 00046 <span class="comment">{</span>
54 00047 <span class="comment"> HDEL; I2C_SCL_HI; HDEL; I2C_SCL_LO;</span>
55 00048 <span class="comment">}</span>
56 00049 <span class="comment"></span>
57 00050 <span class="comment">void i2cstart(void)</span>
58 00051 <span class="comment">{</span>
59 00052 <span class="comment"> I2C_SDL_LO; QDEL; I2C_SCL_LO; </span>
60 00053 <span class="comment">}</span>
61 00054 <span class="comment"></span>
62 00055 <span class="comment">void i2cstop(void)</span>
63 00056 <span class="comment">{</span>
64 00057 <span class="comment"> HDEL; I2C_SCL_HI; QDEL; I2C_SDL_HI; HDEL;</span>
65 00058 <span class="comment">}</span>
66 00059 <span class="comment"></span>
67 00060 <span class="comment"></span>
68 00061 <span class="comment">#define I2C_SCL_TOGGLE i2ct();</span>
69 00062 <span class="comment">#define I2C_START i2cstart();</span>
70 00063 <span class="comment">#define I2C_STOP i2cstop(); </span>
71 00064 <span class="comment">*/</span>
72 00065
73 00066 UINT i2cPutbyte(u08 b)
74 00067 {
75 00068 <span class="keywordtype">int</span> i;
76 00069
77 00070 <span class="keywordflow">for</span> (i=7;i&gt;=0;i--)
78 00071 {
79 00072 <span class="keywordflow">if</span> ( b &amp; (1&lt;&lt;i) )
80 00073 I2C_SDL_HI;
81 00074 <span class="keywordflow">else</span>
82 00075 I2C_SDL_LO; <span class="comment">// address bit</span>
83 00076 I2C_SCL_TOGGLE; <span class="comment">// clock HI, delay, then LO</span>
84 00077 }
85 00078
86 00079 I2C_SDL_HI; <span class="comment">// leave SDL HI</span>
87 00080 <span class="comment">// added </span>
88 00081 cbi(SDADDR, SDA); <span class="comment">// change direction to input on SDA line (may not be needed)</span>
89 00082 HDEL;
90 00083 I2C_SCL_HI; <span class="comment">// clock back up</span>
91 00084 b = inb(SDAPIN) &amp; (1&lt;&lt;SDA); <span class="comment">// get the ACK bit</span>
92 00085
93 00086 HDEL;
94 00087 I2C_SCL_LO; <span class="comment">// not really ??</span>
95 00088 sbi(SDADDR, SDA); <span class="comment">// change direction back to output</span>
96 00089 HDEL;
97 00090 <span class="keywordflow">return</span> (b == 0); <span class="comment">// return ACK value</span>
98 00091 }
99 00092
100 00093
101 00094 u08 i2cGetbyte(UINT last)
102 00095 {
103 00096 <span class="keywordtype">int</span> i;
104 00097 u08 c,b = 0;
105 00098
106 00099 I2C_SDL_HI; <span class="comment">// make sure pullups are ativated</span>
107 00100 cbi(SDADDR, SDA); <span class="comment">// change direction to input on SDA line (may not be needed)</span>
108 00101
109 00102 <span class="keywordflow">for</span>(i=7;i&gt;=0;i--)
110 00103 {
111 00104 HDEL;
112 00105 I2C_SCL_HI; <span class="comment">// clock HI</span>
113 00106 c = inb(SDAPIN) &amp; (1&lt;&lt;SDA);
114 00107 b &lt;&lt;= 1;
115 00108 <span class="keywordflow">if</span>(c) b |= 1;
116 00109 HDEL;
117 00110 I2C_SCL_LO; <span class="comment">// clock LO</span>
118 00111 }
119 00112
120 00113 sbi(SDADDR, SDA); <span class="comment">// change direction to output on SDA line</span>
121 00114
122 00115 <span class="keywordflow">if</span> (last)
123 00116 I2C_SDL_HI; <span class="comment">// set NAK</span>
124 00117 <span class="keywordflow">else</span>
125 00118 I2C_SDL_LO; <span class="comment">// set ACK</span>
126 00119
127 00120 I2C_SCL_TOGGLE; <span class="comment">// clock pulse</span>
128 00121 I2C_SDL_HI; <span class="comment">// leave with SDL HI</span>
129 00122 <span class="keywordflow">return</span> b; <span class="comment">// return received byte</span>
130 00123 }
131 00124
132 00125
133 00126 <span class="comment">//************************</span>
134 00127 <span class="comment">//* I2C public functions *</span>
135 00128 <span class="comment">//************************</span>
136 00129 <span class="comment"></span>
137 00130 <span class="comment">//! Initialize I2C communication</span>
138 <a name="l00131"></a><a class="code" href="i2csw_8c.html#a11">00131</a> <span class="comment"></span><span class="keywordtype">void</span> <a class="code" href="i2c_8c.html#a10">i2cInit</a>(<span class="keywordtype">void</span>)
139 00132 {
140 00133 sbi( SDADDR, SDA); <span class="comment">// set SDA as output</span>
141 00134 sbi( SCLDDR, SCL); <span class="comment">// set SCL as output</span>
142 00135 I2C_SDL_HI; <span class="comment">// set I/O state and pull-ups</span>
143 00136 I2C_SCL_HI; <span class="comment">// set I/O state and pull-ups</span>
144 00137 }
145 00138 <span class="comment"></span>
146 00139 <span class="comment">//! Send a byte sequence on the I2C bus</span>
147 <a name="l00140"></a><a class="code" href="i2csw_8c.html#a12">00140</a> <span class="comment"></span><span class="keywordtype">void</span> <a class="code" href="i2csw_8c.html#a12">i2cSend</a>(u08 device, u08 subAddr, u08 length, u08 *data)
148 00141 {
149 00142 I2C_START; <span class="comment">// do start transition</span>
150 00143 i2cPutbyte(device); <span class="comment">// send DEVICE address</span>
151 00144 i2cPutbyte(subAddr); <span class="comment">// and the subaddress</span>
152 00145
153 00146 <span class="comment">// send the data</span>
154 00147 <span class="keywordflow">while</span> (length--)
155 00148 i2cPutbyte(*data++);
156 00149
157 00150 I2C_SDL_LO; <span class="comment">// clear data line and</span>
158 00151 I2C_STOP; <span class="comment">// send STOP transition</span>
159 00152 }
160 00153 <span class="comment"></span>
161 00154 <span class="comment">//! Retrieve a byte sequence on the I2C bus</span>
162 <a name="l00155"></a><a class="code" href="i2csw_8c.html#a13">00155</a> <span class="comment"></span><span class="keywordtype">void</span> <a class="code" href="i2csw_8c.html#a13">i2cReceive</a>(u08 device, u08 subAddr, u08 length, u08 *data)
163 00156 {
164 00157 <span class="keywordtype">int</span> j = length;
165 00158 u08 *p = data;
166 00159
167 00160 I2C_START; <span class="comment">// do start transition</span>
168 00161 i2cPutbyte(device); <span class="comment">// send DEVICE address</span>
169 00162 i2cPutbyte(subAddr); <span class="comment">// and the subaddress</span>
170 00163 HDEL;
171 00164 I2C_SCL_HI; <span class="comment">// do a repeated START</span>
172 00165 I2C_START; <span class="comment">// transition</span>
173 00166
174 00167 i2cPutbyte(device | READ); <span class="comment">// resend DEVICE, with READ bit set</span>
175 00168
176 00169 <span class="comment">// receive data bytes</span>
177 00170 <span class="keywordflow">while</span> (j--)
178 00171 *p++ = i2cGetbyte(j == 0);
179 00172
180 00173 I2C_SDL_LO; <span class="comment">// clear data line and</span>
181 00174 I2C_STOP; <span class="comment">// send STOP transition</span>
182 00175 }
183 00176
184 </pre></div><hr size="1"><address style="align: right;"><small>Generated on Sun Oct 29 03:41:07 2006 for Procyon AVRlib by&nbsp;
185 <a href="http://www.doxygen.org/index.html">
186 <img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.4.2 </small></address>
187 </body>
188 </html>
{BLAME END}
{FOOTER START}

Powered by WebSVN v2.8.3