[其他MCU] LPC54102 通过I2C读取总线上的TMP275

[复制链接]
 楼主| ccw1986 发表于 2015-12-30 21:01 | 显示全部楼层 |阅读模式
目的:M0+核通过I2C总线,通过板上TMP275温度传感器,了解当前角度传感器使用的环境温度

占用端口:GPIO口

占用硬件资源   I2C
一、初始化I2C
  1. int Init_I2C(void)

  2. {

  3.         uint32_t memSize, *devMem;

  4.         ROM_I2CM_INIT_T i2cmInit;



  5.         /* Setup I2C pin muxing, enable I2C clock and reset I2C peripheral */

  6.         //Init_I2C_PinMux();

  7.         Chip_Clock_EnablePeriphClock(LPC_I2CM_CLOCK);



 楼主| ccw1986 发表于 2015-12-30 21:01 | 显示全部楼层
  1. Chip_SYSCON_PeriphReset(LPC_I2CM_RESET);



  2.         /* Get needed size for driver context memory */

  3.         memSize = ROM_I2CM_GetMemSize();

  4.         if (memSize > sizeof(drvData)) {

  5.         }

  6.         devMem = drvData;        /* Or just use malloc(memSize) */
 楼主| ccw1986 发表于 2015-12-30 21:02 | 显示全部楼层
  1.   /* Initialize driver */

  2.         i2cmInit.pUserData = NULL;

  3.         i2cmInit.base = (uint32_t) LPC_I2C_PORT;

  4.         i2cmHandle = ROM_I2CM_Init(devMem, &i2cmInit);

  5.         if (i2cmHandle == NULL) {

  6.                 /* Error initializing I2C */

  7.         }
 楼主| ccw1986 发表于 2015-12-30 21:02 | 显示全部楼层
  1. /* Set I2C clock rate */

  2.         ROM_I2CM_SetClockRate(i2cmHandle, Chip_Clock_GetAsyncSyscon_ClockRate(), I2C_BITRATE);





  3.         /* Code never reaches here. Only used to satisfy standard main() */

  4.         return 0;

  5. }

 楼主| ccw1986 发表于 2015-12-30 21:03 | 显示全部楼层
