cancel
Showing results for 
Search instead for 
Did you mean: 

TIMER starter

siiiiiiiiiib_red
Associate II
Posted on May 14, 2016 at 06:29

Hi everyone !

Im a starter in ST and need help for timers.

I need a timer to give me an interrupt every 1 sec. A  second timer with external clock keep counting the clock source and in first timer interrupt; second timer counted number display.

can any1 help me with this? ty
6 REPLIES 6
Posted on May 14, 2016 at 16:05

Suggest you review some library examples for the specific STM32 family part/board you are planning on using. Get familiar with how the code builds and runs on your board. Once you have some basic understanding of the tools, then move on to specific tasks/implementation.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
siiiiiiiiiib_red
Associate II
Posted on May 15, 2016 at 06:16

tx for your replay. I just made a Timebase for count every 1 sec and I enabled it's interrupt. now all  I need is a counter to count it's external clock.
siiiiiiiiiib_red
Associate II
Posted on May 15, 2016 at 10:51

I wrote this code. can u check it plz

And I used usart

#include ''main.h''

#include ''stm32f10x_gpio.h''

#include ''stm32f10x_rcc.h''

#include ''stm32f10x_usart.h''

#include ''stm32f10x_tim.h''

#include <misc.h>

void SystemInit ()

{}

char a[4]={ 's' , 'l' , 'a' , 'm' };

  int i=0;

 int main()

{

  

  GPIO_InitTypeDef gpio_struct;

   USART_InitTypeDef USART_struct; 

  NVIC_InitTypeDef NVIC_struct;

  TIM_TimeBaseInitTypeDef  TIM_TimeBasestruct; 

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

  gpio_struct.GPIO_Pin = USARTy_RxPin;

    gpio_struct.GPIO_Speed = GPIO_Speed_2MHz;

    gpio_struct.GPIO_Mode = GPIO_Mode_IPU;

    GPIO_Init(USARTy_GPIO, &gpio_struct);

  

  

  gpio_struct.GPIO_Pin = USARTy_TxPin;

    gpio_struct.GPIO_Speed = GPIO_Speed_2MHz;

    gpio_struct.GPIO_Mode = GPIO_Mode_Out_PP;

    GPIO_Init(USARTy_GPIO, &gpio_struct);

  

  

   USART_struct.USART_BaudRate = 115200;

  USART_struct.USART_WordLength = USART_WordLength_8b;

  USART_struct.USART_StopBits = USART_StopBits_1;

  USART_struct.USART_Parity = USART_Parity_No;

  USART_struct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;

  USART_struct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

  USART_Init (USART1, &USART_struct);

  

  

  USART_ITConfig(USARTy, USART_IT_RXNE, ENABLE);

  USART_Cmd(USARTy, ENABLE);

  

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);

  TIM_TimeBasestruct.TIM_Period = 8999999;

    TIM_TimeBasestruct.TIM_Prescaler = 1;

  TIM_TimeBasestruct.TIM_ClockDivision = 2;

  TIM_TimeBasestruct.TIM_CounterMode = TIM_CounterMode_Up;

  TIM_TimeBaseInit(TIM3, &TIM_TimeBasestruct);

  /* Prescaler configuration */

 // TIM_PrescalerConfig(TIM2, 0 , TIM_PSCReloadMode_Immediate);

  

  TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);

  TIM_Cmd(TIM3, ENABLE);

  

   

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

  TIM_TIxExternalClockConfig  ( TIM2 , TIM_TIxExternalCLK1Source_TI2 , TIM_ICPolarity_Rising , 0x0 );

  TIM_TimeBasestruct.TIM_Period = 0;

    TIM_TimeBasestruct.TIM_Prescaler = 0;

  TIM_TimeBasestruct.TIM_ClockDivision = 0;

  TIM_TimeBasestruct.TIM_CounterMode = TIM_CounterMode_Up;

  TIM_TimeBaseInit(TIM2, &TIM_TimeBasestruct);

  

  TIM_Cmd(TIM2, ENABLE);

  

  NVIC_struct.NVIC_IRQChannel = USARTy_IRQn;

  NVIC_struct.NVIC_IRQChannelSubPriority = 0;  

  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);

  NVIC_struct.NVIC_IRQChannelCmd = ENABLE;

  NVIC_Init(&NVIC_struct);

  

 while (1)

{

}

}

void TIM3_IRQHandler(void)/*            STM32F10x Peripherals Interrupt Handlers                        */

{

 int b;

 if(TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)

  {

  b = TIM_GetCounter  ( TIM3 ) ;

    TIM_SetCounter  ( TIM3 , 0);

  

  

  

  

  

  

  

  

  

  

  

  i=0;

  for (i=0 ; i<5 ; i++)

  {

  USART_SendData(USART1 , a[i]);

  } 

  }

  TIM_ClearITPendingBit (TIM3 , TIM_IT_Update); 

 }

 

 

 

 

 void assert_param (int a)

{

if (a==0)

{

while (1) {};

}

}
Posted on May 15, 2016 at 17:57

TIM_TimeBasestruct.TIM_Period = 8999999; 

    TIM_TimeBasestruct.TIM_Prescaler = 1;

  TIM_TimeBasestruct.TIM_ClockDivision = 2;

The period and prescaler values are only 16-bit wide, clock division probably doesn't do what you think it might. Period = 0 is also not workable. The update interrupt occurs when the counter transitions to zero, no value to setting the counter to zero in the interrupt.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
siiiiiiiiiib_red
Associate II
Posted on May 16, 2016 at 06:23

Thank you for Your answers. I appreciate that.

Walid FTITI_O
Senior II
Posted on May 16, 2016 at 12:09

Hi golem.jj,

As a solution for your requirement: you will need to configure two timers interconnected (synchronized) in master/slave :

The first one will be the master (i.e TIM1) and will trigger the second timer (i.e TIM2) when it reaches the programmed CCx value (Pulse value), an update event occurs and the trigs through a TRGO signal. The second one TIM2, is configured as slave trigger to be triggred by TIM1 through its ITx input. In other hand , TIM2 is configured in input capture mode: the external signal is connected in TIM1_channelx used as input pin. To measure the frequency we use HAL_TIM_TriggerCallback() where we calculate the frequency.

To get started in implementation, I suggest that you you get bases and start from the following HAL example of STM32CubeF4 (i.e):

- For Timer synchronization: run the ''TIM_Synchronization'' example.

- For input capture configuration: run th ''TIM_InputCapture'' example ( in your case the calculation will be done in HAL_TIM_TriggerCallback() instead TIM1_CC_IRQHandler ())

Ps: Both example are available under this path: STM32Cube_FW_F4_V1.11.0\Projects\STM324x9I_EVAL\Examples\TIM\TIM_Synchronization

-Hannibal-