【MCXN947开发板测评】 P3T1755温度检测与显示
[复制链接]
本帖最后由 jennyzhaojie 于 2024-12-14 22:40 编辑
在MCXN947开发板上配有温度传感器P3T1755,该传感器以I2C的方式工作,其电路如图1所示。
图1 传感器电路
在连接串口的情况下,其检测结果如图2所示。
图2 检测结果
为便于观察,在配接LCD显示屏的情况下,其实现温度检测与显示的主程序是:
int main(void)
{
status_t result = kStatus_Success;
i3c_master_config_t masterConfig;
p3t1755_config_t p3t1755Config;
double temperature,d;
gpio_pin_config_t led_config = {
kGPIO_DigitalOutput,
0,
};
CLOCK_SetClkDiv(kCLOCK_DivFlexcom4Clk, 1);
CLOCK_AttachClk(BOARD_DEBUG_UART_CLK_ATTACH);
CLOCK_EnableClock(kCLOCK_Gpio0);
CLOCK_SetClkDiv(kCLOCK_DivI3c1FClk, 6U);
CLOCK_AttachClk(kPLL0_to_I3C1FCLK);
BOARD_InitPins();
BOARD_InitBootClocks();
BOARD_InitDebugConsole();
GPIO_PinInit(GPIO0, 25U, &led_config);
GPIO_PinInit(GPIO0, 26U, &led_config);
GPIO_PinInit(GPIO0, 24U, &led_config);
GPIO_PinInit(GPIO0, 27U, &led_config);
GPIO_PinInit(GPIO0, 10U, &led_config);
GPIO_PinInit(GPIO0, 28U, &led_config);
PRINTF("\r\nI3C master read sensor data example.\r\n");
I3C_MasterGetDefaultConfig(&masterConfig);
masterConfig.baudRate_Hz.i2cBaud = EXAMPLE_I2C_BAUDRATE;
masterConfig.baudRate_Hz.i3cPushPullBaud = EXAMPLE_I3C_PP_BAUDRATE;
masterConfig.baudRate_Hz.i3cOpenDrainBaud = EXAMPLE_I3C_OD_BAUDRATE;
masterConfig.enableOpenDrainStop = false;
masterConfig.disableTimeout = true;
I3C_MasterInit(EXAMPLE_MASTER, &masterConfig, I3C_MASTER_CLOCK_FREQUENCY);
I3C_MasterTransferCreateHandle(EXAMPLE_MASTER, &g_i3c_m_handle, &masterCallback, NULL);
result = p3t1755_set_dynamic_address();
if (result != kStatus_Success)
{
PRINTF("\r\nP3T1755 set dynamic address failed.\r\n");
}
p3t1755Config.writeTransfer = I3C_WriteSensor;
p3t1755Config.readTransfer = I3C_ReadSensor;
p3t1755Config.sensorAddress = SENSOR_ADDR;
P3T1755_Init(&p3t1755Handle, &p3t1755Config);
Lcd_Init();
LCD_Clear(RED);
LCD_ShowString(20,10,"MCXN947",YELLOW);
LCD_ShowString(20,30,"P3T1755",YELLOW);
LCD_ShowString(20,50,"t=",YELLOW);
while (1)
{
result = P3T1755_ReadTemperature(&p3t1755Handle, &temperature);
if (result != kStatus_Success)
{
PRINTF("\r\nP3T1755 read temperature failed.\r\n");
}
else
{
LCD_ShowNum(36,50,temperature,3,YELLOW);
d=((int)(temperature*100))%100;
LCD_ShowChar(60,50,'.',0,YELLOW);
LCD_ShowNum(68,50,d,2,YELLOW);
}
SDK_DelayAtLeastUs(1000000, CLOCK_GetCoreSysClkFreq());
}
}
经程序的编译和下载,其显示效果如图4所示。
图3 电路连接
图4 检测效果
|