您的位置:首页 > 娱乐 > 八卦 > Renesas MCU使用SCI_I2C驱动OLED

Renesas MCU使用SCI_I2C驱动OLED

2024/10/5 21:15:58 来源:https://blog.csdn.net/mftang/article/details/139524226  浏览:    关键词:Renesas MCU使用SCI_I2C驱动OLED

目录

概述

1 软硬件

 1.1 软件版本信息

1.2 OLED屏幕

1.2.1 OLED简介

1.2.2 SSD1306介绍

1.2.3 0.9寸OLED模块介绍

 2 FSP配置项目

2.1 配置项目参数

 2.2 生成项目文件架构

3 代码实现

3.1 I2C的库函数

3.1.1 R_SCI_I2C_Open()

3.1.2 R_SCI_I2C_Read()

3.1.3  R_SCI_I2C_Write()

3.2 应用函数接口

3.2.1 初始化函数

3.2.2 读数据函数

 3.2.3 写数据函数

3.2.4 回调函数

4 OLED驱动实现

4.1 编写驱动程序

4.2 编写测试函数

4.3 源代码

5 测试


概述

本文主要介绍使用Renesas MCU的SCI_I2C模块驱动OLED,文中介绍了OLED的相关知识,包括模块参数,接口信息,还介绍了FSP库中I2C相关的接口函数。最后使用HS3003采集环境温湿度数据,并在OLED UI上显示出来。

1 软硬件

 1.1 软件版本信息

软硬件信息版本信息
Renesas MCUR7FA4M2AD3C
KeilMDK ARM 5.38
FSP 版本5.3.0
调试工具:st-linkST-LINK/V2-1

 注意:

在Keil MDK中可以更改FSP的版本,方法如下

1.2 OLED屏幕

1.2.1 OLED简介

     OLED(有机发光二极管)是一种显示技术,它使用有机物质来发出光线。与传统液晶显示屏相比,OLED具有更高的对比度和更广的视角。它没有背光源,因此可以提供更深的黑色和更明亮的白色。

       OLED显示屏由许多小的发光二极管组成,每个像素都可以独立发光。这意味着OLED显示屏可以实现真正的黑色,因为当像素关闭时,没有任何光线发射。

     OLED具有快速的响应时间,因此在显示动态图像时可以提供更流畅的画面。此外,OLED显示屏拥有更宽广的视角,因此无论从哪个角度观看,图像都能保持清晰和准确。

      OLED显示屏还具有较高的能源效率。因为每个像素都可以独立点亮,所以在显示黑色时可以关闭相应的像素,从而节省能源。

      OLED技术可应用于各种设备,包括智能手机、电视、电子书阅读器、车载显示屏等。由于OLED显示屏的薄度和柔性,它还可以用于可弯曲的显示设备和可穿戴设备。

       然而,OLED显示屏也有一些限制。首先,长时间显示相同的图像可能会导致“烧结”效应,即在屏幕上留下轮廓。其次,OLED显示屏的寿命相对较短,使用寿命通常较液晶显示屏更短。

      总的来说,OLED技术在显示领域具有许多优势,如高对比度、广视角和能源效率。随着技术的不断发展,OLED显示屏将在各种设备中得到更广泛的应用。

1.2.2 SSD1306介绍

      SSD1306是一款常用的单色OLED驱动芯片,由Solomon Systech Limited(国巨控股)设计和生产。它被广泛应用于各种嵌入式系统和电子设备中,用于驱动OLED显示屏。

      SSD1306采用低功耗、高对比度和快速刷新率的技术,使得显示效果清晰亮丽。它具备128x64或128x32像素的分辨率,并支持四种显示模式:正常显示、反转显示、镜像显示和指令显示。SSD1306还可以通过I2C或SPI接口与微控制器或其他外部设备进行通信。

      SSD1306内置了丰富的图形绘制和文本显示功能,可以轻松实现直线、矩形、圆形、位图等图形的绘制,以及字符、数字和自定义字体的显示。它还提供了多种显示模式和亮度调节选项,可以根据实际需求灵活控制显示效果。

      SSD1306还具备内置的电源管理功能,支持低电压检测和电池电量显示,可以实现智能电源管理。此外,它还支持多种硬件和软件配置选项,方便用户进行定制化开发。

      总的来说,SSD1306是一款功能强大、易于使用的OLED驱动芯片,适用于各种嵌入式系统和电子设备中,能够实现高质量、低功耗的显示效果。

1.2.3 0.9寸OLED模块介绍

笔者使用的0.9寸OLED模块, 驱动芯片为SSD1306,该模块采用I2C驱动方式,对外有四个引脚:

1) GND: 电源地

2)VCC: 电源正极 ,工作电压区间为3~5.5V

3)SCL: I2C 时钟引脚

4) SDA: I2C数据引脚

模块的原理图如下:

 2 FSP配置项目

2.1 配置项目参数

1)配置系统时钟,根据硬件特性配置时钟,笔者的板卡上的晶振为12M Hz

 2) 在Pins面板配置SCI3,使能I2C,并且配置IO端口

3)在Stacks面板创建i2c的object

 配置参数

 2.2 生成项目文件架构

