Timer starts and then freezes.

Post Reply
Fabaum
Posts: 24
Joined: Tue Mar 17, 2020 4:10 pm

Timer starts and then freezes.

Post 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?

ABOSTM
Posts: 25
Joined: Wed Jan 08, 2020 8:40 am
Answers: 1

Re: Timer starts and then freezes.

Post 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.

Fabaum
Posts: 24
Joined: Tue Mar 17, 2020 4:10 pm

Re: Timer starts and then freezes.

Post 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!

User avatar
Bakisha
Posts: 47
Joined: Fri Dec 20, 2019 6:50 pm
Answers: 3
Contact:

Re: Timer starts and then freezes.

Post 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.

User avatar
fpiSTM
Posts: 594
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 21
Location: Le Mans
Contact:

Re: Timer starts and then freezes.

Post by fpiSTM »

Just to be sure @Fabaum
STM32 works with 3.3V not 5V. So ADC range is for 0-3.3V.

ABOSTM
Posts: 25
Joined: Wed Jan 08, 2020 8:40 am
Answers: 1

Re: Timer starts and then freezes.

Post by ABOSTM »

From my point of view there is no reason to call pause() and resume() within Update_IT_callback()

Fabaum
Posts: 24
Joined: Tue Mar 17, 2020 4:10 pm

Re: Timer starts and then freezes.

Post 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!

User avatar
fpiSTM
Posts: 594
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 21
Location: Le Mans
Contact:

Re: Timer starts and then freezes.

Post by fpiSTM »

Fortunately several STM32 pins are 5V tolerant. They are referenced in the datasheet.

kishor2stm32
Posts: 1
Joined: Wed Apr 08, 2020 5:33 pm

Re: Timer starts and then freezes.

Post 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.

stas2z
Posts: 131
Joined: Mon Feb 24, 2020 8:17 pm
Answers: 8

Re: Timer starts and then freezes.

Post by stas2z »


Post Reply

Return to “STM32L4 based boards”