7.1 原理圖分析
查看EK-RA6M4的原理圖,如下圖所示,該開發(fā)板上的MikroBus接口上有提供一個I2C接口。
根據(jù)原理圖可知,I2C1接口的兩個引腳分別為:
按鍵 | SDA1 | SCL1 |
---|---|---|
引腳 | P511 | P512 |
這里我們將在該I2C接口上連接 SHT20 傳感器(3.3V供電),實時采樣當(dāng)前環(huán)境的溫濕度情況。
7.2 I2C接口配置
首先,在FSP配置中將I2C1所用的兩個GPIO口配置成I2C模式。
接著選擇 Stacks ,點擊 "New Stack" -> "Connectivity" -> "I2C Master(r_iic_master)" 添加I2C Master協(xié)議棧。
接下來設(shè)置I2C Master的相關(guān)配置,并重新生成代碼。
- 通過 Name 字段可以修改I2C的設(shè)備名稱為 g_i2c1_master,它將在IDE自動生成的文件 ra_gen/hal_data.c/h 中定義I2C操作相關(guān)的變量;
- 通過 Channel 字段可以修改I2C的通道號,這里設(shè)置為1,下面的 Pins 將自動選擇 P511和 P512 這個引腳;
- 通過 Slava Address 字段可以設(shè)置I2C從設(shè)備的地址,其中SHT20 的從設(shè)備地址為 0x40;
- 通過 Callback 字段設(shè)置I2C收發(fā)的中斷回調(diào)函數(shù),它將配置在 g_i2c1_master_cfg 變量中,該函數(shù)需要我們自己實現(xiàn);
- 在這里我們也可以修改I2C中斷的優(yōu)先級為2級別;
7.3 源碼修改
創(chuàng)建SHT20溫濕度傳感器操作相關(guān)的頭文件 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_ */
創(chuàng)建SHT20溫濕度傳感器操作相關(guān)的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 編譯運行
代碼修改完成后,在開發(fā)板上編譯運行。
這時串口終端上將會實時打印SHT20傳感器采樣的溫濕度。
-
傳感器
+關(guān)注
關(guān)注
2553文章
51427瀏覽量
756787 -
單片機
+關(guān)注
關(guān)注
6043文章
44623瀏覽量
638789 -
接口
+關(guān)注
關(guān)注
33文章
8706瀏覽量
151974 -
I2C
+關(guān)注
關(guān)注
28文章
1495瀏覽量
124668
發(fā)布評論請先 登錄
相關(guān)推薦
新人發(fā)帖問關(guān)于i2c溫濕度傳感器SHT20在藍(lán)牙協(xié)議棧的問題
使用PSOC I2C獲取SHT20溫濕度
【OneNET麒麟座試用體驗】3.OneNET之HAL i2c的使用(SHT20溫濕度測量)
如何利用軟件模擬I2C讀寫SHT20溫濕度傳感器
STM32硬件i2c,CubeMX,Hal庫
使用STM32F030單片機讀取溫濕度傳感器SHT20的C語言源代碼免費下載
![使用STM32F030<b class='flag-5'>單片機</b>讀取溫濕度<b class='flag-5'>傳感器</b><b class='flag-5'>SHT20</b>的<b class='flag-5'>C</b>語言源代碼免費下載](https://file.elecfans.com/web1/M00/95/4A/o4YBAFz-HmSAGjz4AA5StlYw54E844.png)
基于MCS-51單片機I2C總線接口電路的設(shè)計
![基于MCS-51<b class='flag-5'>單片機</b><b class='flag-5'>I2C</b>總線<b class='flag-5'>接口</b>電路的設(shè)計](https://file.elecfans.com/web1/M00/AA/61/pIYBAF2lgI-AMlzLAAA6xXqj1eA731.png)
SHT20溫濕度傳感器的數(shù)據(jù)手冊免費下載
![<b class='flag-5'>SHT20</b>溫濕度<b class='flag-5'>傳感器</b>的數(shù)據(jù)手冊免費下載](https://file.elecfans.com/web1/M00/BB/72/pIYBAF6lABKAFs1nAAD5n_vmA8Y885.png)
STM32CubeMX I2C SHT20的工程文件免費下載
使用單片機實現(xiàn)I2C接口溫度傳感器DS1621應(yīng)用測試的C語言實例
I2C協(xié)議之軟件模擬(二)-- 實際應(yīng)用之SHT20
![<b class='flag-5'>I2C</b>協(xié)議之軟件模擬(二)-- 實際應(yīng)用之<b class='flag-5'>SHT20</b>](https://file.elecfans.com/web1/M00/D9/4E/pIYBAF_1ac2Ac0EEAABDkS1IP1s689.png)
評論