点击Generate Project content 按钮,生成项目文件,其文件架构如下:

3 代码实现

3.1 I2C的库函数

3.1.1 R_SCI_I2C_Open()

函数原型:


fsp_err_t R_SCI_I2C_Open	(	i2c_master_ctrl_t *const 	p_api_ctrl,i2c_master_cfg_t const *const 	p_cfg )	

函数功能: 打开I2C设备

返回值介绍:

FSP_SUCCESSRequested clock rate was set exactly.
FSP_ERR_ALREADY_OPENModule is already open.
FSP_ERR_IP_CHANNEL_NOT_PRESENTChannel is not available on this MCU.
FSP_ERR_ASSERTIONParameter check failure due to one or more reasons below:
  1. p_api_ctrl or p_cfg is NULL.
  2. extended parameter is NULL.
  3. Callback parameter is NULL.
  4. Clock rate requested is greater than 400KHz
  5. Invalid IRQ number assigned

3.1.2 R_SCI_I2C_Read()

函数原型:


fsp_err_t R_SCI_I2C_Read	(	i2c_master_ctrl_t *const 	p_api_ctrl,uint8_t *const 	p_dest,uint32_t const 	bytes,bool const 	restart )	

函数功能: 从I2C设备执行读取操作。当操作(成功)完成时,调用者将通过回调中的I2C_MASTER_EVENT_RX_COMPLETE收到通知。

返回值:

FSP_SUCCESSFunction executed without issue.
FSP_ERR_ASSERTIONThe parameter p_ctrl, p_dest is NULL, bytes is 0.
FSP_ERR_INVALID_SIZEProvided number of bytes more than uint16_t size (65535) while DTC is used for data transfer.
FSP_ERR_NOT_OPENDevice was not even opened.

3.1.3  R_SCI_I2C_Write()

函数原型:


fsp_err_t R_SCI_I2C_Write	(	i2c_master_ctrl_t *const 	p_api_ctrl,uint8_t *const 	p_src,uint32_t const 	bytes,bool const 	restart )	

函数功能: 对I2C设备进行写操作,如果相关通道上已经有正在进行的I2C传输,则此函数将失败。否则,将开始I2C写操作。当用户没有提供回调时,该函数执行阻塞写入。否则,写操作是非阻塞的,并且当操作完成时将通过回调中的I2C_EVENT_TX_COMPLETE通知调用者。

返回值:

FSP_SUCCESSFunction executed without issue.
FSP_ERR_ASSERTIONp_ctrl, p_src is NULL.
FSP_ERR_INVALID_SIZEProvided number of bytes more than uint16_t size (65535) while DTC is used for data transfer.
FSP_ERR_NOT_OPENDevice was not even opened.

3.2 应用函数接口

创建bsp_i2c.c文件,实现i2c的应用函数接口

3.2.1 初始化函数

代码第24行:打开端口

代码第27行:设置设备地址

3.2.2 读数据函数

代码第54行:从设备中读取数据

代码第57行:判断接收数据是否完成

 3.2.3 写数据函数

代码第36行:从设备中写数据

代码第41行:判断写数据是否完成

3.2.4 回调函数

代码第16行:接收当前event

 

4 OLED驱动实现

4.1 编写驱动程序

1)创建oled_drv.c文件,编写如下代码:

