本次主要是使用IIC读取功能,上次的OLED屏幕,我们是只发送不读取数据,这次就是我们通过读取SHT20温湿度传感器测试下其读取功能。
接线保持不变,还是IIC0。
还是一致的。
同样通过FSP软件进行生成代码。配置好从机地址。
fsp_err_t err;
float TemValue,RhValue;
#define SHT20_Measurement_RH_HM 0XE5
#define SHT20_Measurement_T_HM 0XE3
i2c_master_event_t g_i2c_callback_event = I2C_MASTER_EVENT_ABORTED;;
void SHT20_ReadValues(void)
{
uint8_t tem[2],rh[2];
uint16_t Tdata=0,RHdata=0;
uint16_t timeout_ms = 0;
uint8_t cmd=SHT20_Measurement_T_HM;
err = R_IIC_MASTER_Write(&g_i2c_master0_ctrl, &cmd, 1 , false);
timeout_ms = 750;
while ((I2C_MASTER_EVENT_TX_COMPLETE != g_i2c_callback_event) && timeout_ms)
{
R_BSP_SoftwareDelay(1U, BSP_DELAY_UNITS_MILLISECONDS);
timeout_ms--;
}
R_BSP_SoftwareDelay(10, BSP_DELAY_UNITS_MILLISECONDS); // NOLINT
err = R_IIC_MASTER_Read(&g_i2c_master0_ctrl, tem, 2 , false);
timeout_ms = 750;
while ((I2C_MASTER_EVENT_RX_COMPLETE != g_i2c_callback_event) && timeout_ms)
{
R_BSP_SoftwareDelay(1U, BSP_DELAY_UNITS_MILLISECONDS);
timeout_ms--;
}
cmd=SHT20_Measurement_RH_HM;
err = R_IIC_MASTER_Write(&g_i2c_master0_ctrl, &cmd, 1 , false);
timeout_ms = 750;
while ((I2C_MASTER_EVENT_TX_COMPLETE != g_i2c_callback_event) && timeout_ms)
{
R_BSP_SoftwareDelay(1U, BSP_DELAY_UNITS_MILLISECONDS);
timeout_ms--;
}
R_BSP_SoftwareDelay(10, BSP_DELAY_UNITS_MILLISECONDS); // NOLINT
err = R_IIC_MASTER_Read(&g_i2c_master0_ctrl, rh, 2 , false);
timeout_ms = 750;
while ((I2C_MASTER_EVENT_RX_COMPLETE != g_i2c_callback_event) && timeout_ms)
{
R_BSP_SoftwareDelay(1U, BSP_DELAY_UNITS_MILLISECONDS);
timeout_ms--;
}
Tdata = tem[0];
Tdata <<= 8;
Tdata += tem[1] & 0xfe;
RHdata = rh[0];
RHdata <<= 8;
RHdata += rh[1] & 0xfe;
TemValue = Tdata * 175.72f / 65536 - 46.85f;
RhValue = RHdata * 125.0f / 65536 - 6.0f;
}
void SHT20_SoftReset(void)
{
uint8_t cmd=0xfe;
uint16_t timeout_ms = 0;
err = R_IIC_MASTER_Write(&g_i2c_master0_ctrl, &cmd, 1 , false);
timeout_ms = 1000;
while ((I2C_MASTER_EVENT_TX_COMPLETE != g_i2c_callback_event) && timeout_ms)
{
R_BSP_SoftwareDelay(1U, BSP_DELAY_UNITS_MILLISECONDS);
timeout_ms--;
}
// R_BSP_SoftwareDelay(1000, BSP_DELAY_UNITS_MILLISECONDS); // NOLINT
}
/*******************************************************************************************************************//**
* main() is generated by the RA Configuration editor and is used to generate threads if an RTOS is used. This function
* is called by main() when no RTOS is used.
**********************************************************************************************************************/
void hal_entry(void)
{
/* TODO: add your own code here */
err = R_IIC_MASTER_Open(g_i2c_master0.p_ctrl, &g_i2c_master0_cfg);
assert(FSP_SUCCESS == err);
R_BSP_SoftwareDelay(500, BSP_DELAY_UNITS_MILLISECONDS); // NOLINT
SHT20_SoftReset();
while(1)
{
SHT20_ReadValues();
R_BSP_SoftwareDelay(1000, BSP_DELAY_UNITS_MILLISECONDS); // NOLINT
}
#if BSP_TZ_SECURE_BUILD
/* Enter non-secure code */
R_BSP_NonSecureEnter();
#endif
}
/*******************************************************************************************************************//**
* This function is called at various points during the startup process. This implementation uses the event that is
* called right before main() to set up the pins.
*
* @param[in] event Where at in the start up process the code is currently at
**********************************************************************************************************************/
void R_BSP_WarmStart(bsp_warm_start_event_t event)
{
if (BSP_WARM_START_RESET == event)
{
#if BSP_FEATURE_FLASH_LP_VERSION != 0
/* Enable reading from data flash. */
R_FACI_LP->DFLCTL = 1U;
/* Would normally have to wait tDSTOP(6us) for data flash recovery. Placing the enable here, before clock and
* C runtime initialization, should negate the need for a delay since the initialization will typically take more than 6us. */
#endif
}
if (BSP_WARM_START_POST_C == event)
{
/* C runtime environment and system clocks are setup. */
/* Configure pins. */
R_IOPORT_Open (&g_ioport_ctrl, &IOPORT_CFG_NAME);
}
}
void i2c_callback (i2c_master_callback_args_t * p_args)
{
g_i2c_callback_event = p_args->event;
}
代码上还是维持之前的测试好的,添加上读取即可。
然后我们用仿真软件看下,可以看到两个变量的值,这个值必须通过打中断才能刷新。
上面就是测试结果。