From 9f964e1532daf502c21c8bb43c50ae5743010cc0 Mon Sep 17 00:00:00 2001 From: RB Date: Tue, 6 May 2025 00:32:46 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0pwm=EF=BC=88=E9=9C=80?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .DS_Store | Bin 8196 -> 8196 bytes User/bsp/servo_pwm.c | 48 +++++++++++++++++++++++ User/bsp/servo_pwm.h | 45 +++++++++++++++++++++ User/device/.DS_Store | Bin 0 -> 6148 bytes User/device/key_gpio.c | 86 +++++++++++++++++++++++++++++++++++++++++ User/device/key_gpio.h | 38 ++++++++++++++++++ User/device/servo.c | 36 +++++++++++++++++ User/device/servo.h | 41 ++++++++++++++++++++ 8 files changed, 294 insertions(+) create mode 100644 User/bsp/servo_pwm.c create mode 100644 User/bsp/servo_pwm.h create mode 100644 User/device/.DS_Store create mode 100644 User/device/key_gpio.c create mode 100644 User/device/key_gpio.h create mode 100644 User/device/servo.c create mode 100644 User/device/servo.h diff --git a/.DS_Store b/.DS_Store index b3388df1176fbf220203aca2ab267dd1a17fb774..c294515e9e66c1003eb76dd6328a120e90f42607 100644 GIT binary patch delta 192 zcmZp1XmOa}&nU4mU^hRb#AF_UbZ%CLB!*IkOokkWl*tML@{?x^aBi*<&}NcN_RPso zPRhwoVqjnpU|?YU38d%!2LlF%$z4J>wYeF58G;z{fd=F=lrZQq6f+bsq%xrCV{ioN zn;aoLm7N)=qKF}R@&>_^f?!rza8X`PeqK6I1>@#d!tW BHs}BV delta 54 zcmV-60LlM^K!iY$PXQCLP`eKS6O#-OXS1CUGy; 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; + + +}; + + + + diff --git a/User/bsp/servo_pwm.h b/User/bsp/servo_pwm.h new file mode 100644 index 0000000..aefb7f1 --- /dev/null +++ b/User/bsp/servo_pwm.h @@ -0,0 +1,45 @@ +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + +/* Includes ----------------------------------------------------------------- */ +#include +#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 + + + + + diff --git a/User/device/.DS_Store b/User/device/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..f792790daca7cb1ad34251b2198bbe6857c63fa4 GIT binary patch literal 6148 zcmeHKO-}+b5Pd~V5HaEC#bl3qA>zs7O7P&>9{{otBqD26NWAUc|KMNn_j%GcZ9{fj zh&MH6CTU+k=5=;ncH1og(_4k<~#E&%6fyySSs@i>$t3<+oBs2i|jlDW7kK9LMP5 zWJ`=Pvl%htX!2a&al%|A_bMmHoY{=m`)nNNBgntWJVH(or#1~Zd(1y$j3sXHi04>4 z`!9lI)|;X zEx!uFNj#<=3x}Mc8Al~Ls_|D02g}&{A0W + +/* 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) { + // ס20msHOLD״̬ + 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) + { + //ֹͣ߼ + } +} \ No newline at end of file diff --git a/User/device/key_gpio.h b/User/device/key_gpio.h new file mode 100644 index 0000000..861279c --- /dev/null +++ b/User/device/key_gpio.h @@ -0,0 +1,38 @@ +#pragma once + +/* Includes ----------------------------------------------------------------- */ +#include +#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); \ No newline at end of file diff --git a/User/device/servo.c b/User/device/servo.c new file mode 100644 index 0000000..12841fe --- /dev/null +++ b/User/device/servo.c @@ -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; +} + diff --git a/User/device/servo.h b/User/device/servo.h new file mode 100644 index 0000000..97337df --- /dev/null +++ b/User/device/servo.h @@ -0,0 +1,41 @@ +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + +/* Includes ----------------------------------------------------------------- */ +#include +#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 + + + + +