/** \file$Id: oled.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 "bsp_i2c.h" 
#include "oled_drv.h"
/*******************************************************************************
* LOCAL INCLUDE FILES
*******************************************************************************/
#include "font.h"static uint8_t charSize;
/******************************************************************************
* LOCAL FUNCTION DECLARATIONS
******************************************************************************/
static void oled_WriteRegister(uint8_t val,  uint8_t reg);/******************************************************************************
* EXPORTED FUNCTIONS
******************************************************************************/
void oled_Init(void)
{ i2c2_init_para(OLED_ADDRESS);oled_WriteRegister(0xAE,PARA);//--display offoled_WriteRegister(0x00,PARA);//---set low column addressoled_WriteRegister(0x10,PARA);//---set high column addressoled_WriteRegister(0x40,PARA);//--set start line address  oled_WriteRegister(0xB0,PARA);//--set page addressoled_WriteRegister(0x81,PARA); // contract controloled_WriteRegister(0xFF,PARA);//--128   oled_WriteRegister(0xA1,PARA);//set segment remap oled_WriteRegister(0xA6,PARA);//--normal / reverseoled_WriteRegister(0xA8,PARA);//--set multiplex ratio(1 to 64)oled_WriteRegister(0x3F,PARA);//--1/32 dutyoled_WriteRegister(0xC8,PARA);//Com scan directionoled_WriteRegister(0xD3,PARA);//-set display offsetoled_WriteRegister(0x00,PARA);//oled_WriteRegister(0xD5,PARA);//set osc divisionoled_WriteRegister(0x80,PARA);//oled_WriteRegister(0xD8,PARA);//set area color mode offoled_WriteRegister(0x05,PARA);//oled_WriteRegister(0xD9,PARA);//Set Pre-Charge Periodoled_WriteRegister(0xF1,PARA);//oled_WriteRegister(0xDA,PARA);//set com pin configuartionoled_WriteRegister(0x12,PARA);//oled_WriteRegister(0xDB,PARA);//set Vcomholed_WriteRegister(0x30,PARA);//oled_WriteRegister(0x8D,PARA);//set charge pump enableoled_WriteRegister(0x14,PARA);//oled_WriteRegister(0xAF,PARA);//--turn on oled paneloled_Clear();#if OLED_DEBUG  oled_SetCharSize( FONT_16 );  oled_PrintfString(6,0,(uint8_t*)"0.96' OLED TEST");  oled_SetCharSize( FONT_12 );oled_PrintfString(2,6,(uint8_t*)"ASCII:");  oled_PrintfString(60,6,(uint8_t*)"-CODE:");
#endif} void oled_SetCharSize( uint8_t val )
{charSize = val;
}void oled_SetPostion( uint8_t x, uint8_t y) 
{oled_WriteRegister(0xb0+y,PARA);oled_WriteRegister(((x&0xf0)>>4)|0x10,PARA);oled_WriteRegister((x&0x0f),PARA); 
}void oled_displayOn(void)
{oled_WriteRegister(0X8D,PARA); oled_WriteRegister(0X14,PARA); oled_WriteRegister(0XAF,PARA);
}void oled_DisplayOff(void)
{oled_WriteRegister(0X8D,PARA);oled_WriteRegister(0X10,PARA);oled_WriteRegister(0XAE,PARA);
}void oled_Clear(void)  
{  uint8_t i,n;for(i=0;i<8;i++)  {  oled_WriteRegister (0xb0+i,PARA);   oled_WriteRegister (0x00,PARA);oled_WriteRegister (0x10,PARA); for( n = 0;n < 128; n++){oled_WriteRegister(0,DATA);} } 
}void oled_UpScreenOn(void)  
{  uint8_t i,n;for(i=0;i<8;i++)  {  oled_WriteRegister (0xb0+i,PARA);oled_WriteRegister (0x00,PARA); oled_WriteRegister (0x10,PARA);for(n=0;n<128;n++){oled_WriteRegister(1,DATA); }} 
}void oled_PrintfChar(uint8_t x,uint8_t y,uint8_t val )
{      	uint8_t character = 0;uint8_t i=0;character = val-' ';if( x > X_WIDTH-1){x = 0;y += 2;}if( charSize == FONT_16 ){oled_SetPostion(x,y);	for( i = 0; i < 8; i++){oled_WriteRegister(F8X16[character*16+i],DATA);}oled_SetPostion(x,y+1);for(i=0;i<8;i++){oled_WriteRegister(F8X16[character*16+i+8],DATA);}}else {oled_SetPostion(x,y);for(i=0;i<6;i++){oled_WriteRegister(F6x8[character*6+i],DATA);}}
}void oled_PrintfString(uint8_t x,uint8_t y,uint8_t *str  )
{uint8_t index = 0;while ( str[index]!='\0' ){oled_PrintfChar(x,y,str[index] );x += 8;if( x > 120 ){  x=0;y+=2;}index++;}
}void oled_PrintmutiChar(uint8_t x,uint8_t y,uint8_t *str,  uint8_t length )
{for(uint8_t index = 0; index < length; index++ ){oled_PrintfChar(x,y,str[index] );x += 8;if( x > 120 ){  x=0;y+=2;}}
}/******************************************************************************
* LOCAL FUNCTIONS
******************************************************************************/
static void oled_WriteRegister(uint8_t val,  uint8_t reg)
{  fsp_err_t err;unsigned char buff[2];err = R_SCI_I2C_SlaveAddressSet( &g_i2c2_ctrl, OLED_ADDRESS, I2C_MASTER_ADDR_MODE_7BIT);assert(FSP_SUCCESS == err);buff[0] = reg;buff[1] = val;i2c2_write_bytes(buff, 2);
}
/* End of this file */

2)创建oled_drv.h文件,编写如下代码:

/** \file$Id: oled.h 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.
*/
#ifndef __OLED_DRV_H
#define __OLED_DRV_H/******************************************************************************
* C++ DECLARATION WRAPPER
******************************************************************************/#ifdef __cplusplus
extern "C" {
#endif#include "hal_data.h"
/******************************************************************************
* LOCAL MACROS AND DEFINITIONS
******************************************************************************/
#define OLED_DEBUG      1
#define OLED_ADDRESS    0x3ctypedef enum
{FONT_16 = 0,FONT_12 = 1,
}OLED_CHARSIZE;typedef enum
{PARA = 0,DATA = 0x40,
}OLED_PARE_REG;typedef enum
{X_WIDTH = 128,Y_WIDTH = 64,
}OLED_SCREEN_SIZE;/******************************************************************************
* EXPORTED MACROS AND DEFINITIONS
******************************************************************************/ /******************************************************************************
* EXPORTED FUNCTIONS
******************************************************************************/
void oled_Clear(void) ;
void oled_Init(void);
void oled_UpScreenOn(void);void oled_PrintfString(uint8_t x,uint8_t y,uint8_t *str);
void oled_PrintfChar(uint8_t x,uint8_t y,uint8_t val);
void oled_SetPostion( uint8_t x, uint8_t y) ;
void oled_SetCharSize( uint8_t val );
void oled_PrintmutiChar(uint8_t x,uint8_t y,uint8_t *str,  uint8_t length);/******************************************************************************
* END OF C++ DECLARATION WRAPPER
******************************************************************************/#ifdef __cplusplus
}
#endif#endif /* __OLED_DRV_H */

