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();
}
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?