恩智浦LPC1549@eeworld ( 五 ) 跑 I2C [url=https://bbs.eeworld.com.cn/thread-438795-1-1.html]例程读TMP275[/url]
目的:
熟悉LPCOPEN库函数
I2C的操作读写
一、硬件连接:
1:温度传感器连接
TMP275-2 SCL = LPC1549_P0.22
TMP275-1 SDA = LPC1549_P0.23
2:UART连接
LPC1549_P0.13( RXD) = PL2303_RXD
LPC1549_P0.18( TXD) = PL2303_RXD
二、初始化配置程序:
/* 设置I2C引脚复用 */
Init_I2C_PinMux();
/* 配置I2C句柄,设置的I2C速率,并初始化I2C时钟 */
setupI2CMaster();
/* 禁用I2C中断 */
NVIC_DisableIRQ(I2C0_IRQn);
/* 设置I2C引脚复用 */
static void Init_I2C_PinMux(void)
{
#if defined(BOARD_NXP_LPCXPRESSO_1549)
Chip_IOCON_PinMuxSet(LPC_IOCON, 0, 22, IOCON_DIGMODE_EN | I2C_MODE);//
SCL TMP275-2 SCL
Chip_IOCON_PinMuxSet(LPC_IOCON, 0, 23, IOCON_DIGMODE_EN | I2C_MODE);//
SDA TMP275-1 SDA
Chip_SWM_EnableFixedPin(SWM_FIXED_I2C0_SCL);
Chip_SWM_EnableFixedPin(SWM_FIXED_I2C0_SDA);
#else
#error "No I2C Pin Muxing defined for this example"
#endif
}
/* 设置I2C句柄和参数*/
static void setupI2CMaster()
{
/* 使能I2C时钟和复位I2C外设 - 引导ROM不这样做 */
Chip_I2C_Init(
LPC_I2C0);
/* 设置时钟速率为I2C*/
Chip_I2C_SetClockDiv(
LPC_I2C0, I2C_CLK_DIVIDER);
/* 设置I2CM传输速率*/
Chip_I2CM_SetBusSpeed(
LPC_I2C0, I2C_BITRATE);
/* 启用“主”模式*/
Chip_I2CM_Enable(
LPC_I2C0);
}
//指定读写
static void SetupXferRecAndExecute(uint8_t devAddr,
uint8_t *txBuffPtr,
uint16_t txSize,
uint8_t *rxBuffPtr,
uint16_t rxSize)
{
/* Setup I2C transfer record */
i2cmXferRec.slaveAddr = devAddr;
i2cmXferRec.status = 0;
i2cmXferRec.txSz = txSize;
i2cmXferRec.rxSz = rxSize;
i2cmXferRec.txBuff = txBuffPtr;
i2cmXferRec.rxBuff = rxBuffPtr;
//在主模式下发送和接收数据
Chip_I2CM_XferBlocking(LPC_I2C0, &i2cmXferRec);
}
/* 主I2CM 轮询模式 接收 */
#if defined(BOARD_NXP_LPCXPRESSO_1549)
/* 函数读取的I2C 温度传感器和输出结果 */
static void ReadTemperatureI2CM(void)
{
float temp=0.0;
uint16_t itemp=0;
uint8_t temperature[2];
uint8_t lm75TempRegisterAddress = 0;
/* 读温度传感器 */
SetupXferRecAndExecute(
/* I2C总线地址 */
I2C_TEMP_ADDR_7BIT,
/* 发送一个字节,温度寄存器地址*/
&lm75TempRegisterAddress, 1,
/* 接收回来的两个字节中,温度寄存器的内容 */
temperature, 2);
//temp=(float)itemp*0.5;
/* Test for valid operation */
if (i2cmXferRec.status == I2CM_STATUS_OK) {
/* Output temperature. */
DEBUGOUT("Temperature = %d.%d degrees C.\r\n", //temp);
(int) temperature[0], (temperature[1] & 0x80) ? 5 : 0 );//LM75 9位有效
}
else {
DEBUGOUT("Error %d reading temperature.\r\n", i2cmXferRec.status );
}
}
#endif
三、UART输出结果