3) 创建font.h文件,编写如下代码:

/** \file$Id: font.h 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 Lighting 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.
*/#ifndef __FONT_H
#define __FONT_H/*******************************************************************************
* EXPORT INCLUDE FILES
*******************************************************************************//******************************************************************************
* C++ DECLARATION WRAPPER
******************************************************************************/
#ifdef __cplusplus
extern "C" {
#endifconst unsigned char F6x8[] =
{
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,// sp
0x00, 0x00, 0x00, 0x2f, 0x00, 0x00,// !
0x00, 0x00, 0x07, 0x00, 0x07, 0x00,// "
0x00, 0x14, 0x7f, 0x14, 0x7f, 0x14,// #
0x00, 0x24, 0x2a, 0x7f, 0x2a, 0x12,// $
0x00, 0x62, 0x64, 0x08, 0x13, 0x23,// %
0x00, 0x36, 0x49, 0x55, 0x22, 0x50,// &
0x00, 0x00, 0x05, 0x03, 0x00, 0x00,// '
0x00, 0x00, 0x1c, 0x22, 0x41, 0x00,// (
0x00, 0x00, 0x41, 0x22, 0x1c, 0x00,// )
0x00, 0x14, 0x08, 0x3E, 0x08, 0x14,// *
0x00, 0x08, 0x08, 0x3E, 0x08, 0x08,// +
0x00, 0x00, 0x00, 0xA0, 0x60, 0x00,// ,
0x00, 0x08, 0x08, 0x08, 0x08, 0x08,// -
0x00, 0x00, 0x60, 0x60, 0x00, 0x00,// .
0x00, 0x20, 0x10, 0x08, 0x04, 0x02,// /
0x00, 0x3E, 0x51, 0x49, 0x45, 0x3E,// 0
0x00, 0x00, 0x42, 0x7F, 0x40, 0x00,// 1
0x00, 0x42, 0x61, 0x51, 0x49, 0x46,// 2
0x00, 0x21, 0x41, 0x45, 0x4B, 0x31,// 3
0x00, 0x18, 0x14, 0x12, 0x7F, 0x10,// 4
0x00, 0x27, 0x45, 0x45, 0x45, 0x39,// 5
0x00, 0x3C, 0x4A, 0x49, 0x49, 0x30,// 6
0x00, 0x01, 0x71, 0x09, 0x05, 0x03,// 7
0x00, 0x36, 0x49, 0x49, 0x49, 0x36,// 8
0x00, 0x06, 0x49, 0x49, 0x29, 0x1E,// 9
0x00, 0x00, 0x36, 0x36, 0x00, 0x00,// :
0x00, 0x00, 0x56, 0x36, 0x00, 0x00,// ;
0x00, 0x08, 0x14, 0x22, 0x41, 0x00,// <
0x00, 0x14, 0x14, 0x14, 0x14, 0x14,// =
0x00, 0x00, 0x41, 0x22, 0x14, 0x08,// >
0x00, 0x02, 0x01, 0x51, 0x09, 0x06,// ?
0x00, 0x32, 0x49, 0x59, 0x51, 0x3E,// @
0x00, 0x7C, 0x12, 0x11, 0x12, 0x7C,// A
0x00, 0x7F, 0x49, 0x49, 0x49, 0x36,// B
0x00, 0x3E, 0x41, 0x41, 0x41, 0x22,// C
0x00, 0x7F, 0x41, 0x41, 0x22, 0x1C,// D
0x00, 0x7F, 0x49, 0x49, 0x49, 0x41,// E
0x00, 0x7F, 0x09, 0x09, 0x09, 0x01,// F
0x00, 0x3E, 0x41, 0x49, 0x49, 0x7A,// G
0x00, 0x7F, 0x08, 0x08, 0x08, 0x7F,// H
0x00, 0x00, 0x41, 0x7F, 0x41, 0x00,// I
0x00, 0x20, 0x40, 0x41, 0x3F, 0x01,// J
0x00, 0x7F, 0x08, 0x14, 0x22, 0x41,// K
0x00, 0x7F, 0x40, 0x40, 0x40, 0x40,// L
0x00, 0x7F, 0x02, 0x0C, 0x02, 0x7F,// M
0x00, 0x7F, 0x04, 0x08, 0x10, 0x7F,// N
0x00, 0x3E, 0x41, 0x41, 0x41, 0x3E,// O
0x00, 0x7F, 0x09, 0x09, 0x09, 0x06,// P
0x00, 0x3E, 0x41, 0x51, 0x21, 0x5E,// Q
0x00, 0x7F, 0x09, 0x19, 0x29, 0x46,// R
0x00, 0x46, 0x49, 0x49, 0x49, 0x31,// S
0x00, 0x01, 0x01, 0x7F, 0x01, 0x01,// T
0x00, 0x3F, 0x40, 0x40, 0x40, 0x3F,// U
0x00, 0x1F, 0x20, 0x40, 0x20, 0x1F,// V
0x00, 0x3F, 0x40, 0x38, 0x40, 0x3F,// W
0x00, 0x63, 0x14, 0x08, 0x14, 0x63,// X
0x00, 0x07, 0x08, 0x70, 0x08, 0x07,// Y
0x00, 0x61, 0x51, 0x49, 0x45, 0x43,// Z
0x00, 0x00, 0x7F, 0x41, 0x41, 0x00,// [
0x00, 0x55, 0x2A, 0x55, 0x2A, 0x55,// 55
0x00, 0x00, 0x41, 0x41, 0x7F, 0x00,// ]
0x00, 0x04, 0x02, 0x01, 0x02, 0x04,// ^
0x00, 0x40, 0x40, 0x40, 0x40, 0x40,// _
0x00, 0x00, 0x01, 0x02, 0x04, 0x00,// '
0x00, 0x20, 0x54, 0x54, 0x54, 0x78,// a
0x00, 0x7F, 0x48, 0x44, 0x44, 0x38,// b
0x00, 0x38, 0x44, 0x44, 0x44, 0x20,// c
0x00, 0x38, 0x44, 0x44, 0x48, 0x7F,// d
0x00, 0x38, 0x54, 0x54, 0x54, 0x18,// e
0x00, 0x08, 0x7E, 0x09, 0x01, 0x02,// f
0x00, 0x18, 0xA4, 0xA4, 0xA4, 0x7C,// g
0x00, 0x7F, 0x08, 0x04, 0x04, 0x78,// h
0x00, 0x00, 0x44, 0x7D, 0x40, 0x00,// i
0x00, 0x40, 0x80, 0x84, 0x7D, 0x00,// j
0x00, 0x7F, 0x10, 0x28, 0x44, 0x00,// k
0x00, 0x00, 0x41, 0x7F, 0x40, 0x00,// l
0x00, 0x7C, 0x04, 0x18, 0x04, 0x78,// m
0x00, 0x7C, 0x08, 0x04, 0x04, 0x78,// n
0x00, 0x38, 0x44, 0x44, 0x44, 0x38,// o
0x00, 0xFC, 0x24, 0x24, 0x24, 0x18,// p
0x00, 0x18, 0x24, 0x24, 0x18, 0xFC,// q
0x00, 0x7C, 0x08, 0x04, 0x04, 0x08,// r
0x00, 0x48, 0x54, 0x54, 0x54, 0x20,// s
0x00, 0x04, 0x3F, 0x44, 0x40, 0x20,// t
0x00, 0x3C, 0x40, 0x40, 0x20, 0x7C,// u
0x00, 0x1C, 0x20, 0x40, 0x20, 0x1C,// v
0x00, 0x3C, 0x40, 0x30, 0x40, 0x3C,// w
0x00, 0x44, 0x28, 0x10, 0x28, 0x44,// x
0x00, 0x1C, 0xA0, 0xA0, 0xA0, 0x7C,// y
0x00, 0x44, 0x64, 0x54, 0x4C, 0x44,// z
0x14, 0x14, 0x14, 0x14, 0x14, 0x14,// horiz lines
};/****************************************8*16************************************/
const unsigned char F8X16[]=  
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,// 00x00,0x00,0x00,0xF8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x33,0x30,0x00,0x00,0x00,//! 10x00,0x10,0x0C,0x06,0x10,0x0C,0x06,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//" 20x40,0xC0,0x78,0x40,0xC0,0x78,0x40,0x00,0x04,0x3F,0x04,0x04,0x3F,0x04,0x04,0x00,//# 30x00,0x70,0x88,0xFC,0x08,0x30,0x00,0x00,0x00,0x18,0x20,0xFF,0x21,0x1E,0x00,0x00,//$ 40xF0,0x08,0xF0,0x00,0xE0,0x18,0x00,0x00,0x00,0x21,0x1C,0x03,0x1E,0x21,0x1E,0x00,//% 50x00,0xF0,0x08,0x88,0x70,0x00,0x00,0x00,0x1E,0x21,0x23,0x24,0x19,0x27,0x21,0x10,//& 60x10,0x16,0x0E,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//' 70x00,0x00,0x00,0xE0,0x18,0x04,0x02,0x00,0x00,0x00,0x00,0x07,0x18,0x20,0x40,0x00,//( 80x00,0x02,0x04,0x18,0xE0,0x00,0x00,0x00,0x00,0x40,0x20,0x18,0x07,0x00,0x00,0x00,//) 90x40,0x40,0x80,0xF0,0x80,0x40,0x40,0x00,0x02,0x02,0x01,0x0F,0x01,0x02,0x02,0x00,//* 100x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x1F,0x01,0x01,0x01,0x00,//+ 110x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xB0,0x70,0x00,0x00,0x00,0x00,0x00,//, 120x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x01,//- 130x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,0x00,0x00,//. 140x00,0x00,0x00,0x00,0x80,0x60,0x18,0x04,0x00,0x60,0x18,0x06,0x01,0x00,0x00,0x00,/// 150x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x0F,0x10,0x20,0x20,0x10,0x0F,0x00,//0 160x00,0x10,0x10,0xF8,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//1 170x00,0x70,0x08,0x08,0x08,0x88,0x70,0x00,0x00,0x30,0x28,0x24,0x22,0x21,0x30,0x00,//2 180x00,0x30,0x08,0x88,0x88,0x48,0x30,0x00,0x00,0x18,0x20,0x20,0x20,0x11,0x0E,0x00,//3 190x00,0x00,0xC0,0x20,0x10,0xF8,0x00,0x00,0x00,0x07,0x04,0x24,0x24,0x3F,0x24,0x00,//4 200x00,0xF8,0x08,0x88,0x88,0x08,0x08,0x00,0x00,0x19,0x21,0x20,0x20,0x11,0x0E,0x00,//5 210x00,0xE0,0x10,0x88,0x88,0x18,0x00,0x00,0x00,0x0F,0x11,0x20,0x20,0x11,0x0E,0x00,//6 220x00,0x38,0x08,0x08,0xC8,0x38,0x08,0x00,0x00,0x00,0x00,0x3F,0x00,0x00,0x00,0x00,//7 230x00,0x70,0x88,0x08,0x08,0x88,0x70,0x00,0x00,0x1C,0x22,0x21,0x21,0x22,0x1C,0x00,//8 240x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x00,0x31,0x22,0x22,0x11,0x0F,0x00,//9 250x00,0x00,0x00,0xC0,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,//: 260x00,0x00,0x00,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x60,0x00,0x00,0x00,0x00,//; 270x00,0x00,0x80,0x40,0x20,0x10,0x08,0x00,0x00,0x01,0x02,0x04,0x08,0x10,0x20,0x00,//< 280x40,0x40,0x40,0x40,0x40,0x40,0x40,0x00,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x00,//= 290x00,0x08,0x10,0x20,0x40,0x80,0x00,0x00,0x00,0x20,0x10,0x08,0x04,0x02,0x01,0x00,//> 300x00,0x70,0x48,0x08,0x08,0x08,0xF0,0x00,0x00,0x00,0x00,0x30,0x36,0x01,0x00,0x00,//? 310xC0,0x30,0xC8,0x28,0xE8,0x10,0xE0,0x00,0x07,0x18,0x27,0x24,0x23,0x14,0x0B,0x00,//@ 320x00,0x00,0xC0,0x38,0xE0,0x00,0x00,0x00,0x20,0x3C,0x23,0x02,0x02,0x27,0x38,0x20,//A 330x08,0xF8,0x88,0x88,0x88,0x70,0x00,0x00,0x20,0x3F,0x20,0x20,0x20,0x11,0x0E,0x00,//B 340xC0,0x30,0x08,0x08,0x08,0x08,0x38,0x00,0x07,0x18,0x20,0x20,0x20,0x10,0x08,0x00,//C 350x08,0xF8,0x08,0x08,0x08,0x10,0xE0,0x00,0x20,0x3F,0x20,0x20,0x20,0x10,0x0F,0x00,//D 360x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,0x20,0x3F,0x20,0x20,0x23,0x20,0x18,0x00,//E 370x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,0x20,0x3F,0x20,0x00,0x03,0x00,0x00,0x00,//F 380xC0,0x30,0x08,0x08,0x08,0x38,0x00,0x00,0x07,0x18,0x20,0x20,0x22,0x1E,0x02,0x00,//G 390x08,0xF8,0x08,0x00,0x00,0x08,0xF8,0x08,0x20,0x3F,0x21,0x01,0x01,0x21,0x3F,0x20,//H 400x00,0x08,0x08,0xF8,0x08,0x08,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//I 410x00,0x00,0x08,0x08,0xF8,0x08,0x08,0x00,0xC0,0x80,0x80,0x80,0x7F,0x00,0x00,0x00,//J 420x08,0xF8,0x88,0xC0,0x28,0x18,0x08,0x00,0x20,0x3F,0x20,0x01,0x26,0x38,0x20,0x00,//K 430x08,0xF8,0x08,0x00,0x00,0x00,0x00,0x00,0x20,0x3F,0x20,0x20,0x20,0x20,0x30,0x00,//L 440x08,0xF8,0xF8,0x00,0xF8,0xF8,0x08,0x00,0x20,0x3F,0x00,0x3F,0x00,0x3F,0x20,0x00,//M 450x08,0xF8,0x30,0xC0,0x00,0x08,0xF8,0x08,0x20,0x3F,0x20,0x00,0x07,0x18,0x3F,0x00,//N 460xE0,0x10,0x08,0x08,0x08,0x10,0xE0,0x00,0x0F,0x10,0x20,0x20,0x20,0x10,0x0F,0x00,//O 470x08,0xF8,0x08,0x08,0x08,0x08,0xF0,0x00,0x20,0x3F,0x21,0x01,0x01,0x01,0x00,0x00,//P 480xE0,0x10,0x08,0x08,0x08,0x10,0xE0,0x00,0x0F,0x18,0x24,0x24,0x38,0x50,0x4F,0x00,//Q 490x08,0xF8,0x88,0x88,0x88,0x88,0x70,0x00,0x20,0x3F,0x20,0x00,0x03,0x0C,0x30,0x20,//R 500x00,0x70,0x88,0x08,0x08,0x08,0x38,0x00,0x00,0x38,0x20,0x21,0x21,0x22,0x1C,0x00,//S 510x18,0x08,0x08,0xF8,0x08,0x08,0x18,0x00,0x00,0x00,0x20,0x3F,0x20,0x00,0x00,0x00,//T 520x08,0xF8,0x08,0x00,0x00,0x08,0xF8,0x08,0x00,0x1F,0x20,0x20,0x20,0x20,0x1F,0x00,//U 530x08,0x78,0x88,0x00,0x00,0xC8,0x38,0x08,0x00,0x00,0x07,0x38,0x0E,0x01,0x00,0x00,//V 540xF8,0x08,0x00,0xF8,0x00,0x08,0xF8,0x00,0x03,0x3C,0x07,0x00,0x07,0x3C,0x03,0x00,//W 550x08,0x18,0x68,0x80,0x80,0x68,0x18,0x08,0x20,0x30,0x2C,0x03,0x03,0x2C,0x30,0x20,//X 560x08,0x38,0xC8,0x00,0xC8,0x38,0x08,0x00,0x00,0x00,0x20,0x3F,0x20,0x00,0x00,0x00,//Y 570x10,0x08,0x08,0x08,0xC8,0x38,0x08,0x00,0x20,0x38,0x26,0x21,0x20,0x20,0x18,0x00,//Z 580x00,0x00,0x00,0xFE,0x02,0x02,0x02,0x00,0x00,0x00,0x00,0x7F,0x40,0x40,0x40,0x00,//[ 590x00,0x0C,0x30,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x06,0x38,0xC0,0x00,//\ 600x00,0x02,0x02,0x02,0xFE,0x00,0x00,0x00,0x00,0x40,0x40,0x40,0x7F,0x00,0x00,0x00,//] 610x00,0x00,0x04,0x02,0x02,0x02,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//^ 620x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,//_ 630x00,0x02,0x02,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//` 640x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x19,0x24,0x22,0x22,0x22,0x3F,0x20,//a 650x08,0xF8,0x00,0x80,0x80,0x00,0x00,0x00,0x00,0x3F,0x11,0x20,0x20,0x11,0x0E,0x00,//b 660x00,0x00,0x00,0x80,0x80,0x80,0x00,0x00,0x00,0x0E,0x11,0x20,0x20,0x20,0x11,0x00,//c 670x00,0x00,0x00,0x80,0x80,0x88,0xF8,0x00,0x00,0x0E,0x11,0x20,0x20,0x10,0x3F,0x20,//d 680x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x1F,0x22,0x22,0x22,0x22,0x13,0x00,//e 690x00,0x80,0x80,0xF0,0x88,0x88,0x88,0x18,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//f 700x00,0x00,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x6B,0x94,0x94,0x94,0x93,0x60,0x00,//g 710x08,0xF8,0x00,0x80,0x80,0x80,0x00,0x00,0x20,0x3F,0x21,0x00,0x00,0x20,0x3F,0x20,//h 720x00,0x80,0x98,0x98,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//i 730x00,0x00,0x00,0x80,0x98,0x98,0x00,0x00,0x00,0xC0,0x80,0x80,0x80,0x7F,0x00,0x00,//j 740x08,0xF8,0x00,0x00,0x80,0x80,0x80,0x00,0x20,0x3F,0x24,0x02,0x2D,0x30,0x20,0x00,//k 750x00,0x08,0x08,0xF8,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//l 760x80,0x80,0x80,0x80,0x80,0x80,0x80,0x00,0x20,0x3F,0x20,0x00,0x3F,0x20,0x00,0x3F,//m 770x80,0x80,0x00,0x80,0x80,0x80,0x00,0x00,0x20,0x3F,0x21,0x00,0x00,0x20,0x3F,0x20,//n 780x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x1F,0x20,0x20,0x20,0x20,0x1F,0x00,//o 790x80,0x80,0x00,0x80,0x80,0x00,0x00,0x00,0x80,0xFF,0xA1,0x20,0x20,0x11,0x0E,0x00,//p 800x00,0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x0E,0x11,0x20,0x20,0xA0,0xFF,0x80,//q 810x80,0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x20,0x20,0x3F,0x21,0x20,0x00,0x01,0x00,//r 820x00,0x00,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x33,0x24,0x24,0x24,0x24,0x19,0x00,//s 830x00,0x80,0x80,0xE0,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x1F,0x20,0x20,0x00,0x00,//t 840x80,0x80,0x00,0x00,0x00,0x80,0x80,0x00,0x00,0x1F,0x20,0x20,0x20,0x10,0x3F,0x20,//u 850x80,0x80,0x80,0x00,0x00,0x80,0x80,0x80,0x00,0x01,0x0E,0x30,0x08,0x06,0x01,0x00,//v 860x80,0x80,0x00,0x80,0x00,0x80,0x80,0x80,0x0F,0x30,0x0C,0x03,0x0C,0x30,0x0F,0x00,//w 870x00,0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x00,0x20,0x31,0x2E,0x0E,0x31,0x20,0x00,//x 880x80,0x80,0x80,0x00,0x00,0x80,0x80,0x80,0x80,0x81,0x8E,0x70,0x18,0x06,0x01,0x00,//y 890x00,0x80,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x21,0x30,0x2C,0x22,0x21,0x30,0x00,//z 900x00,0x00,0x00,0x00,0x80,0x7C,0x02,0x02,0x00,0x00,0x00,0x00,0x00,0x3F,0x40,0x40,//{ 910x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,//| 920x00,0x02,0x02,0x7C,0x80,0x00,0x00,0x00,0x00,0x40,0x40,0x3F,0x00,0x00,0x00,0x00,//} 930x00,0x06,0x01,0x01,0x02,0x02,0x04,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//~ 94
};/******************************************************************************
* END OF C++ DECLARATION WRAPPER
******************************************************************************/#ifdef __cplusplus
}
#endif#endif /* __FONT_H */  

