瑞萨单片机william hill官网
直播中

大饭米粒

3年用户 7经验值
擅长:嵌入式技术 控制/MCU RF/无线
私信 关注
[经验]

【RA-Eco-RA0E1-32PIN-V1.0开发板试用】配合HX711完成电子秤

回顾

上一章节提到了如何配置开发环境和尝试编写一个点灯程序,可以通过以下传送门查看上一章节内容:
【RA-Eco-RA0E1-32PIN-V1.0开发板试用】配置开发环境和编写一个点灯程序

开始制作电子秤

配置串口

在开始之前,先配置一下串口用于调试输出内容,查看开发板原理图可得知板载的USB转TTL串口连接的是芯片的UART2,也就是P1.09和P1.10
image.png
回到IDE中,打开透视图,这里推荐把板载的时钟启用了并切换到外置时钟
image.png

在X1那里填写时钟频率,查看板载的开发板可以得知搭载的是8Mhz的晶振。
接下来配置UART引脚,切换到Pin选项卡,配置UART
image.png

随后切换到stacks选项卡,点击右上角的New Stack添加UART进来
image.png
然后设置一下uart的属性,由于用到的是UART2,这里的通道改成2,默认是0。其余选项不动,默认的波特率为115200
image.png

然后再配置一下HX711的引脚,HX711需要用到两个引脚,DOUT和SCK,其中DOUT需要配置为输入模式,SCK配置为输出模式
image.png

一切准备就绪后,点击右上角的image.png
生成项目文件

编写嵌入式代码

首先添加UART部分驱动代码,目前的项目SRC文件夹下的结构为
image.png

bsp_uart.h代码

/*
 * bsp_uart.h
 *
 *  Created on: 2024年8月26日
 *      Author: ityun
 */

#ifndef DEBUG_UART_BSP_UART_H_
#define DEBUG_UART_BSP_UART_H_
#include "hal_data.h"
#include <stdio.h>

void Debug_UART2_Init(void);
void debug_printf(char *str, ...);

#endif /* DEBUG_UART_BSP_UART_H_ */

bsp_uart.c 代码

#include"debug_uart/bsp_uart.h"

#include"stdio.h"

#include"stdlib.h"

#include"stdarg.h"



voidDebug_UART2_Init(void)

{

fsp_err_t err = FSP_SUCCESS;



    err = R_SAU_UART_Open (&g_uart0_ctrl, &g_uart0_cfg);

    assert(FSP_SUCCESS == err);

}







voiddebug_printf(char *str, ...)

{

uint8_t s[600] =

    { 0 };

fsp_err_t err = FSP_SUCCESS;

va_list args;

int len;



if ((str == NULL) || (strlen (str) == 0))

    {

return;

    }



    va_start(args, str);

    len = vsnprintf ((char*) s, 600, str, args);

    va_end(args);

    err = R_SAU_UART_Write (&g_uart0_ctrl, s, (uint32_t) len);

if (err != FSP_SUCCESS)

    {

        __BKPT(0);

    }

}

这里主要通过R_SAU_UART_Open来初始化串口,然后编写了一个用于打印输出的debug_printf方法,内部通过R_SAU_UART_Write来把数据发送到串口

接下来看HX711的驱动代码,主要核心部分为
image.png

这里需要注意的是瑞萨读取IO的电平需要先赋值给变量然后才能判定IO口电平状态

一切准备就绪后,开始编写入口文件

#include <stdio.h>
#include "string.h"
#include "adc_driver/hx711.h"
#include <debug_uart/bsp_uart.h>
#include "hal_data.h"

FSP_CPP_HEADER
void R_BSP_WarmStart(bsp_warm_start_event_t event);
FSP_CPP_FOOTER

extern long Weight_Shiwu;

/*******************************************************************************************************************//**
 * 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 */
    Debug_UART2_Init ();
    Get_Maopi (); // 电子秤去皮
    R_BSP_SoftwareDelay(1, BSP_DELAY_UNITS_SECONDS);
    Get_Maopi (); // 再次去皮

    while (1)
    {
        Get_Weight ();
        char buff_str[50];
        sprintf(buff_str, "weigh: %ld g\r\n", Weight_Shiwu);
        R_SAU_UART_Write (&g_uart0_ctrl, (uint8_t *)buff_str, (uint32_t) strlen(buff_str));
        R_BSP_SoftwareDelay(1, BSP_DELAY_UNITS_SECONDS);

    }
#if BSP_TZ_SECURE_BUILD
    /* Enter non-secure code */
    R_BSP_NonSecureEnter();
#endif
}

将固件编译之后烧录到芯片中验证结果,请看下方结果视频,目前验证可以正确的进行称重

WeChat_20240826172526

更多回帖

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