一、模块来源
二、规格参数
驱动电压:3V~7.2V
工作扭矩:1.6KG/CM
控制方式:PWM
转动角度:180度
以上信息见厂家资料文件
三、移植过程
我们的目标是将例程移植至开发板上【能够控制舵机旋转的功能】。首先要获取资料,查看数据手册应如何实现读取数据,再移植至我们的工程。
1、查看资料
在购买时,需要分清楚你的舵机可以转180度,还是360度。360度的舵机是无法控制角度的,只可以控制旋转速度。
SG90的舵机转速不是很快,一般为0.22/60 度或0.18/60 度,所以假如你更改角度控制脉冲的宽度太快时,舵机可能反应不过来。如果需要更快速的反应,就需要更高的转速了。
2、引脚选择
棕色线 | GND |
黄色线 | PA1(TIMER1_CH_1) |
红色线 | 5V0 |
3、代码编写
新建两个文件 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 "gd32vw55x.h"
#include "systick.h"
#define Module_RCU_Enable() \
rcu_periph_clock_enable(RCU_GPIOA); \
rcu_periph_clock_enable(RCU_TIMER1);
/* TIMER */
#define BSP_PWM_TIMER_RCU RCU_TIMER1 // 定时器时钟
#define BSP_PWM_TIMER TIMER1 // 定时器
#define BSP_PWM_TIMER_CH TIMER_CH_1 // 定时器通道
/* PA5 TIMER1_CH1 */
#define BSP_PWM_RCU RCU_GPIOA
#define BSP_PWM_PORT GPIOA
#define BSP_PWM_PIN GPIO_PIN_1
#define BSP_PWM_AF GPIO_AF_1
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
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
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)
t = 0.5ms-------------------舵机会转动 0 °
t = 1.0ms-------------------舵机会转动 45°
t = 1.5ms-------------------舵机会转动 90°
t = 2.0ms-------------------舵机会转动 135°
t = 2.5ms-------------------舵机会转动180°
******************************************************************/
/******************************************************************
* 函 数 名 称:Set_SG90_Servo_Angle
* 函 数 说 明:设置角度
* 函 数 形 参:angle=要设置的角度,范围0-180
* 函 数 返 回:无
* 作 者:LCKFB
* 备 注:无
******************************************************************/
void Set_SG90_Servo_Angle(uint32_t angle)
{
if(angle > 180)
{
angle = 180; // 限制角度在0到180度之间
}
Servo_Angle = angle;
// 计算PWM占空比
// 0.5ms对应的计数 ~= 500
// 2.5ms对应的计数 ~= 2500
float min_count = 500.0f;
float max_count = 2500.0f;
float range = max_count - min_count;
float ServoAngle = min_count + (((float)angle / 180.0f) * range);
timer_channel_output_pulse_value_config(BSP_PWM_TIMER, BSP_PWM_TIMER_CH, (uint32_t)ServoAngle);
}
/******************************************************************
* 函 数 名 称:读取当前角度
* 函 数 说 明: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)
{
/* 使能相关时钟 */
Module_RCU_Enable();
gpio_mode_set(BSP_PWM_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, BSP_PWM_PIN);
gpio_output_options_set(BSP_PWM_PORT,GPIO_OTYPE_PP, GPIO_OSPEED_25MHZ,BSP_PWM_PIN);
/* 配置IO为定时器的通道 */
gpio_af_set(BSP_PWM_PORT, BSP_PWM_AF, BSP_PWM_PIN);
timer_parameter_struct timer_initpara; // 定义定时器结构体
/* 开启时钟 */
rcu_periph_clock_enable(BSP_PWM_TIMER_RCU); // 开启定时器时钟
/* CK_TIMERx = 2 x CK_APB1 = 2x80M = 160MHZ */
rcu_timer_clock_prescaler_config(RCU_TIMER_PSC_MUL2); // 配置定时器时钟
timer_deinit(BSP_PWM_TIMER); // 复位定时器
/* 配置定时器参数 */
timer_initpara.prescaler = 160-1; // 时钟预分频值 PSC_CLK= 160MHZ / 160 = 1MHZ
timer_initpara.alignedmode = TIMER_COUNTER_EDGE; // 边缘对齐
timer_initpara.counterdirection = TIMER_COUNTER_UP; // 向上计数
timer_initpara.period = 20000 -1; // 周期 T = 20000 T-time = 20ms f = 50HZ
/* 在输入捕获的时候使用 数字滤波器使用的采样频率之间的分频比例 */
timer_initpara.clockdivision = TIMER_CKDIV_DIV1; // 分频因子
/* 只有高级定时器才有。配置为x,就重复x+1次进入中断 */
timer_initpara.repetitioncounter = 0; // 重复计数器 0-255
timer_init(BSP_PWM_TIMER,&timer_initpara); // 初始化定时器
/* 使能定时器 */
timer_enable(BSP_PWM_TIMER);
/* 定义定时器输出参数 */
timer_oc_parameter_struct timer_ocintpara;
timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_HIGH; // 输出极性为高
timer_ocintpara.outputstate = TIMER_CCX_ENABLE; // 使能输出
/* 配置定时器输出功能 */
timer_channel_output_config(BSP_PWM_TIMER, BSP_PWM_TIMER_CH, &timer_ocintpara);
/* 配置定时器通道输出脉冲值 */
timer_channel_output_pulse_value_config(BSP_PWM_TIMER,BSP_PWM_TIMER_CH, 0);
/* 配置定时器通道输出比较模式 */
timer_channel_output_mode_config(BSP_PWM_TIMER,BSP_PWM_TIMER_CH,TIMER_OC_MODE_PWM0);
/* 配置定时器通道输出影子寄存器 */
timer_channel_output_shadow_config(BSP_PWM_TIMER,BSP_PWM_TIMER_CH,TIMER_OC_SHADOW_DISABLE);
/* 使能自动重载影子寄存器 */
timer_auto_reload_shadow_enable(BSP_PWM_TIMER);
}
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
124
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
124
四、移植验证
在 src\main.c
中输入代码如下:
c
#include "gd32vw55x.h"
#include "systick.h"
#include <stdio.h>
#include "main.h"
#include "gd32vw553h_eval.h"
#include "bsp_sg90.h"
/*!
\brief toggle the led every 500ms
\param[in] none
\param[out] none
\retval none
*/
void led_spark(void)
{
static __IO uint32_t timingdelaylocal = 0U;
if(timingdelaylocal) {
if(timingdelaylocal < 500U) {
gd_eval_led_on(LED1);
} else {
gd_eval_led_off(LED1);
}
timingdelaylocal--;
} else {
timingdelaylocal = 1000U;
}
}
/*!
\brief main function
\param[in] none
\param[out] none
\retval none
*/
int main(void)
{
#ifdef __FIRMWARE_VERSION_DEFINE
uint32_t fw_ver = 0;
#endif /* __FIRMWARE_VERSION_DEFINE */
/* configure systick */
systick_config();
eclic_priority_group_set(ECLIC_PRIGROUP_LEVEL3_PRIO1);
/* initilize the LEDs, USART and key */
gd_eval_led_init(LED1);
gd_eval_com_init(EVAL_COM0);
gd_eval_key_init(KEY_TAMPER_WAKEUP, KEY_MODE_GPIO);
#ifdef __FIRMWARE_VERSION_DEFINE
fw_ver = gd32vw55x_firmware_version_get();
printf("\r\n= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\r\n\n");
printf("\r\n=== Welcome to use the LC-GD32VW553-HMQ6 development board ====\r\n\n");
printf("\r\n======================= www.lckfb.com =========================\r\n\n");
printf("\r\n======================= wiki.lckfb.com ========================\r\n\n");
printf("\r\n= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\r\n");
/* print firmware version */
printf("\r\nGD32VW55X series firmware version: V%d.%d.%d", (uint8_t)(fw_ver >> 24), (uint8_t)(fw_ver >> 16), (uint8_t)(fw_ver >> 8));
#endif /* __FIRMWARE_VERSION_DEFINE */
/* print out the clock frequency of system, AHB, APB1 and APB2 */
printf("\r\nCK_SYS is %d\r\n", rcu_clock_freq_get(CK_SYS));
printf("\r\nCK_AHB is %d\r\n", rcu_clock_freq_get(CK_AHB));
printf("\r\nCK_APB1 is %d\r\n", rcu_clock_freq_get(CK_APB1));
printf("\r\nCK_APB2 is %d\r\n", rcu_clock_freq_get(CK_APB2));
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
88
89
90
91
92
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
编译烧录。
【代码下载】
- 跳转到
下载中心
去下载模块移植代码:【点击跳转🚀】