一、模块来源
二、规格参数
驱动电压:3V~7.2V
工作扭矩:1.6KG/CM
控制方式:PWM
转动角度:180度
以上信息见厂家资料文件
三、移植过程
我们的目标是将例程移植至开发板上【能够控制舵机旋转的功能】。首先要获取资料,查看数据手册应如何实现读取数据,再移植至我们的工程。
1、查看资料
在购买时,需要分清楚你的舵机可以转180度,还是360度。360度的舵机是无法控制角度的,只可以控制旋转速度。
SG90的舵机转速不是很快,一般为0.22/60 度或0.18/60 度,所以假如你更改角度控制脉冲的宽度太快时,舵机可能反应不过来。如果需要更快速的反应,就需要更高的转速了。
2、引脚选择
棕色线 | GND |
黄色线 | P109(GPT-A) |
红色线 | 5V0 |
3、图形化配置
- 打开图形化界面:
- 设置
ADC
:
Ctrl + S
保存!- 点击右上角
Generate Project Content
生成代码。
4、代码编写
新建两个文件 bsp_sg90.c
和 bsp_sg90.h
,并且将头文件路径添加到编译器中。
在文件 bsp_sg90.c
和 bsp_sg90.h
中,编写如下代码。
c
/*
* 立创开发板软硬件资料与相关扩展板软硬件资料官网全部开源
* 开发板官网:www.lckfb.com
* 文档网站:wiki.lckfb.com
* 技术支持常驻论坛,任何技术问题欢迎随时交流学习
* 嘉立创社区问答:https://www.jlc-bbs.com/lckfb
* 关注bilibili账号:【立创开发板】,掌握我们的最新动态!
* 不靠卖板赚钱,以培养中国工程师为己任
*/
#ifndef BSP_CODE_BSP_SG90_H_
#define BSP_CODE_BSP_SG90_H_
#include "hal_data.h"
#include <stdio.h>
#define SG90_DEBUG 0 //调试开关 1-打开调试 0-关闭调试
#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
#ifndef u8
#define u8 uint8_t
#endif
#ifndef u16
#define u16 uint16_t
#endif
#ifndef u32
#define u32 uint32_t
#endif
void SG90_Init(void);
void Set_SG90_Servo_Angle(uint32_t angle);
uint8_t Get_SG90_Servo_Angle(void);
#endif /* BSP_CODE_BSP_SG90_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
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
c
/*
* 立创开发板软硬件资料与相关扩展板软硬件资料官网全部开源
* 开发板官网:www.lckfb.com
* 文档网站:wiki.lckfb.com
* 技术支持常驻论坛,任何技术问题欢迎随时交流学习
* 嘉立创社区问答:https://www.jlc-bbs.com/lckfb
* 关注bilibili账号:【立创开发板】,掌握我们的最新动态!
* 不靠卖板赚钱,以培养中国工程师为己任
*/
#include "bsp_sg90.h"
uint8_t Servo_Angle = 0; //舵机角度
/******************************************************************
配置占空比 范围 0 ~ (per-1)
周期:20ms
t = 0.5ms(500us) ------占空比2.5%-------------舵机会转动 0 °
t = 1.0ms(1000us)------占空比5%---------------舵机会转动 45°
t = 1.5ms(1500us) ------占空比7.5%-------------舵机会转动 90°
t = 2.0ms(2000us) ------占空比10%--------------舵机会转动 135°
t = 2.5ms(2500us) ------占空比12.5%------------舵机会转动180°
******************************************************************/
/******************************************************************
* 函 数 名 称:Set_SG90_Servo_Angle
* 函 数 说 明:设置角度
* 函 数 形 参:angle=要设置的角度,范围0-180
* 函 数 返 回:无
* 作 者:LCKFB
* 备 注:无
******************************************************************/
void Set_SG90_Servo_Angle(uint32_t angle)
{
timer_info_t timer_info;
fsp_err_t err;
if (angle > 180) angle = 180; // 角度限幅
Servo_Angle = angle; // 记录角度(可选)
// 获取定时器信息(关键:获取总周期计数值)
err = R_GPT_InfoGet(&g_timer1_ctrl, &timer_info);
if (err != FSP_SUCCESS) {
printf("%s Error: Timer info failed (%d)\n", __func__, err);
return;
}
#if SG90_DEBUG
printf("Set SG90 Angle: %d\n", angle);
printf("Timer Period Counts: %d\n", timer_info.period_counts);
#endif
// 计算20ms周期内高电平时间(µs)
float high_time_us = 500.0 + ((float)angle * (2000.0 / 180.0));
// 计算占空比所对应的计数值
uint32_t duty_counts = (high_time_us / 20000.0) * timer_info.period_counts;
// 设置PWM占空比
err = R_GPT_DutyCycleSet(&g_timer1_ctrl, duty_counts, GPT_IO_PIN_GTIOCA);
if (err != FSP_SUCCESS) {
printf("%s Error: Duty set failed (%d)\n", __func__, err);
}
}
/******************************************************************
* 函 数 名 称:读取当前角度
* 函 数 说 明:Get_SG90_Servo_Angle
* 函 数 形 参:无
* 函 数 返 回:当前角度
* 作 者:LCKFB
* 备 注:使用前必须确保之前使用过
Set_SG90_Servo_Angle
函数设置过角度
******************************************************************/
uint8_t Get_SG90_Servo_Angle(void)
{
return Servo_Angle;
}
/******************************************************************
* 函 数 名 称:SG90_Init
* 函 数 说 明:SG90舵机初始化
* 函 数 形 参:无
* 函 数 返 回:无
* 作 者:LCKFB
* 备 注:无
******************************************************************/
void SG90_Init(void)
{
R_GPT_Open(&g_timer1_ctrl, &g_timer1_cfg);
R_GPT_Start(&g_timer1_ctrl);
delay_1ms(100); // 等待100ms,确保舵机初始化完成
}
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
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
四、移植验证
在 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_sg90.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");
uint8_t i = 0;
SG90_Init();
printf("\r\nSG90 Init Success\r\n");
/* 先让舵机转到最大角度位置,等待1000ms */
Set_SG90_Servo_Angle(180);
delay_1ms(1000);
/* 再让舵机转到最小角度位置,等待1000ms */
Set_SG90_Servo_Angle(0);
delay_1ms(1000);
printf("\r\nSG90 Set Angle Success\r\n");
while(1)
{
Set_SG90_Servo_Angle(i++);
if( i >= 180 )
{
i = 0;
}
delay_1ms(10);
}
}
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
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
编译烧录。
【代码下载】
- 跳转去下载模块移植代码:【点击跳转🚀】