我的led灯接口为PD0,低电平时亮。想实现呼吸灯的效果,不知道哪里有问题,不起作用
int main(void)
{
TIM3_PWMConfig();
while(1);
}
void TIM3_PWMConfig(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
/* TIM3 IO configuration *************************************/
/* Enable GPIOD clock */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
/* Connect TIM3 pin (PD0) to AF2 */
GPIO_PinAFConfig(GPIOD, GPIO_PinSource0, GPIO_AF_TIM3);
/* Configure TIM3 CH1 pin (PC6) as alternate function */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
GPIO_Init(GPIOD, &GPIO_InitStructure);
/* TIM3 configuration */
/* Enable TIM3 clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = 255;
TIM_TimeBaseStructure.TIM_Prescaler = 1999;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
/* Configure CH1 in PWM1 Mode */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 0;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OC1Init(TIM3, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM3, ENABLE);
/* Enable TIM3 counter */
TIM_Cmd(TIM3, ENABLE);
TIM_ITConfig(TIM3,TIM_IT_Update, ENABLE);
NVIC_Config_PWM();
}
中断函数
void TIM3_IRQHandler(void)
{
static uint8_t pwm_index = 0; //用于PWM查表
static uint8_t period_cnt = 0; //用于计算周期数
if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET) //TIM_IT_Update
{
period_cnt++;
if(period_cnt >= 10) //若输出的周期数大于10,输出下一种脉冲宽的PWM波
{
TIM3->CCR3 = indexWave[pwm_index]; //根据PWM表修改定时器的比较寄存器值
pwm_index++; //标志PWM表的下一个元素
if( pwm_index >= 40) //若PWM脉冲表已经输出完成一遍,重置PWM查表标志
{
pwm_index=0;
}
period_cnt=0; //重置周期计数标志
}
TIM_ClearITPendingBit (TIM3, TIM_IT_Update); //必须要清除中断标志位
}
} |