添加pwm(需修改)

This commit is contained in:
RB 2025-05-06 00:32:46 +08:00
parent 52acfaf20c
commit 9f964e1532
8 changed files with 294 additions and 0 deletions

BIN
.DS_Store vendored

Binary file not shown.

48
User/bsp/servo_pwm.c Normal file
View File

@ -0,0 +1,48 @@
/* Includes ----------------------------------------------------------------- */
#include "servo_pwm.h"
#include "main.h"
/* Private define ----------------------------------------------------------- */
/* Private macro ------------------------------------------------------------ */
/* Private typedef ---------------------------------------------------------- */
/* Private variables -------------------------------------------------------- */
/* Private function -------------------------------------------------------- */
/* Exported functions ------------------------------------------------------- */
int8_t BSP_PWM_Start(BSP_PWM_Channel_t ch) {
TIM_HandleTypeDef* htim = pwm_channel_config[ch].htim;
uint32_t channel = pwm_channel_config[ch].channel;
if(HAL_TIM_PWM_Start(htim, channel)!=HAL_OK){
return -1;
}else return 0;
}
int8_t BSP_PWM_Set(BSP_PWM_Channel_t ch, float duty_cycle) {
if (duty_cycle > 1.0f) return -1;
uint16_t pulse = duty_cycle/CYCLE * PWM_RESOLUTION;
if(__HAL_TIM_SET_COMPARE(pwm_channel_config[ch].htim, pwm_channel_config[ch].channel, pulse)!=HAL_OK){
return -1;
}else return 0;
}
int8_t BSP_PWM_Stop(BSP_PWM_Channel_t ch){
TIM_HandleTypeDef* htim = pwm_channel_config[ch].htim;
uint32_t channel = pwm_channel_config[ch].channel;
if(HAL_TIM_PWM_Stop(htim, channel)!=HAL_OK){
return -1;
}else return 0;
};

45
User/bsp/servo_pwm.h Normal file
View File

@ -0,0 +1,45 @@
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
/* Includes ----------------------------------------------------------------- */
#include <stdint.h>
#include "tim.h"
#include "bsp/bsp.h"
/* Exported constants ------------------------------------------------------- */
/* Exported macro ----------------------------------------------------------- */
/* Exported types ----------------------------------------------------------- */
typedef struct {
TIM_HandleTypeDef* htim; // 定时器句柄
uint32_t channel; // 定时器通道
} PWM_Channel_Config_t;
#define PWM_RESOLUTION 1000 // ARR change begin
#define CYCLE 20 //ms
typedef enum {
BSP_PWM_SERVO = 0,
BSP_PWM_IMU_HEAT = 1,
} BSP_PWM_Channel_t;
const PWM_Channel_Config_t pwm_channel_config[] = {
[BSP_PWM_SERVO] = { &htim1, TIM_CHANNEL_1 }, // xxx 对应 TIMx 通道x
[BSP_PWM_IMU_HEAT] = { &htim1, TIM_CHANNEL_2 }
}; //change end
/* Exported functions prototypes -------------------------------------------- */
int8_t BSP_PWM_Start(BSP_PWM_Channel_t ch);
int8_t BSP_PWM_Set(BSP_PWM_Channel_t ch, float duty_cycle);
int8_t BSP_PWM_Stop(BSP_PWM_Channel_t ch);
#ifdef __cplusplus
}
#endif

BIN
User/device/.DS_Store vendored Normal file

Binary file not shown.

86
User/device/key_gpio.c Normal file
View File

