#include "./BSP/TIMER/atim.h"
static uint16_t npwm_remain;
TIM_HandleTypeDef atim_timx_chy_oc_handle;
void atim_timx_chy_oc_init(uint16_t psc, uint16_t arr)
{
/* 时钟使能 */
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_TIM8_CLK_ENABLE();
/* TIM初始化 */
atim_timx_chy_oc_handle.Instance = TIM8;
atim_timx_chy_oc_handle.Init.Prescaler = psc;
atim_timx_chy_oc_handle.Init.Period = arr;
atim_timx_chy_oc_handle.Init.CounterMode = TIM_COUNTERMODE_UP;
atim_timx_chy_oc_handle.Init.RepetitionCounter = 5-1; /* 默认初始化为输出5个pwm波 */
atim_timx_chy_oc_handle.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE; /* 使能TIMx_ARR进行缓冲 */
HAL_TIM_PWM_Init(&atim_timx_chy_oc_handle);
/* GPIO引脚定义 */
GPIO_InitTypeDef gpio_init_struct;
gpio_init_struct.Pin = GPIO_PIN_6; /* LED0引脚 */
gpio_init_struct.Mode = GPIO_MODE_AF_PP; /* 推挽输出 */
gpio_init_struct.Pull = GPIO_PULLUP; /* 上拉 */
gpio_init_struct.Speed = GPIO_SPEED_FREQ_HIGH; /* 高速 */
gpio_init_struct.Alternate = GPIO_AF3_TIM8;
HAL_GPIO_Init(GPIOC, &gpio_init_struct); /* 初始化LED0引脚 */
// /* 从模式初始化 */
// TIM_SlaveConfigTypeDef timx_chy_slave_config = {0};
// timx_chy_slave_config.SlaveMode = TIM_SLAVEMODE_EXTERNAL1;
// timx_chy_slave_config.InputTrigger = TIM_TS_TI1FP1;
// timx_chy_slave_config.TriggerPolarity = TIM_TRIGGERPOLARITY_RISING;
// timx_chy_slave_config.TriggerPrescaler = TIM_TRIGGERPRESCALER_DIV1;
// timx_chy_slave_config.TriggerFilter = 0;
// HAL_TIM_SlaveConfigSynchro(&atim_timx_chy_oc_handle, &timx_chy_slave_config);
/* 输出比较产生PWM */
TIM_OC_InitTypeDef timx_chy_oc_config = {0};
timx_chy_oc_config.OCMode = TIM_OCMODE_PWM1;
timx_chy_oc_config.Pulse = arr/2;
timx_chy_oc_config.OCPolarity = TIM_OCPOLARITY_HIGH;
HAL_TIM_PWM_ConfigChannel(&atim_timx_chy_oc_handle,&timx_chy_oc_config,TIM_CHANNEL_1);
/* 使能定时中断并设置优先级,使能更新中断,使能通道 */
HAL_NVIC_SetPriority(TIM8_UP_TIM13_IRQn,1,3);
HAL_NVIC_EnableIRQ(TIM8_UP_TIM13_IRQn);
__HAL_TIM_ENABLE_IT(&atim_timx_chy_oc_handle,TIM_IT_UPDATE);
HAL_TIM_PWM_Start(&atim_timx_chy_oc_handle, TIM_CHANNEL_1);
}
void npwm_set(uint16_t npwm)
{
if(npwm == 0) return;
npwm_remain = npwm;
HAL_TIM_GenerateEvent(&atim_timx_chy_oc_handle, TIM_EVENTSOURCE_UPDATE); /* 产生一次更新事件,在中断里面处理脉冲输出 */
__HAL_TIM_ENABLE(&atim_timx_chy_oc_handle); /* 使能定时器TIMX */
}
void TIM8_UP_TIM13_IRQHandler(void)
{
if(__HAL_TIM_GET_FLAG(&atim_timx_chy_oc_handle,TIM_FLAG_UPDATE) != RESET)
{
if(npwm_remain)
{
TIM8->RCR = npwm_remain - 1;
HAL_TIM_GenerateEvent(&atim_timx_chy_oc_handle, TIM_EVENTSOURCE_UPDATE); /* 产生一次更新事件,在中断里面处理脉冲输出 */
__HAL_TIM_ENABLE(&atim_timx_chy_oc_handle); /* 使能定时器TIMX */
npwm_remain = 0;
}
else
{
TIM8->CR1 &= ~(1 << 0); /* 1左移0位,CEN置0,关闭定时器TIMX,使用HAL Disable会清除PWM通道信息,此处不用 */
}
}
__HAL_TIM_CLEAR_IT(&atim_timx_chy_oc_handle,TIM_IT_UPDATE); /* 清除定时器溢出中断标志位 */
}
#include "./SYSTEM/sys/sys.h"
#include "./SYSTEM/usart/usart.h"
#include "./SYSTEM/delay/delay.h"
#include "./BSP/LED/led.h"
#include "./BSP/KEY/key.h"
#include "./BSP/TIMER/atim.h"
int main(void)
{
uint8_t t;
uint8_t key;
HAL_Init(); /* 初始化HAL库 */
sys_stm32_clock_init(336, 8, 2, 7); /* 设置时钟,168Mhz */
delay_init(168); /* 延时初始化 */
usart_init(115200); /* 串口初始化为115200 */
led_init(); /* 初始化LED */
key_init(); /* 初始化按键 */
GPIO_InitTypeDef gpio_init_struct;
gpio_init_struct.Pin = GPIO_PIN_10; /* LED1引脚 */
gpio_init_struct.Mode = GPIO_MODE_INPUT; /* 推挽输出 */
gpio_init_struct.Pull = GPIO_PULLUP; /* 上拉 */
gpio_init_struct.Speed = GPIO_SPEED_FREQ_HIGH; /* 高速 */
HAL_GPIO_Init(GPIOF, &gpio_init_struct); /* 初始化LED1引脚 */
atim_timx_chy_oc_init(8400 - 1, 10000 - 1);
while (1)
{
key = key_scan(0);
if(key)
{
npwm_set(5);
}
t++;
if(t > 20)
{
t = 0;
LED0_TOGGLE();
}
delay_ms(10);
}
}
HAL_TIM_PWM_Start(&g_timx_pwm_chy_handle, TIM_CHANNEL_1);
能够把MOE位置1