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