cancel
Showing results for 
Search instead for 
Did you mean: 

timer3 capture issue on stm32f103c8 device

Bogdan
Senior
Posted on June 19, 2016 at 10:08

hello all.

i have a signal of 1800us period which consists of 1500us tON and 300us tOFF. this signal is provided to PB0 and PB1 which are the timer3 inputs 3&4 my timer3 clock is 72mhz , the init code is bellow

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3 , ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOA , ENABLE);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);

GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
// configure Timer 3 time base 
TIM_TimeBaseStructure.TIM_Prescaler = 65000;
TIM_TimeBaseStructure.TIM_Period = 71;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
// configure Timer3 Channel 3 
TIM_ICInitStructure.TIM_Channel = TIM_Channel_3;
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 = 0x00;
TIM_ICInit(TIM3, &TIM_ICInitStructure);
// configure Timer3 Channel 4 
TIM_ICInitStructure.TIM_Channel = TIM_Channel_4;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_IndirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICFilter = 0x00;
TIM_ICInit(TIM3, &TIM_ICInitStructure);
// Configure Timer3 Interupt Sources 
TIM_ITConfig(TIM3, TIM_IT_CC3, ENABLE); // enable Channel 3 capture interupt
TIM_ITConfig(TIM3, TIM_IT_CC4, ENABLE);// enable Channel 4 capture interupt
TIM_SelectInputTrigger(TIM3,TIM_TS_TI1FP1);
TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_Reset);
//enable tim3 irq
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority =6;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 6;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_Cmd(TIM3, ENABLE); // Start the Timer

now comes the irq handler

void TIM3_IRQHandler(void)
{
if(TIM_GetITStatus(TIM3, TIM_IT_CC3) == SET){ // If Low-Hi transition occured
TIM_ClearITPendingBit(TIM3, TIM_IT_CC3); 
sss[0] = TIM_GetCapture3(TIM3); // Get timer counts
}
if(TIM_GetITStatus(TIM3, TIM_IT_CC4) == SET){ // If Hi-Low transition occured
TIM_ClearITPendingBit(TIM3, TIM_IT_CC4); 
sss[1] = TIM_GetCapture4(TIM3); // Get timer counts
delta=sss[1]-sss[0];
}
}

So, from my judgement the counter is configured to tick at a rate of 1us ( 1MHZ), this means the CNT regiter is counting with a speed of 1us. My problem is i am not able to read the expected values from the counter register in the falling edge ( channel4) the return value is always 0..2, and i would expect at least 1000 ticks, why is that?
5 REPLIES 5
Posted on June 19, 2016 at 14:19

No, the prescaler needs to be 72-1 for the ticks to be 1MHz. The period needs to be 65535 for the 16-bit math to work properly.

The slave and reset settings should not be used here.

The indirect mode means it is looking at a single pin, ch3 in this case.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Bogdan
Senior
Posted on June 19, 2016 at 19:21

Hello Clive,

i tryied even with 71, or 72... and the same result. I even tryied to move from ch3 & ch4 to channel 1 &2 with the same result....

I disabled the code for the slave reset ... when i read the rising edge counted quantity i see its rising from 0 to max 60.... Its like the cnt register ticks until 60, instead of 65535...

I also checked the RCC_APB2Periph_GPIOB clock source and its 72mhz like expected...

What is strange, if i put a update interrupt at a lower frequency than 1MHZ and put a toggle gpio port, i can see it toogles at the corect frequency

Posted on June 19, 2016 at 20:00

Unfortunately incomplete fragments of code, and rehashing the same issues gets tiresome.

Post complete and concise examples, where all the variables, interactions, and context are clear. If the CNT is wrapping at 60 something else is going on. I have clarity of thought not clairvoyance. Put your signal on ONE pin. Do your debugging outside the interrupt, ideally outputting telemetry to a terminal.

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3 , ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOA , ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);

GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
// configure Timer 3 time base 
TIM_TimeBaseStructure.TIM_Prescaler = 72-1;
TIM_TimeBaseStructure.TIM_Period = 65535; // Maximal
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
// configure Timer3 Channel 3
TIM_ICInitStructure.TIM_Channel = TIM_Channel_3;
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 = 0x00;
TIM_ICInit(TIM3, &TIM_ICInitStructure);
// configure Timer3 Channel 4
TIM_ICInitStructure.TIM_Channel = TIM_Channel_4;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_IndirectTI; // Using CH3 input
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICFilter = 0x00;
TIM_ICInit(TIM3, &TIM_ICInitStructure);
// Configure Timer3 Interupt Sources 
TIM_ITConfig(TIM3, TIM_IT_CC3 | TIM_IT_CC4, ENABLE); // enable Channel 3/4 capture interupt
//enable tim3 irq
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority =6; //?
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 6; //?
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_Cmd(TIM3, ENABLE); // Start the Timer

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Bogdan
Senior
Posted on June 19, 2016 at 20:10

Hmmm.... with your code it works.... i will do a comparison to see where is the problem.

In the main program i had a usart routine which sends the ''Delta'' from the IRQ handler to a terminal program

I own you some b33rs :)

Bogdan
Senior
Posted on June 19, 2016 at 20:28

Oh... my problem was the mixing from the prescaler and the period values.... i put them viceversa... i fixed this, and also my code works

Sometimes you can't see the trees because of the forest