不知道干什麼我的TIM1用不了,同樣的設置TIM2,3,4都可以用,以下是我的code,請各位指點一下。 #define ENCODER_TIMER TIM1 // Encoder unit connected to TIM1 #define ENCODER_TIM_PERIOD (u16) 10000 // number of pulses per revolution #define TIMx_Channel TIM1_UP_IRQn//TIM4_IRQn //Timer channel #define TIMx_PRE_EMPTION_PRIORITY 0 #define TIMx_SUB_PRIORITY 1 /* Private macro -------------------------------------------------------------*/ /* Private variables ---------------------------------------------------------*/ int32_t tick; int32_t mag; /* Private function prototypes -----------------------------------------------*/ /* Private functions ---------------------------------------------------------*/ /******************************************************************************* * Function Name : ENC_Init * Description : General Purpose Timer x set-up for encoder speed/position * sensors * Input : None * Output : None * Return : None *******************************************************************************/ void timer1_Init(void) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; NVIC_InitTypeDef NVIC_InitStructure; /* TIM1 clock source enable */ RCC_APB1PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); #ifdef VECT_TAB_RAM NVIC_SetVectorTable(NVIC_VectTab_RAM,0x0); #else NVIC_SetVectorTable(NVIC_VectTab_FLASH,0x0); #endif NVIC_InitStructure.NVIC_IRQChannel = TIMx_Channel; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); /* Timer configuration in Encoder mode */ TIM_DeInit(ENCODER_TIMER); TIM_TimeBaseStructure.TIM_Prescaler = 719; // APB2=72M 72M/(71+1)=1M TIM_TimeBaseStructure.TIM_Period = ENCODER_TIM_PERIOD; //count 100 reset to 0 TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseStructure.TIM_RepetitionCounter = 0; TIM_TimeBaseInit(ENCODER_TIMER, &TIM_TimeBaseStructure); //TIM_ARRPreloadConfig(TIM1, ENABLE); //TIM_GenerateEvent(TIM1, TIM_EventSource_Update); // Clear all pending interrupts TIM_ClearFlag(ENCODER_TIMER, TIM_FLAG_Update); TIM_ClearITPendingBit(ENCODER_TIMER, TIM_IT_Update); //enable interrupt TIM_ITConfig(ENCODER_TIMER, TIM_IT_Update, ENABLE); //TIM_Cmd(TIM1, ENABLE); TIM_Cmd(ENCODER_TIMER, ENABLE); //TIM_CtrlPWMOutputs(TIM1, ENABLE); } /******************************************************************************/ /* STM32F10x Peripherals Interrupt Handlers */ /* Add here the Interrupt Handler for the used peripheral(s) (PPP), for the */ /* available peripheral interrupt handler's name please refer to the startup */ /* file (startup_stm32f10x_xx.s). */ /******************************************************************************/ /** * @brief This function handles TIM5 global interrupt request. * @param None * @retval None */ void TIM1_UP_IRQHandler(void) { tick=tick+1; mag=PID_data_update2(0); /* Clear the interrupt pending flag */ TIM_ClearFlag(ENCODER_TIMER, TIM_FLAG_Update); TIM_ClearITPendingBit(ENCODER_TIMER, TIM_IT_Update); } |
RE:STM32-F103 TIM1不能用
RE:STM32-F103 TIM1不能用
这里错误