This repository has been archived on 2025-02-28. You can view files and clone it, but cannot push or open issues or pull requests.
controller-hd/User/application/inc/pdctrl.h

57 lines
2.2 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#ifndef _PDCTRL_H_
#define _PDCTRL_H_
#include "lib.h"
#include "bsp.h"
#define PWM_WID 13 // PWM正频宽
#define PDCTRL_PWM_PRESCALER (1) // 预分频系数
#define PDCTRL_PWM_FREQUENCY (4 * 1000) // 主频KHZ
#define PDCTRL_PWM_TIM (TIM2) // 定时器
#define PDCTRL_PWM_TIM_IRQn (TIM2_IRQn) // 中断
#define PDCTRL_PWM_CHINNEL (LL_TIM_CHANNEL_CH4) // 通道
#define PDCTRL_PWM_GPIO_Port (PWM_CONTROL_GPIO_Port) // 输出引脚
#define PDCTRL_PWM_Pin (PWM_CONTROL_Pin) // 输出引脚
#define PDCTRL_DAC_TIM_IRQn (TIM6_IRQn) // 中断
#define PDCTRL_DAC_CHINNEL (LL_DAC_CHANNEL_2) // 通道
#define PDCTRL_DAC_GPIO_Port (DAC_CONTROL_GPIO_Port) // 输出引脚
#define PDCTRL_DAC_Pin (DAC_CONTROL_Pin) // 输出引脚
#define PDCTRL_PWMP_TIM (TIM2) // 定时器
#define PDCTRL_PWMP_TIM_IRQn (TIM2_IRQn) // 中断
#define PDCTRL_PWMP_CHINNEL (LL_TIM_CHANNEL_CH4) // 通道
#define PDCTRL_PWMP_GPIO_Port (PWM_CONTROL_GPIO_Port) // 输出引脚
#define PDCTRL_PWMP_Pin (PWM_CONTROL_Pin) // 输出引脚
typedef enum
{
PDCTRL_DAC = 1, // DAC输出模式
PDCTRL_PWM = 2, // PWM输出模式
PDCTRL_PWMP = 3, // PWM占空比模式
PDCTRL_MINOR = 4, // PWM占空比模式
} pdctrl_mode_e;
typedef struct
{
__IO uint16_t last_out;
uint16_t pwm_arr_default;
uint32_t sysclk;
uint32_t psc;
uint32_t arr;
float32 freq;
float32 arr_us;
float32 duty_percent;
} pdctrl_t;
void pdctrl_init(pdctrl_mode_e mode); ///< 控制器初始化
void pdctrl_out(uint16_t out); ///< 控制器输出
void pdctrl_pwm_set_arr(uint32_t arr); ///< 动态修改频率
uint16_t calculate_pwm_duty(uint32_t arr); ///< 变频计算占空比
uint16_t pdctrl_pwm_duty_convert_ccr(float32 pwm_duty); ///< 占空比转换为CCR值
void pdctrl_stop(void); ///< 控制停止
void pdctrl_run(void); ///< 控制运行
uint8_t get_pdctrl_mode(void); ///< 获取控制模式
pdctrl_t *pdctrl_get(void); ///< 获取控制器
#endif // _PDCTRL_H_