|
TIM1中断入口的写法和在中断中更新占空数据的demo
我写过一个通过串口更改PWM周期和占空比的例子,修改了一下,有什么不正确的请指教。(main()函数自己添加) //1.用户函数定义 u16 DutyCycle;
//定义TIM1 Channel1输出口 void GPIO_Configutation(void) { GPIO_InitTypeDef GPIO_InitStructure;
/* GPIOA Configuration: Channel 1 Output */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); } //占空比 = (TIM1_Pulse = 0x800)/(TIM1_Period = 0xFFF + 1) = 0.5 void TIM1_Configuration(void) { TIM1_TimeBaseInitTypeDef TIM1_TimeBaseStructure; TIM1_OCInitTypeDef TIM1_OCInitStructure; TIM1_DeInit();
/* Time Base configuration */ TIM1_TimeBaseStructure.TIM1_Prescaler = 0x00; TIM1_TimeBaseStructure.TIM1_CounterMode = TIM1_CounterMode_Up; TIM1_TimeBaseStructure.TIM1_Period = 0xFFF; TIM1_TimeBaseStructure.TIM1_ClockDivision = 0x0; TIM1_TimeBaseStructure.TIM1_RepetitionCounter = 0x0; TIM1_TimeBaseInit(&TIM1_TimeBaseStructure);
/* Channel 1 Configuration in PWM mode */ TIM1_OCInitStructure.TIM1_OCMode = TIM1_OCMode_PWM2; TIM1_OCInitStructure.TIM1_OutputState = TIM1_OutputState_Enable; TIM1_OCInitStructure.TIM1_OutputNState = TIM1_OutputNState_Enable; TIM1_OCInitStructure.TIM1_Pulse = 0x800; TIM1_OCInitStructure.TIM1_OCPolarity = TIM1_OCPolarity_Low; TIM1_OCInitStructure.TIM1_OCNPolarity = TIM1_OCNPolarity_High; TIM1_OCInitStructure.TIM1_OCIdleState = TIM1_OCIdleState_Set; TIM1_OCInitStructure.TIM1_OCNIdleState = TIM1_OCIdleState_Reset; TIM1_OC1Init(&TIM1_OCInitStructure);
TIM1_ITConfig(TIM1_IT_Update,ENABLE);//TIM1更新中断使能(1)
/* TIM1 counter enable */ TIM1_Cmd(ENABLE);
/* TIM1 Main Output Enable */ TIM1_CtrlPWMOutputs(ENABLE);
}
//NVIC使能,开启TIM1更新中断,要配置两个地方(1)和(2) void NVIC_Configuration(void)//(2) { NVIC_InitTypeDef NVIC_InitStructure; /* Set the Vector Table base location at 0x08000000 */ NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x00);
/* Configure one bit for preemption priority */ NVIC_PriorityGroupConfig(NVIC_PriorityGroup_3);
/* TIM2 IRQ configuration Priority : 0 -------------------------------------*/ NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_IRQChannel; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); }
void RefreshDutyCycle(void) { if(TIM1_GetITStatus(TIM1_IT_Update) != RESET) { DutyCycle = ...;//在此处修改数据 TIM1->CCR1 = DutyCycle;//更新占空比 TIM1_ClearITPendingBit(TIM1_IT_Update)//清除中断标志位 } }
//2.stm32f10x_it.c修改如下 void TIM1_UP_IRQHandler(void) { RefreshDutyCycle(); } |
|