@ -0,0 +1,86 @@
/* Includes ----------------------------------------------------------------- */
#include "key_gpio.h"
#include "device.h"
#include <gpio.h>
/* Private define ----------------------------------------------------------- */
/* Private macro ------------------------------------------------------------ */
/* Private typedef ---------------------------------------------------------- */
/* Private variables -------------------------------------------------------- */
static uint32_t key_stats; // 使用位掩码记录每个通道的状态最多支持32LED
/* 外部声明标志位(标志问) */
extern volatile uint8_t key_flag; // 1=按下0=松开
/* 按键状态结构 */
typedef struct {
uint16_t state; // 当前状态
uint32_t press_timestamp; // 按下时间戳
uint8_t last_flag; // 上一次的标志位(用于消抖)
} Key_Status_t;
static Key_Status_t key = {
.state = DEVICE_KEY_RELEASED,
.press_timestamp = 0,
.last_flag = 0
};
/* 按键中断处理 */
void Key_Process(void) {
uint32_t now = HAL_GetTick();
uint8_t current_flag = key_flag; // 获取当前标志位
switch(key.state) {
case DEVICE_KEY_RELEASED:
if(current_flag == 1) {
// 首次检测到按下,记录时间戳
if(key.last_flag == 0) {
key.press_timestamp = now;
}
// 消抖确认20ms后仍为按下
if((now - key.press_timestamp) > 20) {
key.state = DEVICE_KEY_PRESSED;
}
}
break;
case DEVICE_KEY_PRESSED:
case DEVICE_KEY_LONG_PRESS:
if(current_flag == 1) {
// 持续按住超过20ms进入HOLD状态
if((now - key.press_timestamp) > 20) {
key.state = DEVICE_KEY_LONG_PRESS;
}
} else {
key.state = DEVICE_KEY_RELEASED; // 松开复位
}
break;
}
key.last_flag = current_flag; // 更新历史状态
}
//获取当前按键状态
uint16_t Get_Key_State(void) {
return key.state;
}
//也许是后续逻辑(跟在前面的代码后面放在循环里)
void KeyTask(void)
{
//仅按下就执行逻辑
// if( key.state == DEVICE_KEY_PRESSED)
// {
// //循环执行逻辑(按下)
// }
//按住才执行逻辑,松开就停下
if (key.state == DEVICE_KEY_LONG_PRESS)
{
//执行逻辑
}
else if (key.state == DEVICE_KEY_RELEASED)
{
//停止逻辑
}
}

38
User/device/key_gpio.h Normal file
View File

@ -0,0 +1,38 @@
#pragma once
/* Includes ----------------------------------------------------------------- */
#include <stdint.h>
#include "main.h"
//#include "key_gpio.h"
/* Exported constants ------------------------------------------------------- */
/* Exported macro ----------------------------------------------------------- */
/* Exported types ----------------------------------------------------------- */
///* KEY按键状态设置用 */
typedef enum
{
DEVICE_KEY_RELEASED, //按键释放
DEVICE_KEY_PRESSED, //按键按下
DEVICE_KEY_LONG_PRESS, //按键长按
} DEVICE_KEY_Status_t;
/* 按键通道定义 */
typedef enum {
DEVICE_KEY_1,
DEVICE_KEY_2,
/* 可根据需要扩展 */
DEVICE_KEY_COUNT
} DEVICE_Key_Channel_t;
/* 按键硬件配置结构体 */
typedef struct {
GPIO_TypeDef *port; // GPIO端口
uint16_t pin; // 引脚编号
uint8_t active_level; // 有效电平GPIO_PIN_SET/RESET
uint32_t trigger_edge; // 中断触发边沿RISING/FALLING
} DEVICE_Key_Config_t;
void Key_Process(void);
uint16_t Get_Key_State(void);
void KeyTask(void);

36
User/device/servo.c Normal file
View File

@ -0,0 +1,36 @@
/* Includes ----------------------------------------------------------------- */
#include "main.h"
#include "servo.h"
#include "bsp/servo_pwm.h"
/* Private define ----------------------------------------------------------- */
#define MIN_CYCLE 0.5f //change begin
#define MAX_CYCLE 2.5f
#define ANGLE_LIMIT 180 //change end
/* Private macro ------------------------------------------------------------ */
/* Private typedef ---------------------------------------------------------- */
/* Private variables -------------------------------------------------------- */
/* Private function -------------------------------------------------------- */
/* Exported functions ------------------------------------------------------- */
int serve_Init(BSP_PWM_Channel_t ch)
{
if(BSP_PWM_Start(ch)!=0){
return -1;
}else return 0;
}
int set_servo_angle(BSP_PWM_Channel_t ch,float angle)
{
if (angle < 0.0f || angle > ANGLE_LIMIT) {
return -1; // ÎÞЧµÄ½Ç¶È
}
float duty_cycle=MIN_CYCLE+(MAX_CYCLE-MIN_CYCLE)*(angle/ANGLE_LIMIT);
if(BSP_PWM_Set(ch,duty_cycle)!=0){
return -1;
}else return 0;
}

41
User/device/servo.h Normal file
View File

@ -0,0 +1,41 @@
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
/* Includes ----------------------------------------------------------------- */
#include <stdint.h>
#include "tim.h"
#include "bsp/bsp.h"
#include "bsp/servo_pwm.h"
/* Exported constants ------------------------------------------------------- */
/* Exported macro ----------------------------------------------------------- */
/* Exported types ----------------------------------------------------------- */
extern int serve_Init(BSP_PWM_Channel_t ch);
extern int set_servo_angle(BSP_PWM_Channel_t ch,float angle);
#ifdef __cplusplus
}
#endif