【BLE】TLSR8258开发记录之9--IIC驱动TMP102
生活随笔
收集整理的這篇文章主要介紹了
【BLE】TLSR8258开发记录之9--IIC驱动TMP102
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1、說明
基于SDK3.4完成溫度傳感器TMP102的驅(qū)動(參考甜甜大香瓜的博客點(diǎn)擊)
TMP102 參數(shù)如下:
2、操作
A.在MG_Application目錄下新建MG_TMP102.c和MG_TMP102.h,
其中,MG_TMP102.c的代碼為
MG_TMP102.h的代碼如下
/* * MG_TMP102.h * * Created on: 2019-9-23 * Author: huangjinxu */ #ifndef MG_TMP102_H_ #define MG_TMP102_H_ //公共頭文件 #include "tl_common.h" #define TPM102_REGISTER_READ 0 #define TPM102_REGISTER_WRITE 1 //任意字節(jié)獲取 #define BYTE0(Temp) ( *( ( (u8*)(&Temp)+0 ) ) ) #define BYTE1(Temp) ( *( ( (u8*)(&Temp)+1 ) ) ) #define BYTE2(Temp) ( *( ( (u8*)(&Temp)+2 ) ) ) #define BYTE3(Temp) ( *( ( (u8*)(&Temp)+3 ) ) ) //外部函數(shù) extern void MG_Sensor_Init(); extern void MG_Sensor_Work(unsigned char *pData); #endif /* MG_TMP102_H_ */B.tl_common.h中添加以下頭文件路徑
#include "drivers/8258/i2c.h" #include "vendor/8258_ble_remote/MG_Application/MG_TMP102.h"C.在app.c的user_init_normal函數(shù)中中添加測試代碼
u8 sensor_buf[2]={0x00,0x00};u16 sensor_temp=0;u8 sensor_out[2]={0x00,0x00};MG_Sensor_Init();MG_Sensor_Work(sensor_buf);sensor_temp = (sensor_buf[0]*256);sensor_temp |= sensor_buf[1];sensor_temp = sensor_temp>>4;//低4位無作用printf("Sensor data[1] is: %d\r\n",sensor_temp);// sensor_data = (float)(sensor_temp*0.0625);// sensor_temp = (u16)(sensor_data*100);//printf("Sensor data[1] is: %d\r\n",sensor_temp);sensor_temp *= 100;//先擴(kuò)大100倍sensor_temp = sensor_temp>>4;//sensor_temp*0.0625sensor_out[0] = (u8)((sensor_temp>>8)&0xFF);sensor_out[1] = (u8)((sensor_temp)&0xFF);printf("Sensor data[0] is: %x\r\n",sensor_out[0]);printf("Sensor data[1] is: %x\r\n",sensor_out[1]);printf("Sensor data is: %d\r\n",sensor_temp);3、現(xiàn)象
一上電顯示讀取到的溫度為2731/100 = 27.31℃
總結(jié)
以上是生活随笔為你收集整理的【BLE】TLSR8258开发记录之9--IIC驱动TMP102的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【生活】1024,快乐
- 下一篇: 【BLE】TLSR8258开发记录之10