二、初始化TMP275
  1. /****************************************************************************

  2. * 函数名:

  3. * 输入:

  4. * 返回:

  5. * 描述:

  6. * ************************************************************************/
  1. void Init_TMP275(void)

  2. {

  3.         unsigned char RxData[3],TxData[3];

  4.         Init_I2C();

  5.       

  6.         //读配置寄存器

  7.         Read_TMP275(TMP275_Con_Reg,&RxData[0]);
 楼主| ccw1986 发表于 2015-12-30 21:03 | 显示全部楼层
  1. TxData[0]=0xFF;

  2.         Write_TMP275(TMP275_Con_Reg,&TxData[0]);

  3.         //读配置寄存器

  4.         Read_TMP275(TMP275_Con_Reg,&RxData[0]);

  5.       

  6.         TxData[0]=0x00;

  7.         Write_TMP275(TMP275_Con_Reg,&TxData[0]);
 楼主| ccw1986 发表于 2015-12-30 21:04 | 显示全部楼层
  1. /读配置寄存器

  2.         Read_TMP275(TMP275_Con_Reg,&RxData[0]);

  3.         //读配置寄存器

  4.         Read_TMP275(TMP275_Con_Reg,&RxData[0]);
 楼主| ccw1986 发表于 2015-12-30 21:05 | 显示全部楼层
  1. //写配置寄存器

  2.         TxData[0] =        TMP275_CR_12BITS|//12 Bits (0.0625°C)转换时间 220ms

  3.                                 TMP275_FQ6                |//故障队列

  4.                                 TMP275_ALERT_L        |//ALERT脚为H

  5.                                 TMP275_SM_E                |//连续转换模式

  6.                                 TMP275_TM_C                ;//比较模式

  7.                         

  8.         Write_TMP275(TMP275_Con_Reg,&TxData[0]);

  9. }      
 楼主| ccw1986 发表于 2015-12-30 21:05 | 显示全部楼层
  1. 三、读写TMP275
  1. /****************************************************************************

  2. * 函数名:

  3. * 输入:

  4. * 返回:

  5. * 描述:

  6. * ************************************************************************/

  7. void Write_TMP275(unsigned char Reg_Address,unsigned char * data)

  8. {

  9.                  

  10.                 /* Write address, write 1 btye, read 8 bytes back */

  11.                 i2cmXfer.slaveAddr = TMP275_ADDRESS;

  12.                 tx[0] = Reg_Address;        //寄存器地址

  13.                 tx[1]=data[0];                //寄存器值 MSB

  14.                 //tx[2]=data[1];                //寄存器值 LSB
 楼主| ccw1986 发表于 2015-12-30 21:06 | 显示全部楼层
  1. i2cmXfer.txBuff = tx;

  2.                 i2cmXfer.rxBuff = rx;

  3.                 i2cmXfer.txSz = 2;

  4.                 i2cmXfer.rxSz = 0;



  5.                 /* I2C master driver will block if blocking flag is used */

  6.                 i2cmXfer.flags = ROM_I2CM_FLAG_BLOCKING;
 楼主| ccw1986 发表于 2015-12-30 21:07 | 显示全部楼层
  1. /* Start transfer and wait for completion */

  2.                 ROM_I2CM_Transfer(i2cmHandle, &i2cmXfer);



  3. }  
  1. /****************************************************************************

  2. * 函数名:

  3. * 输入:

  4. * 返回:

  5. * 描述:

  6. * ************************************************************************/

  7. void Read_TMP275(unsigned char Reg_Address,unsigned char *RxData )

  8. {  

  9.                 /* Write address, write 1 btye, read 8 bytes back */

  10.                 i2cmXfer.slaveAddr = TMP275_ADDRESS;

  11.                 tx[0] = Reg_Address;        //寄存器地址
 楼主| ccw1986 发表于 2015-12-30 21:07 | 显示全部楼层
  1. i2cmXfer.txBuff = tx;

  2.                 i2cmXfer.rxBuff = rx;

  3.                 i2cmXfer.txSz = 1;

  4.                 i2cmXfer.rxSz = 2;



  5.                 /* I2C master driver will block if blocking flag is used */

  6.                 i2cmXfer.flags = ROM_I2CM_FLAG_BLOCKING;  
 楼主| ccw1986 发表于 2015-12-30 21:08 | 显示全部楼层
  1.   /* Start transfer and wait for completion */

  2.                 ROM_I2CM_Transfer(i2cmHandle, &i2cmXfer);

  3.       

  4.           RxData[0]=rx[0];

  5.           RxData[1]=rx[1];

  6. }

 楼主| ccw1986 发表于 2015-12-30 21:08 | 显示全部楼层
四、读温度
  1. nsigned int Read_TMP275_TEMP(void)

  2. {

  3.         unsigned char RxData[2] ;

  4.         _ui2uc ui2uc;

  5.         unsigned int temp=0;

  6.       

  7.         //读配置寄存器

  8.         Read_TMP275(TMP275_Con_Reg,&RxData[0]);
 楼主| ccw1986 发表于 2015-12-30 21:09 | 显示全部楼层
  1. //读温度寄存器

  2.         Read_TMP275(TMP275_Tem_Reg,&RxData[0]);

  3.          

  4.         ui2uc.ucdata[1]=RxData[0];

  5.         ui2uc.ucdata[0]=RxData[1];

  6.             

  7.       

  8.         temp=(ui2uc.uidata>>4);

  9.         return temp;

  10. }
您需要登录后才可以回帖 登录 | 注册

本版积分规则

84

主题

925

帖子

6

粉丝
快速回复 在线客服 返回列表 返回顶部