AnsweredAssumed Answered

TIM5 External Clock Counter Issue

Question asked by Hannah Rodgers on Jul 17, 2017
Latest reply on Jul 20, 2017 by Hannah Rodgers

I am using the STM32F4 board's TIM8 in order to generate a quadrature PWM signal at varying frequencies. That is working great; however, I need to decode the signal as it is being produced, aka, count the rising edges of channels A and B. I've tried using TIM8 to count it and I got huge numbers. When I tried to use TIM5 as shown below, the TIM_GetCounter(TIMx) function returns zero.  Any suggestions? Thank you in advance!

 

void InitializeTIM()
{
//sets clock for TIM8
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8, ENABLE);
//sets clock for TIM5
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE); //MAY NEED TO CHANGE CLOCK

// PERIOD -> DEPENDS ON VELOCITY
float velocity = 0.25; //0.1; //meters/s
uint16_t half_period = Calculate_Period(velocity);

TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure8; //creates structure for TIM8
TIM_OCInitTypeDef TIM_OCInitStructure; //creates structure for PWM
//Time base configuration TIM8
TIM_TimeBaseStructure8.TIM_Prescaler = PSC_Value;
TIM_TimeBaseStructure8.TIM_Period = half_period;
TIM_TimeBaseStructure8.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure8.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM8, &TIM_TimeBaseStructure8);

//Enable TIM8 Preload register on ARR
TIM_ARRPreloadConfig(TIM8, ENABLE);

//TIM8 PWM1 Configuration --> 50% duty cycle
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;

//CHANNEL A -> PC8
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_Pulse = half_period; // PHASE SHIFT
TIM_OC3Init(TIM8, &TIM_OCInitStructure);

//CHANNEL B -> PC9
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OCInitStructure.TIM_Pulse = (half_period/2); // PHASE SHIFT
TIM_OC4Init(TIM8, &TIM_OCInitStructure);


TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure5; //creates structure for TIM5
//Time base configuration TIM5
TIM_TimeBaseStructure5.TIM_Prescaler = PSC_Value*0.3071;
TIM_TimeBaseStructure5.TIM_Period = 65535;
TIM_TimeBaseStructure5.TIM_ClockDivision = 0;
TIM_TimeBaseStructure5.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM5, &TIM_TimeBaseStructure5);

//Encoder count configuration TIM5
TIM_ETRClockMode1Config(TIM5, TIM_ExtTRGPSC_OFF, TIM_ExtTRGPolarity_NonInverted, 0x00); //timer used, no prescaler, active on rising edge, no filter

// Enables counter
TIM_Cmd(TIM8, ENABLE);
TIM_Cmd(TIM5, ENABLE);

}

void InitializePINS()
{
//******************** TIM 8 ********************//
//GENERATING PWM
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
//creates structure for GPIOC
GPIO_InitTypeDef GPIO_InitStructure;

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;//PC8 & PC9
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //alternative function
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //operating output
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; //push or pull of pin
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; // speed of pins -> slower saves power and cancels out some of the noise
GPIO_Init(GPIOC, &GPIO_InitStructure); //intializes structure

// Sets pin's alternative function to that of TIM8
GPIO_PinAFConfig(GPIOC, GPIO_PinSource8, GPIO_AF_TIM8);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource9, GPIO_AF_TIM8);



//******************** TIM 5 ********************//
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
//creates structure for GPIOE
GPIO_InitTypeDef GPIO_InitStructure2;

GPIO_InitStructure2.GPIO_Pin = GPIO_Pin_2; //| GPIO_Pin_3;
GPIO_InitStructure2.GPIO_Mode = GPIO_Mode_AF; //alternative function
GPIO_InitStructure2.GPIO_PuPd = GPIO_PuPd_UP; //push or pull of pin
GPIO_Init(GPIOA, &GPIO_InitStructure2); //intializes structure

// Sets pin's alternative function to that of TIM5
GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_TIM5);
//GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_TIM5);
//******************** TIM 5 ********************/


}

int main(void)
{
//Initializing functions
InitializePINS();
InitializeTIM();

pulse_counter = 0;
previous_round = 0;

while(1)
{
TIM_CtrlPWMOutputs(TIM8, ENABLE);
pulse_counter = TIM_GetCounter(TIM5);
totalPulses = cycle_counter - previous_round;
previous_round = cycle_counter;

}

return 0;
}

Outcomes