学习开源项目CH32V003输出PWM驱动WS2812中,作者开发是TSSOP20,而我只有SOP8,使用重映射功能将PC2映射为TIM2,下载验证失败,PC2没有PWM输出,不知道问题出自哪里。
关键部分代码:
void TIM2_PWMOut_Init(u16 arr, u16 psc, u16 ccp)
{
GPIO_InitTypeDef GPIO_InitStructure = {0};
TIM_OCInitTypeDef TIM_OCInitStructure = {0};
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure = {0};
// 启用 GPIOD 和 TIM2 的时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD , ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
// 重映射 TIM2 到 GPIO
GPIO_PinRemapConfig(GPIO_PartialRemap2_TIM2, ENABLE);
// 配置 GPIO 引脚
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // 设为推挽复用输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD, &GPIO_InitStructure);
// 定时器基础设置
TIM_TimeBaseInitStructure.TIM_Period = arr; // 设置计数周期
TIM_TimeBaseInitStructure.TIM_Prescaler = psc; // 设置预分频
TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStructure);
// 输出比较配置
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = ccp; // 设置占空比
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC2Init(TIM2, &TIM_OCInitStructure);
// 启动定时器
TIM_Cmd(TIM2, ENABLE);
}
开源项目:WS2812.zip