该模块采用24位高精度的A/D转换器芯片hx711,是一款专为高精度电子秤而设计的,具有两路模拟通道输入,内部集成128倍增益可编程放大器。输入电路可配置为提供桥压的电桥式(如压力、称重)传感器模式,是一款理想的高精度、低成本采样前端模块。
一、模块来源
二、规格参数
工作电压:2.6V-5.5V
工作电流:100~1500uA
ADC精度:24位
输出方式: 串行输出
管脚数量:4 Pin
以上信息见厂家资料文件
三、移植过程
我们的目标是将例程移植至开发板上【能够判断测量10Kg以内的称重】。首先要获取资料,查看数据手册应如何实现读取数据,再移植至我们的工程。
1、查看资料
2、引脚选择
VCC | 3V3 |
GND | GND |
SCK | P408 |
DT | P409 |
3、图形化配置
- 打开图形化界面:
- 设置
Pin
:
Ctrl + S
保存!- 点击右上角
Generate Project Content
生成代码。
4、代码编写
新建两个文件 bsp_hx711.c
和 bsp_hx711.h
,并且将头文件路径添加到编译器中。
在文件 bsp_hx711.c
和 bsp_hx711.h
中,编写如下代码。
c
/*
* 立创开发板软硬件资料与相关扩展板软硬件资料官网全部开源
* 开发板官网:www.lckfb.com
* 文档网站:wiki.lckfb.com
* 技术支持常驻论坛,任何技术问题欢迎随时交流学习
* 嘉立创社区问答:https://www.jlc-bbs.com/lckfb
* 关注bilibili账号:【立创开发板】,掌握我们的最新动态!
* 不靠卖板赚钱,以培养中国工程师为己任
*/
#ifndef BSP_CODE_BSP_HX711_H_
#define BSP_CODE_BSP_HX711_H_
#include "hal_data.h"
#include <stdio.h>
#ifndef u8
#define u8 uint8_t
#endif
#ifndef u16
#define u16 uint16_t
#endif
#ifndef u32
#define u32 uint32_t
#endif
#ifndef delay_ms
#define delay_ms(x) R_BSP_SoftwareDelay(x, BSP_DELAY_UNITS_MILLISECONDS)
#endif
#ifndef delay_1ms
#define delay_1ms(x) R_BSP_SoftwareDelay(x, BSP_DELAY_UNITS_MILLISECONDS)
#endif
#ifndef delay_us
#define delay_us(x) R_BSP_SoftwareDelay(x, BSP_DELAY_UNITS_MICROSECONDS)
#endif
#ifndef delay_1us
#define delay_1us(x) R_BSP_SoftwareDelay(x, BSP_DELAY_UNITS_MICROSECONDS)
#endif
#define Module_SCL_PIN BSP_IO_PORT_04_PIN_08 // SCL
#define Module_SDA_PIN BSP_IO_PORT_04_PIN_09 // SDA
//SDA输入模式
#define SDA_IN() { \
fsp_err_t err = R_IOPORT_PinCfg(&g_ioport_ctrl, Module_SDA_PIN, \
(uint32_t) IOPORT_CFG_PORT_DIRECTION_INPUT \
| (uint32_t) IOPORT_CFG_PULLUP_ENABLE); \
if(err != FSP_SUCCESS) { \
printf("R_IOPORT_PinCfg Failed!!\r\n"); \
} \
}
//SDA输出模式
#define SDA_OUT() { \
fsp_err_t err = R_IOPORT_PinCfg(&g_ioport_ctrl, Module_SDA_PIN, \
((uint32_t) IOPORT_CFG_DRIVE_HIGH \
| (uint32_t) IOPORT_CFG_NMOS_ENABLE \
| (uint32_t) IOPORT_CFG_PORT_DIRECTION_OUTPUT \
| (uint32_t) IOPORT_CFG_PORT_OUTPUT_HIGH)); \
if(err != FSP_SUCCESS) { \
printf("R_IOPORT_PinCfg Failed!!\r\n"); \
} \
}
// SCL引脚和SDA引脚的输出
#define SCL(BIT) R_IOPORT_PinWrite(&g_ioport_ctrl, Module_SCL_PIN, BIT)
#define SDA(BIT) R_IOPORT_PinWrite(&g_ioport_ctrl, Module_SDA_PIN, BIT)
// 获取SDA引脚的电平状态
static inline bsp_io_level_t GET_SDA(void) {
bsp_io_level_t p_pin_value;
fsp_err_t err = R_IOPORT_PinRead(&g_ioport_ctrl, Module_SDA_PIN, &p_pin_value);
if(err != FSP_SUCCESS) {
printf("GPIO Input Read Failed!!\r\n");
}
return p_pin_value;
}
void HX711_Init(void);
void HX711_Get_InitValue(void);
float HX711_Get_Weight(void);
#endif /* BSP_CODE_BSP_HX711_H_ */
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
c
/*
* 立创开发板软硬件资料与相关扩展板软硬件资料官网全部开源
* 开发板官网:www.lckfb.com
* 文档网站:wiki.lckfb.com
* 技术支持常驻论坛,任何技术问题欢迎随时交流学习
* 嘉立创社区问答:https://www.jlc-bbs.com/lckfb
* 关注bilibili账号:【立创开发板】,掌握我们的最新动态!
* 不靠卖板赚钱,以培养中国工程师为己任
*/
#include "bsp_hx711.h"
#include "stdio.h"
uint32_t HX711_Weight_Init;
//校准参数
//因为不同的传感器特性曲线不是很一致,因此,每一个传感器需要矫正这里这个参数才能使测量值很准确。
//当发现测试出来的重量偏大时,增加该数值。
//如果测试出来的重量偏小时,减小改数值。
//该值可以为小数
#define GAP_VALUE 207.00
/******************************************************************
* 函 数 名 称:HX711_Init
* 函 数 说 明:HX711初始化
* 函 数 形 参:无
* 函 数 返 回:无
* 作 者:LCKFB
* 备 注:无
******************************************************************/
void HX711_Init(void)
{
SDA_OUT(); // 设置SDA引脚为输出模式
SDA(1);
SCL(1);
delay_ms(100); // 等待传感器稳定
}
/******************************************************************
* 函 数 名 称:HX711_Read
* 函 数 说 明:读取HX711
* 函 数 形 参:无
* 函 数 返 回:读取到的值
* 作 者:LCKFB
* 备 注:无
******************************************************************/
static uint32_t HX711_Read(void) //增益128
{
unsigned long count;
unsigned char i;
SDA_OUT();
delay_us(5);
SDA(1);
delay_us(1);
SCL(0);
count=0;
SDA_IN();
delay_us(5);
while(GET_SDA());
for(i=0;i<24;i++)
{
SCL(1);
count=count<<1;
delay_us(1);
SCL(0);
if(GET_SDA())
count++;
delay_us(1);
}
SCL(1);
count=count^0x800000;//第25个脉冲下降沿来时,转换数据
delay_us(1);
SCL(0);
return(count);
}
/******************************************************************
* 函 数 名 称:HX711_Get_InitValue
* 函 数 说 明:获取初始值
* 函 数 形 参:无
* 函 数 返 回:无
* 作 者:LCKFB
* 备 注:后续的重量都是以该初始重量为0值,因此在初始化时,秤上不要放任何东西
******************************************************************/
void HX711_Get_InitValue(void)
{
HX711_Weight_Init = HX711_Read();
}
/******************************************************************
* 函 数 名 称:HX711_Get_Weight
* 函 数 说 明:称重
* 函 数 形 参:无
* 函 数 返 回:称重值,单位g
* 作 者:LCKFB
* 备 注:无
******************************************************************/
float HX711_Get_Weight(void)
{
uint32_t HX711_Read_Buffer = 0;
float Return_Buffer = 0;
HX711_Read_Buffer = HX711_Read();
// printf("""HX711 Read Buffer = %d\r\n", HX711_Read_Buffer);
if(HX711_Read_Buffer > HX711_Weight_Init)
{
HX711_Read_Buffer -= HX711_Weight_Init; //获取实物的采样数值。
Return_Buffer = (float)HX711_Read_Buffer / (float)GAP_VALUE;//计算实物的实际重量
//因为不同的传感器特性曲线不一样,因此,每一个传感器需要矫正这里的 GAP_VALUE 这个除数。
//当发现测试出来的重量偏大时,增加该数值。
//如果测试出来的重量偏小时,减小改数值。
}
return Return_Buffer;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
四、移植验证
在 src\Applay\app.c
中输入代码如下:
c
/*
* 立创开发板软硬件资料与相关扩展板软硬件资料官网全部开源
* 开发板官网:www.lckfb.com
* 文档网站:wiki.lckfb.com
* 技术支持常驻论坛,任何技术问题欢迎随时交流学习
* 嘉立创社区问答:https://www.jlc-bbs.com/lckfb
* 关注bilibili账号:【立创开发板】,掌握我们的最新动态!
* 不靠卖板赚钱,以培养中国工程师为己任
*/
#include "app.h"
#include "stdio.h"
#include "bsp_uart.h"
#include"bsp_hx711.h"
/******************************************************************
* 函 数 名 称:led_blink
* 函 数 说 明:该函数用于控制LED灯的闪烁效果
* 运行时,LED灯每隔500毫秒闪烁一次
* 完整运行此函数需要1s时间
* 函 数 形 参:无
* 函 数 返 回:无
* 作 者:LC
* 备 注:无
******************************************************************/
static void led_blink(void)
{
/* Set the pin to low */
R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_04_PIN_02, BSP_IO_LEVEL_LOW);
/* Delay for 500 milliseconds */
R_BSP_SoftwareDelay(500, BSP_DELAY_UNITS_MILLISECONDS);
/* Set the pin to high */
R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_04_PIN_02, BSP_IO_LEVEL_HIGH);
/* Delay for another 500 milliseconds */
R_BSP_SoftwareDelay(500, BSP_DELAY_UNITS_MILLISECONDS);
}
/******************************************************************
* 函 数 名 称:Run
* 函 数 说 明:该函数是用户自定义的入口函数,等效于 main_app() 函数。
* 在此函数中可以编写用户的应用逻辑代码。
* 函 数 形 参:无
* 函 数 返 回:无
* 作 者:LC
* 备 注:无
******************************************************************/
void Run(void)
{
/* 初始化调试串口 */
/* | RX:P100 | TX:P101 | */
UART0_Debug_Init();
printf("\r\n= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\r\n");
printf("\r\n=== Welcome to use the DQX-R7FA6E2BB3CNE development board ====\r\n");
printf("\r\n======================= www.lckfb.com =========================\r\n");
printf("\r\n======================= wiki.lckfb.com ========================\r\n");
printf("\r\n======================= [Debug Uart0] =========================\r\n");
printf("\r\n=================== | RX:P100 | TX:P101 | =====================\r\n");
printf("\r\n= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\r\n");
/* 初始化 */
HX711_Init();
/* 获取初始值 */
HX711_Get_InitValue();
delay_1ms(300);
while(1)
{
printf("\r\nHX711 Get Weight = [%.2fg]\r\n", HX711_Get_Weight());
delay_1ms(300);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
编译烧录。
【代码下载】
- 跳转去下载模块移植代码:【点击跳转🚀】