#ifndef __BSP_MOTOR_TIM_H
#define __BSP_MOTOR_TIM_H
#include "stm32f4xx.h"
#include ".\bldcm_control\bsp_bldcm_control.h"
/* 电机控制定时器 */
#define MOTOR_TIM TIM8
#define MOTOR_TIM_CLK_ENABLE() __TIM8_CLK_ENABLE()//其实就是时钟 霍尔库的使能
/* 累计 TIM_Period个后产生一个更新或者中断
当定时器从0计数到5599,即为5600次,为一个定时周期 */
#define PWM_PERIOD_COUNT (5600)
#define PWM_MAX_PERIOD_COUNT (PWM_PERIOD_COUNT - 100)//最大时钟频率不让超过5500
/* 高级控制定时器时钟源TIMxCLK = HCLK = 168MHz
设定定时器频率为=TIMxCLK/(PWM_PRESCALER_COUNT)/PWM_PERIOD_COUNT = 15KHz 168000000/2/56000=15000 */
#define PWM_PRESCALER_COUNT (2)
/* TIM8通道1输出引脚 */
#define MOTOR_OCPWM1_PIN GPIO_PIN_5
#define MOTOR_OCPWM1_GPIO_PORT GPIOI
#define MOTOR_OCPWM1_GPIO_CLK_ENABLE() __GPIOI_CLK_ENABLE()
#define MOTOR_OCPWM1_AF GPIO_AF3_TIM8
/* TIM8通道2输出引脚 */
#define MOTOR_OCPWM2_PIN GPIO_PIN_6
#define MOTOR_OCPWM2_GPIO_PORT GPIOI
#define MOTOR_OCPWM2_GPIO_CLK_ENABLE() __GPIOI_CLK_ENABLE()
#define MOTOR_OCPWM2_AF GPIO_AF3_TIM8
/* TIM8通道3输出引脚 */
#define MOTOR_OCPWM3_PIN GPIO_PIN_7
#define MOTOR_OCPWM3_GPIO_PORT GPIOI
#define MOTOR_OCPWM3_GPIO_CLK_ENABLE() __GPIOI_CLK_ENABLE()
#define MOTOR_OCPWM3_AF GPIO_AF3_TIM8
/* TIM8通道1互补输出引脚 */
#define MOTOR_OCNPWM1_PIN GPIO_PIN_13
#define MOTOR_OCNPWM1_GPIO_PORT GPIOH
#define MOTOR_OCNPWM1_GPIO_CLK_ENABLE() __GPIOH_CLK_ENABLE()
#define MOTOR_OCNPWM1_AF GPIO_AF3_TIM8
/* TIM8通道2互补输出引脚 */
#define MOTOR_OCNPWM2_PIN GPIO_PIN_14
#define MOTOR_OCNPWM2_GPIO_PORT GPIOH
#define MOTOR_OCNPWM2_GPIO_CLK_ENABLE() __GPIOH_CLK_ENABLE()
#define MOTOR_OCNPWM2_AF GPIO_AF3_TIM8
/* TIM8通道3互补输出引脚 */
#define MOTOR_OCNPWM3_PIN GPIO_PIN_15
#define MOTOR_OCNPWM3_GPIO_PORT GPIOH
#define MOTOR_OCNPWM3_GPIO_CLK_ENABLE() __GPIOH_CLK_ENABLE()
#define MOTOR_OCNPWM3_AF GPIO_AF3_TIM8
#define TIM_COM_TS_ITRx TIM_TS_ITR3 // 内部触发配置(TIM8->ITR3->TIM5)
/* 霍尔传感器定时器 */
#define HALL_TIM TIM5
#define HALL_TIM_CLK_ENABLE() __TIM5_CLK_ENABLE()
extern TIM_HandleTypeDef htimx_hall;
/* 累计 TIM_Period个后产生一个更新或者中断
当定时器从0计数到0xffff,即为2的16次方,为一个定时周期 */
#define HALL_PERIOD_COUNT (0xFFFF)
/* 通用控制定时器时钟源TIMxCLK = HCLK / 2 = 84MHz
设定定时器频率为 = TIMxCLK / (PWM_PRESCALER_COUNT) / PWM_PERIOD_COUNT = 10.01Hz
周期 T = 100ms */
#define HALL_PRESCALER_COUNT (128)
/* TIM5 通道 1 引脚 */
#define HALL_INPUTU_PIN GPIO_PIN_10
#define HALL_INPUTU_GPIO_PORT GPIOH
#define HALL_INPUTU_GPIO_CLK_ENABLE() __GPIOH_CLK_ENABLE()
#define HALL_INPUTU_AF GPIO_AF2_TIM5
/* TIM5 通道 2 引脚 */
#define HALL_INPUTV_PIN GPIO_PIN_11
#define HALL_INPUTV_GPIO_PORT GPIOH
#define HALL_INPUTV_GPIO_CLK_ENABLE() __GPIOH_CLK_ENABLE()
#define HALL_INPUTV_AF GPIO_AF2_TIM5
/* TIM5 通道 3 引脚 */
#define HALL_INPUTW_PIN GPIO_PIN_12
#define HALL_INPUTW_GPIO_PORT GPIOH
#define HALL_INPUTW_GPIO_CLK_ENABLE() __GPIOH_CLK_ENABLE()
#define HALL_INPUTW_AF GPIO_AF2_TIM5
#define HALL_TIM_IRQn TIM5_IRQn
#define HALL_TIM_IRQHandler TIM5_IRQHandler
void TIMx_Configuration(void);
void stop_pwm_output(void);
void set_pwm_pulse(uint16_t pulse);
void hall_enable(void);
void hall_disable(void);
void hall_tim_config(void);
#endif /* __BSP_MOTOR_TIM_H */
这个头文件是关于电机控制定时器的使用配置。
电机控制定时器 选择的是定时器8:
/* 电机控制定时器 */
#define MOTOR_TIM TIM8
#define MOTOR_TIM_CLK_ENABLE() __TIM8_CLK_ENABLE()//其实就是时钟 霍尔库的使能
定时器8属于高级定时器,与其他定时器互相独立。
这里应该设置的是ARR 也就是自动重载寄存器,按道理来说16位的寄存器是可以累加到65536的,但这里只让他累加到5599,也就是5600
/* 累计 TIM_Period个后产生一个更新或者中断
当定时器从0计数到5599,即为5600次,为一个定时周期 */
#define PWM_PERIOD_COUNT (5600)
#define PWM_MAX_PERIOD_COUNT (PWM_PERIOD_COUNT - 100)
定时器的频率计算需要上面那个ARR之外,还需要预分频器寄存器PSC,也就是说如果你想要TIM8产生一个15K的定时,那么你预分频需要是2,ARR需要是5600,这样你的定时器频率就是15K了。
/* 高级控制定时器时钟源TIMxCLK = HCLK = 168MHz
设定定时器频率为=TIMxCLK/(PWM_PRESCALER_COUNT)/PWM_PERIOD_COUNT = 15KHz 168000000/2/56000=15000 */
#define PWM_PRESCALER_COUNT (2)
接下来就要确定是从哪一个引脚引出PWM了,PI的5、6、7,与PH的13、14、15
/* TIM8通道1输出引脚 */
#define MOTOR_OCPWM1_PIN GPIO_PIN_5
#define MOTOR_OCPWM1_GPIO_PORT GPIOI
#define MOTOR_OCPWM1_GPIO_CLK_ENABLE() __GPIOI_CLK_ENABLE()
#define MOTOR_OCPWM1_AF GPIO_AF3_TIM8
/* TIM8通道2输出引脚 */
#define MOTOR_OCPWM2_PIN GPIO_PIN_6
#define MOTOR_OCPWM2_GPIO_PORT GPIOI
#define MOTOR_OCPWM2_GPIO_CLK_ENABLE() __GPIOI_CLK_ENABLE()
#define MOTOR_OCPWM2_AF GPIO_AF3_TIM8
/* TIM8通道3输出引脚 */
#define MOTOR_OCPWM3_PIN GPIO_PIN_7
#define MOTOR_OCPWM3_GPIO_PORT GPIOI
#define MOTOR_OCPWM3_GPIO_CLK_ENABLE() __GPIOI_CLK_ENABLE()
#define MOTOR_OCPWM3_AF GPIO_AF3_TIM8
/* TIM8通道1互补输出引脚 */
#define MOTOR_OCNPWM1_PIN GPIO_PIN_13
#define MOTOR_OCNPWM1_GPIO_PORT GPIOH
#define MOTOR_OCNPWM1_GPIO_CLK_ENABLE() __GPIOH_CLK_ENABLE()
#define MOTOR_OCNPWM1_AF GPIO_AF3_TIM8
/* TIM8通道2互补输出引脚 */
#define MOTOR_OCNPWM2_PIN GPIO_PIN_14
#define MOTOR_OCNPWM2_GPIO_PORT GPIOH
#define MOTOR_OCNPWM2_GPIO_CLK_ENABLE() __GPIOH_CLK_ENABLE()
#define MOTOR_OCNPWM2_AF GPIO_AF3_TIM8
/* TIM8通道3互补输出引脚 */
#define MOTOR_OCNPWM3_PIN GPIO_PIN_15
#define MOTOR_OCNPWM3_GPIO_PORT GPIOH
#define MOTOR_OCNPWM3_GPIO_CLK_ENABLE() __GPIOH_CLK_ENABLE()
#define MOTOR_OCNPWM3_AF GPIO_AF3_TIM8
互补输出引脚
主从模式,内部触发,这里应该是使用TIM8内部触发TIM5(霍尔的传感器定时器)
#define TIM_COM_TS_ITRx TIM_TS_ITR3 // 内部触发配置(TIM8->ITR3->TIM5)
之后就是霍尔定时器的一些配置,用的是通用定时器TIM5,设置其定时器频率为10HZ
/* 累计 TIM_Period个后产生一个更新或者中断
当定时器从0计数到0xffff,即为2的16次方,为一个定时周期 */
#define HALL_PERIOD_COUNT (0xFFFF)
/* 通用控制定时器时钟源TIMxCLK = HCLK / 2 = 84MHz
设定定时器频率为 = TIMxCLK / (PWM_PRESCALER_COUNT) / PWM_PERIOD_COUNT = 10.01Hz
周期 T = 100ms */
#define HALL_PRESCALER_COUNT (128)
注意还有一个引脚复用的知识点