📄 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.4.2 --><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="dirs.html">Directories</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> | <a class="qindex" href="pages.html">Related Pages</a></div><h1>i2c.c</h1><a href="i2c_8c.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment">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">// This code is distributed under the GNU Public License</span>00014 <span class="comment">// which can be found at http://www.gnu.org/licenses/gpl.txt</span>00015 <span class="comment">//</span>00016 <span class="comment">//*****************************************************************************</span>00017 00018 <span class="preprocessor">#include <avr/io.h></span>00019 <span class="preprocessor">#include <avr/interrupt.h></span>00020 00021 <span class="preprocessor">#include "<a class="code" href="i2c_8h.html">i2c.h</a>"</span>00022 00023 <span class="preprocessor">#include "<a class="code" href="rprintf_8h.html">rprintf.h</a>"</span> <span class="comment">// include printf function library</span>00024 <span class="preprocessor">#include "<a class="code" href="uart2_8h.html">uart2.h</a>"</span>00025 00026 <span class="comment">// Standard I2C bit rates are:</span>00027 <span class="comment">// 100KHz for slow speed</span>00028 <span class="comment">// 400KHz for high speed</span>00029 00030 <span class="comment">//#define I2C_DEBUG</span>00031 00032 <span class="comment">// I2C state and address variables</span>00033 <span class="keyword">static</span> <span class="keyword">volatile</span> eI2cStateType I2cState;00034 <span class="keyword">static</span> u08 I2cDeviceAddrRW;00035 <span class="comment">// send/transmit buffer (outgoing data)</span>00036 <span class="keyword">static</span> u08 I2cSendData[I2C_SEND_DATA_BUFFER_SIZE];00037 <span class="keyword">static</span> u08 I2cSendDataIndex;00038 <span class="keyword">static</span> u08 I2cSendDataLength;00039 <span class="comment">// receive buffer (incoming data)</span>00040 <span class="keyword">static</span> u08 I2cReceiveData[I2C_RECEIVE_DATA_BUFFER_SIZE];00041 <span class="keyword">static</span> u08 I2cReceiveDataIndex;00042 <span class="keyword">static</span> u08 I2cReceiveDataLength;00043 00044 <span class="comment">// function pointer to i2c receive routine</span><span class="comment"></span>00045 <span class="comment">//! I2cSlaveReceive is called when this processor</span>00046 <span class="comment"></span><span class="comment">// is addressed as a slave for writing</span>00047 <span class="keyword">static</span> void (*i2cSlaveReceive)(u08 receiveDataLength, u08* recieveData);<span class="comment"></span>00048 <span class="comment">//! I2cSlaveTransmit is called when this processor</span>00049 <span class="comment"></span><span class="comment">// is addressed as a slave for reading</span>00050 <span class="keyword">static</span> u08 (*i2cSlaveTransmit)(u08 transmitDataLengthMax, u08* transmitData);00051 00052 <span class="comment">// functions</span><a name="l00053"></a><a class="code" href="i2csw_8h.html#a1">00053</a> <span class="keywordtype">void</span> <a class="code" href="i2c_8c.html#a10">i2cInit</a>(<span class="keywordtype">void</span>)00054 {00055 <span class="comment">// set pull-up resistors on I2C bus pins</span>00056 <span class="comment">// TODO: should #ifdef these</span>00057 sbi(PORTC, 0); <span class="comment">// i2c SCL on ATmega163,323,16,32,etc</span>00058 sbi(PORTC, 1); <span class="comment">// i2c SDA on ATmega163,323,16,32,etc</span>00059 sbi(PORTD, 0); <span class="comment">// i2c SCL on ATmega128,64</span>00060 sbi(PORTD, 1); <span class="comment">// i2c SDA on ATmega128,64</span>00061 00062 <span class="comment">// clear SlaveReceive and SlaveTransmit handler to null</span>00063 i2cSlaveReceive = 0;00064 i2cSlaveTransmit = 0;00065 <span class="comment">// set i2c bit rate to 100KHz</span>00066 <a class="code" href="i2c_8c.html#a11">i2cSetBitrate</a>(100);00067 <span class="comment">// enable TWI (two-wire interface)</span>00068 sbi(TWCR, TWEN);00069 <span class="comment">// set state</span>00070 I2cState = I2C_IDLE;00071 <span class="comment">// enable TWI interrupt and slave address ACK</span>00072 sbi(TWCR, TWIE);00073 sbi(TWCR, TWEA);00074 <span class="comment">//outb(TWCR, (inb(TWCR)&TWCR_CMD_MASK)|BV(TWINT)|BV(TWEA));</span>00075 <span class="comment">// enable interrupts</span>00076 sei();00077 }00078 <a name="l00079"></a><a class="code" href="i2c_8h.html#a39">00079</a> <span class="keywordtype">void</span> <a class="code" href="i2c_8c.html#a11">i2cSetBitrate</a>(u16 bitrateKHz)00080 {00081 u08 bitrate_div;00082 <span class="comment">// set i2c bitrate</span>00083 <span class="comment">// SCL freq = F_CPU/(16+2*TWBR))</span>00084 <span class="preprocessor"> #ifdef TWPS0</span>00085 <span class="preprocessor"></span> <span class="comment">// for processors with additional bitrate division (mega128)</span>00086 <span class="comment">// SCL freq = F_CPU/(16+2*TWBR*4^TWPS)</span>00087 <span class="comment">// set TWPS to zero</span>00088 cbi(TWSR, TWPS0);00089 cbi(TWSR, TWPS1);00090 <span class="preprocessor"> #endif</span>00091 <span class="preprocessor"></span> <span class="comment">// calculate bitrate division </span>00092 bitrate_div = ((F_CPU/1000l)/bitrateKHz);00093 <span class="keywordflow">if</span>(bitrate_div >= 16)00094 bitrate_div = (bitrate_div-16)/2;00095 outb(TWBR, bitrate_div);00096 }00097 <a name="l00098"></a><a class="code" href="i2c_8h.html#a40">00098</a> <span class="keywordtype">void</span> <a class="code" href="i2c_8c.html#a12">i2cSetLocalDeviceAddr</a>(u08 deviceAddr, u08 genCallEn)00099 {00100 <span class="comment">// set local device address (used in slave mode only)</span>00101 outb(TWAR, ((deviceAddr&0xFE) | (genCallEn?1:0)) );00102 }00103 <a name="l00104"></a><a class="code" href="i2c_8h.html#a41">00104</a> <span class="keywordtype">void</span> <a class="code" href="i2c_8c.html#a13">i2cSetSlaveReceiveHandler</a>(<span class="keywordtype">void</span> (*i2cSlaveRx_func)(u08 receiveDataLength, u08* recieveData))00105 {00106 i2cSlaveReceive = i2cSlaveRx_func;00107 }00108 <a name="l00109"></a><a class="code" href="i2c_8h.html#a42">00109</a> <span class="keywordtype">void</span> <a class="code" href="i2c_8c.html#a14">i2cSetSlaveTransmitHandler</a>(u08 (*i2cSlaveTx_func)(u08 transmitDataLengthMax, u08* transmitData))00110 {00111 i2cSlaveTransmit = i2cSlaveTx_func;00112 }00113 <a name="l00114"></a><a class="code" href="i2c_8h.html#a43">00114</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>)00115 {00116 <span class="comment">// send start condition</span>00117 outb(TWCR, (inb(TWCR)&TWCR_CMD_MASK)|BV(TWINT)|BV(TWSTA));00118 }00119 <a name="l00120"></a><a class="code" href="i2c_8h.html#a44">00120</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>)00121 {00122 <span class="comment">// transmit stop condition</span>00123 <span class="comment">// leave with TWEA on for slave receiving</span>00124 outb(TWCR, (inb(TWCR)&TWCR_CMD_MASK)|BV(TWINT)|BV(TWEA)|BV(TWSTO));00125 }00126 <a name="l00127"></a><a class="code" href="i2c_8h.html#a45">00127</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>)00128 {00129 <span class="comment">// wait for i2c interface to complete operation</span>00130 <span class="keywordflow">while</span>( !(inb(TWCR) & BV(TWINT)) );00131 }00132 <a name="l00133"></a><a class="code" href="i2c_8h.html#a46">00133</a> <span class="keyword">inline</span> <span class="keywordtype">void</span> <a class="code" href="i2c_8c.html#a18">i2cSendByte</a>(u08 data)00134 {00135 <span class="comment">// save data to the TWDR</span>00136 outb(TWDR, data);00137 <span class="comment">// begin send</span>00138 outb(TWCR, (inb(TWCR)&TWCR_CMD_MASK)|BV(TWINT));00139 }00140 <a name="l00141"></a><a class="code" href="i2c_8h.html#a47">00141</a> <span class="keyword">inline</span> <span class="keywordtype">void</span> <a class="code" href="i2c_8c.html#a19">i2cReceiveByte</a>(u08 ackFlag)00142 {00143 <span class="comment">// begin receive over i2c</span>00144 <span class="keywordflow">if</span>( ackFlag )00145 {00146 <span class="comment">// ackFlag = TRUE: ACK the recevied data</span>00147 outb(TWCR, (inb(TWCR)&TWCR_CMD_MASK)|BV(TWINT)|BV(TWEA));00148 }00149 <span class="keywordflow">else</span>00150 {00151 <span class="comment">// ackFlag = FALSE: NACK the recevied data</span>00152 outb(TWCR, (inb(TWCR)&TWCR_CMD_MASK)|BV(TWINT));00153 }00154 }00155 <a name="l00156"></a><a class="code" href="i2c_8h.html#a48">00156</a> <span class="keyword">inline</span> u08 <a class="code" href="i2c_8c.html#a20">i2cGetReceivedByte</a>(<span class="keywordtype">void</span>)00157 {00158 <span class="comment">// retieve received data byte from i2c TWDR</span>00159 <span class="keywordflow">return</span>( inb(TWDR) );00160 }00161 <a name="l00162"></a><a class="code" href="i2c_8h.html#a49">00162</a> <span class="keyword">inline</span> u08 <a class="code" href="i2c_8c.html#a21">i2cGetStatus</a>(<span class="keywordtype">void</span>)00163 {00164 <span class="comment">// retieve current i2c status from i2c TWSR</span>00165 <span class="keywordflow">return</span>( inb(TWSR) );00166 }00167 <a name="l00168"></a><a class="code" href="i2c_8h.html#a50">00168</a> <span class="keywordtype">void</span> <a class="code" href="i2c_8c.html#a22">i2cMasterSend</a>(u08 deviceAddr, u08 length, u08* data)00169 {00170 u08 i;00171 <span class="comment">// wait for interface to be ready</span>00172 <span class="keywordflow">while</span>(I2cState);00173 <span class="comment">// set state</span>00174 I2cState = I2C_MASTER_TX;00175 <span class="comment">// save data</span>00176 I2cDeviceAddrRW = (deviceAddr & 0xFE); <span class="comment">// RW cleared: write operation</span>00177 <span class="keywordflow">for</span>(i=0; i<length; i++)00178 I2cSendData[i] = *data++;00179 I2cSendDataIndex = 0;00180 I2cSendDataLength = length;00181 <span class="comment">// send start condition</span>00182 <a class="code" href="i2c_8c.html#a15">i2cSendStart</a>();00183 }00184 <a name="l00185"></a><a class="code" href="i2c_8h.html#a51">00185</a> <span class="keywordtype">void</span> <a class="code" href="i2c_8c.html#a23">i2cMasterReceive</a>(u08 deviceAddr, u08 length, u08* data)00186 {00187 u08 i;00188 <span class="comment">// wait for interface to be ready</span>00189 <span class="keywordflow">while</span>(I2cState);00190 <span class="comment">// set state</span>00191 I2cState = I2C_MASTER_RX;00192 <span class="comment">// save data</span>00193 I2cDeviceAddrRW = (deviceAddr|0x01); <span class="comment">// RW set: read operation</span>00194 I2cReceiveDataIndex = 0;00195 I2cReceiveDataLength = length;00196 <span class="comment">// send start condition</span>00197 <a class="code" href="i2c_8c.html#a15">i2cSendStart</a>();00198 <span class="comment">// wait for data</span>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -