资源简介
基于LPC1768的I2C通信实例代码,代码以LPC1768为主机,通过I2C总线通信,读取LM75的监控温度信息
代码片段和文件信息
/*****************************************************************************
* i2c.c: I2C C file for NXP LPC17xx Family Microprocessors
*
* Copyright(C) 2009 NXP Semiconductor
* All rights reserved.
*
* History
* 2009.05.26 ver 1.00 Prelimnary version first Release
*
*****************************************************************************/
#include “lpc17xx.h“
#include “i2c.h“
volatile uint32_t I2CMasterState = I2C_IDLE;
volatile uint32_t I2CSlaveState = I2C_IDLE;
volatile uint32_t I2CCmd;
volatile uint32_t I2CMode;
volatile uint8_t I2CMasterBuffer[BUFSIZE];
volatile uint8_t I2CSlaveBuffer[BUFSIZE];
volatile uint32_t I2CCount = 0;
volatile uint32_t I2CReadLength;
volatile uint32_t I2CWriteLength;
volatile uint32_t RdIndex = 0;
volatile uint32_t WrIndex = 0;
/*
From device to device the I2C communication protocol may vary
in the example below the protocol uses repeated start to read data from or
write to the device:
For master read: the sequence is: STAAddr(W)offsetRE-STAAddr(r)data...STO
for master write: the sequence is: STAAddr(W)lengthRE-STAAddr(w)data...STO
Thus in state 8 the address is always WRITE. in state 10 the address could
be READ or WRITE depending on the I2CCmd.
*/
/*****************************************************************************
** Function name: I2C0_IRQHandler
**
** Descriptions: I2C0 interrupt handler deal with master mode
** only.
**
** parameters: None
** Returned value: None
**
*****************************************************************************/
void I2C0_IRQHandler(void)
{
uint8_t StatValue;
/* this handler deals with master read and master write only */
StatValue = LPC_I2C0->I2STAT;
switch ( StatValue )
{
case 0x08: /* A Start condition is issued. */
LPC_I2C0->I2DAT = I2CMasterBuffer[0];
LPC_I2C0->I2CONCLR = (I2CONCLR_SIC | I2CONCLR_STAC);
I2CMasterState = I2C_STARTED;
break;
case 0x10: /* A repeated started is issued */
if ( I2CCmd == LM75_TEMP )
{
LPC_I2C0->I2DAT = I2CMasterBuffer[2];
}
LPC_I2C0->I2CONCLR = (I2CONCLR_SIC | I2CONCLR_STAC);
I2CMasterState = I2C_RESTARTED;
break;
case 0x18: /* Regardless it‘s a ACK */
if ( I2CMasterState == I2C_STARTED )
{
LPC_I2C0->I2DAT = I2CMasterBuffer[1+WrIndex];
WrIndex++;
I2CMasterState = DATA_ACK;
}
LPC_I2C0->I2CONCLR = I2CONCLR_SIC;
break;
case 0x28: /* Data byte has been transmitted regardless ACK or NACK */
case 0x30:
if ( WrIndex != I2CWriteLength )
{
LPC_I2C0->I2DAT = I2CMasterBuffer[1+WrIndex]; /* this should be the last one */
WrIndex++;
if ( WrIndex != I2CWriteLength )
{
I2CMasterState = DATA_ACK;
}
else
{
I2CMasterState = DATA_NACK;
if ( I2CReadLength != 0 )
{
LPC_I2C0->I2CONSET = I2CONSET_STA; /* Set Repeated-start flag */
I2CMasterState = I2C_REPEATED_START;
}
}
}
els
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 8431 2010-06-21 17:56 I2C\Jli
文件 18043 2010-05-13 10:13 I2C\LandTiger_I2C.plg
文件 80567 2011-01-04 18:40 I2C\LandTiger_I2C.uvopt
文件 15663 2011-01-04 18:40 I2C\LandTiger_I2C.uvproj
文件 2141 2011-01-04 18:40 I2C\LandTiger_I2C_LandTiger_I2C.dep
文件 80 2010-05-13 09:27 I2C\LandTiger_I2C_Target 1.dep
文件 62258 2010-06-21 16:10 I2C\LandTiger_I2C_uvopt.bak
文件 15663 2010-05-13 09:54 I2C\LandTiger_I2C_uvproj.bak
文件 55225 2011-01-04 18:40 I2C\List\LandTiger_I2C.map
文件 42027 2011-01-04 18:40 I2C\List\startup_LPC17xx.lst
文件 3550 2011-01-04 18:40 I2C\Obj\core_cm3.crf
文件 108 2011-01-04 18:40 I2C\Obj\core_cm3.d
文件 10144 2011-01-04 18:40 I2C\Obj\core_cm3.o
文件 60152 2011-01-04 18:40 I2C\Obj\i2c.crf
文件 263 2011-01-04 18:40 I2C\Obj\i2c.d
文件 51712 2011-01-04 18:40 I2C\Obj\i2c.o
文件 58887 2011-01-04 18:40 I2C\Obj\i2cmst.crf
文件 284 2011-01-04 18:40 I2C\Obj\i2cmst.d
文件 49036 2011-01-04 18:40 I2C\Obj\i2cmst.o
文件 56656 2011-01-04 18:40 I2C\Obj\LandTiger_I2C.axf
文件 5983 2011-01-04 18:40 I2C\Obj\LandTiger_I2C.hex
文件 31052 2011-01-04 18:40 I2C\Obj\LandTiger_I2C.htm
文件 368 2011-01-04 18:40 I2C\Obj\LandTiger_I2C.lnp
文件 497 2011-01-04 18:40 I2C\Obj\LandTiger_I2C.plg
文件 479 2010-05-13 09:33 I2C\Obj\LandTiger_I2C.sct
文件 1246 2011-01-04 18:40 I2C\Obj\LandTiger_I2C.tra
文件 99 2011-01-04 18:40 I2C\Obj\startup_LPC17xx.d
文件 5868 2011-01-04 18:40 I2C\Obj\startup_LPC17xx.o
文件 59508 2011-01-04 18:40 I2C\Obj\system_lpc17xx.crf
文件 360 2011-01-04 18:40 I2C\Obj\system_lpc17xx.d
............此处省略47个文件信息
评论
共有 条评论