cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F4 - read timer value

pano
Associate II
Posted on November 24, 2014 at 17:27

Hi,

I have this timer

void InitializeTimer(void)
{ 

TIM_TimeBaseInitTypeDef timerInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
timerInitStructure.TIM_Prescaler = 0;
timerInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
timerInitStructure.TIM_Period = (84000000 / 2000) - 1; //2000Hz 

timerInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
timerInitStructure.TIM_RepetitionCounter = 0;
/* Activación de la interrupción */
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn; //Activa todos los canales
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
TIM_TimeBaseInit(TIM2, &timerInitStructure); // guarda cambios en init timer
NVIC_Init(&NVIC_InitStructure); // guarda cambios en el registro nvic
//Activa el Timer 
TIM_Cmd(TIM2, ENABLE); 
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
}

how to read the value from the main fuction? thanks
3 REPLIES 3
Posted on November 24, 2014 at 19:17

current = TIM2->CNT;

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
pano
Associate II
Posted on November 25, 2014 at 11:44

ok thank.

I have a new simple timer

void INTTIM_Config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
TIM_TimeBaseStructure.TIM_Period = 100000 - 1; // 1 MHz down to 10 Hz
TIM_TimeBaseStructure.TIM_Prescaler = 84 - 1; // 84Mhz Down to 1 MHz 
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
/* TIM2 enable counter */
TIM_Cmd(TIM3, ENABLE);
}

10 Hz. in the main function:

while(1) {
current=TIM3->CNT;
printf(''%i
'', current); }

but it's too fast respect the timer; how to

synchronize

the main fuction with the timer? I would like to read 10 printf per second, not

thousand

s thanks
Posted on November 25, 2014 at 14:00

Consider using SysTick to mark time, or evaluate how far the counter has advanced before printing it.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..