?? i2c_8c-source.html
字號:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"><title>Procyon AVRlib: i2c.c Source File</title><link href="dox.css" rel="stylesheet" type="text/css"></head><body><!-- Generated by Doxygen 1.3.6 --><div class="qindex"><a class="qindex" href="main.html">Main Page</a> | <a class="qindex" href="modules.html">Modules</a> | <a class="qindex" href="annotated.html">Data Structures</a> | <a class="qindex" href="files.html">File List</a> | <a class="qindex" href="functions.html">Data Fields</a> | <a class="qindex" href="globals.html">Globals</a></div><h1>i2c.c</h1><a href="i2c_8c.html">Go to the documentation of this file.</a><div class="fragment"><pre>00001 <span class="comment">/*! \file i2c.c \brief I2C interface using AVR Two-Wire Interface (TWI) hardware. */</span>00002 <span class="comment">//*****************************************************************************</span>00003 <span class="comment">//</span>00004 <span class="comment">// File Name : 'i2c.c'</span>00005 <span class="comment">// Title : I2C interface using AVR Two-Wire Interface (TWI) hardware</span>00006 <span class="comment">// Author : Pascal Stang - Copyright (C) 2002-2003</span>00007 <span class="comment">// Created : 2002.06.25</span>00008 <span class="comment">// Revised : 2003.03.02</span>00009 <span class="comment">// Version : 0.9</span>00010 <span class="comment">// Target MCU : Atmel AVR series</span>00011 <span class="comment">// Editor Tabs : 4</span>00012 <span class="comment">//</span>00013 <span class="comment">// Description : I2C (pronounced "eye-squared-see") is a two-wire bidirectional</span>00014 <span class="comment">// network designed for easy transfer of information between a wide variety</span>00015 <span class="comment">// of intelligent devices. Many of the Atmel AVR series processors have</span>00016 <span class="comment">// hardware support for transmitting and receiving using an I2C-type bus.</span>00017 <span class="comment">// In addition to the AVRs, there are thousands of other parts made by</span>00018 <span class="comment">// manufacturers like Philips, Maxim, National, TI, etc that use I2C as</span>00019 <span class="comment">// their primary means of communication and control. Common device types</span>00020 <span class="comment">// are A/D & D/A converters, temp sensors, intelligent battery monitors,</span>00021 <span class="comment">// MP3 decoder chips, EEPROM chips, multiplexing switches, etc.</span>00022 <span class="comment">//</span>00023 <span class="comment">// I2C uses only two wires (SDA and SCL) to communicate bidirectionally</span>00024 <span class="comment">// between devices. I2C is a multidrop network, meaning that you can have</span>00025 <span class="comment">// several devices on a single bus. Because I2C uses a 7-bit number to</span>00026 <span class="comment">// identify which device it wants to talk to, you cannot have more than</span>00027 <span class="comment">// 127 devices on a single bus.</span>00028 <span class="comment">//</span>00029 <span class="comment">// I2C ordinarily requires two 4.7K pull-up resistors to power (one each on</span>00030 <span class="comment">// SDA and SCL), but for small numbers of devices (maybe 1-4), it is enough</span>00031 <span class="comment">// to activate the internal pull-up resistors in the AVR processor. To do</span>00032 <span class="comment">// this, set the port pins, which correspond to the I2C pins SDA/SCL, high.</span>00033 <span class="comment">// For example, on the mega163, sbi(PORTC, 0); sbi(PORTC, 1);.</span>00034 <span class="comment">//</span>00035 <span class="comment">// For complete information about I2C, see the Philips Semiconductor</span>00036 <span class="comment">// website. They created I2C and have the largest family of devices that</span>00037 <span class="comment">// work with I2C.</span>00038 <span class="comment">//</span>00039 <span class="comment">// Note: Many manufacturers market I2C bus devices under a different or generic</span>00040 <span class="comment">// bus name like "Two-Wire Interface". This is because Philips still holds</span>00041 <span class="comment">// "I2C" as a trademark. For example, SMBus and SMBus devices are hardware</span>00042 <span class="comment">// compatible and closely related to I2C. They can be directly connected</span>00043 <span class="comment">// to an I2C bus along with other I2C devices are are generally accessed in</span>00044 <span class="comment">// the same way as I2C devices. SMBus is often found on modern motherboards</span>00045 <span class="comment">// for temp sensing and other low-level control tasks.</span>00046 <span class="comment">//</span>00047 <span class="comment">// This code is distributed under the GNU Public License</span>00048 <span class="comment">// which can be found at http://www.gnu.org/licenses/gpl.txt</span>00049 <span class="comment">//</span>00050 <span class="comment">//*****************************************************************************</span>00051 00052 <span class="preprocessor">#include <avr/io.h></span>00053 <span class="preprocessor">#include <avr/signal.h></span>00054 <span class="preprocessor">#include <avr/interrupt.h></span>00055 <span class="preprocessor">#include <avr/twi.h></span>00056 00057 <span class="preprocessor">#include "<a class="code" href="i2c_8h.html">i2c.h</a>"</span>00058 00059 <span class="preprocessor">#include "<a class="code" href="rprintf_8h.html">rprintf.h</a>"</span> <span class="comment">// include printf function library</span>00060 <span class="preprocessor">#include "<a class="code" href="uart2_8h.html">uart2.h</a>"</span>00061 00062 <span class="comment">// Standard I2C bit rates are:</span>00063 <span class="comment">// 100KHz for slow speed</span>00064 <span class="comment">// 400KHz for high speed</span>00065 00066 <span class="comment">//#define I2C_DEBUG</span>00067 00068 <span class="comment">// I2C state and address variables</span>00069 <span class="keyword">static</span> <span class="keyword">volatile</span> eI2cStateType I2cState;00070 <span class="keyword">static</span> u08 I2cDeviceAddrRW;00071 <span class="comment">// send/transmit buffer (outgoing data)</span>00072 <span class="keyword">static</span> u08 I2cSendData[I2C_SEND_DATA_BUFFER_SIZE];00073 <span class="keyword">static</span> u08 I2cSendDataIndex;00074 <span class="keyword">static</span> u08 I2cSendDataLength;00075 <span class="comment">// receive buffer (incoming data)</span>00076 <span class="keyword">static</span> u08 I2cReceiveData[I2C_RECEIVE_DATA_BUFFER_SIZE];00077 <span class="keyword">static</span> u08 I2cReceiveDataIndex;00078 <span class="keyword">static</span> u08 I2cReceiveDataLength;00079 00080 <span class="comment">// function pointer to i2c receive routine</span><span class="comment"></span>00081 <span class="comment">//! I2cSlaveReceive is called when this processor</span>00082 <span class="comment"></span><span class="comment">// is addressed as a slave for writing</span>00083 <span class="keyword">static</span> void (*i2cSlaveReceive)(u08 receiveDataLength, u08* recieveData);<span class="comment"></span>00084 <span class="comment">//! I2cSlaveTransmit is called when this processor</span>00085 <span class="comment"></span><span class="comment">// is addressed as a slave for reading</span>00086 <span class="keyword">static</span> u08 (*i2cSlaveTransmit)(u08 transmitDataLengthMax, u08* transmitData);00087 00088 <span class="comment">// functions</span><a name="l00089"></a><a class="code" href="i2csw_8h.html#a1">00089</a> <span class="keywordtype">void</span> <a class="code" href="i2c_8c.html#a10">i2cInit</a>(<span class="keywordtype">void</span>)00090 {00091 <span class="comment">// set pull-up resistors on I2C bus pins</span>00092 <span class="comment">// TODO: should #ifdef these</span>00093 sbi(PORTC, 0); <span class="comment">// i2c SCL on ATmega163,323,16,32,etc</span>00094 sbi(PORTC, 1); <span class="comment">// i2c SDA on ATmega163,323,16,32,etc</span>00095 sbi(PORTD, 0); <span class="comment">// i2c SCL on ATmega128,64</span>00096 sbi(PORTD, 1); <span class="comment">// i2c SDA on ATmega128,64</span>00097 00098 <span class="comment">// clear SlaveReceive and SlaveTransmit handler to null</span>00099 i2cSlaveReceive = 0;00100 i2cSlaveTransmit = 0;00101 <span class="comment">// set i2c bit rate to 100KHz</span>00102 <a class="code" href="i2c_8h.html#a11">i2cSetBitrate</a>(100);00103 <span class="comment">// enable TWI (two-wire interface)</span>00104 sbi(TWCR, TWEN);00105 <span class="comment">// set state</span>00106 I2cState = I2C_IDLE;00107 <span class="comment">// enable TWI interrupt and slave address ACK</span>00108 sbi(TWCR, TWIE);00109 sbi(TWCR, TWEA);00110 <span class="comment">//outb(TWCR, (inb(TWCR)&TWCR_CMD_MASK)|BV(TWINT)|BV(TWEA));</span>00111 <span class="comment">// enable interrupts</span>00112 sei();00113 }00114 <a name="l00115"></a><a class="code" href="i2c_8h.html#a11">00115</a> <span class="keywordtype">void</span> <a class="code" href="i2c_8h.html#a11">i2cSetBitrate</a>(u16 bitrateKHz)00116 {00117 u08 bitrate_div;00118 <span class="comment">// set i2c bitrate</span>00119 <span class="comment">// SCL freq = F_CPU/(16+2*TWBR))</span>00120 <span class="preprocessor"> #ifdef TWPS0</span>00121 <span class="preprocessor"></span> <span class="comment">// for processors with additional bitrate division (mega128)</span>00122 <span class="comment">// SCL freq = F_CPU/(16+2*TWBR*4^TWPS)</span>00123 <span class="comment">// set TWPS to zero</span>00124 cbi(TWSR, TWPS0);00125 cbi(TWSR, TWPS1);00126 <span class="preprocessor"> #endif</span>00127 <span class="preprocessor"></span> <span class="comment">// calculate bitrate division </span>00128 bitrate_div = ((F_CPU/1000l)/bitrateKHz);00129 <span class="keywordflow">if</span>(bitrate_div >= 16)00130 bitrate_div = (bitrate_div-16)/2;00131 outb(TWBR, bitrate_div);00132 }00133 <a name="l00134"></a><a class="code" href="i2c_8h.html#a12">00134</a> <span class="keywordtype">void</span> <a class="code" href="i2c_8h.html#a12">i2cSetLocalDeviceAddr</a>(u08 deviceAddr, u08 genCallEn)00135 {00136 <span class="comment">// set local device address (used in slave mode only)</span>00137 outb(TWAR, ((deviceAddr&0xFE) | (genCallEn?1:0)) );00138 }00139 <a name="l00140"></a><a class="code" href="i2c_8h.html#a13">00140</a> <span class="keywordtype">void</span> <a class="code" href="i2c_8h.html#a13">i2cSetSlaveReceiveHandler</a>(<span class="keywordtype">void</span> (*i2cSlaveRx_func)(u08 receiveDataLength, u08* recieveData))00141 {00142 i2cSlaveReceive = i2cSlaveRx_func;00143 }00144 <a name="l00145"></a><a class="code" href="i2c_8h.html#a14">00145</a> <span class="keywordtype">void</span> <a class="code" href="i2c_8h.html#a14">i2cSetSlaveTransmitHandler</a>(u08 (*i2cSlaveTx_func)(u08 transmitDataLengthMax, u08* transmitData))00146 {00147 i2cSlaveTransmit = i2cSlaveTx_func;00148 }00149 <a name="l00150"></a><a class="code" href="i2c_8h.html#a15">00150</a> <span class="keyword">inline</span> <span class="keywordtype">void</span> <a class="code" href="i2c_8c.html#a15">i2cSendStart</a>(<span class="keywordtype">void</span>)00151 {00152 <span class="comment">// send start condition</span>00153 outb(TWCR, (inb(TWCR)&TWCR_CMD_MASK)|BV(TWINT)|BV(TWSTA));00154 }00155 <a name="l00156"></a><a class="code" href="i2c_8h.html#a16">00156</a> <span class="keyword">inline</span> <span class="keywordtype">void</span> <a class="code" href="i2c_8c.html#a16">i2cSendStop</a>(<span class="keywordtype">void</span>)00157 {00158 <span class="comment">// transmit stop condition</span>00159 <span class="comment">// leave with TWEA on for slave receiving</span>00160 outb(TWCR, (inb(TWCR)&TWCR_CMD_MASK)|BV(TWINT)|BV(TWEA)|BV(TWSTO));00161 }00162 <a name="l00163"></a><a class="code" href="i2c_8h.html#a17">00163</a> <span class="keyword">inline</span> <span class="keywordtype">void</span> <a class="code" href="i2c_8c.html#a17">i2cWaitForComplete</a>(<span class="keywordtype">void</span>)00164 {00165 <span class="comment">// wait for i2c interface to complete operation</span>00166 <span class="keywordflow">while</span>( !(inb(TWCR) & BV(TWINT)) );00167 }00168 <a name="l00169"></a><a class="code" href="i2c_8h.html#a18">00169</a> <span class="keyword">inline</span> <span class="keywordtype">void</span> <a class="code" href="i2c_8h.html#a18">i2cSendByte</a>(u08 data)00170 {00171 <span class="comment">// save data to the TWDR</span>00172 outb(TWDR, data);00173 <span class="comment">// begin send</span>00174 outb(TWCR, (inb(TWCR)&TWCR_CMD_MASK)|BV(TWINT));00175 }00176 <a name="l00177"></a><a class="code" href="i2c_8h.html#a19">00177</a> <span class="keyword">inline</span> <span class="keywordtype">void</span> <a class="code" href="i2c_8h.html#a19">i2cReceiveByte</a>(u08 ackFlag)00178 {00179 <span class="comment">// begin receive over i2c</span>00180 <span class="keywordflow">if</span>( ackFlag )00181 {00182 <span class="comment">// ackFlag = TRUE: ACK the recevied data</span>00183 outb(TWCR, (inb(TWCR)&TWCR_CMD_MASK)|BV(TWINT)|BV(TWEA));00184 }00185 <span class="keywordflow">else</span>00186 {00187 <span class="comment">// ackFlag = FALSE: NACK the recevied data</span>00188 outb(TWCR, (inb(TWCR)&TWCR_CMD_MASK)|BV(TWINT));00189 }00190 }00191 <a name="l00192"></a><a class="code" href="i2c_8h.html#a20">00192</a> <span class="keyword">inline</span> u08 <a class="code" href="i2c_8c.html#a20">i2cGetReceivedByte</a>(<span class="keywordtype">void</span>)00193 {00194 <span class="comment">// retieve received data byte from i2c TWDR</span>00195 <span class="keywordflow">return</span>( inb(TWDR) );00196 }00197 <a name="l00198"></a><a class="code" href="i2c_8h.html#a21">00198</a> <span class="keyword">inline</span> u08 <a class="code" href="i2c_8c.html#a21">i2cGetStatus</a>(<span class="keywordtype">void</span>)00199 {00200 <span class="comment">// retieve current i2c status from i2c TWSR</span>00201 <span class="keywordflow">return</span>( inb(TWSR) );00202 }00203 <a name="l00204"></a><a class="code" href="i2c_8h.html#a22">00204</a> <span class="keywordtype">void</span> <a class="code" href="i2c_8h.html#a22">i2cMasterSend</a>(u08 deviceAddr, u08 length, u08* data)00205 {00206 u08 i;00207 <span class="comment">// wait for interface to be ready</span>00208 <span class="keywordflow">while</span>(I2cState);00209 <span class="comment">// set state</span>00210 I2cState = I2C_MASTER_TX;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -