新手入门
- 积分
- 10
- 金钱
- 10
- 注册时间
- 2023-12-30
- 在线时间
- 1 小时
|
#include "tim.h"
TIM_HandleTypeDef tim_handle;
void tim_init(uint16_t arr,uint16_t psc)
{
tim_handle.Instance=TIM6;
tim_handle.Init.CounterMode=TIM_COUNTERMODE_UP;
tim_handle.Init.ClockDivision=TIM_CLOCKDIVISION_DIV1;
tim_handle.Init.Period=arr;
tim_handle.Init.Prescaler=psc;
HAL_TIM_Base_Init(&tim_handle);
HAL_TIM_Base_Start(&tim_handle);
}
void HAL_TIM_Base_MspInit(TIM_HandleTypeDef *htim)
{
if(htim->Instance==TIM6)
{
__HAL_RCC_TIM6_CLK_ENABLE();
}
}
TIM_HandleTypeDef tim_pwm_handle;
void tim_pwm_init(uint16_t arr,uint16_t psc)
{
TIM_OC_InitTypeDef timx_oc_pwm_chy;
tim_pwm_handle.Instance=TIM3;
tim_pwm_handle.Init.CounterMode=TIM_COUNTERMODE_UP;
tim_pwm_handle.Init.ClockDivision=TIM_CLOCKDIVISION_DIV1;
tim_pwm_handle.Init.Period=arr;
tim_pwm_handle.Init.Prescaler=psc;
HAL_TIM_PWM_Init(&tim_pwm_handle);
timx_oc_pwm_chy.OCMode=TIM_OCMODE_PWM1;
timx_oc_pwm_chy.OCPolarity=TIM_OCPOLARITY_HIGH;
timx_oc_pwm_chy.Pulse=arr/2;
HAL_TIM_PWM_ConfigChannel(&tim_pwm_handle,&timx_oc_pwm_chy,TIM_CHANNEL_3);
timx_oc_pwm_chy.OCMode=TIM_OCMODE_PWM1;
timx_oc_pwm_chy.OCPolarity=TIM_OCPOLARITY_HIGH;
timx_oc_pwm_chy.Pulse=arr/2;
HAL_TIM_PWM_ConfigChannel(&tim_pwm_handle,&timx_oc_pwm_chy,TIM_CHANNEL_4);
HAL_TIM_PWM_Start(&tim_pwm_handle,TIM_CHANNEL_3);
HAL_TIM_PWM_Start(&tim_pwm_handle,TIM_CHANNEL_4);
}
void HAL_TIM_PWM_MspInit(TIM_HandleTypeDef *htim)
{
if(htim->Instance == TIM3)
{
GPIO_InitTypeDef gpio_init_struct;
__HAL_RCC_GPIOB_CLK_ENABLE();
__HAL_RCC_TIM3_CLK_ENABLE();
__HAL_RCC_AFIO_CLK_ENABLE();
gpio_init_struct.Pin = GPIO_PIN_0|GPIO_PIN_1;
gpio_init_struct.Mode = GPIO_MODE_AF_PP; /* 推挽复用 */
gpio_init_struct.Pull = GPIO_PULLUP; /* 上拉 */
gpio_init_struct.Speed = GPIO_SPEED_FREQ_HIGH; /* 高速 */
HAL_GPIO_Init(GPIOB, &gpio_init_struct);
}
}
主函数调换tim6和tim3初始化函数位置就可以了
int main(void)
{
HAL_Init(); /* 初始化HAL库 */
sys_stm32_clock_init(RCC_PLL_MUL9); /* 设置时钟, 72Mhz */
delay_init(72); /* 延时初始化 */
usart_init(115200); /* 串口初始化为115200 */
usmart_dev.init(72); /* 初始化USMART */
led_init(); /* 初始化LED */
rtc_init();
// tim_init(499,71);
tim_pwm_init(59999,23);
tim_init(499,71);
这是为什么啊
|
|