/** * @brief This function initialises quadrature encoder input to capture AB phase output from ENB encoder. * @param void * @retval void * @brief * PB4 --> TIM3 CH1 * PB5 --> TIM3 CH2 */ void QEI1_init (void) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_ICInitTypeDef TIM_ICInitStructure; GPIO_InitTypeDef GPIO_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; /* TIM3 clock source enable */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); /* Enable GPIOB, clock */ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); GPIO_StructInit(&GPIO_InitStructure); /* Configure PB.4,5 as encoder alternate function */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ; GPIO_Init(GPIOB, &GPIO_InitStructure); /* Connect TIM3 pins to AF2 */ GPIO_PinAFConfig(GPIOB, GPIO_PinSource4, GPIO_AF_TIM3); GPIO_PinAFConfig(GPIOB, GPIO_PinSource5, GPIO_AF_TIM3); /* Enable the TIM3 Update Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0F; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); /* Timer configuration in Encoder mode */ TIM_DeInit(TIM3); TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); TIM_TimeBaseStructure.TIM_Prescaler = 0x0; // No prescaling TIM_TimeBaseStructure.TIM_Period = (4*4000)-1; TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); TIM_EncoderInterfaceConfig(TIM3, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising); TIM_ICStructInit(&TIM_ICInitStructure); TIM_ICInitStructure.TIM_ICFilter = 6; TIM_ICInit(TIM3, &TIM_ICInitStructure); // Clear all pending interrupts TIM_ClearFlag(TIM3, TIM_FLAG_Update); TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE); //Reset counter TIM3->CNT = 0; //encoder value TIM_Cmd(TIM3, ENABLE); }
void TIM8_Encoder_Init() { RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8, ENABLE); TIM_DeInit(TIM8); TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_ICInitTypeDef TIM_ICInitStructure; TIM_TimeBaseStructure.TIM_Prescaler = 0; TIM_TimeBaseStructure.TIM_Period = 0xFFFFFFFF; // Max value for encoder pulse TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM8, &TIM_TimeBaseStructure); TIM_EncoderInterfaceConfig(TIM8, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising); TIM_ICStructInit(&TIM_ICInitStructure); TIM_ICInitStructure.TIM_ICFilter = 0; TIM_ICInit(TIM8, &TIM_ICInitStructure); // TIM_SetAutoreload(TIM2, 0xFFFF); TIM_SetCounter(TIM8, 0); TIM_Cmd(TIM8, ENABLE); GPIO_InitTypeDef GPIO_InitStruct; GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOC, &GPIO_InitStruct); GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_4); GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_4); }
void encoder_init(void) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_ICInitTypeDef TIM_ICInitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); //TIM2_CH1->PA5, TIM2_CH2->PA1 gpio_af_od_up_init(GPIOA, GPIO_Pin_1|GPIO_Pin_5); //编码器AB相输入口,复用开漏上拉 GPIO_PinAFConfig(GPIOA, GPIO_PinSource1, GPIO_AF_TIM2); GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_TIM2); TIM_DeInit(TIM2); TIM_TimeBaseStructure.TIM_Prescaler = 0; TIM_TimeBaseStructure.TIM_Period = ENCODER_RELOAD; //TIM2为32位定时器,这里把重载值设为最大,可以不考虑计数溢出 TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStructure); TIM_EncoderInterfaceConfig(TIM2, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising); TIM_ICStructInit(&TIM_ICInitStructure); TIM_ICInitStructure.TIM_ICFilter = 6; TIM_ICInit(TIM2, &TIM_ICInitStructure); TIM2->CNT = COUNTER_RESET; TIM_Cmd(TIM2, ENABLE); }
void config_tim3( void ) { NVIC_InitTypeDef NVIC_InitStructure; TIM_ICInitTypeDef TIM_ICInitStructure; /* Enable the TIM3 global Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); TIM_ICStructInit( &TIM_ICInitStructure ); TIM_ICInitStructure.TIM_Channel = TIM_Channel_1; TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; TIM_ICInitStructure.TIM_ICFilter = 0x0; TIM_PWMIConfig(TIM3, &TIM_ICInitStructure); /* Select the TIM3 Input Trigger: TI2FP1 */ TIM_SelectInputTrigger(TIM3, TIM_TS_TI1FP1); /* Select the slave Mode: Reset Mode */ TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_Reset); /* Enable the Master/Slave Mode */ TIM_SelectMasterSlaveMode(TIM3, TIM_MasterSlaveMode_Enable); /* TIM enable counter */ TIM_Cmd(TIM3, ENABLE); /* Enable the CC1 Interrupt Request */ TIM_ITConfig(TIM3, TIM_IT_CC1, ENABLE); }
/************************************************************************** 函数功能:把TIM4初始化为编码器接口模式 入口参数:无 返回 值:无 **************************************************************************/ void Encoder_Init_TIM3(void) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_ICInitTypeDef TIM_ICInitStructure; GPIO_InitTypeDef GPIO_InitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);//使能定时器3的时钟 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);//使能PA端口时钟 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7; //端口配置 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空输入 GPIO_Init(GPIOA, &GPIO_InitStructure); //根据设定参数初始化GPIOA TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); TIM_TimeBaseStructure.TIM_Prescaler = 0x0; // 预分频器 TIM_TimeBaseStructure.TIM_Period = ENCODER_TIM_PERIOD; //设定计数器自动重装值 TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;//选择时钟分频:不分频 TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;////TIM向上计数 TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); TIM_EncoderInterfaceConfig(TIM3, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);//使用编码器模式3 TIM_ICStructInit(&TIM_ICInitStructure); TIM_ICInitStructure.TIM_ICFilter = 10; TIM_ICInit(TIM3, &TIM_ICInitStructure); TIM_ClearFlag(TIM3, TIM_FLAG_Update);//清除TIM的更新标志位 TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE); //Reset counter TIM_SetCounter(TIM3,0); TIM_Cmd(TIM3, ENABLE); }
void ENC_Init(void) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_ICInitTypeDef TIM_ICInitStructure; //Encoder uint connected to TIM2 GPIO_InitTypeDef GPIO_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; //RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE); RCC_APB1PeriphClockCmd(ENC_TIMER_CLK,ENABLE); RCC_APB2PeriphClockCmd(ENC_GPIO_CLK ,ENABLE); GPIO_StructInit(&GPIO_InitStructure); //GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1; GPIO_InitStructure.GPIO_Pin = ENC_GPIO_PIN_A | ENC_GPIO_PIN_B ; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(ENC_GPIO_PORT ,&GPIO_InitStructure); NVIC_InitStructure.NVIC_IRQChannel = ENC_TIMER_IRQn ; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = TIMx_PRE_EMPTION_PRIORITY; NVIC_InitStructure.NVIC_IRQChannelSubPriority = TIMx_SUB_PRIORITY; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); TIM_DeInit(ENC_TIMER); //TIM_TimeBaseInit(&TIM_TimeBaseStructure); TIM_TimeBaseStructure.TIM_Prescaler = 0x0; TIM_TimeBaseStructure.TIM_Period = (4*ENCODER_PPR)-1; TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(ENC_TIMER,&TIM_TimeBaseStructure); TIM_EncoderInterfaceConfig(ENC_TIMER,TIM_EncoderMode_TI12, TIM_ICPolarity_Rising,TIM_ICPolarity_Rising); TIM_ICStructInit(&TIM_ICInitStructure); TIM_ICInitStructure.TIM_ICFilter = ICx_FILTER; TIM_ICInit(ENC_TIMER,&TIM_ICInitStructure); TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;//注意通道 TIM_ICInit(ENC_TIMER, &TIM_ICInitStructure); TIM_ClearFlag(ENC_TIMER,TIM_FLAG_Update); TIM_ITConfig(ENC_TIMER,TIM_IT_Update,ENABLE); TIM2->CNT = COUNTER_RESET;//!!此处注意修改 //局部初始化 ENC_Clear_Speed_Buffer(); hPrevious_angle=0; hRot_Speed=0;; bSpeed_Buffer_Index = 0; hEncoder_Timer_Overflow=0; bIs_First_Measurement = true; hEncoder_Revolutions_Num=0; TIM_Cmd(ENC_TIMER,ENABLE); }
void TIM3_Mode_Config(void) { GPIO_InitTypeDef GPIO_InitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_ICInitTypeDef TIM_ICInitStructure; //TIM_OCInitTypeDef TIM_OCInitStructure; /*----------------------------------------------------------------*/ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); GPIO_StructInit(&GPIO_InitStructure); /* Configure PA.06,07 as encoder input */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure); /*----------------------------------------------------------------*/ RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //使能TIM3 TIM_DeInit(TIM3); TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); TIM_TimeBaseStructure.TIM_Period =0xffff; // TIM_TimeBaseStructure.TIM_Prescaler =0; //设置预分频: TIM_TimeBaseStructure.TIM_ClockDivision =TIM_CKD_DIV1 ; //设置时钟分频系数:不分频 TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数模式 //TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_CenterAligned1; /*初始化TIM2定时器 */ TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); /*-----------------------------------------------------------------*/ //编码配置 编码模式 TIM_EncoderInterfaceConfig(TIM3, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising); //TIM_ICPolarity_Rising上升沿捕获 TIM_ICStructInit(&TIM_ICInitStructure); TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;//配置通道1的滤波器 TIM_ICInitStructure.TIM_ICFilter = 6; //比较滤波器 TIM_ICInit(TIM3, &TIM_ICInitStructure); TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;//配置通道2的滤波器 TIM_ICInitStructure.TIM_ICFilter = 6; //比较滤波器 TIM_ICInit(TIM3, &TIM_ICInitStructure); TIM_ARRPreloadConfig(TIM3, ENABLE); // Clear all pending interrupts TIM_ClearFlag(TIM3, TIM_FLAG_Update); TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE); //使能中断 //Reset counter TIM3->CNT =0; TIM_Cmd(TIM3, ENABLE); //使能定时器3 }
static void hal_soft_serial_init_timer(void) { TIM_TimeBaseInitTypeDef tim_init; TIM_ICInitTypeDef tim_ic_init; // time base configuration: TIM_TimeBaseStructInit(&tim_init); // this serial uart runs at 9600 baud, thus bitlength = 1/9600 = 104.166667 us // a 1mhz counter gives us 1us resolution, a 24mhz counter gives us 1/24 us res // the finer the better -> go for 24mhz counter = prescaler = 0 (:1) tim_init.TIM_Prescaler = (uint16_t) (0); // timer period = bit duration tim_init.TIM_Period = 0xFFFF; tim_init.TIM_ClockDivision = 0; tim_init.TIM_CounterMode = TIM_CounterMode_Up; //set time base. NOTE: this will immediately trigger an INT! TIM_TimeBaseInit(SOFT_SERIAL_TIMER, &tim_init); //clear IT flag (caused by TimeBaseInit()): TIM_ClearITPendingBit(SOFT_SERIAL_TIMER, TIM_IT_Update); TIM_ICStructInit(&tim_ic_init); tim_ic_init.TIM_Channel = SOFT_SERIAL_TIMER_CH; #if HUB_TELEMETRY_INVERTED #if SOFT_SERIAL_INVERTED //board has inverter -> invert twice = no inversion tim_ic_init.TIM_ICPolarity = TIM_ICPolarity_Falling; #else tim_ic_init.TIM_ICPolarity = TIM_ICPolarity_Rising; #endif #else #if SOFT_SERIAL_INVERTED //board has inverter -> invert tim_ic_init.TIM_ICPolarity = TIM_ICPolarity_Rising; #else tim_ic_init.TIM_ICPolarity = TIM_ICPolarity_Falling; #endif #endif tim_ic_init.TIM_ICSelection = TIM_ICSelection_DirectTI; tim_ic_init.TIM_ICPrescaler = TIM_ICPSC_DIV1; tim_ic_init.TIM_ICFilter = 0x0; TIM_ICInit(SOFT_SERIAL_TIMER, &tim_ic_init); TIM_ClearITPendingBit(SOFT_SERIAL_TIMER, SOFT_SERIAL_TIMER_IT_IC); /* //Output Compare Active Mode configuration: TIM_OCStructInit(&tim_oc_init); tim_oc_init.TIM_OCMode = TIM_OCMode_ disable; tim_oc_init.TIM_OutputState = TIM_OutputState_ disable; tim_oc_init.TIM_Pulse = PPM_SYNC_PULS_LEN_TICKS; tim_oc_init.TIM_OCPolarity = TIM_OCPolarity_High; hal_ppm_init_ocx(PPM_TIMER_CH, PPM_TIMER, &tim_oc_init); */ //enable counter TIM_Cmd(SOFT_SERIAL_TIMER, ENABLE); }
/******************************************************************************************************** Function Name: EncoderTIMInit Description : Inputs : None Outputs : None Notes : Encoder TIME初始化 Revision : ********************************************************************************************************/ void EncoderTIMInit(void) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_ICInitTypeDef TIM_ICInitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8, ENABLE); /*********************** ENCODER2 *************************/ TIM_DeInit(ENCODER_R_TIMER); TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); TIM_TimeBaseStructure.TIM_Prescaler = 0; // No prescaling TIM_TimeBaseStructure.TIM_Period = 65535; TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(ENCODER_R_TIMER, &TIM_TimeBaseStructure); TIM_EncoderInterfaceConfig(ENCODER_R_TIMER, TIM_EncoderMode_TI12, TIM_ICPolarity_BothEdge, TIM_ICPolarity_BothEdge); // 编码器计数方式 TIM_ICStructInit(&TIM_ICInitStructure); TIM_ICInitStructure.TIM_ICFilter = ICx_FILTER; // 滤波器 TIM_ICInit(ENCODER_R_TIMER, &TIM_ICInitStructure); ENCODER_R_TIMER->CNT = COUNTER_RESET; TIM_Cmd(ENCODER_R_TIMER, ENABLE); /********************** ENCODER1 *************************/ TIM_DeInit(ENCODER_L_TIMER); TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); TIM_TimeBaseStructure.TIM_Prescaler = 0; // No prescaling TIM_TimeBaseStructure.TIM_Period = 65535; TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(ENCODER_L_TIMER, &TIM_TimeBaseStructure); TIM_EncoderInterfaceConfig(ENCODER_L_TIMER, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising); //选定T1和T2同时计数模式,上升沿计数 TIM_ICStructInit(&TIM_ICInitStructure); TIM_ICInitStructure.TIM_ICFilter = ICx_FILTER; TIM_ICInit(ENCODER_L_TIMER, &TIM_ICInitStructure); ENCODER_L_TIMER->CNT = COUNTER_RESET; TIM_Cmd(ENCODER_L_TIMER, ENABLE); }
void TIM3_Init() //运动电机 { //u16 CCR1_Val = 2500; //u16 CCR2_Val = 1000; GPIO_InitTypeDef GPIO_InitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_ICInitTypeDef TIM_ICInitStructure; //TIM_OCInitTypeDef TIM_OCInitStructure; /*----------------------------------------------------------------*/ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); GPIO_StructInit(&GPIO_InitStructure); /* Configure PA.06,07 as encoder input */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; GPIO_Init(GPIOA, &GPIO_InitStructure); /*----------------------------------------------------------------*/ RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //使能TIM3 TIM_DeInit(TIM3); TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); TIM_TimeBaseStructure.TIM_Period =8000; //此处设置所使用的编码器转一圈产生的计数 TIM_TimeBaseStructure.TIM_Prescaler =0; //设置预分频: TIM_TimeBaseStructure.TIM_ClockDivision =TIM_CKD_DIV1 ; //设置时钟分频系数:不分频 TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数模式 //TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_CenterAligned1; /*初始化TIM3定时器 */ TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); /*-----------------------------------------------------------------*/ //编码配置 编码模式 TIM_EncoderInterfaceConfig(TIM3, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising); //TIM_ICPolarity_Rising上升沿捕获 TIM_ICStructInit(&TIM_ICInitStructure); TIM_ICInitStructure.TIM_ICFilter = 1; //比较滤波器 TIM_ICInit(TIM3, &TIM_ICInitStructure); TIM_ClearFlag(TIM3, TIM_FLAG_Update); TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE); //使能中断 //Reset counter TIM3->CNT =0; TIM_Cmd(TIM3, ENABLE); //使能定时器3 }
void pwmICInit(const TIM_TypeDef *tim, uint16_t channel, uint16_t polarity) { TIM_ICInitTypeDef TIM_ICInitStructure; TIM_ICStructInit(&TIM_ICInitStructure); TIM_ICInitStructure.TIM_Channel = channel; TIM_ICInitStructure.TIM_ICPolarity = polarity; TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; TIM_ICInitStructure.TIM_ICFilter = 0x08; TIM_ICInit((TIM_TypeDef *)tim, &TIM_ICInitStructure); }
void hw::spindle::initialize() { // Index pin GPIO_InitTypeDef GPIO_InitStructure; GPIO_StructInit(&GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = _index_pin; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; GPIO_Init(_index_port, &GPIO_InitStructure); // Get system frequency RCC_ClocksTypeDef RCC_Clocks; RCC_GetClocksFreq(&RCC_Clocks); // Setup timer for index pin TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_DeInit(_index_timer); // Tick every 0.1ms TIM_TimeBaseStructure.TIM_Prescaler = (RCC_Clocks.HCLK_Frequency / 10000) - 1; TIM_TimeBaseStructure.TIM_Period = 0xffff; TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseStructure.TIM_RepetitionCounter = 0; TIM_TimeBaseInit(_index_timer, &TIM_TimeBaseStructure); // Configure capture TIM_ICInitTypeDef TIM_ICInitStructure; TIM_ICStructInit(&TIM_ICInitStructure); TIM_ICInitStructure.TIM_Channel = TIM_Channel_1; TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling; TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; TIM_ICInitStructure.TIM_ICFilter = 2; TIM_ICInit(_index_timer, &TIM_ICInitStructure); // Configure slave mode, reset on capture TIM_SelectSlaveMode(_index_timer, TIM_SlaveMode_Reset); TIM_SelectInputTrigger(_index_timer, TIM_TS_TI1FP1); // We don't want capture event to generate update interrupt. // Update interrupt is used to detect cases when spindle speed is too low // (or spindle is stopped) TIM_UpdateRequestConfig(_index_timer, TIM_UpdateSource_Regular); // Configure interrupts TIM_ClearITPendingBit(_index_timer, TIM_IT_Update | TIM_IT_CC1); TIM_ITConfig(_index_timer, TIM_IT_Update | TIM_IT_CC1, ENABLE); TIM_Cmd(_index_timer, ENABLE); }
static void serialICConfig(TIM_TypeDef *tim, uint8_t channel, uint16_t polarity) { TIM_ICInitTypeDef TIM_ICInitStructure; TIM_ICStructInit(&TIM_ICInitStructure); TIM_ICInitStructure.TIM_Channel = channel; TIM_ICInitStructure.TIM_ICPolarity = polarity; TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; TIM_ICInitStructure.TIM_ICFilter = 0x0; TIM_ICInit(tim, &TIM_ICInitStructure); }
void EncoderR_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_ICInitTypeDef TIM_ICInitStruct; GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE); /* ENCODER clock enable */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); /* Enable the ENCODER Clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE); GPIO_PinRemapConfig(/*GPIO_PartialRemap1_TIM2*/GPIO_FullRemap_TIM2,ENABLE); //重映射TIM2引脚 GPIO_StructInit(&GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15; //ENCODER_CHA, ENCODER_CHB端口配置 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_StructInit(&GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; //ENCODER_CHA, ENCODER_CHB端口配置 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); /* Timer configuration in Encoder mode */ TIM_DeInit(TIM2); TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); TIM_TimeBaseStructure.TIM_Prescaler = 0x0;//预分频器 TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式 TIM_TimeBaseStructure.TIM_Period = 0xffff;//设定计数器自动重装值 TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割:TDTS = Tck_tim TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位 // TIM_ARRPreloadConfig(ENCODER_TIM, ENABLE);//使能ARR自动重装入缓冲器 TIM_EncoderInterfaceConfig(TIM2, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising); TIM_ICStructInit(&TIM_ICInitStruct); TIM_ICInitStruct.TIM_ICFilter = 6;//ICx_FILTER; // TIM_ICFilter选择输入比较滤波器。该参数取值在0x0和0xF之间 TIM_ICInit(TIM2, &TIM_ICInitStruct); TIM2->CNT = ENCODER_INIT_VALUE; //使能定时器 TIM_Cmd(TIM2,ENABLE); //使能定时器 }
void ENC_init(void) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_ICInitTypeDef TIM_ICInitStructure; GPIO_InitTypeDef GPIO_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); GPIO_StructInit(&GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure); /* Enable the TIM3 Update Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 10; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); /* Timer configuration in Encoder mode */ TIM_DeInit(TIM1); TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); TIM_TimeBaseStructure.TIM_Prescaler = 0x0; // No prescaling TIM_TimeBaseStructure.TIM_Period = TIM1_PERIOD; TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); TIM_EncoderInterfaceConfig(TIM1, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising); TIM_ICStructInit(&TIM_ICInitStructure); TIM_ICInitStructure.TIM_ICFilter = ICx_FILTER; TIM_ICInit(TIM1, &TIM_ICInitStructure); // Clear all pending interrupts TIM_ClearFlag(TIM1, TIM_FLAG_Update); TIM_ITConfig(TIM1, TIM_IT_Update, ENABLE); //Reset counter TIM1->CNT = COUNTER_RESET; // ENC_Clear_Speed_Buffer(); TIM_Cmd(TIM1, ENABLE); }
void pwmICConfig(TIM_TypeDef *tim, uint8_t channel, uint16_t polarity) { TIM_ICInitTypeDef TIM_ICInitStructure; TIM_ICStructInit(&TIM_ICInitStructure); TIM_ICInitStructure.TIM_Channel = channel; TIM_ICInitStructure.TIM_ICPolarity = polarity; TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; if (inputFilteringMode == INPUT_FILTERING_ENABLED) { TIM_ICInitStructure.TIM_ICFilter = INPUT_FILTER_TO_HELP_WITH_NOISE_FROM_OPENLRS_TELEMETRY_RX; } else { TIM_ICInitStructure.TIM_ICFilter = 0x00; } TIM_ICInit(tim, &TIM_ICInitStructure); }
/** * @brief Configures TIM2 channel 4 in input capture mode * @param None * @retval None */ void TIM_Config(void) { /* Init Structure definition */ TIM_ICInitTypeDef TIM_ICInitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; NVIC_InitTypeDef NVIC_InitStructure; /* TIM2 clock enable */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); /* TIM2 Time base configuration */ TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); TIM_TimeBaseStructure.TIM_Prescaler = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseStructure.TIM_Period = 65535; TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); TIM_ClearFlag(TIM2, TIM_FLAG_Update); /* TIM2 Channel4 Input capture Mode configuration */ TIM_ICStructInit(&TIM_ICInitStructure); TIM_ICInitStructure.TIM_Channel = TIM_Channel_4; /* TIM2 counter is captured at each transition detection: rising or falling edges (both edges) */ TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_BothEdge; TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; TIM_ICInitStructure.TIM_ICFilter = 0; TIM_ICInit(TIM2, &TIM_ICInitStructure); /* TIM2 IRQChannel enable */ NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); /* Enable capture interrupt */ TIM_ITConfig(TIM2, TIM_IT_CC4, ENABLE); /* Enable the TIM2 counter */ TIM_Cmd(TIM2, ENABLE); /* Reset the flags */ TIM2->SR = 0; }
void ENC1_Init(void) { RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 ; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOA,GPIO_PinSource0,GPIO_AF_TIM2); /*NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure);*/ /* Timer configuration in Encoder mode */ TIM_DeInit(TIM2); TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); TIM_TimeBaseStructure.TIM_Prescaler = 0x0; // No prescaling TIM_TimeBaseStructure.TIM_Period = 40000-1; TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); TIM_EncoderInterfaceConfig(TIM2, TIM_EncoderMode_TI1,TIM_ICPolarity_Rising, TIM_ICPolarity_Rising); TIM_ICStructInit(&TIM_ICInitStructure); TIM_ICInitStructure.TIM_ICFilter = 0xe; TIM_ICInit(TIM2, &TIM_ICInitStructure); // Clear all pending interrupts TIM_ClearFlag(TIM2, TIM_FLAG_Update); TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE); TIM_SetCounter(TIM2,0); TIM2->CNT = 0; TIM_Cmd(TIM2, ENABLE); }
/** * @brief Initialize input capture timer * @details Initializes TIM4 for the input capture library. */ void ic_init() { //enable clocks for TIM4 RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); TIM_TimeBaseInitTypeDef TIM_TimeBase_InitStructure; TIM_ICInitTypeDef TIM_IC_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; //setup TIM4 TIM_TimeBaseStructInit(&TIM_TimeBase_InitStructure); TIM_ICStructInit(&TIM_IC_InitStructure); TIM_TimeBase_InitStructure.TIM_ClockDivision = TIM_CKD_DIV1; TIM_TimeBase_InitStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBase_InitStructure.TIM_Period = 0xffff; TIM_TimeBase_InitStructure.TIM_Prescaler = _ge_ic_prescaler; // 72 Mhz TIM_TimeBase_InitStructure.TIM_RepetitionCounter = 0; TIM_TimeBaseInit(TIM4, &TIM_TimeBase_InitStructure); TIM_ClearFlag(TIM4, TIM_FLAG_Update); //setup NVIC NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); //initialize overflow count and new data flags for (int i = 0; i < 4; i++) { _ge_ic_chan_ovf[i] = 0; _ge_ic_chan_freq_avail[i] = false; } //enable interrupts TIM_ITConfig(TIM4, TIM_IT_Update | TIM_IT_CC1 | TIM_IT_CC2 | TIM_IT_CC3 | TIM_IT_CC4, ENABLE); //enable timer TIM_Cmd(TIM4, ENABLE); }
static void encoderM1Init() { //Init structures GPIO_InitTypeDef GPIO_InitStructure; TIM_ICInitTypeDef TIM_ICInitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; //Enable gpio and the timer RCC_AHB1PeriphClockCmd(ENCODER_M1_GPIO_PERIF, ENABLE); RCC_APB1PeriphClockCmd(ENCODER_M1_TIM_PERIF, ENABLE); // Configure the GPIO for the timer caputure GPIO_InitStructure.GPIO_Pin = ENCODER_M1_GPIO_0 | ENCODER_M1_GPIO_1; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(ENCODER_M1_GPIO_PORT, &GPIO_InitStructure); //Remap M1-2 GPIO_PinAFConfig(ENCODER_M1_GPIO_PORT, ENCODER_M1_GPIO_AF_PIN_0, ENCODER_M1_GPIO_AF); GPIO_PinAFConfig(ENCODER_M1_GPIO_PORT, ENCODER_M1_GPIO_AF_PIN_1, ENCODER_M1_GPIO_AF); //Timer configuration TIM_DeInit(ENCODER_M1_TIM); TIM_TimeBaseStructure.TIM_Period = ENCODER_PERIOD; TIM_TimeBaseStructure.TIM_Prescaler = ENCODER_PRESCALE; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(ENCODER_M1_TIM, &TIM_TimeBaseStructure); TIM_EncoderInterfaceConfig(ENCODER_M1_TIM, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising); TIM_ICStructInit(&TIM_ICInitStructure); TIM_ICInitStructure.TIM_ICFilter = 3; TIM_ICInit(ENCODER_M1_TIM, &TIM_ICInitStructure); TIM_SetCounter(ENCODER_M1_TIM, 0x7FFF); TIM_ClearFlag(ENCODER_M1_TIM, TIM_FLAG_Update); TIM_Cmd(ENCODER_M1_TIM, ENABLE); }
void cppmInit(void) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_ICInitTypeDef TIM_ICInitStructure; GPIO_InitTypeDef GPIO_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_AHB1PeriphClockCmd(CPPM_GPIO_RCC, ENABLE); RCC_APB1PeriphClockCmd(CPPM_TIMER_RCC, ENABLE); // Configure the GPIO for the timer input GPIO_StructInit(&GPIO_InitStructure); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Pin = CPPM_GPIO_PIN; GPIO_Init(CPPM_GPIO_PORT, &GPIO_InitStructure); GPIO_PinAFConfig(CPPM_GPIO_PORT, CPPM_GPIO_SOURCE, CPPM_GPIO_AF); // Time base configuration. 1us tick. TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); TIM_TimeBaseStructure.TIM_Prescaler = CPPM_TIM_PRESCALER; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(CPPM_TIMER, &TIM_TimeBaseStructure); // Setup input capture using default config. TIM_ICStructInit(&TIM_ICInitStructure); TIM_ICInit(CPPM_TIMER, &TIM_ICInitStructure); NVIC_InitStructure.NVIC_IRQChannel = TIM8_TRG_COM_TIM14_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = NVIC_CPPM_PRI; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); captureQueue = xQueueCreate(64, sizeof(uint16_t)); TIM_ITConfig(CPPM_TIMER, TIM_IT_Update | TIM_IT_CC1, ENABLE); TIM_Cmd(CPPM_TIMER, ENABLE); }
/** * @brief Main program * @param None * @retval None */ int main(void) { /*!< At this stage the microcontroller clock setting is already configured, this is done through SystemInit() function which is called from startup file (startup_stm32f10x_xx.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32f10x.c file */ /* System Clocks Configuration */ RCC_Configuration(); /* Configure the GPIO ports */ GPIO_Configuration(); /* TIM4 configuration: One Pulse mode ------------------------ The external signal is connected to TIM4_CH2 pin (PB.07), The Rising edge is used as active edge, The One Pulse signal is output on TIM4_CH1 pin (PB.06) The TIM_Pulse defines the delay value The (TIM_Period - TIM_Pulse) defines the One Pulse value. TIM2CLK = SystemCoreClock, we want to get TIM2 counter clock at 24 MHz: - Prescaler = (TIM2CLK / TIM2 counter clock) - 1 The Autoreload value is 65535 (TIM4->ARR), so the maximum frequency value to trigger the TIM4 input is 24000000/65535 = 300 Hz. The TIM_Pulse defines the delay value, the delay value is fixed to 682.6 us: delay = CCR1/TIM4 counter clock = 682.6 us. The (TIM_Period - TIM_Pulse) defines the One Pulse value, the pulse value is fixed to 2.048 ms: One Pulse value = (TIM_Period - TIM_Pulse) / TIM4 counter clock = 2.048 ms. * SystemCoreClock is set to 72 MHz for Low-density, Medium-density, High-density and Connectivity line devices and to 24 MHz for Value line devices ------------------------------------------------------------ */ /* Compute the prescaler value */ PrescalerValue = (uint16_t) (SystemCoreClock / 24000000) - 1; /* Time base configuration */ TIM_TimeBaseStructure.TIM_Period = 65535; TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); /* TIM4 PWM2 Mode configuration: Channel1 */ TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = 16383; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; TIM_OC1Init(TIM4, &TIM_OCInitStructure); /* TIM4 configuration in Input Capture Mode */ TIM_ICStructInit(&TIM_ICInitStructure); TIM_ICInitStructure.TIM_Channel = TIM_Channel_2; TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; TIM_ICInitStructure.TIM_ICFilter = 0; TIM_ICInit(TIM4, &TIM_ICInitStructure); /* One Pulse Mode selection */ TIM_SelectOnePulseMode(TIM4, TIM_OPMode_Single); /* Input Trigger selection */ TIM_SelectInputTrigger(TIM4, TIM_TS_TI2FP2); /* Slave Mode selection: Trigger Mode */ TIM_SelectSlaveMode(TIM4, TIM_SlaveMode_Trigger); while (1) {} }
void TIM8_CC_IRQHandler() { TIM_ICInitTypeDef TIM_ICInitStructure; TIM_ICStructInit(&TIM_ICInitStructure); //Initialize the structure with default setting if(TIM_GetITStatus(TIM8, TIM_IT_CC1) == SET) { TIM_ClearITPendingBit(TIM8, TIM_IT_CC1); TIM_ICInitStructure.TIM_Channel = TIM_Channel_1; if(capture_state[THROTTLE_CHANNEL] == RISE) { //Rised edge capture rise_value[THROTTLE_CHANNEL] = TIM_GetCapture1(TIM8); //Change to measure the falled edge capture_state[THROTTLE_CHANNEL] = FALL; TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling; } else { //Falled edge capture fall_value[THROTTLE_CHANNEL] = TIM_GetCapture1(TIM8); /* Calculate the RC high pulse value */ if(fall_value[THROTTLE_CHANNEL] > rise_value[THROTTLE_CHANNEL]) rc_value[THROTTLE_CHANNEL] = fall_value[THROTTLE_CHANNEL] - rise_value[THROTTLE_CHANNEL]; else if(fall_value[THROTTLE_CHANNEL] == 0 && rise_value[THROTTLE_CHANNEL] == 0) rc_value[THROTTLE_CHANNEL] = 0; //No signal else /* falled edge measured value is overflow */ rc_value[THROTTLE_CHANNEL] = (fall_value[THROTTLE_CHANNEL] + 10000) - rise_value[THROTTLE_CHANNEL]; //Change to measure the rised edge capture_state[THROTTLE_CHANNEL] = RISE; TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; } TIM_ICInit(TIM8, &TIM_ICInitStructure); } if(TIM_GetITStatus(TIM8, TIM_IT_CC2) == SET) { TIM_ClearITPendingBit(TIM8, TIM_IT_CC2); TIM_ICInitStructure.TIM_Channel = TIM_Channel_2; if(capture_state[YAW_CHANNEL] == RISE) { //Rised edge capture rise_value[YAW_CHANNEL] = TIM_GetCapture2(TIM8); //Change to measure the falled edge capture_state[YAW_CHANNEL] = FALL; TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling; } else { //Falled edge capture fall_value[YAW_CHANNEL] = TIM_GetCapture2(TIM8); /* Calculate the RC high pulse value */ if(fall_value[YAW_CHANNEL] > rise_value[YAW_CHANNEL]) rc_value[YAW_CHANNEL] = fall_value[YAW_CHANNEL] - rise_value[YAW_CHANNEL]; else if(fall_value[YAW_CHANNEL] == 0 && rise_value[YAW_CHANNEL] == 0) rc_value[YAW_CHANNEL] = 0; //No signal else /* falled edge measured value is overflow */ rc_value[YAW_CHANNEL] = (fall_value[YAW_CHANNEL] + 10000) - rise_value[YAW_CHANNEL]; //Change to measure the rised edge capture_state[YAW_CHANNEL] = RISE; TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; } TIM_ICInit(TIM8, &TIM_ICInitStructure); } }
/** * @brief Main program * @param None * @retval None */ int main(void) { /* System Clocks Configuration */ RCC_Configuration(); /* Configure the GPIO ports */ GPIO_Configuration(); /* TIM4 configuration: One Pulse mode ------------------------ The external signal is connected to TIM4_CH2 pin (PA.01), The Rising edge is used as active edge, The One Pulse signal is output on TIM4_CH1 pin (PA.00) The TIM_Pulse defines the delay value The (TIM_Period - TIM_Pulse) defines the One Pulse value. The TIM4CLK is fixed to 72 MHz, the Prescaler is 1, so the TIM4 counter clock is 36 MHz. The Autoreload value is 65535 (TIM4->ARR), so the maximum frequency value to trigger the TIM4 input is 500 Hz. The TIM_Pulse defines the delay value, the delay value is fixed to 455.08 us: delay = CCR1/TIM4 counter clock = 455.08 us. The (TIM_Period - TIM_Pulse) defines the One Pulse value, the pulse value is fixed to 1.365ms: One Pulse value = (TIM_Period - TIM_Pulse)/TIM4 counter clock = 1.365 ms. ------------------------------------------------------------ */ /* Time base configuration */ TIM_TimeBaseStructure.TIM_Period = 65535; TIM_TimeBaseStructure.TIM_Prescaler = 1; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); /* TIM4 PWM2 Mode configuration: Channel1 */ TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = 16383; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; TIM_OC1Init(TIM4, &TIM_OCInitStructure); /* TIM4 configuration in Input Capture Mode */ TIM_ICStructInit(&TIM_ICInitStructure); TIM_ICInitStructure.TIM_Channel = TIM_Channel_2; TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; TIM_ICInitStructure.TIM_ICFilter = 0; TIM_ICInit(TIM4, &TIM_ICInitStructure); /* One Pulse Mode selection */ TIM_SelectOnePulseMode(TIM4, TIM_OPMode_Single); /* Input Trigger selection */ TIM_SelectInputTrigger(TIM4, TIM_TS_TI2FP2); /* Slave Mode selection: Trigger Mode */ TIM_SelectSlaveMode(TIM4, TIM_SlaveMode_Trigger); while (1) {} }
/** * @brief Initialize the RC5 decoder module ( Time range) * @param None * @retval None */ void RC5_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; TIM_ICInitTypeDef TIM_ICInitStructure; /* Clock Configuration for TIMER */ RCC_APB1PeriphClockCmd(IR_TIM_CLK , ENABLE); /* Enable Button GPIO clock */ RCC_AHBPeriphClockCmd(IR_GPIO_PORT_CLK , ENABLE); /* Pin configuration: input floating */ GPIO_InitStructure.GPIO_Pin = IR_GPIO_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(IR_GPIO_PORT, &GPIO_InitStructure); GPIO_PinAFConfig( IR_GPIO_PORT,IR_GPIO_SOURCE,GPIO_AF_2); /* Enable the TIM global Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = IR_TIM_IRQn ; NVIC_InitStructure.NVIC_IRQChannelPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); /* TIMER frequency input */ TIM_PrescalerConfig(IR_TIM, TIM_PRESCALER, TIM_PSCReloadMode_Immediate); TIM_ICStructInit(&TIM_ICInitStructure); /* TIM configuration */ TIM_ICInitStructure.TIM_Channel = IR_TIM_Channel; TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling; TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; TIM_ICInitStructure.TIM_ICFilter = 0x0; TIM_PWMIConfig(IR_TIM, &TIM_ICInitStructure); /* Timer Clock */ TIMCLKValueKHz = TIM_GetCounterCLKValue()/1000; /* Select the TIM Input Trigger: TI2FP2 */ TIM_SelectInputTrigger(IR_TIM, TIM_TS_TI2FP2); /* Select the slave Mode: Reset Mode */ TIM_SelectSlaveMode(IR_TIM, TIM_SlaveMode_Reset); /* Enable the Master/Slave Mode */ TIM_SelectMasterSlaveMode(IR_TIM, TIM_MasterSlaveMode_Enable); /* Configures the TIM Update Request Interrupt source: counter overflow */ TIM_UpdateRequestConfig(IR_TIM, TIM_UpdateSource_Regular); RC5TimeOut = TIMCLKValueKHz * RC5_TIME_OUT_US/1000; /* Set the TIM auto-reload register for each IR protocol */ IR_TIM->ARR = RC5TimeOut; /* Clear update flag */ TIM_ClearFlag(IR_TIM, TIM_FLAG_Update); /* Enable TIM Update Event Interrupt Request */ TIM_ITConfig(IR_TIM, TIM_IT_Update, ENABLE); /* Enable the CC2/CC1 Interrupt Request */ TIM_ITConfig(IR_TIM, TIM_IT_CC2, ENABLE); /* Enable the CC2/CC1 Interrupt Request */ TIM_ITConfig(IR_TIM, TIM_IT_CC1, ENABLE); /* Enable the timer */ TIM_Cmd(IR_TIM, ENABLE); if (CECDemoStatus == 0) { /* Set the LCD Back Color */ LCD_SetBackColor(LCD_COLOR_RED); /* Set the LCD Text Color */ LCD_SetTextColor(LCD_COLOR_GREEN); LCD_DisplayStringLine(LCD_LINE_0, " STM320518-EVAL "); LCD_DisplayStringLine(LCD_LINE_1, " RC5 InfraRed Demo "); LCD_SetBackColor(LCD_COLOR_BLUE); /* Set the LCD Text Color */ LCD_SetTextColor(LCD_COLOR_WHITE); } /* Bit time range */ RC5MinT = (RC5_T_US - RC5_T_TOLERANCE_US) * TIMCLKValueKHz / 1000; RC5MaxT = (RC5_T_US + RC5_T_TOLERANCE_US) * TIMCLKValueKHz / 1000; RC5Min2T = (2 * RC5_T_US - RC5_T_TOLERANCE_US) * TIMCLKValueKHz / 1000; RC5Max2T = (2 * RC5_T_US + RC5_T_TOLERANCE_US) * TIMCLKValueKHz / 1000; /* Default state */ RC5_ResetPacket(); }
/** * @brief Main program * @param None * @retval None */ int main(void) { /*!< At this stage the microcontroller clock setting is already configured, this is done through SystemInit() function which is called from startup files (startup_stm32f40_41xxx.s/startup_stm32f427_437xx.s/startup_stm32f429_439xx.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32f4xx.c file */ /* TIM Configuration */ TIM_Config(); /* -------------------------------------------------------------------------- TIM4 configuration: One Pulse mode The external signal is connected to TIM4_CH2 pin (PB.07), The Rising edge is used as active edge, The One Pulse signal is output on TIM4_CH1 pin (PB.06) The TIM_Pulse defines the delay value The (TIM_Period - TIM_Pulse) defines the One Pulse value. TIM4 input clock (TIM4CLK) is set to 2 * APB1 clock (PCLK1), since APB1 prescaler is different from 1. TIM4CLK = 2 * PCLK1 PCLK1 = HCLK / 4 => TIM4CLK = HCLK / 2 = SystemCoreClock /2 TIM2CLK = SystemCoreClock/2, we want to get TIM2 counter clock at 42 MHz: Prescaler = (TIM2CLK / TIM2 counter clock) - 1 Prescaler = ((SystemCoreClock /2) /42 MHz) - 1 The Autoreload value is 65535 (TIM4->ARR), so the maximum frequency value to trigger the TIM4 input is 42000000/65535 = 641 Hz. The TIM_Pulse defines the delay value, the delay value is fixed to 390 us: delay = CCR1/TIM4 counter clock = 16383 / 42000000 = 390 us. The (TIM_Period - TIM_Pulse) defines the One Pulse value, the pulse value is fixed to 1.170 ms: One Pulse value = (TIM_Period - TIM_Pulse) / TIM4 counter clock = (65535 - 16383) / 42000000 = 1.170 ms. Note: SystemCoreClock variable holds HCLK frequency and is defined in system_stm32f4xx.c file. Each time the core clock (HCLK) changes, user had to call SystemCoreClockUpdate() function to update SystemCoreClock variable value. Otherwise, any configuration based on this variable will be incorrect. --------------------------------------------------------------------------- */ /* Compute the prescaler value */ uhPrescalerValue = (uint16_t) ((SystemCoreClock / 2) / 42000000) - 1; /* Time base configuration */ TIM_TimeBaseStructure.TIM_Period = 65535; TIM_TimeBaseStructure.TIM_Prescaler = uhPrescalerValue; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); /* TIM4 PWM2 Mode configuration: Channel1 */ TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = 16383; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; TIM_OC1Init(TIM4, &TIM_OCInitStructure); /* TIM4 configuration in Input Capture Mode */ TIM_ICStructInit(&TIM_ICInitStructure); TIM_ICInitStructure.TIM_Channel = TIM_Channel_2; TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; TIM_ICInitStructure.TIM_ICFilter = 0; TIM_ICInit(TIM4, &TIM_ICInitStructure); /* One Pulse Mode selection */ TIM_SelectOnePulseMode(TIM4, TIM_OPMode_Single); /* Input Trigger selection */ TIM_SelectInputTrigger(TIM4, TIM_TS_TI2FP2); /* Slave Mode selection: Trigger Mode */ TIM_SelectSlaveMode(TIM4, TIM_SlaveMode_Trigger); while (1) { } }
/** * @brief Configure the TIM2. * @param None * @retval None */ static void TIM_Config(void) { GPIO_InitTypeDef GPIO_InitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_ICInitTypeDef TIM_ICInitStructure; TIM_OCInitTypeDef TIM_OCInitStructure; uint16_t PrescalerValue = 0; /* TIM clock enable */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); /* GPIOA clock enable */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); /* TIM2_CH1 pin (PA.00) and TIM2_CH2 pin (PA.01) configuration */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOA, &GPIO_InitStructure); /* Connect TIM pins to AF1 */ GPIO_PinAFConfig(GPIOA, GPIO_PinSource0, GPIO_AF_1); GPIO_PinAFConfig(GPIOA, GPIO_PinSource1, GPIO_AF_1); /* -------------------------------------------------------------------------- TIM2 configuration: One Pulse mode The external signal is connected to TIM2_CH2 pin (PA.01), The Rising edge is used as active edge, The One Pulse signal is output on TIM2_CH1 pin (PA.00) The TIM_Pulse defines the delay value The (TIM_Period - TIM_Pulse) defines the One Pulse value. TIM2 input clock (TIM2CLK) is set to 2*APB1 clock (PCLK1) TIM2CLK = 2*PCLK1 PCLK1 = HCLK => TIM2CLK = HCLK = SystemCoreClock TIM2CLK = SystemCoreClock, we want to get TIM2 counter clock at 36 MHz: Prescaler = (TIM2CLK / TIM2 counter clock) - 1 Prescaler = (SystemCoreClock /36 MHz) - 1 The Autoreload value is 65535 (TIM2->ARR), so the maximum frequency value to trigger the TIM2 input is 36000000/65535 = 549.3 Hz. The TIM_Pulse defines the delay value, the delay value is fixed to 455.1 us: delay = CCR1/TIM2 counter clock = 455.1 us. The (TIM_Period - TIM_Pulse) defines the One Pulse value, the pulse value is fixed to 1.36 ms: One Pulse value = (TIM_Period - TIM_Pulse) / TIM2 counter clock = 1.36 ms. Note: SystemCoreClock variable holds HCLK frequency and is defined in system_stm32f30x.c file. Each time the core clock (HCLK) changes, user had to call SystemCoreClockUpdate() function to update SystemCoreClock variable value. Otherwise, any configuration based on this variable will be incorrect. --------------------------------------------------------------------------- */ /* Compute the prescaler value */ PrescalerValue = (uint16_t) ((SystemCoreClock ) / 36000000) - 1; /* Time base configuration */ TIM_TimeBaseStructure.TIM_Period = 65535; TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); /* Init TIM_OCInitStructure */ TIM_OCStructInit(&TIM_OCInitStructure); /* TIM2 PWM2 Mode configuration: Channel1 */ TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = 16383; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; TIM_OC1Init(TIM2, &TIM_OCInitStructure); /* TIM configuration in Input Capture Mode */ TIM_ICStructInit(&TIM_ICInitStructure); TIM_ICInitStructure.TIM_Channel = TIM_Channel_2; TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; TIM_ICInitStructure.TIM_ICFilter = 0; TIM_ICInit(TIM2, &TIM_ICInitStructure); /* One Pulse Mode selection */ TIM_SelectOnePulseMode(TIM2, TIM_OPMode_Single); /* Input Trigger selection */ TIM_SelectInputTrigger(TIM2, TIM_TS_TI2FP2); /* Slave Mode selection: Trigger Mode */ TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_Trigger); }
/** * @brief Enable a pin as an input capture pin * @details Enables the appropriate timer channel and GPIO setting it * as an input capture input. Note that this could possibly overwrite * timer settings if multiple used pins share the same timer channel. * * @param pin Pin to use for IC * @return The associated IC timer channel */ ICTimerChan ic_enable_pin(uint16_t pin, float min_freq) { assert_param(IS_IC1_PIN(pin) || IS_IC4_PIN(pin)); ICTimerChan chan = _ic_get_chan(pin); // initialize IC timer channel TIM_ICInitTypeDef ic_init_struct; TIM_ICStructInit(&ic_init_struct); switch (chan) { case IC_CHAN1: ic_init_struct.TIM_Channel = TIM_Channel_1; break; case IC_CHAN2: ic_init_struct.TIM_Channel = TIM_Channel_2; break; case IC_CHAN3: ic_init_struct.TIM_Channel = TIM_Channel_3; break; case IC_CHAN4: ic_init_struct.TIM_Channel = TIM_Channel_4; break; default: //error ic_init_struct.TIM_Channel = 0xff; break; } ic_init_struct.TIM_ICPolarity = TIM_ICPolarity_Rising; ic_init_struct.TIM_ICSelection = TIM_ICSelection_DirectTI; ic_init_struct.TIM_ICPrescaler = TIM_ICPSC_DIV1; ic_init_struct.TIM_ICFilter = GE_IC_IFILTER; TIM_ICInit(TIM4, &ic_init_struct); //calculate new prescaler if minimum frequency is lower than previous //frequencies // 72e6/2^16 = 1098.6328125 uint16_t new_prescaler = (uint16_t) (ceilf(1098.6328125/min_freq) - 1); if (new_prescaler > _ge_ic_prescaler) { //change prescaler _ge_ic_prescaler = new_prescaler; TIM_PrescalerConfig(TIM4, _ge_ic_prescaler, TIM_PSCReloadMode_Immediate); } //enable pin GPIO_InitTypeDef gpio_struct; gpio_struct.GPIO_Mode = GPIO_Mode_AF; gpio_struct.GPIO_Pin = _ge_pin_num[pin]; gpio_struct.GPIO_OType = GPIO_OType_PP; gpio_struct.GPIO_Speed = GPIO_Speed_50MHz; gpio_struct.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(_ge_pin_port[pin], &gpio_struct); uint8_t pin_af; switch (pin) { case PD12: case PD13: case PD14: case PD15: case PF6: pin_af = GPIO_AF_2; break; case PA11: case PA12: case PA13: pin_af = GPIO_AF_10; break; default: //error pin_af = 0xff; } GPIO_PinAFConfig(_ge_pin_port[pin], _ge_pin_source[pin], pin_af); return chan; }
void TIM2_IRQHandler(void) { uint32_t current[4]; TIM_ICInitTypeDef TIM_ICInitStructure; TIM_ICStructInit(&TIM_ICInitStructure); if (TIM_GetITStatus(TIM2, TIM_IT_CC1) == SET) { /* Clear TIM1 Capture compare interrupt pending bit */ TIM_ClearITPendingBit(TIM2, TIM_IT_CC1); if ( inc[INC6].status == RISING ) { TIM_ICInitStructure.TIM_Channel = TIM_Channel_1; TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling; /* Get the Input Capture value */ inc[INC6].prev_value = TIM_GetCapture1(TIM2); inc[INC6].status = FALLING; } else { TIM_ICInitStructure.TIM_Channel = TIM_Channel_1; TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //Get the Input Capture value current[0] = TIM_GetCapture1(TIM2); if (current[0] > inc[INC6].prev_value) inc[INC6].curr_value = current[0] - inc[INC6].prev_value ; else if (current[0] < inc[INC6].prev_value) inc[INC6].curr_value = 0xFFFF - inc[INC6].prev_value + current[0] ; inc[INC6].status = RISING; } TIM_ICInit(TIM2, &TIM_ICInitStructure); } if (TIM_GetITStatus(TIM2, TIM_IT_CC2) == SET) { /* Clear TIM1 Capture compare interrupt pending bit */ TIM_ClearITPendingBit(TIM2, TIM_IT_CC2); if (inc[INC5].status == RISING) { TIM_ICInitStructure.TIM_Channel = TIM_Channel_2; TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling; /* Get the Input Capture value */ inc[INC5].prev_value = TIM_GetCapture2(TIM2); inc[INC5].status = FALLING; } else { TIM_ICInitStructure.TIM_Channel = TIM_Channel_2; TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; /* Get the Input Capture value */ current[1] = TIM_GetCapture2(TIM2); if (current[1] > inc[INC5].prev_value) inc[INC5].curr_value = current[1] - inc[INC5].prev_value ; else if (current[1] < inc[INC5].prev_value) inc[INC5].curr_value = 0xFFFF - inc[INC5].prev_value + current[1] ; inc[INC5].status = RISING; } TIM_ICInit(TIM2, &TIM_ICInitStructure); } if (TIM_GetITStatus(TIM2, TIM_IT_CC3) == SET) { /* Clear TIM1 Capture compare interrupt pending bit */ TIM_ClearITPendingBit(TIM2, TIM_IT_CC3); if (inc[INC2].status == RISING) { TIM_ICInitStructure.TIM_Channel = TIM_Channel_3; TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling; /* Get the Input Capture value */ inc[INC2].prev_value = TIM_GetCapture3(TIM2); inc[INC2].status = FALLING; } else { TIM_ICInitStructure.TIM_Channel = TIM_Channel_3; TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; /* Get the Input Capture value */ current[2] = TIM_GetCapture3(TIM2); if (current[2] > inc[INC2].prev_value) inc[INC2].curr_value= current[2] - inc[INC2].prev_value; else if (current[2] < inc[INC2].prev_value) inc[INC2].curr_value = 0xFFFF - inc[INC2].prev_value + current[2] ; inc[INC2].status = RISING; } TIM_ICInit(TIM2, &TIM_ICInitStructure); } if (TIM_GetITStatus(TIM2, TIM_IT_CC4) == SET) { /* Clear TIM1 Capture compare interrupt pending bit */ TIM_ClearITPendingBit(TIM2, TIM_IT_CC4); if (inc[INC1].status == RISING) { TIM_ICInitStructure.TIM_Channel = TIM_Channel_4; TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling; /* Get the Input Capture value */ inc[INC1].prev_value = TIM_GetCapture4(TIM2); inc[INC1].status = FALLING; } else { TIM_ICInitStructure.TIM_Channel = TIM_Channel_4; TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; /* Get the Input Capture value */ current[3] = TIM_GetCapture4(TIM2); if (current[3] > inc[INC1].prev_value) inc[INC1].curr_value = current[3] - inc[INC1].prev_value; else if (current[3] < inc[INC1].prev_value) inc[INC1].curr_value = 0xFFFF - inc[INC1].prev_value+ current[3] ; inc[INC1].status = RISING; } TIM_ICInit(TIM2, &TIM_ICInitStructure); } }
/** * @brief Handler for TIM4 global interrupt * @details TIM4 handler */ void TIM4_IRQHandler(void) { if (TIM_GetITStatus(TIM4, TIM_IT_CC1) != RESET) { TIM_ClearITPendingBit(TIM4, TIM_IT_CC1); _ge_ic_chan_captures_last[IC_CHAN1] = _ge_ic_chan_captures[IC_CHAN1]; _ge_ic_chan_captures[IC_CHAN1] = TIM_GetCapture1(TIM4); // set updated available flag _ge_ic_chan_freq_avail[IC_CHAN1] = true; //calculate actual counts based on timer overflow count if (_ge_ic_chan_captures_last[IC_CHAN1] > _ge_ic_chan_captures[IC_CHAN1]) { _ge_ic_chan_period[IC_CHAN1] = (uint32_t) _ge_ic_chan_captures[IC_CHAN1] + 65536 * (_ge_ic_chan_ovf[IC_CHAN1] - 1) - _ge_ic_chan_captures_last[IC_CHAN1]; } else { _ge_ic_chan_period[IC_CHAN1] = (uint32_t) _ge_ic_chan_captures[IC_CHAN1] + 65536 * (_ge_ic_chan_ovf[IC_CHAN1]) - _ge_ic_chan_captures_last[IC_CHAN1]; } _ge_ic_chan_ovf[IC_CHAN1] = 0; //reenable chan 1 IC TIM_ICInitTypeDef ic_init_struct; TIM_ICStructInit(&ic_init_struct); ic_init_struct.TIM_Channel = TIM_Channel_1; ic_init_struct.TIM_ICPolarity = TIM_ICPolarity_Rising; ic_init_struct.TIM_ICSelection = TIM_ICSelection_DirectTI; ic_init_struct.TIM_ICPrescaler = TIM_ICPSC_DIV1; ic_init_struct.TIM_ICFilter = GE_IC_IFILTER; TIM_ICInit(TIM4, &ic_init_struct); // _ge_ic_chan_captures[IC_CHAN1] = 3000; } if (TIM_GetITStatus(TIM4, TIM_IT_CC2) != RESET) { TIM_ClearITPendingBit(TIM4, TIM_IT_CC2); _ge_ic_chan_captures_last[IC_CHAN2] = _ge_ic_chan_captures[IC_CHAN2]; _ge_ic_chan_captures[IC_CHAN2] = TIM_GetCapture2(TIM4); // set updated available flag _ge_ic_chan_freq_avail[IC_CHAN2] = true; //calculate actual counts based on timer overflow count if (_ge_ic_chan_captures_last[IC_CHAN2] > _ge_ic_chan_captures[IC_CHAN2]) { _ge_ic_chan_period[IC_CHAN2] = (uint32_t) _ge_ic_chan_captures[IC_CHAN2] + 65536 * (_ge_ic_chan_ovf[IC_CHAN2] - 1) - _ge_ic_chan_captures_last[IC_CHAN2]; } else { _ge_ic_chan_period[IC_CHAN2] = (uint32_t) _ge_ic_chan_captures[IC_CHAN2] + 65536 * (_ge_ic_chan_ovf[IC_CHAN2]) - _ge_ic_chan_captures_last[IC_CHAN2]; } _ge_ic_chan_ovf[IC_CHAN2] = 0; //reenable chan 2 IC TIM_ICInitTypeDef ic_init_struct; TIM_ICStructInit(&ic_init_struct); ic_init_struct.TIM_Channel = TIM_Channel_2; ic_init_struct.TIM_ICPolarity = TIM_ICPolarity_Rising; ic_init_struct.TIM_ICSelection = TIM_ICSelection_DirectTI; ic_init_struct.TIM_ICPrescaler = TIM_ICPSC_DIV1; ic_init_struct.TIM_ICFilter = GE_IC_IFILTER; TIM_ICInit(TIM4, &ic_init_struct); } if (TIM_GetITStatus(TIM4, TIM_IT_CC3) != RESET) { TIM_ClearITPendingBit(TIM4, TIM_IT_CC3); _ge_ic_chan_captures_last[IC_CHAN3] = _ge_ic_chan_captures[IC_CHAN3]; _ge_ic_chan_captures[IC_CHAN3] = TIM_GetCapture3(TIM4); // set updated available flag _ge_ic_chan_freq_avail[IC_CHAN3] = true; //calculate actual counts based on timer overflow count if (_ge_ic_chan_captures_last[IC_CHAN3] > _ge_ic_chan_captures[IC_CHAN3]) { _ge_ic_chan_period[IC_CHAN3] = (uint32_t) _ge_ic_chan_captures[IC_CHAN3] + 65536 * (_ge_ic_chan_ovf[IC_CHAN3] - 1) - _ge_ic_chan_captures_last[IC_CHAN3]; } else { _ge_ic_chan_period[IC_CHAN3] = (uint32_t) _ge_ic_chan_captures[IC_CHAN3] + 65536 * (_ge_ic_chan_ovf[IC_CHAN3]) - _ge_ic_chan_captures_last[IC_CHAN3]; } _ge_ic_chan_ovf[IC_CHAN3] = 0; //reenable chan 3 IC TIM_ICInitTypeDef ic_init_struct; TIM_ICStructInit(&ic_init_struct); ic_init_struct.TIM_Channel = TIM_Channel_3; ic_init_struct.TIM_ICPolarity = TIM_ICPolarity_Rising; ic_init_struct.TIM_ICSelection = TIM_ICSelection_DirectTI; ic_init_struct.TIM_ICPrescaler = TIM_ICPSC_DIV1; ic_init_struct.TIM_ICFilter = GE_IC_IFILTER; TIM_ICInit(TIM4, &ic_init_struct); } if (TIM_GetITStatus(TIM4, TIM_IT_CC4) != RESET) { TIM_ClearITPendingBit(TIM4, TIM_IT_CC4); _ge_ic_chan_captures_last[IC_CHAN4] = _ge_ic_chan_captures[IC_CHAN4]; _ge_ic_chan_captures[IC_CHAN4] = TIM_GetCapture4(TIM4); // set updated available flag _ge_ic_chan_freq_avail[IC_CHAN4] = true; //calculate actual counts based on timer overflow count if (_ge_ic_chan_captures_last[IC_CHAN4] > _ge_ic_chan_captures[IC_CHAN4]) { _ge_ic_chan_period[IC_CHAN4] = (uint32_t) _ge_ic_chan_captures[IC_CHAN4] + 65536 * (_ge_ic_chan_ovf[IC_CHAN4] - 1) - _ge_ic_chan_captures_last[IC_CHAN4]; } else { _ge_ic_chan_period[IC_CHAN4] = (uint32_t) _ge_ic_chan_captures[IC_CHAN4] + 65536 * (_ge_ic_chan_ovf[IC_CHAN4]) - _ge_ic_chan_captures_last[IC_CHAN4]; } _ge_ic_chan_ovf[IC_CHAN4] = 0; //reenable chan 4 IC TIM_ICInitTypeDef ic_init_struct; TIM_ICStructInit(&ic_init_struct); ic_init_struct.TIM_Channel = TIM_Channel_4; ic_init_struct.TIM_ICPolarity = TIM_ICPolarity_Rising; ic_init_struct.TIM_ICSelection = TIM_ICSelection_DirectTI; ic_init_struct.TIM_ICPrescaler = TIM_ICPSC_DIV1; ic_init_struct.TIM_ICFilter = GE_IC_IFILTER; TIM_ICInit(TIM4, &ic_init_struct); } if (TIM_GetITStatus(TIM4, TIM_IT_Update) != RESET) { TIM_ClearITPendingBit(TIM4, TIM_IT_Update); for (int i = 0; i < 4; i++) { _ge_ic_chan_ovf[i]++; } } }