本项目初步设计思路是打算以瑞萨单片机作为控制和数据处理的单元,使用温湿度,光照传感器去监测周围的环境参数,在屏幕上完成传感器数据和相关信息的显示。同时,使用WIFI无线模组与单片机之间进行数据交互,接入物联网云平台。尝试利用上位机实现对于温度、湿度、光照强度等相关环境参数监测。但是由于时间原因以及个人能力有限制,只完成了部分设计。
本项目是基于启明6M5开发板完成了环境监测系统设计,传感器DHT11获取温度湿度的数据;传感器GY39获取光照强度数据;0.96寸OLED屏幕提供显示功能,能够显示传感器数据以及相关信息。编写上位机软件,实现温湿度传感器数据的上报。
RA 产品家族单片机 (MCU) 于 2019 年 10 月推出,丰富了瑞萨的 32 位 MCU 系列产品。瑞萨 RA 系列产品家族包括:RA2 系列 RA4 系列,RA6 系列 RA8 系列
本项目是基于启明6M5开发板完成设计,RA6M5芯片配置如下:采用 Arm Cortex-M33 内核的瑞萨 RA 系列 32 位 MCU; 主芯片为的R7FA6M5BH3CFC,主高达200MHz,2MB的代码闪 存,8KB数据闪存,以及512KB的带奇偶校验/ECC的SRAM。启明6M5开发板具有丰富的接口与外设,例如基本的LED,按键,电容按键,蜂鸣器等,同时也具有RS232,RS485,CAN,以太网接口,板载ESP8266无线WiFi等.
3.1 传感器模块
瑞萨RA系列的MCU,提供了图形化配置工具,和STM32CubeMX类似,这大大的方便了开发者配置。同时,瑞萨的Flexible Software Package 旨在以较低的内存占用量提供快速高效的驱动程序和协议栈。 FSP 集成了中间件协议栈、独立于 RTOS 的硬件抽象层(HAL)驱动程序, 以及最基础的板级支持包(BSP)驱动程序。
在本次的设计中,主要是对于传感器模块,显示模块进行相关的配置,下面是配置过程。
//==================================================================================================
// 实现功能: 0.96寸OLED 接口演示例程
// 说明:
// GND 电源地
// VCC 接5V或3.3v电源
// SCL 接P505(SCL6)
// SDA 接P506(SDA6)
//==================================================================================================------------------------------------------------------------------------
// | - | - | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
//==================================================================================================
//==================================================================================================
#include "oled.h"
#include "stdlib.h"
#include "oledfont.h"
#include "math.h"
#include "hal_data.h"
extern fsp_err_t err;
extern int timeout_ms;
extern i2c_master_event_t i2c_event ;
//OLED的显存
//存放格式如下.
//[0]0 1 2 3 ... 127
//[1]0 1 2 3 ... 127
//[2]0 1 2 3 ... 127
//[3]0 1 2 3 ... 127
//[4]0 1 2 3 ... 127
//[5]0 1 2 3 ... 127
//[6]0 1 2 3 ... 127
//[7]0 1 2 3 ... 127
//==================================================================================================
// 函数功能: IIC外设驱动函数部分
// 函数标记: Write_IIC_Command
// 函数说明: 无
//-------------------------------------------------------------------------------------------------
// | - | - | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
//==================================================================================================
void Write_IIC_Command(unsigned char IIC_Command)
{
uint8_t ii[2]={0x00,0x00};
ii[1] = IIC_Command;
err = R_SCI_I2C_Write(&g_sci6_i2c_ctrl, ii, 0x02, true);
assert(FSP_SUCCESS == err);
/* Since there is nothing else to do, block until Callback triggers*/
//while ((I2C_MASTER_EVENT_TX_COMPLETE != i2c_event) && timeout_ms)
while ((I2C_MASTER_EVENT_TX_COMPLETE != i2c_event) && timeout_ms>0)
{
R_BSP_SoftwareDelay(100U, BSP_DELAY_UNITS_MICROSECONDS);
timeout_ms--;
}
if (I2C_MASTER_EVENT_ABORTED == i2c_event)
{
__BKPT(0);
}
/* Read data back from the I2C slave */
i2c_event = I2C_MASTER_EVENT_ABORTED;
timeout_ms = 100000;
}
//==================================================================================================
// 函数功能: IIC外设驱动函数部分
// 函数标记: Write_IIC_Data
// 函数说明: 无
//-------------------------------------------------------------------------------------------------
// | - | - | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
//==================================================================================================
void Write_IIC_Data(unsigned char IIC_Data)
{
uint8_t ii[2]={0x40,0x00};
ii[0] = 0x40;
ii[1] = IIC_Data;
err = R_SCI_I2C_Write(&g_sci6_i2c_ctrl, ii, 0x02, true);
assert(FSP_SUCCESS == err);
/* Since there is nothing else to do, block until Callback triggers*/
//while ((I2C_MASTER_EVENT_TX_COMPLETE != i2c_event) && timeout_ms)
while ((I2C_MASTER_EVENT_TX_COMPLETE != i2c_event) && timeout_ms>0)
{
R_BSP_SoftwareDelay(100U, BSP_DELAY_UNITS_MICROSECONDS);
timeout_ms--;
}
if (I2C_MASTER_EVENT_ABORTED == i2c_event)
{
__BKPT(0);
}
/* Read data back from the I2C slave */
i2c_event = I2C_MASTER_EVENT_ABORTED;
timeout_ms = 100000;
}
//==================================================================================================
// 函数功能: IIC外设驱动函数部分
// 函数标记: Write_IIC_Data
// 函数说明: 无
//-------------------------------------------------------------------------------------------------
// | - | - | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
//==================================================================================================
void OLED_WR_Byte(unsigned dat,unsigned cmd)
{
if(cmd)
{
Write_IIC_Data(dat);
}
else
{
Write_IIC_Command(dat);
}
}
/********************************************
// fill_Picture
********************************************/
void fill_picture(unsigned char fill_Data)
{
unsigned char m,n;
for(m=0;m<8;m++)
{
OLED_WR_Byte(0xb0+m,0); //page0-page1
OLED_WR_Byte(0x00,0); //low column start address
OLED_WR_Byte(0x10,0); //high column start address
for(n=0;n<128;n++)
{
OLED_WR_Byte(fill_Data,1);
}
}
}
/***********************Delay****************************************/
void Delay_50ms(unsigned int Del_50ms)
{
unsigned int m;
for(;Del_50ms>0;Del_50ms--)
for(m=6245;m>0;m--);
}
void Delay_1ms(unsigned int Del_1ms)
{
unsigned char j;
while(Del_1ms--)
{
for(j=0;j<123;j++);
}
}
//坐标设置
void OLED_Set_Pos(unsigned char x, unsigned char y)
{ OLED_WR_Byte(0xb0+y,OLED_CMD);
OLED_WR_Byte(((x&0xf0)>>4)|0x10,OLED_CMD);
OLED_WR_Byte((x&0x0f),OLED_CMD);
}
//开启OLED显示
void OLED_Display_On(void)
{
OLED_WR_Byte(0X8D,OLED_CMD); //SET DCDC命令
OLED_WR_Byte(0X14,OLED_CMD); //DCDC ON
OLED_WR_Byte(0XAF,OLED_CMD); //DISPLAY ON
}
//关闭OLED显示
void OLED_Display_Off(void)
{
OLED_WR_Byte(0X8D,OLED_CMD); //SET DCDC命令
OLED_WR_Byte(0X10,OLED_CMD); //DCDC OFF
OLED_WR_Byte(0XAE,OLED_CMD); //DISPLAY OFF
}
//清屏函数,清完屏,整个屏幕是黑色的!和没点亮一样!!!
void OLED_Clear(void)
{
u8 i,n;
for(i=0;i<8;i++)
{
OLED_WR_Byte(0xb0+i,OLED_CMD); //设置页地址(0~7)
OLED_WR_Byte(0x00,OLED_CMD); //设置显示位置—列低地址
OLED_WR_Byte(0x10,OLED_CMD); //设置显示位置—列高地址
for(n=0;n<128;n++)OLED_WR_Byte(0,OLED_DATA);
} //更新显示
}
void OLED_On(void)
{
u8 i,n;
for(i=0;i<8;i++)
{
OLED_WR_Byte (0xb0+i,OLED_CMD); //设置页地址(0~7)
OLED_WR_Byte (0x00,OLED_CMD); //设置显示位置—列低地址
OLED_WR_Byte (0x10,OLED_CMD); //设置显示位置—列高地址
for(n=0;n<128;n++)OLED_WR_Byte(1,OLED_DATA);
} //更新显示
}
//在指定位置显示一个字符,包括部分字符
//x:0~127
//y:0~63
//mode:0,反白显示;1,正常显示
//size:选择字体 16/12
void OLED_ShowChar(u8 x,u8 y,u8 chr,u8 Char_Size)
{
unsigned char c=0,i=0;
c=chr-' ';//得到偏移后的值
if(x>Max_Column-1){x=0;y=y+2;}
if(Char_Size ==16)
{
OLED_Set_Pos(x,y);
for(i=0;i<8;i++)
OLED_WR_Byte(F8X16[c*16+i],OLED_DATA);
OLED_Set_Pos(x,y+1);
for(i=0;i<8;i++)
OLED_WR_Byte(F8X16[c*16+i+8],OLED_DATA);
}
else {
OLED_Set_Pos(x,y);
for(i=0;i<6;i++)
OLED_WR_Byte(F6x8[c][i],OLED_DATA);
}
}
//m^n函数
u32 oled_pow(u8 m,u8 n)
{
u32 result=1;
while(n--)result*=m;
return result;
}
//显示2个数字
//x,y :起点坐标
//len :数字的位数
//size:字体大小
//mode:模式 0,填充模式;1,叠加模式
//num:数值(0~4294967295);
void OLED_ShowNum(u8 x,u8 y,u32 num,u8 len,u8 size2)
{
u8 t,temp;
u8 enshow=0;
for(t=0;t<len;t++)
{
temp=(num/oled_pow(10,len-t-1))%10;
if(enshow==0&&t<(len-1))
{
if(temp==0)
{
OLED_ShowChar(x+(size2/2)*t,y,' ',size2);
continue;
}else enshow=1;
}
OLED_ShowChar(x+(size2/2)*t,y,temp+'0',size2);
}
}
//显示一个字符号串
void OLED_ShowString(u8 x,u8 y,u8 *chr,u8 Char_Size)
{
unsigned char j=0;
while (chr[j]!='\0')
{ OLED_ShowChar(x,y,chr[j],Char_Size);
x+=8;
if(x>120){x=0;y+=2;}
j++;
}
}
//显示汉字
void OLED_ShowCHinese(u8 x,u8 y,u8 no)
{
u8 t,adder=0;
OLED_Set_Pos(x,y);
for(t=0;t<16;t++)
{
OLED_WR_Byte(Hzk[2*no][t],OLED_DATA);
adder+=1;
}
OLED_Set_Pos(x,y+1);
for(t=0;t<16;t++)
{
OLED_WR_Byte(Hzk[2*no+1][t],OLED_DATA);
adder+=1;
}
}
/***********功能描述:显示显示BMP图片128×64起始点坐标(x,y),x的范围0~127,y为页的范围0~7*****************/
void OLED_DrawBMP(unsigned char x0, unsigned char y0,unsigned char x1, unsigned char y1,unsigned char BMP[])
{
unsigned int j=0;
unsigned char x,y;
if(y1%8==0) y=y1/8;
else y=y1/8+1;
for(y=y0;y<y1;y++)
{
OLED_Set_Pos(x0,y);
for(x=x0;x<x1;x++)
{
OLED_WR_Byte(BMP[j++],OLED_DATA);
}
}
}
//初始化SSD1306
void OLED_Init(void)
{
/* Wait for minimum time required between transfers. */
R_BSP_SoftwareDelay(800, BSP_DELAY_UNITS_MICROSECONDS);
OLED_WR_Byte(0xAE,OLED_CMD);//--display off
OLED_WR_Byte(0x00,OLED_CMD);//---set low column address
OLED_WR_Byte(0x10,OLED_CMD);//---set high column address
OLED_WR_Byte(0x40,OLED_CMD);//--set start line address
OLED_WR_Byte(0xB0,OLED_CMD);//--set page address
OLED_WR_Byte(0x81,OLED_CMD); // contract control
OLED_WR_Byte(0xFF,OLED_CMD);//--128
OLED_WR_Byte(0xA1,OLED_CMD);//set segment remap
OLED_WR_Byte(0xA6,OLED_CMD);//--normal / reverse
OLED_WR_Byte(0xA8,OLED_CMD);//--set multiplex ratio(1 to 64)
OLED_WR_Byte(0x3F,OLED_CMD);//--1/32 duty
OLED_WR_Byte(0xC8,OLED_CMD);//Com scan direction
OLED_WR_Byte(0xD3,OLED_CMD);//-set display offset
OLED_WR_Byte(0x00,OLED_CMD);//
OLED_WR_Byte(0xD5,OLED_CMD);//set osc division
OLED_WR_Byte(0x80,OLED_CMD);//
OLED_WR_Byte(0xD8,OLED_CMD);//set area color mode off
OLED_WR_Byte(0x05,OLED_CMD);//
OLED_WR_Byte(0xD9,OLED_CMD);//Set Pre-Charge Period
OLED_WR_Byte(0xF1,OLED_CMD);//
OLED_WR_Byte(0xDA,OLED_CMD);//set com pin configuartion
OLED_WR_Byte(0x12,OLED_CMD);//
OLED_WR_Byte(0xDB,OLED_CMD);//set Vcomh
OLED_WR_Byte(0x30,OLED_CMD);//
OLED_WR_Byte(0x8D,OLED_CMD);//set charge pump enable
OLED_WR_Byte(0x14,OLED_CMD);//
OLED_WR_Byte(0xAF,OLED_CMD);//--turn on oled panel
}
/*
* oled.h
*
* Created on: 2023年1月31日
* Author: a8456
*/
#ifndef OLED_H_
#define OLED_H_
#include "stdlib.h"
#include "stdint.h"
#define OLED_MODE 0
#define XLevelL 0x00
#define XLevelH 0x10
#define Max_Column 128
#define Max_Row 64
#define Brightness 0xFF
#define X_WIDTH 128
#define Y_WIDTH 64
#define OLED_CMD 0 //写命令
#define OLED_DATA 1 //写数据
typedef __uint8_t u8 ;
typedef __uint32_t u32 ;
//OLED控制用函数
void OLED_WR_Byte(unsigned dat,unsigned cmd);
void OLED_Display_On(void);
void OLED_Display_Off(void);
void OLED_Init(void);
void OLED_Clear(void);
void OLED_DrawPoint(u8 x,u8 y,u8 t);
void OLED_Fill(u8 x1,u8 y1,u8 x2,u8 y2,u8 dot);
void OLED_ShowChar(u8 x,u8 y,u8 chr,u8 Char_Size);
void OLED_ShowNum(u8 x,u8 y,u32 num,u8 len,u8 size);
void OLED_ShowString(u8 x,u8 y, u8 *p,u8 Char_Size);
void OLED_Set_Pos(unsigned char x, unsigned char y);
void OLED_ShowCHinese(u8 x,u8 y,u8 no);
void OLED_DrawBMP(unsigned char x0, unsigned char y0,unsigned char x1, unsigned char y1,unsigned char BMP[]);
void Delay_50ms(unsigned int Del_50ms);
void Delay_1ms(unsigned int Del_1ms);
void fill_picture(unsigned char fill_Data);
void Picture();
void IIC_Start();
void IIC_Stop();
void Write_IIC_Command(unsigned char IIC_Command);
void Write_IIC_Data(unsigned char IIC_Data);
void Write_IIC_Byte(unsigned char IIC_Byte);
void IIC_Wait_Ack();
#endif /* OLED_H_ */
#ifndef __BSP_DHT11_H
#define __BSP_DHT11_H
#include "hal_data.h"
#define Bit_RESET 0
#define Bit_SET 1
#define DHT11_LOW 0
#define DHT11_HIGH 1
#define DHT11_PORT BSP_IO_PORT_00_PIN_01
#define DHT_HIGH R_BSP_PinWrite(DHT11_PORT, BSP_IO_LEVEL_HIGH);
#define DHT_LOW R_BSP_PinWrite(DHT11_PORT, BSP_IO_LEVEL_LOW);
#define Read_Data R_BSP_PinRead(DHT11_PORT)
#define DHT11_DATA_OUT(a) if (a) \ DHT_HIGH\ else \ DHT_LOW
typedef struct
{
uint8_t humi_int; //湿度的整数部分
uint8_t humi_deci; //湿度的小数部分
uint8_t temp_int; //温度的整数部分
uint8_t temp_deci; //温度的小数部分
uint8_t check_sum; //校验和
}DHT11_Data_TypeDef;
void DHT11_Init (void);
void DHT11_Start (void);
void DHT11_DELAY_US (uint32_t delay);
void DHT11_DELAY_MS (uint32_t delay);
uint8_t Read_DHT11(DHT11_Data_TypeDef *DHT11_Data);
#endif
#include "bsp_dht11.h" // Device header
/* DHT11初始化函数 */
void DHT11_Init(void)
{
/* 初始化配置引脚(这里重复初始化了,可以注释掉) */
R_IOPORT_Open (&g_ioport_ctrl, g_ioport.p_cfg);
}
void DHT11_DELAY_US(uint32_t delay)
{
R_BSP_SoftwareDelay(delay, BSP_DELAY_UNITS_MICROSECONDS);
}
void DHT11_DELAY_MS(uint32_t delay)
{
R_BSP_SoftwareDelay(delay, BSP_DELAY_UNITS_MILLISECONDS);
}
//主机发送开始信号
void DHT11_Start(void)
{
DHT_HIGH; //先拉高
DHT11_DELAY_US(30);
DHT_LOW; //拉低电平至少18us
DHT11_DELAY_MS(20);
DHT_HIGH; //拉高电平20~40us
DHT11_DELAY_US(30);
}
/*
* 从DHT11读取一个字节,MSB先行
*/
static uint8_t Read_Byte(void)
{
uint8_t i, temp=0;
for(i=0;i<8;i++)
{
/*每bit以50us低电平标置开始,轮询直到从机发出 的50us 低电平 结束*/
while(Read_Data == Bit_RESET);
/*DHT11 以26~28us的高电平表示“0”,以70us高电平表示“1”,
*通过检测 x us后的电平即可区别这两个状 ,x 即下面的延时
*/
DHT11_DELAY_US(40); //延时x us 这个延时需要大于数据0持续的时间即可
if(Read_Data == Bit_SET)/* x us后仍为高电平表示数据“1” */
{
/* 等待数据1的高电平结束 */
while( Read_Data ==Bit_SET);
temp|=(uint8_t)(0x01<<(7-i)); //把第7-i位置1,MSB先行
}
else // x us后为低电平表示数据“0”
{
temp&=(uint8_t)~(0x01<<(7-i)); //把第7-i位置0,MSB先行
}
}
return temp;
}
/*
* 一次完整的数据传输为40bit,高位先出
* 8bit 湿度整数 + 8bit 湿度小数 + 8bit 温度整数 + 8bit 温度小数 + 8bit 校验和
*/
uint8_t Read_DHT11(DHT11_Data_TypeDef *DHT11_Data)
{
uint16_t count;
DHT11_Start();
DHT_HIGH; //拉高电平
/*判断从机是否有低电平响应信号 如不响应则跳出,响应则向下运行*/
if( Read_Data == Bit_RESET)
{
count=0;
/*轮询直到从机发出 的80us 低电平 响应信号结束*/
while( Read_Data ==Bit_RESET)
{
count++;
if(count>1000)
return 0;
DHT11_DELAY_US(10);
}
count=0;
/*轮询直到从机发出的 80us 高电平 标置信号结束*/
while( Read_Data==Bit_SET)
{
count++;
if(count>1000)
return 0;
DHT11_DELAY_US(10);
}
/*开始接收数据*/
DHT11_Data->humi_int= Read_Byte();
DHT11_Data->humi_deci= Read_Byte();
DHT11_Data->temp_int= Read_Byte();
DHT11_Data->temp_deci= Read_Byte();
DHT11_Data->check_sum= Read_Byte();
DHT_LOW;
DHT11_DELAY_US(55);
DHT_HIGH;
/*检查读取的数据是否正确*/
if(DHT11_Data->check_sum == DHT11_Data->humi_int + DHT11_Data->humi_deci + DHT11_Data->temp_int+ DHT11_Data->temp_deci)
return 1;
else
return 0;
}
else
{
return 0;
}
}
#include "hal_data.h"
#include "oled.h"
#include "bmp.h"
#include "bsp_debug_uart.h"
#include "bsp_led.h"
#include "bsp_dht11.h"
#include "bsp_gpt_timing.h"
#define SUCCESS 1
void Hardware_init(void);
FSP_CPP_HEADER
void R_BSP_WarmStart(bsp_warm_start_event_t event);
FSP_CPP_FOOTER
/* Callback function */
i2c_master_event_t i2c_event = I2C_MASTER_EVENT_ABORTED;
void sci6_i2c_master_callback(i2c_master_callback_args_t *p_args)
{
i2c_event = I2C_MASTER_EVENT_ABORTED;
if (NULL != p_args)
{
/* capture callback event for validating the i2c transfer event*/
i2c_event = p_args->event;
}
}
void sci2_i2c_master_callback(i2c_master_callback_args_t *p_args)
{
i2c_event = I2C_MASTER_EVENT_ABORTED;
if (NULL != p_args)
{
/* capture callback event for validating the i2c transfer event*/
i2c_event = p_args->event;
}
}
fsp_err_t err = FSP_SUCCESS;
uint32_t timeout_ms = 1000;
DHT11_Data_TypeDef DHT11_Data;
uint8_t Temperature,Humidity;
extern uint8_t temp_humi_flag;
extern uint32_t time1s_flag;
typedef struct
{
uint32_t P;
uint16_t Temp;
uint16_t Hum;
uint16_t Alt;
} bme;
bme Bme;
uint32_t Lux;
float LightLux;
//==================================================================================================
// 函数说明: 硬件初始化
// 函数备注: Hardware_init
//--------------------------------------------------------------------------------------------------
// | - | - | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
//==================================================================================================
void Hardware_init(void)
{
Debug_UART4_Init(); // SCI4 UART 调试串口初始化
GPT0_Timing_Init();
printf("Debug-UART4-Init OK \r\n");
LED_Init();
printf("LED_Init OK \r\n");
printf("IIC-Config Start \r\n");
DHT11_Init();
printf("DHT11_Init OK \r\n");
/* Initialize the I2C module */
err = R_SCI_I2C_Open(&g_sci6_i2c_ctrl, &g_sci6_i2c_cfg);
/* Handle any errors. This function should be defined by the user. */
assert(FSP_SUCCESS == err);
printf("IIC-Config OK \r\n");
OLED_Init(); //初始化OLED
OLED_Clear();
printf("oled-Init OK \r\n");
}
void read_bme(void)
{
uint16_t data_16[2] ={ 0 };
uint8_t data[10] = { 0x00 }; //接收读取后的数据
uint8_t write_buffer = 0x04; //写数据
err = R_SCI_I2C_Open (&g_sci2_i2c_ctrl, &g_sci2_i2c_cfg);
err = R_SCI_I2C_Write (&g_sci2_i2c_ctrl, &write_buffer, 1, true);
err = R_SCI_I2C_Abort (&g_sci2_i2c_ctrl);
R_BSP_SoftwareDelay (3, BSP_DELAY_UNITS_MILLISECONDS);
err = R_SCI_I2C_Read (&g_sci2_i2c_ctrl, data, 10, false);
R_BSP_SoftwareDelay (3, BSP_DELAY_UNITS_MILLISECONDS);
err = R_SCI_I2C_Abort (&g_sci2_i2c_ctrl);
err = R_SCI_I2C_Close (&g_sci2_i2c_ctrl);
Bme.Temp = (data[0] << 8) | data[1];
data_16[0] = (data[2] << 8) | data[3];
data_16[1] = (data[4] << 8) | data[5];
Bme.P = (((uint32_t) data_16[0]) << 16) | data_16[1];
Bme.Hum = (data[6] << 8) | data[7];
Bme.Alt = (data[8] << 8) | data[9];
}
void read_lux(void)
{
uint16_t data_16[2] ={ 0 };
uint8_t data[4] = { 0 };
uint8_t write_buffer = 0x00; //写数据
err = R_SCI_I2C_Open (&g_sci2_i2c_ctrl, &g_sci2_i2c_cfg);
err = R_SCI_I2C_Write (&g_sci2_i2c_ctrl, &write_buffer, 1, true);
err = R_SCI_I2C_Abort (&g_sci2_i2c_ctrl);
R_BSP_SoftwareDelay (3, BSP_DELAY_UNITS_MILLISECONDS);
err = R_SCI_I2C_Read (&g_sci2_i2c_ctrl, data, 4, false);
R_BSP_SoftwareDelay (3, BSP_DELAY_UNITS_MILLISECONDS);
err = R_SCI_I2C_Abort (&g_sci2_i2c_ctrl);
err = R_SCI_I2C_Close (&g_sci2_i2c_ctrl);
data_16[0] = (data[0] << 8) | data[1];
data_16[1] = (data[2] << 8) | data[3];
Lux = (((uint32_t) data_16[0]) << 16) | data_16[1];
}
//==================================================================================================
// 函数说明: 主函数入口
// 函数备注: hal_entry
//--------------------------------------------------------------------------------------------------
// | - | - | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
//==================================================================================================
/*******************************************************************************************************************//**
* 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.
**********************************************************************************************************************/
void hal_entry(void)
{
/* TODO: add your own code here */
Hardware_init();
printf("RA6M5-Board-Init OK \r\n");
OLED_ShowCHinese(0,0,0);//瑞
OLED_ShowCHinese(16,0,1);//萨
OLED_ShowCHinese(32,0,5);//电
OLED_ShowCHinese(48,0,6);//子
OLED_ShowString(60,0,"Renesas",16);
OLED_ShowNum(0,2,2023,4,16);//显示ASCII字符的码值
OLED_ShowCHinese(32,2,2);//中文字->年
OLED_ShowNum(48,2,8,2,16);//显示ASCII字符的码值
OLED_ShowCHinese(64,2,3);//中文字->月
OLED_ShowNum(80,2,5,2,16);//显示ASCII字符的码值
OLED_ShowCHinese(96,2,4);//中文字->日
OLED_ShowCHinese(0,4,7); //中文字->温
OLED_ShowCHinese(16,4,9); //中文字->度
OLED_ShowCHinese(64,4,8); //中文字->湿
OLED_ShowCHinese(80,4,9); //中文字->度
OLED_ShowString(0,6,"Light:",16);
OLED_ShowString(96,6,"Lux",16);
R_BSP_PinAccessEnable();
R_BSP_PinWrite(BSP_IO_PORT_00_PIN_01, BSP_IO_LEVEL_HIGH); //DHT11端口配置
while(1)
{
if (time1s_flag == 1)
{
//printf("time1s_flag ......\r\n");
printf("T%dPH%dI", Temperature, Humidity);
printf("\r\n");
}
OLED_ShowNum(32,4,Temperature,2,16);
OLED_ShowNum(96,4,Humidity,2,16);
read_bme();
read_lux (); //光照传感器
LightLux = Lux / 100;
//printf("LightLux: %.2f lux \r\n ",(float)LightLux );
OLED_ShowNum(48,6,LightLux,5,16);
}
#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_BUILD
BSP_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