void setup_dma_timer(void) { /* We use Timer 7 */ TIM_TypeDef* const timer = TIM7; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM7, ENABLE); TIM_DeInit(timer); TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure; TIM_TimeBaseStructInit(&TIM_TimeBaseInitStructure); /* Proc is 168MHz, so prescalar is 168 - 1 to make clock 1MHz */ TIM_TimeBaseInitStructure.TIM_Prescaler = 167; /* With above clock, each tick is 1 us. We want interrupts every 1ms */ TIM_TimeBaseInitStructure.TIM_Period = 1000; TIM_TimeBaseInit(timer, &TIM_TimeBaseInitStructure); TIM_UpdateDisableConfig(timer, DISABLE); TIM_UpdateRequestConfig(timer, TIM_UpdateSource_Global); NVIC_InitTypeDef NVIC_InitStructure; NVIC_InitStructure.NVIC_IRQChannel = TIM7_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = DMA_IRQ_PRIORITY; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); TIM_ITConfig(timer, TIM_IT_Update, ENABLE); TIM_Cmd(timer, ENABLE); }
//定时器触发的ADC配置 void adc_tim_trig_config(uint32_t period, uint32_t prescaler) { ADC_CommonInitTypeDef ADC_CommonInitStructure; ADC_InitTypeDef ADC_InitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure; RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); gpio_an_no_init(GPIOC, GPIO_Pin_5); ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent; //独立模式 ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles; //两个采样阶段之间的延迟5个时钟 ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled; //DMA失能 ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div4; //ADCCLK=PCLK2/4=84/4=21Mhz ADC_CommonInit(&ADC_CommonInitStructure); ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; //12位模式 ADC_InitStructure.ADC_ScanConvMode = DISABLE; //关闭扫描模式 ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; //关闭连续转换 ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_Rising; //外部触发上升沿 ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T2_CC2; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //右对齐 ADC_InitStructure.ADC_NbrOfConversion = 1; //规则序列中有1个转换 ADC_Init(ADC1, &ADC_InitStructure); ADC_RegularChannelConfig(ADC1, ADC_Channel_15, 1, ADC_SampleTime_84Cycles ); //设置通道5采样顺序为1, 采样时间为84个周期 ADC_ITConfig(ADC1, ADC_IT_EOC, ENABLE); ADC_Cmd(ADC1, ENABLE); nvic_config(ADC_IRQn, 2); RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); TIM_TimeBaseStructure.TIM_Period = period-1; TIM_TimeBaseStructure.TIM_Prescaler = prescaler-1; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; TIM_OCInitStructure.TIM_Pulse = period/2; TIM_OC2Init(TIM2, &TIM_OCInitStructure); TIM_Cmd(TIM2, ENABLE); TIM_InternalClockConfig(TIM2); TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Enable); TIM_UpdateDisableConfig(TIM2, DISABLE); }
/** * @brief miscTIM_run config miscTIM to run with msec interrupt periodically. * @param TIM_Typedef* miscTIM, can be only basic timer (TM6 and TM7) * @param uint16_t prescaler * @param uint16_t reloadVal * @return void */ void miscTIM_run (TIM_TypeDef* miscTIM, uint16_t prescaler, uint16_t reloadVal){ /**< declare vars */ bool isTIM6; uint32_t PCLK1, prescaler_x_reloadVal; RCC_ClocksTypeDef clocksStruct; NVIC_InitTypeDef nvicStruct; /**< check condition */ if ( miscTIM == TIM6) isTIM6 = true; else if (miscTIM == TIM7) isTIM6 = false; else return; /**< init miscTIM in update mode with prescaler and reloadVal */ RCC_APB1PeriphClockCmd ( (isTIM6 ? RCC_APB1Periph_TIM6 : RCC_APB1Periph_TIM7), ENABLE ); TIM_ARRPreloadConfig (miscTIM, ENABLE); TIM_ITConfig (miscTIM, TIM_IT_Update, ENABLE); TIM_PrescalerConfig (miscTIM, prescaler, TIM_PSCReloadMode_Immediate); TIM_SetAutoreload (miscTIM, reloadVal); TIM_UpdateDisableConfig (miscTIM, DISABLE); TIM_UpdateRequestConfig (miscTIM, TIM_UpdateSource_Global); TIM_SelectOnePulseMode (miscTIM, TIM_OPMode_Repetitive); TIM_Cmd (miscTIM, ENABLE); /**< init NVIC TIM6 channel */ nvicStruct.NVIC_IRQChannel = TIM6_IRQn; nvicStruct.NVIC_IRQChannelCmd = ENABLE; nvicStruct.NVIC_IRQChannelPreemptionPriority = 0x0F; nvicStruct.NVIC_IRQChannelSubPriority = 0x0F; NVIC_Init (&nvicStruct); /**< calculate miscTIM_period */ RCC_GetClocksFreq (&clocksStruct); PCLK1 = clocksStruct.PCLK1_Frequency; PCLK1 /= 1000; prescaler_x_reloadVal = prescaler*reloadVal; miscTIM_period = prescaler_x_reloadVal / PCLK1; return; }
/************************************************************* * TIM4 Initialization **************************************************************/ void TIM4_Init() { #if (STRCMP($tim4IntEn$, 0) == 0) NVIC_InitTypeDef NVIC_InitStructure; #endif #if (STRCMP($ch1_0Pin$, DISABLE) == 0 || STRCMP($ch1_1Pin$, DISABLE) == 0 || \ STRCMP($ch2_0Pin$, DISABLE) == 0 || STRCMP($ch2_1Pin$, DISABLE) == 0 || \ STRCMP($ch3_0Pin$, DISABLE) == 0 || STRCMP($ch3_1Pin$, DISABLE) == 0 || \ STRCMP($ch4_0Pin$, DISABLE) == 0 || STRCMP($ch4_1Pin$, DISABLE) == 0) GPIO_InitTypeDef GPIO_InitStructure; #endif TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; #if ( (STRCMP($ch1En$, DISABLE) == 0 && STRCMP($ch1sel$, CAPTURE_ENABLE) == 0) || \ (STRCMP($ch2En$, DISABLE) == 0 && STRCMP($ch2sel$, CAPTURE_ENABLE) == 0) || \ (STRCMP($ch3En$, DISABLE) == 0 && STRCMP($ch3sel$, CAPTURE_ENABLE) == 0) || \ (STRCMP($ch4En$, DISABLE) == 0 && STRCMP($ch4sel$, CAPTURE_ENABLE) == 0) ) TIM_OCInitTypeDef TIM_OCInitStructure; #endif #if ( (STRCMP($ch1En$, DISABLE) == 0 && STRCMP($ch1sel$, COMPARE_ENABLE) == 0) || \ (STRCMP($ch2En$, DISABLE) == 0 && STRCMP($ch2sel$, COMPARE_ENABLE) == 0) || \ (STRCMP($ch3En$, DISABLE) == 0 && STRCMP($ch3sel$, COMPARE_ENABLE) == 0) || \ (STRCMP($ch4En$, DISABLE) == 0 && STRCMP($ch4sel$, COMPARE_ENABLE) == 0) ) TIM_ICInitTypeDef TIM_ICInitStructure; #endif //PUT_A_NEW_LINE_HERE // // Enable TIM4 clock // RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); #if (STRCMP($tim4PinRemap$, DEFAULT) == 0 ) //PUT_A_NEW_LINE_HERE // // Enable TIM4's GPIOD, GPIOE, AFIO clock // RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE | RCC_APB2Periph_AFIO, ENABLE); #else //PUT_A_NEW_LINE_HERE // // Enable TIM4's GPIOB , GPIOE, AFIO clock // RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOE | RCC_APB2Periph_AFIO, ENABLE); #endif #if (STRCMP($tim4PinRemap$, DEFAULT) == 1) #if (STRCMP($ch1_0Pin$, DISABLE) == 0 || STRCMP($ch2_0Pin$, DISABLE) == 0 || STRCMP($ch3_0Pin$, DISABLE) == 0 || STRCMP($ch4_0Pin$, DISABLE) == 0) // // Configure TIM4 pins: ETR, CH1, CH2, CH3, CH4, when Pin Remap is Default // #endif #if (STRCMP($etrPin$, DISABLE) == 0) GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOE, &GPIO_InitStructure); #endif #if (STRCMP($ch1_0Pin$, DISABLE) == 0) //PUT_A_NEW_LINE_HERE GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; #if (STRCMP($ch1sel$, CAPTURE_ENABLE) == 1) GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; #else GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; #endif GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); #endif #if (STRCMP($ch2_0Pin$, DISABLE) == 0) //PUT_A_NEW_LINE_HERE GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; #if (STRCMP($ch2sel$, CAPTURE_ENABLE) == 1) GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; #else GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; #endif GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); #endif #if (STRCMP($ch3_0Pin$, DISABLE) == 0) //PUT_A_NEW_LINE_HERE GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; #if (STRCMP($ch3sel$, CAPTURE_ENABLE) == 1) GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; #else GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; #endif GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); #endif #if (STRCMP($ch4_0Pin$, DISABLE) == 0) //PUT_A_NEW_LINE_HERE GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; #if (STRCMP($ch4sel$, CAPTURE_ENABLE) == 1) GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; #else GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; #endif GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); #endif #endif #if (STRCMP($tim4PinRemap$, GPIO_Remap_TIM4) == 1) // // Configure TIM4 pins: ETR, CH1, CH2, CH3, CH4, when Pin Remap is PartialRemap1 // GPIO_PinRemapConfig(GPIO_Remap_TIM4, ENABLE); #if (STRCMP($etrPin$, DISABLE) == 0) //PUT_A_NEW_LINE_HERE GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOE, &GPIO_InitStructure); #endif #if (STRCMP($ch1_1Pin$, DISABLE) == 0) //PUT_A_NEW_LINE_HERE GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; #if (STRCMP($ch1sel$, CAPTURE_ENABLE) == 1) GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; #else GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; #endif GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOD, &GPIO_InitStructure); #endif #if (STRCMP($ch2_1Pin$, DISABLE) == 0) //PUT_A_NEW_LINE_HERE GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13; #if (STRCMP($ch2sel$, CAPTURE_ENABLE) == 1) GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; #else GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; #endif GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOD, &GPIO_InitStructure); #endif #if (STRCMP($ch3_0Pin$, DISABLE) == 0) //PUT_A_NEW_LINE_HERE GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14; #if (STRCMP($ch3sel$, CAPTURE_ENABLE) == 1) GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; #else GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; #endif GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOD, &GPIO_InitStructure); #endif #if (STRCMP($ch4_0Pin$, DISABLE) == 0) //PUT_A_NEW_LINE_HERE GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15; #if (STRCMP($ch4sel$, CAPTURE_ENABLE) == 1) GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; #else GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; #endif GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOD, &GPIO_InitStructure); #endif #endif //PUT_A_NEW_LINE_HERE // // Initializes the TIM4 Time Base Unit // TIM_TimeBaseStructure.TIM_Period = $tim4Period$; TIM_TimeBaseStructure.TIM_Prescaler = $tim4Psclr$; TIM_TimeBaseStructure.TIM_ClockDivision = $tim4ClkDiv$; TIM_TimeBaseStructure.TIM_CounterMode = $tim4CntMode$; TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); #if (STRCMP($tim4ARRMode$, DISABLE) == 0) //PUT_A_NEW_LINE_HERE // // Enables Preload register on ARR // TIM_ARRPreloadConfig(TIM3, ENABLE); #endif #if (STRCMP($ch1En$, DISABLE) == 0) #if (STRCMP($ch1sel$, CAPTURE_ENABLE) == 0) //PUT_A_NEW_LINE_HERE // // Initializes Output Compare Channel1 of the TIM4 // TIM_OCInitStructure.TIM_OCMode = $oc1Mode$; TIM_OCInitStructure.TIM_OutputState = $oc1State$; TIM_OCInitStructure.TIM_Pulse = $oc1Pluse$; TIM_OCInitStructure.TIM_OCPolarity = $oc1Polar$; TIM_OC1Init(TIM4, &TIM_OCInitStructure); #if (STRCMP($oc1Preload$, DISABLE) == 0) //PUT_A_NEW_LINE_HERE // // Enables Preload register on CCR1 // TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable); #endif #if (STRCMP($oc1Preload$, DISABLE) == 0) //PUT_A_NEW_LINE_HERE // // Clears or safeguards the OCREF1 signal on an external event // TIM_ClearOC1Ref(TIM4, TIM_OCClear_Enable); #endif #if ( STRCMP($oc1FastEn$, DISABLE) == 0 && (STRCMP($oc1Mode$, TIM_OCMode_PWM1) == 1 || STRCMP($oc1Mode$, TIM_OCMode_PWM2) == 1) ) //PUT_A_NEW_LINE_HERE // // Configures the TIM4 Output Compare Channel1 Fast feature // TIM_OC1FastConfig(TIM4, TIM_OCClear_Enable); #endif #endif #if (STRCMP($ch1sel$, COMPARE_ENABLE) == 0) //PUT_A_NEW_LINE_HERE // // Initializes Input Capture Channel1 of the TIM4 // TIM_ICInitStructure.TIM_Channel = TIM_Channel_1; TIM_ICInitStructure.TIM_ICPolarity = $ic1Polar$; TIM_ICInitStructure.TIM_ICSelection = $ic1Sel$; TIM_ICInitStructure.TIM_ICPrescaler = $ic1Psc$; TIM_ICInitStructure.TIM_ICFilter = $ic1Filter$; TIM_ICInit(TIM4, &TIM_ICInitStructure); #endif #endif #if (STRCMP($ch2En$, DISABLE) == 0) #if (STRCMP($ch2sel$, CAPTURE_ENABLE) == 0) //PUT_A_NEW_LINE_HERE // // Initializes Output Compare Channel2 of the TIM4 // TIM_OCInitStructure.TIM_OCMode = $oc2Mode$; TIM_OCInitStructure.TIM_OutputState = $oc2State$; TIM_OCInitStructure.TIM_Pulse = $oc2Pluse$; TIM_OCInitStructure.TIM_OCPolarity = $oc2Polar$; TIM_OC2Init(TIM4, &TIM_OCInitStructure); #if (STRCMP($oc2Preload$, DISABLE) == 0) //PUT_A_NEW_LINE_HERE // // Enables Preload register on CCR2 // TIM_OC2PreloadConfig(TIM4, TIM_OCPreload_Enable); #endif #if (STRCMP($oc2Preload$, DISABLE) == 0) //PUT_A_NEW_LINE_HERE // // Clears or safeguards the OCREF2 signal on an external event // TIM_ClearOC2Ref(TIM4, TIM_OCClear_Enable); #endif #if ( STRCMP($oc2FastEn$, DISABLE) == 0 && (STRCMP($oc2Mode$, TIM_OCMode_PWM1) == 1 || STRCMP($oc2Mode$, TIM_OCMode_PWM2) == 1) ) //PUT_A_NEW_LINE_HERE // // Configures the TIM4 Output Compare Channel2 Fast feature // TIM_OC2FastConfig(TIM4, TIM_OCClear_Enable); #endif #endif #if (STRCMP($ch2sel$, COMPARE_ENABLE) == 0) //PUT_A_NEW_LINE_HERE // // Initializes Input Capture Channel2 of the TIM4 // TIM_ICInitStructure.TIM_Channel = TIM_Channel_2; TIM_ICInitStructure.TIM_ICPolarity = $ic2Polar$; TIM_ICInitStructure.TIM_ICSelection = $ic2Sel$; TIM_ICInitStructure.TIM_ICPrescaler = $ic2Psc$; TIM_ICInitStructure.TIM_ICFilter = $ic2Filter$; TIM_ICInit(TIM4, &TIM_ICInitStructure); #endif #endif #if (STRCMP($ch3En$, DISABLE) == 0) #if (STRCMP($ch3sel$, CAPTURE_ENABLE) == 0) //PUT_A_NEW_LINE_HERE // // Initializes Output Compare Channel3 of the TIM4 // TIM_OCInitStructure.TIM_OCMode = $oc3Mode$; TIM_OCInitStructure.TIM_OutputState = $oc3State$; TIM_OCInitStructure.TIM_Pulse = $oc3Pluse$; TIM_OCInitStructure.TIM_OCPolarity = $oc3Polar$; TIM_OC3Init(TIM4, &TIM_OCInitStructure); #if (STRCMP($oc3Preload$, DISABLE) == 0) //PUT_A_NEW_LINE_HERE // // Enables Preload register on CCR3 // TIM_OC3PreloadConfig(TIM4, TIM_OCPreload_Enable); #endif #if (STRCMP($oc3Preload$, DISABLE) == 0) //PUT_A_NEW_LINE_HERE // // Clears or safeguards the OCREF3 signal on an external event // TIM_ClearOC3Ref(TIM4, TIM_OCClear_Enable); #endif #if ( STRCMP($oc3FastEn$, DISABLE) == 0 && (STRCMP($oc3Mode$, TIM_OCMode_PWM1) == 1 || STRCMP($oc3Mode$, TIM_OCMode_PWM2) == 1) ) //PUT_A_NEW_LINE_HERE // // Configures the TIM4 Output Compare Channel3 Fast feature // TIM_OC3FastConfig(TIM4, TIM_OCClear_Enable); #endif #endif #if (STRCMP($ch3sel$, COMPARE_ENABLE) == 0) //PUT_A_NEW_LINE_HERE // // Initializes Input Capture Channel3 of the TIM4 // TIM_ICInitStructure.TIM_Channel = TIM_Channel_3; TIM_ICInitStructure.TIM_ICPolarity = $ic3Polar$; TIM_ICInitStructure.TIM_ICSelection = $ic3Sel$; TIM_ICInitStructure.TIM_ICPrescaler = $ic3Psc$; TIM_ICInitStructure.TIM_ICFilter = $ic3Filter$; TIM_ICInit(TIM4, &TIM_ICInitStructure); #endif #endif #if (STRCMP($ch4En$, DISABLE) == 0) #if (STRCMP($ch4sel$, CAPTURE_ENABLE) == 0) //PUT_A_NEW_LINE_HERE // // Initializes Output Compare Channel4 of the TIM4 // TIM_OCInitStructure.TIM_OCMode = $oc4Mode$; TIM_OCInitStructure.TIM_OutputState = $oc4State$; TIM_OCInitStructure.TIM_Pulse = $oc4Pluse$; TIM_OCInitStructure.TIM_OCPolarity = $oc4Polar$; TIM_OC4Init(TIM4, &TIM_OCInitStructure); #if (STRCMP($oc4Preload$, DISABLE) == 0) //PUT_A_NEW_LINE_HERE // // Enables Preload register on CCR4 // TIM_OC4PreloadConfig(TIM4, TIM_OCPreload_Enable); #endif #if (STRCMP($oc4Preload$, DISABLE) == 0) //PUT_A_NEW_LINE_HERE // // Clears or safeguards the OCREF4 signal on an external event // TIM_ClearOC4Ref(TIM4, TIM_OCClear_Enable); #endif #if ( STRCMP($oc4FastEn$, DISABLE) == 0 && (STRCMP($oc4Mode$, TIM_OCMode_PWM1) == 1 || STRCMP($oc4Mode$, TIM_OCMode_PWM2) == 1) ) //PUT_A_NEW_LINE_HERE // // Configures the TIM4 Output Compare Channel4 Fast feature // TIM_OC4FastConfig(TIM4, TIM_OCClear_Enable); #endif #endif #if (STRCMP($ch4sel$, COMPARE_ENABLE) == 0) //PUT_A_NEW_LINE_HERE // // Initializes Input Capture Channel4 of the TIM4 // TIM_ICInitStructure.TIM_Channel = TIM_Channel_4; TIM_ICInitStructure.TIM_ICPolarity = $ic4Polar$; TIM_ICInitStructure.TIM_ICSelection = $ic4Sel$; TIM_ICInitStructure.TIM_ICPrescaler = $ic4Psc$; TIM_ICInitStructure.TIM_ICFilter = $ic4Filter$; TIM_ICInit(TIM4, &TIM_ICInitStructure); #endif #endif //PUT_A_NEW_LINE_HERE // // Enables TIM4 peripheral // TIM_Cmd(TIM4, ENABLE); #if (STRCMP($masterEn$, DISABLE) == 0 && STRCMP($masterTRGOSrc$, None) == 0) //PUT_A_NEW_LINE_HERE // // Selects the TIM4 Trigger Output Mode // TIM_SelectOutputTrigger(TIM4, $masterTRGOSrc$); #endif #if (STRCMP($slaveEn$, DISABLE) == 0 ) //PUT_A_NEW_LINE_HERE // // Sets the TIM4 Master/Slave Mode // TIM_SelectMasterSlaveMode(TIM4, TIM_MasterSlaveMode_Enable); #endif #if (STRCMP($slaveEn$, DISABLE) == 0 && STRCMP($slaveModeSet$, Disable(Internal Clock)) == 0) //PUT_A_NEW_LINE_HERE // // Selects the TIM4 Slave Mode // TIM_SelectSlaveMode(TIM4, $slaveModeSet$); #endif #if (STRCMP($onePulseEn$, TIM_OPMode_Repetitive) == 0 ) //PUT_A_NEW_LINE_HERE // // Selects the TIM4's One Pulse Mode // TIM_SelectOnePulseMode(TIM4, TIM_OPMode_Single); #endif #if (STRCMP($hallSensorEn$, DISABLE) == 0 ) //PUT_A_NEW_LINE_HERE // // Enables the TIM4's Hall sensor interface // TIM_SelectHallSensor(TIM4, ENABLE); #endif #if (STRCMP($encoderEn$, DISABLE) == 0 ) //PUT_A_NEW_LINE_HERE // // Configures the TIM4 Encoder Interface // TIM_EncoderInterfaceConfig(TIM4, $extiCh1OutputPin$, $EncodeIC1Polar$, $EncodeIC2Polar$); #endif #if (STRCMP($interClkEn$, ENABLE) == 0 ) #if (STRCMP($interTrigr$, DISABLE) == 0 ) //PUT_A_NEW_LINE_HERE // // Configures the TIM4 Internal Trigger as External Clock // TIM_ITRxExternalClockConfig(TIM4, $interTrigr$); #endif #if (STRCMP($timxTrigr$, DISABLE) == 0 ) //PUT_A_NEW_LINE_HERE // // Configures the TIM4 Trigger as External Clock // TIM_TIxExternalClockConfig(TIM4, $timxTrigr$, TIM_ICPolarity_Rising, 0x0); #endif #if (STRCMP($extTrigr$, DISABLE) == 0 ) #if (STRCMP($extTrigr$, ExternalTriggerMode1) == 1 ) //PUT_A_NEW_LINE_HERE // // Configures the External clock Mode1 // TIM_ETRClockMode1Config(TIM4, TIM_ExtTRGPSC_OFF, TIM_ExtTRGPolarity_Inverted, 0x0); #endif #if (STRCMP($extTrigr$, ExternalTriggerMode2) == 1 ) //PUT_A_NEW_LINE_HERE // // Configures the External clock Mode2 // TIM_ETRClockMode2Config(TIM4, TIM_ExtTRGPSC_OFF, TIM_ExtTRGPolarity_Inverted, 0x0); #endif #endif #endif #if (STRCMP($evtUpdate$, DISABLE) == 0 ) //PUT_A_NEW_LINE_HERE // // Enables the TIM4 Update event // TIM_UpdateRequestConfig(TIM4, $updateRqstSrc$); TIM_UpdateDisableConfig(TIM4, DISABLE); #endif #if (STRCMP($tim4IntEn$, 0) == 0 ) //PUT_A_NEW_LINE_HERE // // Enables TIM4 interrupts // TIM_ITConfig(TIM4, $tim4IntEn$, ENABLE); NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); #endif #if (STRCMP($tim4dmaEn$, DISABLE) == 0 ) //PUT_A_NEW_LINE_HERE // // Enables the TIM4's DMA Requests // TIM_DMAConfig(TIM4, $dmaBaseAddr$, $dmaBurstLen$); #if (STRCMP($tim4dmaRqstSrc$, 0) == 0 ) TIM_DMACmd(TIM4, $tim4dmaRqstSrc$, ENABLE); #endif TIM_SelectCCDMA(TIM4, ENABLE); #endif }
/****************************************************************************** * TIM1 Initialization Code Template ******************************************************************************/ void TIM1_Init() { TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct; #if (((STRCMP($ch1CCSel$, CH1_Cap_Func) == 1) && (STRCMP($ch1CCEnable$, ENABLE) == 1)) || \ ((STRCMP($ch2CCSel$, CH2_Cap_Func) == 1) && (STRCMP($ch2CCEnable$, ENABLE) == 1)) || \ ((STRCMP($ch3CCSel$, CH3_Cap_Func) == 1) && (STRCMP($ch3CCEnable$, ENABLE) == 1)) || \ ((STRCMP($ch4CCSel$, CH4_Cap_Func) == 1) && (STRCMP($ch4CCEnable$, ENABLE) == 1))) TIM_ICInitTypeDef TIM_ICInitStruct; #endif #if (((STRCMP($ch1CCSel$, CH1_Comp_Func) == 1) && (STRCMP($ch1CCEnable$, ENABLE) == 1)) || \ ((STRCMP($ch2CCSel$, CH2_Comp_Func) == 1) && (STRCMP($ch2CCEnable$, ENABLE) == 1)) || \ ((STRCMP($ch3CCSel$, CH3_Comp_Func) == 1) && (STRCMP($ch3CCEnable$, ENABLE) == 1)) || \ ((STRCMP($ch4CCSel$, CH4_Comp_Func) == 1) && (STRCMP($ch4CCEnable$, ENABLE) == 1))) TIM_OCInitTypeDef TIM_OCInitStruct; #endif #if(STRCMP($brkInt$ ,ENABLE) == 1 || STRCMP($upInt$ ,ENABLE) == 1 || STRCMP($tcInt$ ,0) == 0 || STRCMP($ccInt$ ,0) == 0) NVIC_InitTypeDef NVIC_InitStructure; #endif //PUT_A_NEW_LINE_HERE // // Enable peripheral clock of TIM1 // RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); //PUT_A_NEW_LINE_HERE // // Time base configuration // TIM_TimeBaseStructInit(&TIM_TimeBaseInitStruct); TIM_TimeBaseInitStruct.TIM_Period = $counterPeriod$; TIM_TimeBaseInitStruct.TIM_Prescaler = $prescaler$; TIM_TimeBaseInitStruct.TIM_ClockDivision = $clockDivision$; TIM_TimeBaseInitStruct.TIM_CounterMode = $counterMode$; TIM_TimeBaseInitStruct.TIM_RepetitionCounter = $repetition$; TIM_TimeBaseInit(TIM1, &TIM_TimeBaseInitStruct); #if (STRCMP($ch1CCEnable$, ENABLE) == 1) //PUT_A_NEW_LINE_HERE #if (STRCMP($ch1CCSel$, CH1_Cap_Func) == 1) // // TIM1 Channel 1 Input Configuration // TIM_ICStructInit(&TIM_ICInitStruct); TIM_ICInitStruct.TIM_Channel = TIM_Channel_1; TIM_ICInitStruct.TIM_ICPolarity = $ch1ICPol$; TIM_ICInitStruct.TIM_ICSelection = $ch1ICSel$; TIM_ICInitStruct.TIM_ICPrescaler = $ch1ICPSC$; TIM_ICInitStruct.TIM_ICFilter = $ch1ICFilter$; TIM_ICInit(TIM1, &TIM_ICInitStruct); #endif #if (STRCMP($ch1CCSel$, CH1_Comp_Func) ==1) // // TIM1 Channel 1 Output configuration // TIM_OCStructInit(&TIM_OCInitStruct); TIM_OCInitStruct.TIM_OCMode = $ch1OutputMode$; TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStruct.TIM_Pulse = $ch1PsValue$; TIM_OCInitStruct.TIM_OCPolarity = $ch1OCPol$; TIM_OCInitStruct.TIM_OCIdleState = $ch1IdleState$; TIM_OC1Init(TIM1, &TIM_OCInitStruct); TIM_ForcedOC1Config(TIM1, $ch1ForceAction$); #if (STRCMP($ch1CCRPreload$, ENABLE) == 1) TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable); #endif #if (STRCMP($ch1OREFClear$, ENABLE) == 1) TIM_ClearOC1Ref(TIM1, TIM_OCClear_Enable); #endif #endif #endif #if (STRCMP($ch2CCEnable$, ENABLE) == 1) //PUT_A_NEW_LINE_HERE #if (STRCMP($ch2CCSel$, CH2_Cap_Func) == 1) // // TIM1 Channel 2 Input Configuration // TIM_ICStructInit(&TIM_ICInitStruct); TIM_ICInitStruct.TIM_Channel = TIM_Channel_2; TIM_ICInitStruct.TIM_ICPolarity = $ch2ICPol$; TIM_ICInitStruct.TIM_ICSelection = $ch2ICSel$; TIM_ICInitStruct.TIM_ICPrescaler = $ch2ICPSC$; TIM_ICInitStruct.TIM_ICFilter = $ch2ICFilter$; TIM_ICInit(TIM1, &TIM_ICInitStruct); #endif #if (STRCMP($ch2CCSel$, CH2_Comp_Func) ==1) // // TIM1 Channel 2 Output configuration // TIM_OCStructInit(&TIM_OCInitStruct); TIM_OCInitStruct.TIM_OCMode = $ch2OutputMode$; TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStruct.TIM_Pulse = $ch2PsValue$; TIM_OCInitStruct.TIM_OCPolarity = $ch2OCPol$; TIM_OCInitStruct.TIM_OCIdleState = $ch2IdleState$; TIM_OC2Init(TIM1, &TIM_OCInitStruct); TIM_ForcedOC2Config(TIM1, $ch2ForceAction$); #if (STRCMP($ch2CCRPreload$, ENABLE) == 1) TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable); #endif #if (STRCMP($ch2OREFClear$, ENABLE) == 1) TIM_ClearOC2Ref(TIM1, TIM_OCClear_Enable); #endif #endif #endif #if (STRCMP($ch3CCEnable$, ENABLE) == 1) //PUT_A_NEW_LINE_HERE #if (STRCMP($ch3CCSel$, CH3_Cap_Func) == 1) // // TIM1 Channel 3 Input Configuration // TIM_ICStructInit(&TIM_ICInitStruct); TIM_ICInitStruct.TIM_Channel = TIM_Channel_3; TIM_ICInitStruct.TIM_ICPolarity = $ch3ICPol$; TIM_ICInitStruct.TIM_ICSelection = $ch3ICSel$; TIM_ICInitStruct.TIM_ICPrescaler = $ch3ICPSC$; TIM_ICInitStruct.TIM_ICFilter = $ch3ICFilter$; TIM_ICInit(TIM1, &TIM_ICInitStruct); #endif #if (STRCMP($ch3CCSel$, CH3_Comp_Func) ==1) // // TIM1 Channel 3 Output configuration // TIM_OCStructInit(&TIM_OCInitStruct); TIM_OCInitStruct.TIM_OCMode = $ch3OutputMode$; TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStruct.TIM_Pulse = $ch3PsValue$; TIM_OCInitStruct.TIM_OCPolarity = $ch3OCPol$; TIM_OCInitStruct.TIM_OCIdleState = $ch3IdleState$; TIM_OC3Init(TIM1, &TIM_OCInitStruct); TIM_ForcedOC3Config(TIM1, $ch3ForceAction$); #if (STRCMP($ch3CCRPreload$, ENABLE) == 1) TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable); #endif #if (STRCMP($ch3OREFClear$, ENABLE) == 1) TIM_ClearOC3Ref(TIM1, TIM_OCClear_Enable); #endif #endif #endif #if (STRCMP($ch4CCEnable$, ENABLE) == 1) //PUT_A_NEW_LINE_HERE #if (STRCMP($ch4CCSel$, CH4_Cap_Func) == 1) // // TIM1 Channel 4 Input Configuration // TIM_ICStructInit(&TIM_ICInitStruct); TIM_ICInitStruct.TIM_Channel = TIM_Channel_4; TIM_ICInitStruct.TIM_ICPolarity = $ch4ICPol$; TIM_ICInitStruct.TIM_ICSelection = $ch4ICSel$; TIM_ICInitStruct.TIM_ICPrescaler = $ch4ICPSC$; TIM_ICInitStruct.TIM_ICFilter = $ch4ICFilter$; TIM_ICInit(TIM1, &TIM_ICInitStruct); #endif #if (STRCMP($ch4CCSel$, CH4_Comp_Func) ==1) // // TIM1 Channel 4 Output configuration // TIM_OCStructInit(&TIM_OCInitStruct); TIM_OCInitStruct.TIM_OCMode = $ch4OutputMode$; TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStruct.TIM_Pulse = $ch4PsValue$; TIM_OCInitStruct.TIM_OCPolarity = $ch4OCPol$; TIM_OCInitStruct.TIM_OCIdleState = $ch4IdleState$; TIM_OC4Init(TIM1, &TIM_OCInitStruct); TIM_ForcedOC4Config(TIM1, $ch4ForceAction$); #if (STRCMP($ch4CCRPreload$, ENABLE) == 1) TIM_OC4PreloadConfig(TIM1, TIM_OCPreload_Enable); #endif #if (STRCMP($ch4OREFClear$, ENABLE) == 1) TIM_ClearOC4Ref(TIM1, TIM_OCClear_Enable); #endif #endif #endif #if (STRCMP($upEVDis$, ENABLE) == 1) //PUT_A_NEW_LINE_HERE // // Enable the Update event // TIM_UpdateDisableConfig(TIM1, DISABLE); TIM_UpdateRequestConfig(TIM1, $requestIntSrc$); #endif #if ((STRCMP($extTriEnable$, ENABLE) == 1) && (STRCMP($extClkEnable$, ENABLE) == 1)) #if (STRCMP($extClkMode$, MODE1) == 1) //PUT_A_NEW_LINE_HERE // // ETR clock Mode1 Config // TIM_ETRClockMode1Config(TIM1, $exTriPSCSet$, $exTriPolSet$, $exTriFilterSet$); #endif #if (STRCMP($extClkMode$, MODE2) == 1) //PUT_A_NEW_LINE_HERE // // ETR clock Mode2 Config // TIM_ETRClockMode2Config(TIM1, $exTriPSCSet$, $exTriPolSet$, $exTriFilterSet$); #endif #endif #if ((STRCMP($intTriEnable$, ENABLE) == 1) && (STRCMP($extClkEnable$, ENABLE) == 1)) //PUT_A_NEW_LINE_HERE // // ITR clock Config // TIM_ITRxExternalClockConfig(TIM1, $intTriSrc$); #endif #if (STRCMP($mmEnable$, ENABLE) == 1) //PUT_A_NEW_LINE_HERE // // Master Mode selection // TIM_SelectMasterSlaveMode(TIM1, TIM_MasterSlaveMode_Enable); #endif #if (STRCMP($smEnable$, ENABLE) == 1) //PUT_A_NEW_LINE_HERE // // Slave Mode selection // TIM_SelectSlaveMode(TIM1, $smSel$); #endif #if (STRCMP($hsiEnable$, ENABLE) == 1) //PUT_A_NEW_LINE_HERE // // Enable Hall Sensor Interface // TIM_SelectHallSensor(TIM1, ENABLE); #endif #if (STRCMP($spmEnable$, ENABLE) == 1) //PUT_A_NEW_LINE_HERE // // Single Pulse Mode selection // TIM_SelectOnePulseMode(TIM1, $spmSet$); #endif #if (STRCMP($tomEnable$, ENABLE) == 1) //PUT_A_NEW_LINE_HERE // // Trigger Output Mode selection // TIM_SelectOutputTrigger(TIM1, $tomSrc$); #endif #if (STRCMP($eimEnable$, ENABLE) == 1) //PUT_A_NEW_LINE_HERE // // Encoder Interface Mode selection // TIM_EncoderInterfaceConfig(TIM1, $eimSet$, $encodeIC1Polar$, $encodeIC2Polar$); #endif //PUT_A_NEW_LINE_HERE // // Enable TIM1 // TIM_Cmd(TIM1, ENABLE); #if(STRCMP($brkInt$, ENABLE) == 1) //PUT_A_NEW_LINE_HERE // // Enable BRK Interrupt // TIM_ITConfig(TIM1, TIM_IT_Break, ENABLE); NVIC_InitStructure.NVIC_IRQChannel = TIM1_BRK_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); #endif #if(STRCMP($upInt$, ENABLE) == 1) //PUT_A_NEW_LINE_HERE // // Enable UP Interrupt // TIM_ITConfig(TIM1, TIM_IT_Update, ENABLE); NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); #endif #if(STRCMP($tcInt$, 0) == 0) //PUT_A_NEW_LINE_HERE // // Enable TRG/COM Interrupt // TIM_ITConfig(TIM1, $TCInt$, ENABLE); NVIC_InitStructure.NVIC_IRQChannel = TIM1_TRG_COM_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); #endif #if(STRCMP($ccInt$, 0) == 0) //PUT_A_NEW_LINE_HERE // // Enable CC Interrupt // TIM_ITConfig(TIM1, $CCInt$, ENABLE); NVIC_InitStructure.NVIC_IRQChannel = TIM1_CC_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); #endif }
//============================================================================== // void STPDRV_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure; SystemCoreClockUpdate(); //----- GPIO AHB1Periph clock enable /*RCC_AHB1PeriphClockCmd(__GPIO2AHB1Periph(MOTOR1_STEP_PORT) , ENABLE); RCC_AHB1PeriphClockCmd(__GPIO2AHB1Periph(MOTOR1_DIR_PORT) , ENABLE); RCC_AHB1PeriphClockCmd(__GPIO2AHB1Periph(MOTOR2_STEP_PORT) , ENABLE); RCC_AHB1PeriphClockCmd(__GPIO2AHB1Periph(MOTOR2_DIR_PORT) , ENABLE);*/ RCC_APB2PeriphClockCmd(__GPIO2AHB1Periph(MOTOR1_STEP_PORT) , ENABLE); RCC_APB2PeriphClockCmd(__GPIO2AHB1Periph(MOTOR1_DIR_PORT) , ENABLE); RCC_APB2PeriphClockCmd(__GPIO2AHB1Periph(MOTOR2_STEP_PORT) , ENABLE); RCC_APB2PeriphClockCmd(__GPIO2AHB1Periph(MOTOR2_DIR_PORT) , ENABLE); // GPIO Configuration - Step PINs & DIR PINs //GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; //GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Pin = MOTOR1_STEP_PIN; GPIO_Init(MOTOR1_STEP_PORT, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = MOTOR1_DIR_PIN; GPIO_Init(MOTOR1_DIR_PORT, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = MOTOR2_STEP_PIN; GPIO_Init(MOTOR2_STEP_PORT, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = MOTOR2_DIR_PIN; GPIO_Init(MOTOR2_DIR_PORT, &GPIO_InitStructure); //----- API struc INIT (after GPIO init) Motors[0].CurDelay = 0xffff; __ResetTargetSpeed(0); __MotorSetDir(0, dir_CW); STPDRV_SetRamp(0, 4); Motors[1].CurDelay = 0xffff; __MotorSetDir(1, dir_CW); __ResetTargetSpeed(1); STPDRV_SetRamp(1, 4); //----- TIM Periph clock enable RCC_APB1PeriphClockCmd(STPDRV_TIM_APB , ENABLE); //--- TIMx Configuration: Output Compare Timing Mode ----------------------- // TIM3 input clock (TIM3CLK) is set to 2 * APB1 clock (PCLK1), since APB1 prescaler is different from 1. // TIM3CLK = 2 * PCLK1 // PCLK1 = HCLK / 4 // => TIM3CLK = 2 * (HCLK / 4) = HCLK / 2 = SystemCoreClock / 2 // // Para um "TIMx counter clock" de 100KHz (exemplo com resolução de 10uS) o "Prescaler" deve ser calculado assim: // Prescaler = (uint16_t) (TIMxCLK / 100000) - 1; // // O "Period" neste caso não interessa pois a IRQ (TIMx_IRQHandler) dos CCR (capture/compare register) controla o // togles dos estados HiGH e LOW dos PINs do STEP // // A resolução do sistema é definida em "stm32f_stpdrv.h" no define STPDRV_TIMFREQ // TIM_TimeBaseStructure.TIM_Period = 65535; TIM_TimeBaseStructure.TIM_Prescaler = (uint16_t) ((SystemCoreClock / 2) / (STPDRV_TIMFREQ * 2)) - 1; TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseStructure.TIM_RepetitionCounter = 0x0000; TIM_TimeBaseInit(STPDRV_TIM, &TIM_TimeBaseStructure); TIM_UpdateDisableConfig(STPDRV_TIM, ENABLE); // deve ser ENABLE pois para desactiver o Update Event o bit deve ser 1 /* Prescaler configuration */ //TIM_PrescalerConfig(STPDRV_TIM, PrescalerValue, TIM_PSCReloadMode_Immediate); // Output Compare Toggle Mode configuration: All Channels TIM_OCStructInit(&TIM_OCInitStructure); TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Timing; //TIM_OCMode_Toggle TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Disable; // Disable por defeito, API liga quando necessario TIM_OCInitStructure.TIM_Pulse = 0; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; // Output Compare Toggle Mode configuration: Channel1 - MOTOR 1 TIM_OC1Init(STPDRV_TIM, &TIM_OCInitStructure); TIM_OC1PreloadConfig(STPDRV_TIM, TIM_OCPreload_Disable); // Output Compare Toggle Mode configuration: Channel2 - MOTOR 2 TIM_OC2Init(STPDRV_TIM, &TIM_OCInitStructure); TIM_OC2PreloadConfig(STPDRV_TIM, TIM_OCPreload_Disable); // Output Compare Toggle Mode configuration: Channel3 - MOTOR 1 ACELL e DECEL TIM_OC3Init(STPDRV_TIM, &TIM_OCInitStructure); TIM_OC3PreloadConfig(STPDRV_TIM, TIM_OCPreload_Disable); // Output Compare Toggle Mode configuration: Channel4 - MOTOR 2 ACELL e DECEL TIM_OC4Init(STPDRV_TIM, &TIM_OCInitStructure); TIM_OC4PreloadConfig(STPDRV_TIM, TIM_OCPreload_Disable); // Enable the TIM gloabal Interrupt NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; // USER EDIT - Mudar o nome do IRQ se o TIMER for alterado NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = IRQ_STPDRV_PrePriority; NVIC_InitStructure.NVIC_IRQChannelSubPriority = IRQ_STPDRV_Priority; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); // TIM INT enable // A interrupt é activada na função Motor_On() //TIM_ITConfig(STPDRV_TIM, TIM_IT_CC1 | TIM_IT_CC2 | TIM_IT_CC3 | TIM_IT_CC4, ENABLE); // TIM enable counter TIM_Cmd(STPDRV_TIM, ENABLE); }