通用型I2C接口的应用之综合应用(N32G45XVL-STB)
目录
概述
1 软硬件接口
1.1 硬件接口
1.2 开发软硬件信息
1.3 SHT-20模块电路
1.4 0.9寸OLED模块介绍
2 驱动接口实现
2.1 SHT20驱动接口
2.2 OLED驱动接口
3 应用接口实现
3.1 软件框架
3.2 sht2x I2C驱动调用
3.3 OLED I2C驱动调用
4 功能实现
4.1 功能介绍
4.2 代码实现
5 测试
5.1 测试代码实现
5.2 板卡运行代码
源代码下载地址:
通用性I2C接口的应用之驱动SHT20(N32G45XVL-STB)资源-CSDN文库
概述
本文主要应用通用型I2C接口的代码库,使用二组不同IO的pin,分别实现I2C功能,同时驱动SHT20X和OLED。在主函数中实时读取SHT20的数据,并将数据实时的显示在OLED屏幕上。温湿度的数据也通过串口log,发送至串口终端上。
1 软硬件接口
1.1 硬件接口
1) SHT20接口
MCU接口 | SHT20 | 功能 |
---|---|---|
PB6 | SDA | I2C数据端口 |
PB7 | SCL | I2C时钟接口 |
2) OLED接口
MCU接口 | OLED | 功能 |
---|---|---|
PB12 | SDA | I2C数据端口 |
PB11 | SCL | I2C时钟接口 |
1.2 开发软硬件信息
软硬件信息 | 版本信息 |
---|---|
N32G45x MCU | N32G457VEL7 |
Keil | MDK ARM 5.38 |
调试工具:CMSIS-DAP | NS-LINK |
1.3 SHT-20模块电路
1)SHT20硬件接口图
2) sensor模块电路
1.4 0.9寸OLED模块介绍
笔者使用的0.9寸OLED模块, 驱动芯片为SSD1306,该模块采用I2C驱动方式,对外有四个引脚:
1) GND: 电源地
2)VCC: 电源正极 ,工作电压区间为3~5.5V
3)SCL: I2C 时钟引脚
4) SDA: I2C数据引脚
模块的原理图如下:
2 驱动接口实现
2.1 SHT20驱动接口
创建sht20x_port.c 文件实现如下代码:
/** \file
$Id: sht20x_port.c 40486 2018-08-12 13:50:21Z tangmingfei2013@126.com $
Copyright (c)tangmingfei2013@126.com Holding B.V.
All Rights Reserved.
This source code and any compilation or derivative thereof is the proprietary
information of mingfei.tang Holding B.V. and is confidential in nature.
Under no circumstances is this software to be combined with any
Open Source Software in any way or placed under an Open Source License
of any type without the express written permission of mingfei.tang Holding B.V.
*/
/*******************************************************************************
* EXPORT INCLUDE FILES
*******************************************************************************/
#include "sht2x_port.h"
/*******************************************************************************
* LOCAL INCLUDE FILES
*******************************************************************************/
#define SHT2X_I2C_PORT_RCC_APB2 RCC_APB2_PERIPH_GPIOB
#define SHT2X_SCL_PIN GPIO_PIN_7
#define SHT2X_SDA_PIN GPIO_PIN_6
#define SHT2X_I2Cx_GPIOx GPIOB
#define I2C_SHT2X_SCL_1() GPIO_SetBits(SHT2X_I2Cx_GPIOx, SHT2X_SCL_PIN) /* SCL = 1 */
#define I2C_SHT2X_SCL_0() GPIO_ResetBits( SHT2X_I2Cx_GPIOx, SHT2X_SCL_PIN) /* SCL = 0 */
#define I2C_SHT2X_SDA_1() GPIO_SetBits( SHT2X_I2Cx_GPIOx, SHT2X_SDA_PIN) /* SDA = 1 */
#define I2C_SHT2X_SDA_0() GPIO_ResetBits( SHT2X_I2Cx_GPIOx, SHT2X_SDA_PIN) /* SDA = 0 */
I2C stru_Sht2xDrv;
/******************************************************************************
* EXPORTED FUNCTIONS
******************************************************************************/
static void i2c_init_port( void )
{
GPIO_InitType i2c1_gpio;
/* Enable GPIO clock */
RCC_EnableAPB2PeriphClk(SHT2X_I2C_PORT_RCC_APB2, ENABLE);
/*
PB6/PB8 -- SCL;
PB7/PB9 -- SDA
*/
i2c1_gpio.Pin = SHT2X_SCL_PIN | SHT2X_SDA_PIN;
i2c1_gpio.GPIO_Speed = GPIO_Speed_2MHz;
i2c1_gpio.GPIO_Mode = GPIO_Mode_Out_OD;
GPIO_InitPeripheral(SHT2X_I2Cx_GPIOx, &i2c1_gpio);
}
static uint8_t i2c_SDA_READ( void )
{
GPIO_InitType GPIO_InitStructure;
uint8_t val;
// set io as input
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.Pin = SHT2X_SDA_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitPeripheral(SHT2X_I2Cx_GPIOx, &GPIO_InitStructure);
// set io as output
val = ((SHT2X_I2Cx_GPIOx->PID & SHT2X_SDA_PIN) != 0);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
GPIO_InitPeripheral(SHT2X_I2Cx_GPIOx, &GPIO_InitStructure);
return val;
}
static uint8_t i2c_SCL_READ( void )
{
GPIO_InitType GPIO_InitStructure;
uint8_t val;
// set io as input
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.Pin = SHT2X_SCL_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitPeripheral(SHT2X_I2Cx_GPIOx, &GPIO_InitStructure);
// set io as output
val = ((SHT2X_I2Cx_GPIOx->PID & SHT2X_SCL_PIN) != 0);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
GPIO_InitPeripheral(SHT2X_I2Cx_GPIOx, &GPIO_InitStructure);
return val;
}
static void set_i2c_scl_1( void )
{
I2C_SHT2X_SCL_1();
}
static void set_i2c_scl_0( void )
{
I2C_SHT2X_SCL_0();
}
static void set_i2c_sda_1( void )
{
I2C_SHT2X_SDA_1();
}
static void set_i2c_sda_0( void )
{
I2C_SHT2X_SDA_0();
}
void i2c_Sht2xDrvRegisterPort(void)
{
stru_Sht2xDrv.IIC_InitPort = i2c_init_port;
stru_Sht2xDrv.IIC_READ_SCL = i2c_SCL_READ;
stru_Sht2xDrv.IIC_READ_SDA = i2c_SDA_READ;
stru_Sht2xDrv.IIC_SCL_H = set_i2c_scl_1;
stru_Sht2xDrv.IIC_SCL_L = set_i2c_scl_0;
stru_Sht2xDrv.IIC_SDA_H = set_i2c_sda_1;
stru_Sht2xDrv.IIC_SDA_L = set_i2c_sda_0;
}
2.2 OLED驱动接口
创建oled_port.c 文件,实现如下代码:
/** \file
$Id: oled_port.c 40486 2018-08-12 13:50:21Z tangmingfei2013@126.com $
Copyright (c)tangmingfei2013@126.com Holding B.V.
All Rights Reserved.
This source code and any compilation or derivative thereof is the proprietary
information of mingfei.tang Holding B.V. and is confidential in nature.
Under no circumstances is this software to be combined with any
Open Source Software in any way or placed under an Open Source License
of any type without the express written permission of mingfei.tang Holding B.V.
*/
/*******************************************************************************
* EXPORT INCLUDE FILES
*******************************************************************************/
#include "oled_port.h"
/*******************************************************************************
* LOCAL INCLUDE FILES
*******************************************************************************/
#define OLED_I2C_PORT_RCC_APB2 RCC_APB2_PERIPH_GPIOB
#define OLED_SCL_PIN GPIO_PIN_11
#define OLED_SDA_PIN GPIO_PIN_12
#define OLED_I2Cx_GPIOx GPIOB
#define I2C_SCL_1() GPIO_SetBits(OLED_I2Cx_GPIOx, OLED_SCL_PIN) /* SCL = 1 */
#define I2C_SCL_0() GPIO_ResetBits( OLED_I2Cx_GPIOx, OLED_SCL_PIN) /* SCL = 0 */
#define I2C_SDA_1() GPIO_SetBits( OLED_I2Cx_GPIOx, OLED_SDA_PIN) /* SDA = 1 */
#define I2C_SDA_0() GPIO_ResetBits( OLED_I2Cx_GPIOx, OLED_SDA_PIN) /* SDA = 0 */
I2C stru_OledDrv;
/******************************************************************************
* EXPORTED FUNCTIONS
******************************************************************************/
static void i2c_init_port( void )
{
GPIO_InitType i2c1_gpio;
/* Enable GPIO clock */
RCC_EnableAPB2PeriphClk(OLED_I2C_PORT_RCC_APB2, ENABLE);
/*
PB11 -- SCL;
PB12 -- SDA
*/
i2c1_gpio.Pin = OLED_SCL_PIN | OLED_SDA_PIN;
i2c1_gpio.GPIO_Speed = GPIO_Speed_2MHz;
i2c1_gpio.GPIO_Mode = GPIO_Mode_Out_OD;
GPIO_InitPeripheral(OLED_I2Cx_GPIOx, &i2c1_gpio);
}
static uint8_t i2c_SDA_READ( void )
{
GPIO_InitType GPIO_InitStructure;
uint8_t val;
// set io as input
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.Pin = OLED_SDA_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitPeripheral(OLED_I2Cx_GPIOx, &GPIO_InitStructure);
// set io as output
val = ((OLED_I2Cx_GPIOx->PID & OLED_SDA_PIN) != 0);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
GPIO_InitPeripheral(OLED_I2Cx_GPIOx, &GPIO_InitStructure);
return val;
}
static uint8_t i2c_SCL_READ( void )
{
GPIO_InitType GPIO_InitStructure;
uint8_t val;
// set io as input
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.Pin = OLED_SCL_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitPeripheral(OLED_I2Cx_GPIOx, &GPIO_InitStructure);
// set io as output
val = ((OLED_I2Cx_GPIOx->PID & OLED_SCL_PIN) != 0);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
GPIO_InitPeripheral(OLED_I2Cx_GPIOx, &GPIO_InitStructure);
return val;
}
static void set_i2c_scl_1( void )
{
I2C_SCL_1();
}
static void set_i2c_scl_0( void )
{
I2C_SCL_0();
}
static void set_i2c_sda_1( void )
{
I2C_SDA_1();
}
static void set_i2c_sda_0( void )
{
I2C_SDA_0();
}
void i2c_OledRegisterPort(void)
{
stru_OledDrv.IIC_InitPort = i2c_init_port;
stru_OledDrv.IIC_READ_SCL = i2c_SCL_READ;
stru_OledDrv.IIC_READ_SDA = i2c_SDA_READ;
stru_OledDrv.IIC_SCL_H = set_i2c_scl_1;
stru_OledDrv.IIC_SCL_L = set_i2c_scl_0;
stru_OledDrv.IIC_SDA_H = set_i2c_sda_1;
stru_OledDrv.IIC_SDA_L = set_i2c_sda_0;
}
3 应用接口实现
3.1 软件框架
在i2c_master.c文件中实现了所有i2c功能函数
1)sht2x驱动部分
- 在sht2x_port.c中实现和mcu相关的硬件接口
- sht2x.c 应用层的驱动函数库
2)OLED驱动部分
- 在oled_port.c中实现和mcu相关的硬件接口
- oled.c 应用层的驱动函数库
3.2 sht2x I2C驱动调用
在sht2x.c中调用驱动程序
代码383行:注册驱动接口
代码384行: 初始化设备
3.3 OLED I2C驱动调用
代码55行: 注册驱动接口
代码57行:初始化IO端口
4 功能实现
4.1 功能介绍
本系统实现的功能如下:
1) 使用两组IO( 4个pin)分别实现I2C功能
2)一组i2c 驱动SHT20
3) 另一组i2c驱动OLED
4.2 代码实现
1)初始化OLED UI, 显示温度和湿度的log
2)数据显示函数
UI.c文件中实现了所有和显示相关的函数接口
代码48行:获取sht2x的数据
代码51~62行:处理和显示温度数据
代码66~77行:处理和显示湿度数据
5 测试
5.1 测试代码实现
在main.c文件中实现所有功能函数的调用
代码36行:初始化sht20驱动
代码37行:初始化OLED,并显示UI
代码41行:实时读取sht20的温湿度数据
代码44行:OLED上实时更新温湿度数据
5.2 板卡运行代码
编译代码,下载到板卡中,运行程序。
串口终端实时监测数据