HardwareTimer Unusually slow on F103, does not accept pin names from PeripheralPins.c #2560
-
Original post by @fpistm in #1922 (reply in thread) I have attempted as above with the F103 black pill board and it does not work. Pins must be referenced as "B0" or "A8". Using something like "PB_0_ALTn" there is no output and no execution of callback functions. The timer is also running very slow like it's prescaled when it shouldn't be. Highest frequency is about 1kHz. I can set timings below this but it is not practical for buck converter. The code is nearly identical. #define pin PB_0_ALT2
#define pin2 LED_BUILTIN
#if !defined(STM32_CORE_VERSION) || (STM32_CORE_VERSION < 0x01090000)
#error "Due to API change, this sketch is compatible with STM32_CORE_VERSION >= 0x01090000"
#endif
// Automatically retrieve TIM instance and channel associated to pin
// This is used to be compatible with all STM32 series automatically.
TIM_TypeDef *Instance = (TIM_TypeDef *)pinmap_peripheral(digitalPinToPinName(pin), PinMap_PWM);
uint32_t channel = STM_PIN_CHANNEL(pinmap_function(digitalPinToPinName(pin), PinMap_PWM));
// Instantiate HardwareTimer object. Thanks to 'new' instantiation, HardwareTimer is not destructed when setup function is finished.
HardwareTimer *MyTim = new HardwareTimer(Instance);
void Update_IT_callback(void)
{ // Update event correspond to Rising edge of PWM when configured in PWM1 mode
digitalWrite(pin2, LOW); // pin2 will be complementary to pin
}
void Compare_IT_callback(void)
{ // Compare match event correspond to falling edge of PWM when configured in PWM1 mode
digitalWrite(pin2, HIGH);
}
void setup()
{
// No need to configure pin, it will be done by HardwareTimer configuration
// pinMode(pin, OUTPUT);
// Need to configure pin2, as it is not managed by HardwareTimer
pinMode(pin2, OUTPUT);
MyTim->setMode(channel, TIMER_OUTPUT_COMPARE_PWM1, pin);
MyTim->setPrescaleFactor(1); // Due to setOverflow with MICROSEC_FORMAT, prescaler will be computed automatically based on timer input clock
MyTim->setOverflow(128, TICK_FORMAT); // 100000 microseconds = 100 milliseconds
MyTim->setCaptureCompare(channel, 50, PERCENT_COMPARE_FORMAT); // 50%
MyTim->attachInterrupt(Update_IT_callback);
MyTim->attachInterrupt(channel, Compare_IT_callback);
MyTim->resume();
}
void loop()
{
/* Nothing is done */
} |
Beta Was this translation helpful? Give feedback.
Answered by
LightningStalker
Nov 13, 2024
Replies: 1 comment 4 replies
-
Hi @LightningStalker |
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
When interrupts callbacks are not attached, it begins to work! I comment out lines 38&39
attachInterrupt
. I don't need them. Then it works as expected. Must be something to do with too much interrupt triggering. Thanks for your help.