初级会员
- 积分
- 190
- 金钱
- 190
- 注册时间
- 2022-10-26
- 在线时间
- 127 小时
|
楼主 |
发表于 2023-4-17 22:04:17
|
显示全部楼层
void TIM3_PWM_Init(u16 alr,u16 slr) { GPIO_InitTypeDef GPIO_InitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure; TIM_OCInitTypeDef TIM_OCInitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO,ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE); GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3, ENABLE); GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5; GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; GPIO_Init(GPIOB,&GPIO_InitStructure); TIM_TimeBaseInitStructure.TIM_Period=alr; TIM_TimeBaseInitStructure.TIM_ClockDivision=0; TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up; TIM_TimeBaseInitStructure.TIM_Prescaler=slr; TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStructure); NVIC_InitStructure.NVIC_IRQChannel=TIM3_IRQn; NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0; NVIC_InitStructure.NVIC_IRQChannelSubPriority=1; NVIC_Init(& NVIC_InitStructure); TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM2; TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OC2Init(TIM3,&TIM_OCInitStructure); TIM_OC2PreloadConfig(TIM3,TIM_OCPreload_Enable); TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE); TIM_Cmd(TIM3,ENABLE); } void TIM3_IRQHandler(void) { if(TIM_GetITStatus(TIM3,TIM_IT_Update)==SET) { TIM_ClearITPendingBit(TIM3,TIM_IT_Update); LED1=!LED1; } } |
|