In my project i'm using one timer and 3 channels (one for interrupt, one for PWM2 and one for toggle) and i'm planning to use pins based on timer, not a timer based on pin. Main reason is because i use STM32F103C8, STM32F401CE, STM32F411CC and STM32F407VE boards for testing, and they have different timers on same pin name. Yes, i could use per board definitions, but i'm trying to avoid that.
Here is small sketch (original sketch is too large):
Code: Select all
uint32_t HSYNC_pin ; // TIM2 CH3 // TIM5 CH3
uint32_t channel_HSYNC = 3 ; // TIM2 CH3 // TIM5 CH3 // PA2
#ifdef TIM1
HardwareTimer *MyTim = new HardwareTimer(TIM1);
#else
HardwareTimer *MyTim = new HardwareTimer(TIM2);
#endif
TIM_TypeDef * Instance = NULL; // Instance can be used for direct writing to timer's registers
void changeTimer(TIM_TypeDef * NEW_timer) {
delete MyTim ; // destroy previous instance
HardwareTimer *MyTim = new HardwareTimer(NEW_timer); // set new instance
Instance = NEW_timer;
// HSYNC_pin = ?????
}
void setup() {
changeTimer(TIM5); // TIM2 for STM32F103 // TIM5 for STM32F401CC
uint32_t TIMER_MHZ = ( MyTim->getTimerClkFreq()) / 1000000;
MyTim->pause();
MyTim->setPrescaleFactor(1); // count timers clock ticks
MyTim->setMode(channel_HSYNC, TIMER_OUTPUT_COMPARE_PWM2, HSYNC_pin); // <--------- HSYNC pin?
MyTim->setOverflow( TIMER_MHZ * 64, TICK_FORMAT); // first value
MyTim->setCaptureCompare(channel_HSYNC, 4.70*TIMER_MHZ, TICK_COMPARE_FORMAT);
MyTim->resume();
}
void loop() {
}