目錄
概述
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 MCU | R7FA4M2AD3C |
Keil | MDK ARM 5.38 |
FSP 版本 | 5.3.0 |
調試工具:st-link | ST-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_SUCCESS Requested clock rate was set exactly. FSP_ERR_ALREADY_OPEN Module is already open. FSP_ERR_IP_CHANNEL_NOT_PRESENT Channel is not available on this MCU. FSP_ERR_ASSERTION Parameter check failure due to one or more reasons below:
- p_api_ctrl or p_cfg is NULL.
- extended parameter is NULL.
- Callback parameter is NULL.
- Clock rate requested is greater than 400KHz
- 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_SUCCESS Function executed without issue. FSP_ERR_ASSERTION The parameter p_ctrl, p_dest is NULL, bytes is 0. FSP_ERR_INVALID_SIZE Provided number of bytes more than uint16_t size (65535) while DTC is used for data transfer. FSP_ERR_NOT_OPEN Device 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_SUCCESS Function executed without issue. FSP_ERR_ASSERTION p_ctrl, p_src is NULL. FSP_ERR_INVALID_SIZE Provided number of bytes more than uint16_t size (65535) while DTC is used for data transfer. FSP_ERR_NOT_OPEN Device 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上的數據會有變化