【LPC54100】LPC54102 通过I2C读取总线上的TMP275(板温度)
目的:M0+核通过I2C总线,通过板上TMP275温度传感器,了解当前角度传感器使用的环境温度
占用端口:GPIO口
占用硬件资源 I2C
一、初始化I2C
int Init_I2C(void)
{
uint32_t memSize, *devMem;
ROM_I2CM_INIT_T i2cmInit;
/* Setup I2C pin muxing, enable I2C clock and reset I2C peripheral */
//Init_I2C_PinMux();
Chip_Clock_EnablePeriphClock(LPC_I2CM_CLOCK);
Chip_SYSCON_PeriphReset(LPC_I2CM_RESET);
/* Get needed size for driver context memory */
memSize = ROM_I2CM_GetMemSize();
if (memSize > sizeof(drvData)) {
}
devMem = drvData; /* Or just use malloc(memSize) */
/* Initialize driver */
i2cmInit.pUserData = NULL;
i2cmInit.base = (uint32_t) LPC_I2C_PORT;
i2cmHandle = ROM_I2CM_Init(devMem, &i2cmInit);
if (i2cmHandle == NULL) {
/* Error initializing I2C */
}
/* Set I2C clock rate */
ROM_I2CM_SetClockRate(i2cmHandle, Chip_Clock_GetAsyncSyscon_ClockRate(), I2C_BITRATE);
/* Code never reaches here. Only used to satisfy standard main() */
return 0;
}
二、初始化TMP275
/****************************************************************************
* 函数名:
* 输入:
* 返回:
* 描述:
* ************************************************************************/
void Init_TMP275(void)
{
unsigned char RxData[3],TxData[3];
Init_I2C();
//读配置寄存器
Read_TMP275(TMP275_Con_Reg,&RxData[0]);
TxData[0]=0xFF;
Write_TMP275(TMP275_Con_Reg,&TxData[0]);
//读配置寄存器
Read_TMP275(TMP275_Con_Reg,&RxData[0]);
TxData[0]=0x00;
Write_TMP275(TMP275_Con_Reg,&TxData[0]);
//读配置寄存器
Read_TMP275(TMP275_Con_Reg,&RxData[0]);
//读配置寄存器
Read_TMP275(TMP275_Con_Reg,&RxData[0]);
//写配置寄存器
TxData[0] = TMP275_CR_12BITS|//12 Bits (0.0625°C)转换时间 220ms
TMP275_FQ6 |//故障队列
TMP275_ALERT_L |//ALERT脚为H
TMP275_SM_E |//连续转换模式
TMP275_TM_C ;//比较模式
Write_TMP275(TMP275_Con_Reg,&TxData[0]);
}
三、读写TMP275
/****************************************************************************
* 函数名:
* 输入:
* 返回:
* 描述:
* ************************************************************************/
void Write_TMP275(unsigned char Reg_Address,unsigned char * data)
{
/* Write address, write 1 btye, read 8 bytes back */
i2cmXfer.slaveAddr = TMP275_ADDRESS;
tx[0] = Reg_Address; //寄存器地址
tx[1]=data[0]; //寄存器值 MSB
//tx[2]=data[1]; //寄存器值 LSB
i2cmXfer.txBuff = tx;
i2cmXfer.rxBuff = rx;
i2cmXfer.txSz = 2;
i2cmXfer.rxSz = 0;
/* I2C master driver will block if blocking flag is used */
i2cmXfer.flags = ROM_I2CM_FLAG_BLOCKING;
/* Start transfer and wait for completion */
ROM_I2CM_Transfer(i2cmHandle, &i2cmXfer);
}
/****************************************************************************
* 函数名:
* 输入:
* 返回:
* 描述:
* ************************************************************************/
void Read_TMP275(unsigned char Reg_Address,unsigned char *RxData )
{
/* Write address, write 1 btye, read 8 bytes back */
i2cmXfer.slaveAddr = TMP275_ADDRESS;
tx[0] = Reg_Address; //寄存器地址
i2cmXfer.txBuff = tx;
i2cmXfer.rxBuff = rx;
i2cmXfer.txSz = 1;
i2cmXfer.rxSz = 2;
/* I2C master driver will block if blocking flag is used */
i2cmXfer.flags = ROM_I2CM_FLAG_BLOCKING;
/* Start transfer and wait for completion */
ROM_I2CM_Transfer(i2cmHandle, &i2cmXfer);
RxData[0]=rx[0];
RxData[1]=rx[1];
}
四、读温度
nsigned int Read_TMP275_TEMP(void)
{
unsigned char RxData[2] ;
_ui2uc ui2uc;
unsigned int temp=0;
//读配置寄存器
Read_TMP275(TMP275_Con_Reg,&RxData[0]);
//读温度寄存器
Read_TMP275(TMP275_Tem_Reg,&RxData[0]);
ui2uc.ucdata[1]=RxData[0];
ui2uc.ucdata[0]=RxData[1];
temp=(ui2uc.uidata>>4);
return temp;
}
这里没有在M0+核中进行浮点数的除法计算,而是把采样的温度码发送给M4,让M4做最终的浮点数计算!