RT1010SDK上手I2C,GXHT30温湿度传感器
上一篇上手SDK的GPIO,串口,以及MCUXpresso Config Tools ,接着上期的内容,测试I2C的使用,手上只有这个温湿度传感器,虽然用了几次了,下次一定换个IC测试。
直接在SDK中导入evkmimxrt1010_lpi2c_polling_b2b_master工程,本工程是采用查询的方式。
步骤如下:
<ignore_js_op> <ignore_js_op>
<ignore_js_op>
EVB接口,直接把温湿度传感器接在此处即可 <ignore_js_op>
直接在SDK上修改,
1.首先是温湿度传感器的地址更改为0x44,
<ignore_js_op>
2.I2C的初始化
复制代码
3.读写函数
复制代码
代码中的 transfer.subaddress, transfer.subaddressSize 都设置为0; <ignore_js_op>
4.根据温湿度传感器的规格书,写温湿度函数的实现函数
<ignore_js_op>
<ignore_js_op>
<ignore_js_op>
<ignore_js_op>
温湿度函数的实现如下:(本来打算用,printf来打印的,SDK上需要修改一些地方,连PRINTF("℃ ;")PRINTF("\%\r\n"); 这两个符号有没能输出,省略了,后期研究好了再来更新)
复制代码
5.直接在主函数中调用:bool GXHT30_Signal_Convert(void),打印出温湿度数据,用手触摸温湿度传感器让温湿度产生变化
打印结果如下:
<ignore_js_op>
直接在SDK中导入evkmimxrt1010_lpi2c_polling_b2b_master工程,本工程是采用查询的方式。
步骤如下:
<ignore_js_op> <ignore_js_op>
<ignore_js_op>
EVB接口,直接把温湿度传感器接在此处即可 <ignore_js_op>
直接在SDK上修改,
1.首先是温湿度传感器的地址更改为0x44,
<ignore_js_op>
2.I2C的初始化
- void I2C_Init(void)
- {
- lpi2c_master_config_t masterConfig;
- /*Clock setting for LPI2C*/
- CLOCK_SetMux(kCLOCK_Lpi2cMux, LPI2C_CLOCK_SOURCE_SELECT);
- CLOCK_SetDiv(kCLOCK_Lpi2cDiv, LPI2C_CLOCK_SOURCE_DIVIDER);
- /* 获取默认配置 */
- LPI2C_MasterGetDefaultConfig(&masterConfig);
- /* 配置LPI2C速度100k */
- masterConfig.baudRate_Hz = LPI2C_BAUDRATE;
- /* Initialize the LPI2C master peripheral */
- LPI2C_MasterInit(EXAMPLE_I2C_MASTER, &masterConfig, LPI2C_MASTER_CLOCK_FREQUENCY);
- }
3.读写函数
- bool I2C_WriteBuffer(uint8_t SalveAddr,uint8_t RegAddr,uint8_t *DateByte,uint32_t DataNum)
- {
- lpi2c_master_transfer_t transfer;
- status_t err_flag;
- /*
- * @data :要发送的数据
- * @datasize :发送的数据个数
- * @direction :读写模式选择
- * @flags :传输失败的标志位
- * @slaveAaddress:从机地址
- * @subaddress :寄存器/内存地址
- * @subaddressSize:地址寄存器大小
- */
- transfer.data = DateByte;
- transfer.dataSize = DataNum;
- transfer.direction = kLPI2C_Write;
- transfer.flags = kLPI2C_TransferDefaultFlag;
- transfer.slaveAddress =SalveAddr;
- transfer.subaddress = RegAddr;
- transfer.subaddressSize = 0x00;
- err_flag = LPI2C_MasterTransferBlocking(LPI2C1,&transfer);
- if(err_flag != kStatus_Success) return false;
- return true;
- }
- bool I2C_ReadBuffer(uint8_t SalveAddr,uint8_t RegAddr,uint8_t *DateByte,uint32_t DataNum)
- {
- lpi2c_master_transfer_t transfer;
- status_t err_flag;
- /*
- * @data :发送、接受的数据
- * @datasize :读取的数据个数
- * @direction :读写模式选择
- * @flags :传输失败的标志位
- * @slaveAaddress:从机地址
- * @subaddress :寄存器/内存地址
- * @subaddressSize:地址寄存器大小
- */
- transfer.data = DateByte;
- transfer.dataSize = DataNum;
- transfer.direction = kLPI2C_Read;
- transfer.flags = kLPI2C_TransferDefaultFlag;
- transfer.slaveAddress =SalveAddr;
- transfer.subaddress = RegAddr;
- transfer.subaddressSize = 0x00;
- err_flag = LPI2C_MasterTransferBlocking(LPI2C1,&transfer);
- if(err_flag != kStatus_Success) return false;
- return true;
- }
4.根据温湿度传感器的规格书,写温湿度函数的实现函数
<ignore_js_op>
<ignore_js_op>
<ignore_js_op>
<ignore_js_op>
温湿度函数的实现如下:(本来打算用,printf来打印的,SDK上需要修改一些地方,连PRINTF("℃ ;")PRINTF("\%\r\n"); 这两个符号有没能输出,省略了,后期研究好了再来更新)
- bool GXHT30_Signal_Convert(void)
- {
- bool reval;
- uint8_t buff[6];
- uint16_t tem,hum,TEM,HUM;
- uint8_t tem_buf[6];
- uint8_t tem_buf1[5];
- reval = I2C_WriteBuffer(LPI2C_MASTER_SLAVE_ADDR_7BIT,0,GXHT30_CMD_BUF,2);
- Delay_ms(20);
- reval = I2C_ReadBuffer (LPI2C_MASTER_SLAVE_ADDR_7BIT,0,buff,6);
- tem = ((buff[0]<<8) | buff[1]);//温度拼接
- hum = ((buff[3]<<8) | buff[4]);//湿度拼接
- Temperature =(175.0 * (float)tem/65535.0 -45.0); //温度计算
- Humidity= (100.0*hum/65535.0);
- if (Temperature < 0.0) //如为负数,首位为1
- TEM = (uint16_t) (Temperature * (-10.0)) + 0x8000; //10倍储存
- else
- TEM = (uint16_t) (Temperature * 10.0); //10倍储存
- HUM = (uint16_t) (Humidity * 10.0); //10倍储存
- if(TEM&0x8000)
- tem_buf[0]='-';
- else
- tem_buf[0] = '+';
- tem_buf[1] = TEM/100+'0';
- tem_buf[2] = (TEM%100)/10+'0';
- tem_buf[3] = '.';
- tem_buf[4] = TEM%10+'0';
- tem_buf[5] = '\0';
- tem_buf1[0] = HUM/100+'0';
- tem_buf1[1] = (HUM%100)/10+'0';
- tem_buf1[2] = '.';
- tem_buf1[3] = HUM%10+'0';
- tem_buf1[4] = '\0';
- PRINTF("Temperature=");
- PRINTF(tem_buf);
- // PRINTF("℃ ;");
- PRINTF(" Humidity= ");
- PRINTF(tem_buf1);
- //PRINTF("\%\r\n");
- return reval;
- }
5.直接在主函数中调用:bool GXHT30_Signal_Convert(void),打印出温湿度数据,用手触摸温湿度传感器让温湿度产生变化
打印结果如下:
<ignore_js_op>