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-09-04 07:51 AM
Probably acting in input capture mode, or whatever the default settings are.
/* External input pulse on CH1 */ TIM1->CCR1 = TIM1->CNT; TIM1->CNT++; Like I said, I don't think it plays any functional role related to the external count input routing.2013-09-05 12:42 AM
Ok clive1 🙂
2014-03-24 04:57 PM
Quick port for F1 series parts
void TIM3_Configuration(void) // STM32F1xx
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
/* GPIOC clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE);
/* TIM3 clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
/* GPIO Configuration: TIM3 CH1 (PC6) */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_PinRemapConfig(GPIO_FullRemap_TIM3, ENABLE); // Get TIM3 to PC6
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
2014-08-07 11:55 AM
/* pulse_counter.phr */
#include ''main.h'' int cnt; void TIM3_Configuration(void); void TIM3_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; /* GPIOC clock enable */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_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_2MHz; 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_0); 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_TI2, TIM_ICPolarity_Rising, 0); TIM_Cmd(TIM3, ENABLE); } int main(void) { while(1) { TIM3_Configuration(); cnt=TIM3->CNT ; } } i used this code to count the external pulse , but the counter is not counting . is there any mistakes in the code??2014-08-07 12:19 PM
You must copy-n-paste better,
TIM_TIxExternalClockConfig(TIM3, TIM_TIxExternalCLK1Source_TI2, TIM_ICPolarity_Rising, 0); // NOT TI2 And don't keep reconfiguring the timer in the while() loop2014-08-07 09:28 PM
Thanks for the guidance Clive1
I moved the timer config statement out of the while loop and replaced TI2 with TI1 but still the counter is not working I am using STM32f051 eval board2014-08-08 12:46 PM
Hi clive1 ,
I did try using different timer (TIM1) this one showed some response to the external pulse but it counted randomly. I tried to my best to make it count but didn't workout . can I learn how to make it work from you. thanks#include ''main.h''
int count=0;
void main()
{
// INTI CLOCK FOR TIM AND GPIO
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
/* GPIOC clock enable */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
/* TIM1 clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_2);
TIM_TimeBaseStructure.TIM_Period = 1000;
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
TIM_TIxExternalClockConfig(TIM1, TIM_TIxExternalCLK1Source_TI2, TIM_ICPolarity_Rising, 0);
TIM_Cmd(TIM1, ENABLE);
while(1)
{
count = TIM1->CNT;
}
}
2014-08-08 09:24 PM
As I don't have a board, try this blind modification
#include ''main.h''
int count=0;
void main()
{
// INTI CLOCK FOR TIM AND GPIO
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
/* GPIOC clock enable */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
/* TIM1 clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_2); // TIM1_CH2
TIM_TimeBaseStructure.TIM_Period = 1000 - 1;
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
TIM_TIxExternalClockConfig(TIM1, TIM_TIxExternalCLK1Source_TI2, TIM_ICPolarity_Rising, 0);
TIM_CtrlPWMOutputs(TIM1, ENABLE);
TIM_Cmd(TIM1, ENABLE);
while(1)
{
count = TIM1->CNT;
}
}
2014-08-09 08:48 AM
Hi Clive ,
I got some example code form the reference manual, and it was working but i was not able to understand these lines.where can I learn to code like this.GPIOA->MODER = (GPIOA->MODER & ~(GPIO_MODER_MODER9))| (GPIO_MODER_MODER9_1); /* (3) */
//GPIOA->PUPDR = 0x24000000;
GPIOA->PUPDR = 0x1 <<
17
;
GPIOA->AFR[1] |= 0x2 << ((9-8)*4); /* (4) */
2014-08-09 09:41 AM
If you want to learn register level minutia, then you'll need to absorb all the detail in the Reference Manual, and the bits and functions, and pin level associations. Hours of fun, not a style of programming I would recommend.