2013-05-17 03:23 PM
Since 4 day I'm trying write code which will be able to count external pulses. I tried to use encoder mode but i supposed that this work only with 2 quadrature encoders. I have only one pulse signal and i have no idea how to do it. Could you help me or show examples how to count external pulses using timer?
#counter #timer #off-topic #pulce2013-05-18 03:59 AM
This is a blind conversion of a previous example to the Discovery-F4
void TIM3_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
/* GPIOC clock enable */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
/* TIM3 clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
/* GPIOA Configuration: TIM3 CH1 (PC6) */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; // Input/Output controlled by peripheral
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; // Button to ground expectation
GPIO_Init(GPIOC, &GPIO_InitStructure);
/* Connect TIM3 pins to AF */
GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_TIM3);
TIM_TimeBaseStructure.TIM_Period = 65535;
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
TIM_TIxExternalClockConfig(TIM3, TIM_TIxExternalCLK1Source_TI1, TIM_ICPolarity_Rising, 0);
TIM_Cmd(TIM3, ENABLE);
} // sourcer32@gmail.com
2013-05-31 09:56 AM
My first post here, hi all! :)
I just wanted to say: thak you so much for that code, Clive, I've been struggling with that configuration for 2 days. Cheers, Mike2013-08-15 05:32 AM
My MCU is an STM32F103x, but I made your example work on it too, thanks.
The question I have: is it possible to have a timer to output clock on one channel and same timer to count external pulses on its other channel? I.e. clocking on one line, and receiving bits/pulses on the other and counting them. With example here i only see how to do this with 2 timers.2013-08-15 06:37 AM
The problem here is that the TIM units have only a single counting element, the channels provide four latches to either compare or hold values with respect to the counter.
2013-08-15 07:07 AM
Yeah, I just thought i give it a shot :)
But using the above pulse counting. Can this be used in a useful way to record PDM ? As in, clock the PDM mic with one timer, and record PDM pulses with the other by configuring it to count the pulses.2013-08-15 09:05 AM
I don't know, you're wading into an area where I have little practical experience.
I can see ways to generate signals, and measure things with the same timer, you'll have to look at the block diagram to see if the task you want to do is possible.2013-09-04 05:04 AM
In this external clock source example, what's the behavior of CCR1? I see it's = CNT-1 always for this config, and. .. not sure on why :), i'm not using channel 1 for capture but for clock source.
adapted to TM1 config: GPIO_InitTypeDefGPIO_InitStructure
;
TIM_TimeBaseInitTypeDef
TIM_TimeBaseStructure
;
RCC_APB2PeriphClockCmd
(
RCC_APB2Periph_GPIOA|
RCC_APB2Periph_TIM1,
ENABLE);
/*
GPIOA
Configuration:
TIM1
CH1
=
PA8
*/
GPIO_InitStructure
.
GPIO_Pin=
GPIO_Pin_8;
GPIO_InitStructure
.
GPIO_Mode=
GPIO_Mode_IN_FLOATING;
GPIO_InitStructure
.
GPIO_Speed=
GPIO_Speed_2MHz;
GPIO_Init
(
GPIOA,
&
GPIO_InitStructure
);
TIM_TimeBaseStructure
.
TIM_Period=
65535
;
TIM_TimeBaseStructure
.
TIM_Prescaler=
0
;
TIM_TimeBaseStructure
.
TIM_ClockDivision=
0
;
TIM_TimeBaseStructure
.
TIM_CounterMode=
TIM_CounterMode_Up;
TIM_TimeBaseInit
(
TIM1,
&
TIM_TimeBaseStructure
);
TIM_TIxExternalClockConfig
(
TIM1,
TIM_TIxExternalCLK1Source_TI1,
TIM_ICPolarity_Rising,
0x0
);
TIM_Cmd(
TIM1,
ENABLE);
2013-09-04 05:23 AM
TIM1 and 8 are special
/* Main Output (and input) Enable */ TIM_CtrlPWMOutputs(TIM1, ENABLE); CCR1 plays NO role2013-09-04 07:18 AM
mmmm, don't know TIM_CtrlPWMOutputs(TIM1, ENABLE);
would show me. I wanted to use CCR1 for an interrupt at some counts but it updates at same rate as CNT, that's why I asked what's it doing. I can use CCR2 fine.