0
\$\begingroup\$

I am using a STM (stm32l432kb) microcontroller. In my application I need to get the data from the sensor at fixed interval(based on the selected Mode). Like I have to select the Modes:-
Mode 1 : which output the data at fixed interval of 5 sec.
Mode 2 : Which output the data at fixed interval of 10 sec.
Mode 3 : Which output the data at fixed interval of 15 sec.

Here I am little bit confused that how to get this data based on modes.

\$\endgroup\$
4
  • \$\begingroup\$ is this a school assignment? .... monitor elapsed time in 5 second increments (call them ticks) .... output data every tick, 2 ticks, etc. \$\endgroup\$
    – jsotola
    Commented May 2, 2018 at 5:47
  • \$\begingroup\$ Do you configure the data transmission time interval at the sensor itself--i.e., the sensor automatically and periodically transmits data, and the microcontroller must then capture this data when it arrives? Or is the microcontroller itself generating these time intervals? \$\endgroup\$ Commented May 2, 2018 at 7:20
  • \$\begingroup\$ No, this not a school assignment. This is my idea to make a such application in which I can make program having all this mode .... Simple and nothing else ..... \$\endgroup\$ Commented May 7, 2018 at 13:02
  • \$\begingroup\$ No i have not configure the data transmission time interval for this. \$\endgroup\$ Commented May 7, 2018 at 13:05

2 Answers 2

2
\$\begingroup\$

An idea is to make use of timer interrupts. The "Mode" will reset the timer and decide the initial value loaded to timer. The loaded value depends on time interval you want. On each timer interrupt, the control will shift to its ISR, where you read the sensor value.

Pseudocode will look something like:

Timer_ISR()    
{
x = readSensor();
}
\$\endgroup\$
1
  • \$\begingroup\$ Okay I will try your timer interrupt ISR to get data from different sensor ... \$\endgroup\$ Commented May 7, 2018 at 13:07
0
\$\begingroup\$

If you use cubemx and HAL_Driver layer, for example your update timer frequency is 40MHZ (you will see in schematic clock in cubeMx), and your initial conditions for the timer was, for example:

  htim2.Instance = TIM2;
  htim2.Init.Prescaler = 40000;                       //40e6/40e3 = 1000 ms
  htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim2.Init.Period = 5000;                   //If you want five seconds for initial interruption
  htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  if (HAL_TIM_Base_Init(&htim2) != HAL_OK)

With this code you would know your timer interrupt each 5 seconds. Then, the code based on your selected mode, I supposed could be something like this:

void prepare_timer(char mode){
    htim2.Init.Period = mode * 5000;
    TIM_Base_SetConfig(TIM2, &htim2.Init);
}

And then, you only should to configurate timer across of two line code. Obviously, before all this, you need to activate NVIC in CubeMX. And put this function prepare_timer() inside of the handler:

void TIM2_IRQHandler(void)
{
  /* USER CODE BEGIN TIM2_IRQn 0 */

  /* USER CODE END TIM2_IRQn 0 */
  HAL_TIM_IRQHandler(&htim2);
  /* USER CODE BEGIN TIM2_IRQn 1 */
--------------maybe here----------------------
  /* USER CODE END TIM2_IRQn 1 */
}

Maybe you must to do __HAL_TIM_ENABLE(&htim2); or something for enabling the count etc.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.