4.2 编写测试函数

功能介绍:

1)读取HS3003传感器数据,包括温度和湿度数据

2)转换温湿度数据为ASCLL

3)实时显示温湿度数据到OLED上

 1)数据转换函数

2)显示数据UI

3) 读取温湿度值,并显示数据到UI

4.3 源代码

#include "hal_data.h"
#include "bsp_pwm.h"
#include "bsp_led.h"
#include "bsp_key.h"
#include "bsp_uart.h"
#include "bsp_timer.h"
#include "lcd_drv.h"
#include "hs3003_drv.h"
#include "oled_drv.h"#define OFFSET                     20FSP_CPP_HEADER
void R_BSP_WarmStart(bsp_warm_start_event_t event);
FSP_CPP_FOOTERstatic hs300x_data stru_hs300x;void lcd_test( void )
{uint16_t color_list[6]= {BRRED, BLUE, MAGENTA, RED, YELLOW, GREEN};static int index = 0;LCD_Clear( color_list[index] );index++;R_BSP_SoftwareDelay( 1, BSP_DELAY_UNITS_SECONDS);if( index > 6 )index = 0;
}/*******************************************************************************************************************//*** 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.**********************************************************************************************************************/
static void ui_convert( int32_t inputVal, uint8_t *dataBuff )
{uint8_t temp;dataBuff[0] = (uint8_t)(inputVal/1000 + 0x30);dataBuff[1] = (uint8_t)((inputVal%1000)/100 + 0x30);dataBuff[2] = '.';temp = (uint8_t)((inputVal%1000)%100);dataBuff[3] = temp/10 + 0x30 ;dataBuff[4] = temp%10 + 0x30;
}void ui_init( void )
{oled_Clear();oled_SetCharSize( FONT_12 );oled_PrintfString(OFFSET,2,(uint8_t*)"TM(C): ");oled_SetCharSize( FONT_12 );oled_PrintfString(OFFSET,6,(uint8_t*)"HM(%): ");
}void ui_displayTempHumidy( void )
{static uint32_t cnt = 0;uint8_t dataBuff[5];int32_t _outVal;hs300x_read_value( &stru_hs300x );if( (cnt %50) == 0 ) {// tempetureif(stru_hs300x.fetch_bit.mask == HS300X_DATA_VALID){   _outVal = (int32_t)(stru_hs300x.tempval*100);memset((char*)dataBuff, ' ', sizeof(dataBuff));ui_convert(_outVal, dataBuff);oled_SetCharSize( FONT_16 );oled_PrintmutiChar(50+OFFSET,2,(uint8_t*)dataBuff, sizeof(dataBuff));}// humidityif(stru_hs300x.fetch_bit.mask == HS300X_DATA_VALID){   _outVal = (int32_t)(stru_hs300x.humival*100);memset((char*)dataBuff, ' ', sizeof(dataBuff));ui_convert(_outVal, dataBuff);oled_SetCharSize( FONT_16 );oled_PrintmutiChar(50+OFFSET,6,(uint8_t*)dataBuff, sizeof(dataBuff));}}cnt++;
}void bsp_Init( void )
{LED1_OFF;LED2_OFF;LED3_OFF;
}void hal_entry(void)
{/* TODO: add your own code here */timer1_Init();GPT_PWM_Init();bsp_Init();r_sci_uart_basic_init();disp_init();hs300x_init();oled_Init();R_BSP_SoftwareDelay(5, BSP_DELAY_UNITS_SECONDS);ui_init();while(1){if( Key_Scan(KEY1_SW2_PIN) == KEY_ON )  {LED1_TOGGLE; }if( Key_Scan(KEY2_SW3_PIN) == KEY_ON ){LED2_TOGGLE; }ui_displayTempHumidy();}
#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, g_ioport.p_cfg);}
}#if BSP_TZ_SECURE_BUILDBSP_CMSE_NONSECURE_ENTRY void template_nonsecure_callable ();/* Trustzone Secure Projects require at least one nonsecure callable function in order to build (Remove this if it is not required to build). */
BSP_CMSE_NONSECURE_ENTRY void template_nonsecure_callable ()
{}
#endif

5 测试

编译代码,并下载到板卡中,UI显示数据如下:

使用手接触到传感器,UI上的数据会有变化

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com