瑞萨单片机论坛
直播中

TLLED

10年用户 206经验值
擅长:嵌入式技术
私信 关注

【RA4M2设计挑战赛】读取HS3003传感器温湿度值

使用i2c读取HS3003温湿度传感器的值,并串口打印结果。

一、串口的配置

1.1、选择SWD下载模式

创建的开发板项目工程,默认是JTAG编程模式,占用CH340的调试。

0519.png

1.2、配置调试串口

调试串口的硬件部分电路

0758.png

配置串口端口

0916.png

设置波特率、回调函数和端口

3330.png

二、i2c配置

配置i2c3

3525.png

三、程序

3.1、串口部分程序

#include "hal_data.h"
#include <stdio.h>


volatile bool uart_send_complete_flag = false;

void init_uart(void)
{
	fsp_err_t err = FSP_SUCCESS;
    
	err = R_SCI_UART_Open (&uart9_ctrl, &uart9_cfg);
	assert(FSP_SUCCESS == err);
}


void uart9_callback(uart_callback_args_t *p_args)
{
	switch(p_args->event)
	{
		case UART_EVENT_RX_CHAR:
			R_SCI_UART_Write(&uart9_ctrl, (uint8_t *)&(p_args->data),1);
			break;
		case UART_EVENT_TX_COMPLETE:
			uart_send_complete_flag=true;
			break;
		default:
			break;
			
	}
}

int fputc(int ch, FILE *f)
{
    (void)f;
    R_SCI_UART_Write(&uart9_ctrl, (uint8_t *)&ch, 1);
    while(uart_send_complete_flag == false);
    uart_send_complete_flag = false;

    return ch;
}

3.2、主程序

#include "hal_data.h"
#include "uart.h"
#include "stdio.h"


void R_BSP_WarmStart(bsp_warm_start_event_t event);

float RH=0.0,TEMP=0.0;
uint8_t buf[4]={0};

void i2c3_callback(i2c_master_callback_args_t * p_args)
{
}
void hal_entry (void)
{
	init_uart();
	R_SCI_I2C_Open(&i2c3_ctrl, &i2c3_cfg);

	while (1)
	{
		buf[0] = buf[1] = buf[2] = buf[3] = 0;
		R_SCI_I2C_Write(&i2c3_ctrl, buf, 4,false);
		R_BSP_SoftwareDelay(10,BSP_DELAY_UNITS_MICROSECONDS);
		R_SCI_I2C_Read(&i2c3_ctrl, buf, 4,false);
		R_BSP_SoftwareDelay(100, BSP_DELAY_UNITS_MILLISECONDS);
		RH =(float)(((short)(buf[0] & 0x3f)<< 8)+ buf[1])/ 16383 * 100; //湿度变换为浮点
		TEMP =(float)((((short)buf[2]<<8)+buf[3])>> 2)/16383 *165 - 40;//温度变换浮点
		printf("RH: %f , temp: %f\r\n",RH,TEMP);
		R_BSP_SoftwareDelay(200, BSP_DELAY_UNITS_MILLISECONDS);
			
	}
}

四、运行

串口输出

4327.png

更多回帖

发帖
×
20
完善资料,
赚取积分