- 学习目标
- 学习内容
- ifndef _BSP_DOG_STEERING_H
- define _BSP_DOG_STEERING_H
- include “gd32f4xx.h”
- include “systick.h”
- define TIMER_PRESCALER (240 - 1)
- define TIMER_PERIOD (20000 - 1)
- define PWM0_RCU RCU_GPIOA
- define PWM0_PORT GPIOA
- define PWM0_PIN GPIO_PIN_3
- define PWM0_AF GPIO_AF_1
- define PWM0_CH TIMER_CH_3
- define PWM1_RCU RCU_GPIOA
- define PWM1_PORT GPIOA
- define PWM1_PIN GPIO_PIN_2
- define PWM1_AF GPIO_AF_1
- define PWM1_CH TIMER_CH_2
- define PWM2_RCU RCU_GPIOA
- define PWM2_PORT GPIOA
- define PWM2_PIN GPIO_PIN_0
- define PWM2_AF GPIO_AF_1
- define PWM2_CH TIMER_CH_0
- define PWM3_RCU RCU_GPIOA
- define PWM3_PORT GPIOA
- define PWM3_PIN GPIO_PIN_1
- define PWM3_AF GPIO_AF_1
- define PWM3_CH TIMER_CH_1
- define PWM4_RCU RCU_GPIOB
- define PWM4_PORT GPIOB
- define PWM4_PIN GPIO_PIN_4
- define PWM4_AF GPIO_AF_2
- define PWM4_CH TIMER_CH_0
- define PWM5_RCU RCU_GPIOB
- define PWM5_PORT GPIOB
- define PWM5_PIN GPIO_PIN_5
- define PWM5_AF GPIO_AF_2
- define PWM5_CH TIMER_CH_1
- define PWM6_RCU RCU_GPIOB
- define PWM6_PORT GPIOB
- define PWM6_PIN GPIO_PIN_0
- define PWM6_AF GPIO_AF_2
- define PWM6_CH TIMER_CH_2
- define PWM7_RCU RCU_GPIOB
- define PWM7_PORT GPIOB
- define PWM7_PIN GPIO_PIN_1
- define PWM7_AF GPIO_AF_2
- define PWM7_CH TIMER_CH_3
- define a1 5
- define a2 0
- define a3 5
- define a4 0
- define a5 5
- define a6 0
- define a7 5
- define a8 5
- endif
学习目标
- 实现舵机控制
- 实现板级舵机驱动
学习内容
原理图
8路PWM来控制舵机转动,需要用到timer1和timer2。驱动封装
```cifndef _BSP_DOG_STEERING_H
define _BSP_DOG_STEERING_H
include “gd32f4xx.h”
include “systick.h”
//PWM周期:1ms PWM频率:1000hz //预分频:10 1s: 240 000 000 pwm周期计数: 240 000 000/10000=24000
define TIMER_PRESCALER (240 - 1)
//pwm周期计数
define TIMER_PERIOD (20000 - 1)
//PWM0
define PWM0_RCU RCU_GPIOA
define PWM0_PORT GPIOA
define PWM0_PIN GPIO_PIN_3
define PWM0_AF GPIO_AF_1
define PWM0_CH TIMER_CH_3
//PWM1
define PWM1_RCU RCU_GPIOA
define PWM1_PORT GPIOA
define PWM1_PIN GPIO_PIN_2
define PWM1_AF GPIO_AF_1
define PWM1_CH TIMER_CH_2
//PWM2
define PWM2_RCU RCU_GPIOA
define PWM2_PORT GPIOA
define PWM2_PIN GPIO_PIN_0
define PWM2_AF GPIO_AF_1
define PWM2_CH TIMER_CH_0
//PWM3
define PWM3_RCU RCU_GPIOA
define PWM3_PORT GPIOA
define PWM3_PIN GPIO_PIN_1
define PWM3_AF GPIO_AF_1
define PWM3_CH TIMER_CH_1
//PWM4
define PWM4_RCU RCU_GPIOB
define PWM4_PORT GPIOB
define PWM4_PIN GPIO_PIN_4
define PWM4_AF GPIO_AF_2
define PWM4_CH TIMER_CH_0
//PWM5
define PWM5_RCU RCU_GPIOB
define PWM5_PORT GPIOB
define PWM5_PIN GPIO_PIN_5
define PWM5_AF GPIO_AF_2
define PWM5_CH TIMER_CH_1
//PWM6
define PWM6_RCU RCU_GPIOB
define PWM6_PORT GPIOB
define PWM6_PIN GPIO_PIN_0
define PWM6_AF GPIO_AF_2
define PWM6_CH TIMER_CH_2
//PWM7
define PWM7_RCU RCU_GPIOB
define PWM7_PORT GPIOB
define PWM7_PIN GPIO_PIN_1
define PWM7_AF GPIO_AF_2
define PWM7_CH TIMER_CH_3
//舵机的角度偏差
define a1 5
define a2 0
define a3 5
define a4 0
define a5 5
define a6 0
define a7 5
define a8 5
void bsp_dog_config(void); //控制每个舵机转动角度 void bsp_pwm0_angle(float angle); void bsp_pwm1_angle(float angle); void bsp_pwm2_angle(float angle); void bsp_pwm3_angle(float angle); void bsp_pwm4_angle(float angle); void bsp_pwm5_angle(float angle); void bsp_pwm6_angle(float angle); void bsp_pwm7_angle(float angle); //给定8个舵机角度 void bsp_dog_angles(double steering_angles[8]); //狗安装角度 void dog_install_angle(void);
endif
```c
#include "bsp_dog_steering.h"
static void timer1_config(void){
/*************** GPIO config **************/
//PA3
rcu_periph_clock_enable(PWM0_RCU);
gpio_mode_set(PWM0_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, PWM0_PIN);
gpio_output_options_set(PWM0_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_MAX, PWM0_PIN);
gpio_af_set(PWM0_PORT, PWM0_AF, PWM0_PIN);
//PA2
rcu_periph_clock_enable(PWM1_RCU);
gpio_mode_set(PWM1_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, PWM1_PIN);
gpio_output_options_set(PWM1_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_MAX, PWM1_PIN);
gpio_af_set(PWM1_PORT, PWM1_AF, PWM1_PIN);
//PA0
rcu_periph_clock_enable(PWM2_RCU);
gpio_mode_set(PWM2_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, PWM2_PIN);
gpio_output_options_set(PWM2_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_MAX, PWM2_PIN);
gpio_af_set(PWM2_PORT, PWM2_AF, PWM2_PIN);
//PA1
rcu_periph_clock_enable(PWM3_RCU);
gpio_mode_set(PWM3_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, PWM3_PIN);
gpio_output_options_set(PWM3_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_MAX, PWM3_PIN);
gpio_af_set(PWM3_PORT, PWM3_AF, PWM3_PIN);
/*************** Timer config *************/
// 时钟配置
rcu_periph_clock_enable(RCU_TIMER1);
// 复位定时器
timer_deinit(TIMER1);
// 倍频配置
rcu_timer_clock_prescaler_config(RCU_TIMER_PSC_MUL4);
// 初始化定时器
timer_parameter_struct tps;
timer_struct_para_init(&tps);
tps.prescaler = TIMER_PRESCALER; // 分频计数 240 000 000 预分频:10000 1s:24000
tps.period = TIMER_PERIOD; // 周期计数 1s
//定时器初始化
timer_init(TIMER1, &tps);
// pwm0
timer_oc_parameter_struct tops;
timer_channel_output_struct_para_init(&tops);
//输出OP的PWM
tops.outputstate = TIMER_CCX_ENABLE;
timer_channel_output_config(TIMER1, PWM0_CH, &tops);
// 输出模式配置 TIMER_OC_MODE_PWM0有效点平是高电平
timer_channel_output_mode_config(TIMER1, PWM0_CH, TIMER_OC_MODE_PWM0);
// pwm1
timer_channel_output_struct_para_init(&tops);
//输出OP的PWM
tops.outputstate = TIMER_CCX_ENABLE;
timer_channel_output_config(TIMER1, PWM1_CH, &tops);
// 输出模式配置 TIMER_OC_MODE_PWM0有效点平是高电平
timer_channel_output_mode_config(TIMER1, PWM1_CH, TIMER_OC_MODE_PWM0);
// pwm2
timer_channel_output_struct_para_init(&tops);
//输出OP的PWM
tops.outputstate = TIMER_CCX_ENABLE;
timer_channel_output_config(TIMER1, PWM2_CH, &tops);
// 输出模式配置 TIMER_OC_MODE_PWM0有效点平是高电平
timer_channel_output_mode_config(TIMER1, PWM2_CH, TIMER_OC_MODE_PWM0);
// pwm3
timer_channel_output_struct_para_init(&tops);
//输出OP的PWM
tops.outputstate = TIMER_CCX_ENABLE;
timer_channel_output_config(TIMER1, PWM3_CH, &tops);
// 输出模式配置 TIMER_OC_MODE_PWM0有效点平是高电平
timer_channel_output_mode_config(TIMER1, PWM3_CH, TIMER_OC_MODE_PWM0);
// 初始化
timer_enable(TIMER1);
}
static void timer2_config(void){
/*************** GPIO config **************/
//PB4
rcu_periph_clock_enable(PWM4_RCU);
gpio_mode_set(PWM4_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, PWM4_PIN);
gpio_output_options_set(PWM4_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_MAX, PWM4_PIN);
gpio_af_set(PWM4_PORT, PWM4_AF, PWM4_PIN);
//PB5
rcu_periph_clock_enable(PWM5_RCU);
gpio_mode_set(PWM5_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, PWM5_PIN);
gpio_output_options_set(PWM5_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_MAX, PWM5_PIN);
gpio_af_set(PWM5_PORT, PWM5_AF, PWM5_PIN);
//PB0
rcu_periph_clock_enable(PWM6_RCU);
gpio_mode_set(PWM6_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, PWM6_PIN);
gpio_output_options_set(PWM6_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_MAX, PWM6_PIN);
gpio_af_set(PWM6_PORT, PWM6_AF, PWM6_PIN);
//PB1
rcu_periph_clock_enable(PWM7_RCU);
gpio_mode_set(PWM7_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, PWM7_PIN);
gpio_output_options_set(PWM7_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_MAX, PWM7_PIN);
gpio_af_set(PWM7_PORT, PWM7_AF, PWM7_PIN);
/*************** Timer config *************/
// 时钟配置
rcu_periph_clock_enable(RCU_TIMER2);
// 复位定时器
timer_deinit(TIMER2);
// 倍频配置
rcu_timer_clock_prescaler_config(RCU_TIMER_PSC_MUL4);
// 初始化定时器
timer_parameter_struct tps;
timer_struct_para_init(&tps);
tps.prescaler = TIMER_PRESCALER; // 分频计数 240 000 000 预分频:10000 1s:24000
tps.period = TIMER_PERIOD; // 周期计数 1s
//定时器初始化
timer_init(TIMER2, &tps);
// pwm4
timer_oc_parameter_struct tops;
timer_channel_output_struct_para_init(&tops);
//输出OP的PWM
tops.outputstate = TIMER_CCX_ENABLE;
timer_channel_output_config(TIMER2, PWM4_CH, &tops);
// 输出模式配置 TIMER_OC_MODE_PWM0有效点平是高电平
timer_channel_output_mode_config(TIMER2, PWM4_CH, TIMER_OC_MODE_PWM0);
// pwm5
timer_channel_output_struct_para_init(&tops);
//输出OP的PWM
tops.outputstate = TIMER_CCX_ENABLE;
timer_channel_output_config(TIMER2, PWM5_CH, &tops);
// 输出模式配置 TIMER_OC_MODE_PWM0有效点平是高电平
timer_channel_output_mode_config(TIMER2, PWM5_CH, TIMER_OC_MODE_PWM0);
// pwm6
timer_channel_output_struct_para_init(&tops);
//输出OP的PWM
tops.outputstate = TIMER_CCX_ENABLE;
timer_channel_output_config(TIMER2, PWM6_CH, &tops);
// 输出模式配置 TIMER_OC_MODE_PWM0有效点平是高电平
timer_channel_output_mode_config(TIMER2, PWM6_CH, TIMER_OC_MODE_PWM0);
// pwm7
timer_channel_output_struct_para_init(&tops);
//输出OP的PWM
tops.outputstate = TIMER_CCX_ENABLE;
timer_channel_output_config(TIMER2, PWM7_CH, &tops);
// 输出模式配置 TIMER_OC_MODE_PWM0有效点平是高电平
timer_channel_output_mode_config(TIMER2, PWM7_CH, TIMER_OC_MODE_PWM0);
// 初始化
timer_enable(TIMER2);
}
void bsp_dog_config(void){
timer1_config();
timer2_config();
}
static void pwm0_update(float duty) {//0-100
//10
uint32_t pulse = (uint32_t)(duty*TIMER_PERIOD/100);
// 配置输出的占空比 占空比 计数值
timer_channel_output_pulse_value_config(TIMER1, PWM0_CH, pulse);
}
static void pwm1_update(float duty) {//0-100
//10
uint32_t pulse = (uint32_t)(duty*TIMER_PERIOD/100);
// 配置输出的占空比 占空比 计数值
timer_channel_output_pulse_value_config(TIMER1, PWM1_CH, pulse);
}
static void pwm2_update(float duty) {//0-100
//10
uint32_t pulse = (uint32_t)(duty*TIMER_PERIOD/100);
// 配置输出的占空比 占空比 计数值
timer_channel_output_pulse_value_config(TIMER1, PWM2_CH, pulse);
}
static void pwm3_update(float duty) {//0-100
//10
uint32_t pulse = (uint32_t)(duty*TIMER_PERIOD/100);
// 配置输出的占空比 占空比 计数值
timer_channel_output_pulse_value_config(TIMER1, PWM3_CH, pulse);
}
static void pwm4_update(float duty) {//0-100
//10
uint32_t pulse = (uint32_t)(duty*TIMER_PERIOD/100);
// 配置输出的占空比 占空比 计数值
timer_channel_output_pulse_value_config(TIMER2, PWM4_CH, pulse);
}
static void pwm5_update(float duty) {//0-100
//10
uint32_t pulse = (uint32_t)(duty*TIMER_PERIOD/100);
// 配置输出的占空比 占空比 计数值
timer_channel_output_pulse_value_config(TIMER2, PWM5_CH, pulse);
}
static void pwm6_update(float duty) {//0-100
//10
uint32_t pulse = (uint32_t)(duty*TIMER_PERIOD/100);
// 配置输出的占空比 占空比 计数值
timer_channel_output_pulse_value_config(TIMER2, PWM6_CH, pulse);
}
static void pwm7_update(float duty) {//0-100
//10
uint32_t pulse = (uint32_t)(duty*TIMER_PERIOD/100);
// 配置输出的占空比 占空比 计数值
timer_channel_output_pulse_value_config(TIMER2, PWM7_CH, pulse);
}
void bsp_pwm0_angle(float angle){
pwm0_update(2.5+(angle+a1)*10.0/180.0);
}
void bsp_pwm1_angle(float angle){
pwm1_update(2.5+(angle+a2)*10.0/180.0);
}
void bsp_pwm2_angle(float angle){
pwm2_update(2.5+(angle+a3)*10.0/180.0);
}
void bsp_pwm3_angle(float angle){
pwm3_update(2.5+(angle+a4)*10.0/180.0);
}
void bsp_pwm4_angle(float angle){
pwm4_update(2.5+(angle+a5)*10.0/180.0);
}
void bsp_pwm5_angle(float angle){
pwm5_update(2.5+(angle+a6)*10.0/180.0);
}
void bsp_pwm6_angle(float angle){
pwm6_update(2.5+(angle+a7)*10.0/180.0);
}
void bsp_pwm7_angle(float angle){
pwm7_update(2.5+(angle+a8)*10.0/180.0);
}
//给定8个舵机角度
void bsp_dog_angles(double steering_angles[8]){
bsp_pwm0_angle(steering_angles[0]);
bsp_pwm1_angle(steering_angles[1]);
bsp_pwm2_angle(steering_angles[2]);
bsp_pwm3_angle(steering_angles[3]);
bsp_pwm4_angle(steering_angles[4]);
bsp_pwm5_angle(steering_angles[5]);
bsp_pwm6_angle(steering_angles[6]);
bsp_pwm7_angle(steering_angles[7]);
}
//狗的安装角度
void dog_install_angle(void){
bsp_pwm0_angle(90);
bsp_pwm1_angle(0);
bsp_pwm2_angle(90);
bsp_pwm3_angle(0);
bsp_pwm4_angle(0);
bsp_pwm5_angle(90);
bsp_pwm6_angle(0);
bsp_pwm7_angle(90);
}