7.1 原理圖分析
查看EK-RA6M4的原理圖,如下圖所示,該開發板上的MikroBus接口上有提供一個I2C接口。
根據原理圖可知,I2C1接口的兩個引腳分別為:
按鍵 | SDA1 | SCL1 |
---|---|---|
引腳 | P511 | P512 |
這里我們將在該I2C接口上連接 SHT20 傳感器(3.3V供電),實時采樣當前環境的溫濕度情況。
7.2 I2C接口配置
首先,在FSP配置中將I2C1所用的兩個GPIO口配置成I2C模式。
接著選擇 Stacks ,點擊 "New Stack" -> "Connectivity" -> "I2C Master(r_iic_master)" 添加I2C Master協議棧。
接下來設置I2C Master的相關配置,并重新生成代碼。
- 通過 Name 字段可以修改I2C的設備名稱為 g_i2c1_master,它將在IDE自動生成的文件 ra_gen/hal_data.c/h 中定義I2C操作相關的變量;
- 通過 Channel 字段可以修改I2C的通道號,這里設置為1,下面的 Pins 將自動選擇 P511和 P512 這個引腳;
- 通過 Slava Address 字段可以設置I2C從設備的地址,其中SHT20 的從設備地址為 0x40;
- 通過 Callback 字段設置I2C收發的中斷回調函數,它將配置在 g_i2c1_master_cfg 變量中,該函數需要我們自己實現;
- 在這里我們也可以修改I2C中斷的優先級為2級別;
7.3 源碼修改
創建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);
}
}
7.4 編譯運行
代碼修改完成后,在開發板上編譯運行。
這時串口終端上將會實時打印SHT20傳感器采樣的溫濕度。
-
傳感器
+關注
關注
2548文章
50668瀏覽量
751981 -
單片機
+關注
關注
6032文章
44514瀏覽量
632980 -
接口
+關注
關注
33文章
8497瀏覽量
150834 -
I2C
+關注
關注
28文章
1481瀏覽量
123283
發布評論請先 登錄
相關推薦
評論