Page 1 of 1

Timer starts and then freezes.

Posted: Wed Apr 01, 2020 12:09 am
by Fabaum
Hello friends,

I am modifying an example program for testing the TIM2 general purpose timer of the STM32L476RG board.
Just to check operation, LED1 is activated periodically by the timer. At the same time, I use an oscilloscope connected to pin PA5, which corresponds to LED1.
With the fixed timing value, the program works perfectly.
However, after implementing a potentiometer to change the timing, I start to have problems.
LED1 just starts to switch, but then it freezes ...
The program is compiled without errors, I do not understand why it is not working in this configuration with the potentiometer.
Here is the program:

Code: Select all

#define pin  PA5
TIM_TypeDef *Instance = TIM2;
HardwareTimer *MyTim = new HardwareTimer(Instance);
int potenciometro;
int tempo=10000;

void setup()
{
  pinMode(pin, OUTPUT);

  MyTim->setMode(2, TIMER_OUTPUT_COMPARE); 
  MyTim->setOverflow(tempo, MICROSEC_FORMAT);
  MyTim->attachInterrupt(Update_IT_callback);
  MyTim->resume();
}

void loop()
{
  potenciometro = analogRead(PA1);
  tempo=map(potenciometro,10,1023,100000,100);
  MyTim->setOverflow(tempo, MICROSEC_FORMAT);
}

void Update_IT_callback(HardwareTimer*)
{ 
  MyTim->pause();
  digitalWrite(pin, !digitalRead(pin));
  MyTim->resume();
}
By eliminating the line "MyTim->setOverflow(tempo, MICROSEC_FORMAT);" inside void loop (), the program works perfectly with fixed timing.
I don't want to make a PWM, I'm just testing the functionality of timer 2 with a potentiometer.
What am I doing wrong?

Re: Timer starts and then freezes.

Posted: Wed Apr 01, 2020 10:53 am
by ABOSTM
Hi @Fabaum,
line below looks strange to me:

Code: Select all

tempo=map(potenciometro,10,1023,100000,100);
What happends if potencimetro is below 10 ?
Also the 3rd argument should be greater than the 4th one.
So can you try with this :

Code: Select all

tempo=map(potenciometro, 0,1024, 100, 100000);
Can you also print the values of potencimetro and tempo to check is there is no abnormal values.

Re: Timer starts and then freezes.

Posted: Wed Apr 01, 2020 1:16 pm
by Fabaum
Hi ABOSTM,

I am viewing the potentiometer values on an LCD display. Even with the potentiometer at its minimum value, the board's analog input reads 10 bytes, certainly due to some residual resistance. So, stating 10 as the minimum value, I establish that this is my zero point.
The fact that I am reversing the output signals means that the more the potentiometer is turned, the less time I must reload on timer2, and the faster it will overflow.
I found out what was wrong: the reload of timer2 must occur during the interruption, that is, within the "void Update_IT_callback (HardwareTimer *)" subroutine, before "MyTim-> resume ();".
Thanks for your help!

Re: Timer starts and then freezes.

Posted: Wed Apr 01, 2020 1:27 pm
by Bakisha
I don't know for sure if integer in Arduino IDE is 16bit or 32bit signed value, but setOverflow accept 32bit unsigned value.

Try with:

Code: Select all

unsigned int tempo=10000;
or better:

Code: Select all

uint32_t tempo = 10000;
Also, i don't think you need to pause timer at all.

Re: Timer starts and then freezes.

Posted: Wed Apr 01, 2020 2:29 pm
by fpiSTM
Just to be sure @Fabaum
STM32 works with 3.3V not 5V. So ADC range is for 0-3.3V.

Re: Timer starts and then freezes.

Posted: Thu Apr 02, 2020 6:49 am
by ABOSTM
From my point of view there is no reason to call pause() and resume() within Update_IT_callback()

Re: Timer starts and then freezes.

Posted: Thu Apr 02, 2020 12:25 pm
by Fabaum
In fact, fpiSTM, I was using 5V on the pot when I realized that reading through the analog port stopped at a certain point. Fortunately, it did not damage the microcontroller!
Bakisha and ABOSTM, I will test the routine without pausing the timer, thanks for the suggestions!

Re: Timer starts and then freezes.

Posted: Thu Apr 02, 2020 2:47 pm
by fpiSTM
Fortunately several STM32 pins are 5V tolerant. They are referenced in the datasheet.

Re: Timer starts and then freezes.

Posted: Tue Apr 14, 2020 7:26 am
by kishor2stm32
Hello everyone!!

I started coding my Discovery B-L475IoT01A board using arduino IDE.
I did basic examples & Wifi Related one.

Now i want to add timer event in my code for time sequenced operation.
I don't know how to use timer functions, Can anyone has done earlier, will help me for this.

regards,
kishor Potdar.

Re: Timer starts and then freezes.

Posted: Tue Apr 14, 2020 7:38 am
by stas2z