查看EK-RA6M4的原理图,如下图所示,该开发板上的MikroBus接口上有提供一个I2C接口。
根据原理图可知,I2C1接口的两个引脚分别为:
按键 | SDA1 | SCL1 |
---|---|---|
引脚 | P511 | P512 |
这里我们将在该I2C接口上连接 SHT20 传感器(3.3V供电),实时采样当前环境的温湿度情况。
首先,在FSP配置中将I2C1所用的两个GPIO口配置成I2C模式。
接着选择 Stacks ,点击 "New Stack" -> "Connectivity" -> "I2C Master(r_iic_master)" 添加I2C Master协议栈。
接下来设置I2C Master的相关配置,并重新生成代码。
创建SHT20温湿度传感器操作相关的头文件 src/bsp_sht20.h 如下:
#ifndef BSP_SHT20_H_
#define BSP_SHT20_H_
#include
int sht20_init(void);
void sht20_deinit(void);
int sht20_read_data(double *temp, double *rh);
#endif /* BSP_SHT20_H_ */
创建SHT20温湿度传感器操作相关的c文件 src/bsp_sht20.c 如下:
#include
#include "hal_data.h"
#include "bsp_sht20.h"
#include "r_i2c_master_api.h"
#define RESET_VALUE 0x00
static int i2c_write(uint8_t *data, uint32_t bytes, bool const restart);
static int i2c_read(uint8_t *buf, uint32_t size, bool const restart);
int sht20_init(void)
{
fsp_err_t err = FSP_SUCCESS;
/* Open I2C master */
err = R_IIC_MASTER_Open(&g_i2c1_master_ctrl, &g_i2c1_master_cfg);
if (FSP_SUCCESS != err)
{
printf("** R_IIC_MASTER_Open API failed **
");
return err;
}
#if 0
/* SHT20 sensor soft reset */
uint8_t buf[1] = 0xfe;
err = i2c_write(buf, 1, true);
if (FSP_SUCCESS != err)
{
printf("** SHT20 sensor soft reset failed **
");
return err;
}
R_BSP_SoftwareDelay(50, BSP_DELAY_UNITS_MILLISECONDS);
#endif
return 0;
}
void sht20_deinit(void)
{
R_IIC_MASTER_Close (&g_i2c1_master_ctrl);
return ;
}
int sht20_read_data(double *temp, double *rh)
{
fsp_err_t err = FSP_SUCCESS;
uint8_t buf[4];
/* send trigger temperature measure command and read the data */
buf[0]=0xF3;
err = i2c_write(buf, 1, true);
if (FSP_SUCCESS != err)
{
printf("** SHT20 send trigger temperature measure command failed **
");
return err;
}
/* datasheet: typ=66, max=85 */
R_BSP_SoftwareDelay(85, BSP_DELAY_UNITS_MILLISECONDS);
/* read temperature measure data */
memset(buf, 0, sizeof(buf) );
err = i2c_read(buf, 3, false);
if (FSP_SUCCESS != err)
{
printf("** SHT20 read ID from Location 1 failed **
");
return err;
}
*temp = 175.72 * (((((int) buf[0]) << 8) + buf[1]) / 65536.0) - 46.85;
/* send trigger humidity measure command and read the data */
buf[0] = 0xF5;
err = i2c_write(buf, 1, true);
if (FSP_SUCCESS != err)
{
printf("** SHT20 send trigger humidity measure command failed **
");
return err;
}
/* datasheet: typ=22, max=29 */
R_BSP_SoftwareDelay(29, BSP_DELAY_UNITS_MILLISECONDS);
/* read humidity measure data */
memset(buf, 0, sizeof(buf) );
err = i2c_read(buf, 3, false);
if (FSP_SUCCESS != err)
{
printf("** SHT20 read ID from Location 1 failed **
");
return err;
}
*rh = 125 * (((((int) buf[0]) << 8) + buf[1]) / 65536.0) - 6;
return 0;
}
/* Reading I2C call back event through i2c_Master callback */
static volatile i2c_master_event_t i2c_event = I2C_MASTER_EVENT_ABORTED;
void i2c1_master_callback(i2c_master_callback_args_t *p_args)
{
if (NULL != p_args)
{
/* capture callback event for validating the i2c transfer event*/
i2c_event = p_args->event;
}
}
static fsp_err_t validate_i2c_event(void)
{
uint16_t local_time_out = UINT16_MAX;
/* resetting call back event capture variable */
i2c_event = (i2c_master_event_t)RESET_VALUE;
do
{
/* This is to avoid infinite loop */
--local_time_out;
if(RESET_VALUE == local_time_out)
{
return FSP_ERR_TRANSFER_ABORTED;
}
}while(i2c_event == RESET_VALUE);
if(i2c_event != I2C_MASTER_EVENT_ABORTED)
{
i2c_event = (i2c_master_event_t)RESET_VALUE; // Make sure this is always Reset before return
return FSP_SUCCESS;
}
i2c_event = (i2c_master_event_t)RESET_VALUE; // Make sure this is always Reset before return
return FSP_ERR_TRANSFER_ABORTED;
}
static int i2c_write(uint8_t *data, uint32_t bytes, bool const restart)
{
fsp_err_t err = FSP_SUCCESS;
if( !data || bytes<=0 )
return FSP_ERR_INVALID_ARGUMENT;
err = R_IIC_MASTER_Write(&g_i2c1_master_ctrl, data, bytes, restart);
if (FSP_SUCCESS != err)
{
printf("** R_IIC_MASTER_Write API failed **
");
return err;
}
err = validate_i2c_event();
if (FSP_SUCCESS != err)
{
printf("** I2C write validate failed, err=%d **
", err);
return err;
}
return FSP_SUCCESS;
}
static int i2c_read(uint8_t *buf, uint32_t size, bool const restart)
{
fsp_err_t err = FSP_SUCCESS;
if( !buf || size<=0 )
return FSP_ERR_INVALID_ARGUMENT;
err = R_IIC_MASTER_Read(&g_i2c1_master_ctrl, buf, size, restart);
if (FSP_SUCCESS != err)
{
printf("** R_IIC_MASTER_Write API failed **
");
return err;
}
err = validate_i2c_event();
if (FSP_SUCCESS != err)
{
printf("** I2C read validate failed, err=%d **
", err);
return err;
}
return FSP_SUCCESS;
}
修改 src/hal_entry.c 源文件,在里面添加 sht20 温湿度传感器采样的代码。
... ...
#include "bsp_sht20.h"
... ...
void hal_entry(void)
{
double temp = 0.0;
double rh = 0.0;
... ...
sht20_init();
while (1)
{
... ...
sht20_read_data(&temp, &rh);
printf("temp: %.3f RH: %.3f
", temp, rh);
}
}
代码修改完成后,在开发板上编译运行。
这时串口终端上将会实时打印SHT20传感器采样的温湿度。
全部0条评论
快来发表一下你的评论吧 !