cannot set mode on hardware timer

Post here first, or if you can't find a relevant section!
Post Reply
admech
Posts: 2
Joined: Sat Oct 15, 2022 11:36 pm

cannot set mode on hardware timer

Post by admech »

Hi i am trying to write a simple code to flash an led on and off @ 1hz of which the stm32 example works fine
however when i pause the timer the pin stays high
i need it to stay low
when i try to change the timer channel with setmode the pin just goes high and stays there
any help would be greatly appreciated


/*
Timebase callback
This example shows how to configure HardwareTimer to execute a callback at regular interval.
Callback toggles pin.
Once configured, there is only CPU load for callbacks executions.
*/

#if !defined(STM32_CORE_VERSION) || (STM32_CORE_VERSION < 0x01090000)
#error "Due to API change, this sketch is compatible with STM32_CORE_VERSION >= 0x01090000"
#endif

#if defined(LED_BUILTIN)
#define pin LED_BUILTIN
#else
#define pin PC13
#endif
#define channel 1
void Update_IT_callback(void)
{ // Toggle pin. 10hz toogle --> 5Hz PWM
digitalWrite(pin, !digitalRead(pin));
}

void setup()
{
pinMode(pin, OUTPUT);
flashon();
}
void loop()
{


/* Nothing to do all is done by hardware. Even no interrupt required. */
}
void flashon ()
{
TIM_TypeDef *Instance = TIM1;

HardwareTimer *MyTim = new HardwareTimer(Instance);

MyTim->setMode(channel, TIMER_OUTPUT_COMPARE_PWM2, pin);
MyTim->setOverflow(10, HERTZ_FORMAT); // 10 Hz
MyTim->attachInterrupt(Update_IT_callback);
MyTim->resume();
}
void flashoff ()
{
TIM_TypeDef *Instance = TIM1;

HardwareTimer *MyTim = new HardwareTimer(Instance);

MyTim->setMode(channel, TIMER_OUTPUT_COMPARE_PWM2, pin);
MyTim->setOverflow(10, HERTZ_FORMAT); // 10 Hz

MyTim->attachInterrupt(Update_IT_callback);
MyTim->pause();
}
GonzoG
Posts: 403
Joined: Wed Jan 15, 2020 11:30 am
Answers: 26
Location: Prudnik, Poland

Re: cannot set mode on hardware timer

Post by GonzoG »

It has nothing to do with timer. It's your code that does it. Pin will stay in state it was when you pause timer.
You want it to go low on timer pause, you need to put that in the code.
ag123
Posts: 1653
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: cannot set mode on hardware timer

Post by ag123 »

PC13 is actually not a valid timer pin, I'm not sure if that affects the call. use a relevant timer pin for the call. It isn't necessary to use the output at the pin as your interrupt would still fire

TIMER_OUTPUT_COMPARE_PWM1 is also more appropriate as PWM2 is probably inverted

actually for a normal interrupt using the update event setMode is probably not needed
https://github.com/stm32duino/STM32Exam ... llback.ino

Code: Select all

void setup()
{
#if defined(TIM1)
  TIM_TypeDef *Instance = TIM1;
#else
  TIM_TypeDef *Instance = TIM2;
#endif

  // Instantiate HardwareTimer object. Thanks to 'new' instanciation, HardwareTimer is not destructed when setup() function is finished.
  HardwareTimer *MyTim = new HardwareTimer(Instance);

  // configure pin in output mode
  pinMode(pin, OUTPUT);

  MyTim->pause();
  MyTim->setOverflow(10, HERTZ_FORMAT); // 10 Hz
  MyTim->attachInterrupt(Update_IT_callback);
  MyTim->refresh();
  MyTim->resume();
}
Post Reply

Return to “General discussion”