📄 i2c_8c-source.html
字号:
00136 <span class="comment">// save data into data register</span>00137 I2DAT = data;00138 <span class="comment">// clear SI bit to begin transfer</span>00139 I2CONCLR = BIT(I2CON_SI);00140 }00141 <a name="l00142"></a><a class="code" href="i2c_8h.html#a50">00142</a> <span class="keywordtype">void</span> <a class="code" href="i2c_8h.html#a50">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 I2CONSET = BIT(I2CON_AA);00149 <span class="comment">//outb(TWCR, (inb(TWCR)&TWCR_CMD_MASK)|BV(TWINT)|BV(TWEA));</span>00150 }00151 <span class="keywordflow">else</span>00152 {00153 <span class="comment">// ackFlag = FALSE: NACK the recevied data</span>00154 I2CONCLR = BIT(I2CON_AA);00155 <span class="comment">//outb(TWCR, (inb(TWCR)&TWCR_CMD_MASK)|BV(TWINT));</span>00156 }00157 <span class="comment">// clear SI bit to begin transfer</span>00158 I2CONCLR = BIT(I2CON_SI);00159 }00160 <a name="l00161"></a><a class="code" href="i2c_8h.html#a51">00161</a> u08 <a class="code" href="i2c_8c.html#a8">i2cGetReceivedByte</a>(<span class="keywordtype">void</span>)00162 {00163 <span class="keywordflow">return</span> I2DAT;00164 }00165 <a name="l00166"></a><a class="code" href="i2c_8h.html#a52">00166</a> u08 <a class="code" href="i2c_8c.html#a9">i2cGetStatus</a>(<span class="keywordtype">void</span>)00167 {00168 <span class="keywordflow">return</span> I2STAT;00169 }00170 00171 <a name="l00172"></a><a class="code" href="i2c_8h.html#a53">00172</a> u08 <a class="code" href="i2c_8h.html#a53">i2cMasterSendNI</a>(u08 deviceAddr, u08 length, u08* data)00173 {00174 u08 retval = I2C_OK;00175 00176 <span class="comment">// disable TWI interrupt</span>00177 <span class="comment">//cbi(TWCR, TWIE);</span>00178 00179 <span class="comment">// send start condition</span>00180 <a class="code" href="i2c_8c.html#a3">i2cSendStart</a>();00181 <a class="code" href="i2c_8c.html#a5">i2cWaitForComplete</a>();00182 I2CONCLR = BIT(I2CON_STA);00183 00184 <span class="comment">// send device address with write</span>00185 <a class="code" href="i2c_8h.html#a49">i2cSendByte</a>( deviceAddr & 0xFE );00186 <a class="code" href="i2c_8c.html#a5">i2cWaitForComplete</a>();00187 00188 <span class="comment">// check if device is present and live</span>00189 <span class="keywordflow">if</span>( I2STAT == TW_MT_SLA_ACK)00190 {00191 <span class="comment">// send data</span>00192 <span class="keywordflow">while</span>(length)00193 {00194 <a class="code" href="i2c_8h.html#a49">i2cSendByte</a>( *data++ );00195 <a class="code" href="i2c_8c.html#a5">i2cWaitForComplete</a>();00196 length--;00197 }00198 }00199 <span class="keywordflow">else</span>00200 {00201 <span class="comment">// device did not ACK it's address,</span>00202 <span class="comment">// data will not be transferred</span>00203 <span class="comment">// return error</span>00204 retval = I2C_ERROR_NODEV;00205 }00206 00207 <span class="comment">// transmit stop condition</span>00208 <span class="comment">// leave with TWEA on for slave receiving</span>00209 I2CONSET = BIT(I2CON_STA);00210 delay(10);00211 I2CONCLR = BIT(I2CON_STO);00212 <span class="comment">//while( !(inb(TWCR) & BV(TWSTO)) );</span>00213 00214 <span class="comment">// enable TWI interrupt</span>00215 <span class="comment">//sbi(TWCR, TWIE);</span>00216 00217 <span class="keywordflow">return</span> retval;00218 }00219 <a name="l00220"></a><a class="code" href="i2c_8h.html#a54">00220</a> u08 <a class="code" href="i2c_8h.html#a54">i2cMasterReceiveNI</a>(u08 deviceAddr, u08 length, u08 *data)00221 {00222 u08 retval = I2C_OK;00223 00224 <span class="comment">// disable TWI interrupt</span><span class="comment"></span>00225 <span class="comment"> ///cbi(TWCR, TWIE);</span>00226 <span class="comment"></span>00227 <span class="comment">// send start condition</span>00228 <a class="code" href="i2c_8c.html#a3">i2cSendStart</a>();00229 <a class="code" href="i2c_8c.html#a5">i2cWaitForComplete</a>();00230 00231 <span class="comment">// send device address with read</span>00232 <a class="code" href="i2c_8h.html#a49">i2cSendByte</a>( deviceAddr | 0x01 );00233 <a class="code" href="i2c_8c.html#a5">i2cWaitForComplete</a>();00234 00235 <span class="comment">// check if device is present and live</span>00236 <span class="keywordflow">if</span>( I2STAT == TW_MR_SLA_ACK)00237 {00238 <span class="comment">// accept receive data and ack it</span>00239 <span class="keywordflow">while</span>(length > 1)00240 {00241 <a class="code" href="i2c_8h.html#a50">i2cReceiveByte</a>(TRUE);00242 <a class="code" href="i2c_8c.html#a5">i2cWaitForComplete</a>();00243 *data++ = <a class="code" href="i2c_8c.html#a8">i2cGetReceivedByte</a>();00244 <span class="comment">// decrement length</span>00245 length--;00246 }00247 00248 <span class="comment">// accept receive data and nack it (last-byte signal)</span>00249 <a class="code" href="i2c_8h.html#a50">i2cReceiveByte</a>(FALSE);00250 <a class="code" href="i2c_8c.html#a5">i2cWaitForComplete</a>();00251 *data++ = <a class="code" href="i2c_8c.html#a8">i2cGetReceivedByte</a>();00252 }00253 <span class="keywordflow">else</span>00254 {00255 <span class="comment">// device did not ACK it's address,</span>00256 <span class="comment">// data will not be transferred</span>00257 <span class="comment">// return error</span>00258 retval = I2C_ERROR_NODEV;00259 }00260 00261 <span class="comment">// transmit stop condition</span>00262 <span class="comment">// leave with TWEA on for slave receiving</span>00263 <a class="code" href="i2c_8c.html#a4">i2cSendStop</a>();00264 00265 <span class="comment">// enable TWI interrupt</span>00266 <span class="comment">//sbi(TWCR, TWIE);</span>00267 00268 <span class="keywordflow">return</span> retval;00269 }00270 </pre></div><hr size="1"><address style="align: right;"><small>Generated on Tue Jul 13 03:38:11 2004 for Procyon ARMlib-LPC2100 by<a href="http://www.doxygen.org/index.html"><img src="doxygen.png" alt="doxygen" align="middle" border=0 > </a>1.3.6 </small></address></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -