How to use the Basic Timer 6 on STM32F407 MCU.
I have to keep a pin high for a certain amount of time, about 1us, I figured that it probably makes sense to do this with a timer. I was going to use SysTick but I decided that it makes mores sense to learn the timer peripherals.
I think I have the configuration and execution steps correct to make a simple function to create an approx. delay (for testing purposes) but I cannot seem to get it working as desired. I did notice that I was not able to see the CEN bit enabled when debugging, but I imagine that this is not possible? Below is my code, any hints appreciated!
// GPIO registers previously enabled and set up, they are
// confirmed to work.
/* config TIM6 */
// Enable TIMER6 as it is on the APB1 bus
RCC->APB1ENR |= RCC_APB1ENR_TIM6EN;
// One pulse mode (counter stops counting after event)
TIM6->CR1 |= (TIM_CR1_OPM);
/* Function for delay */
void delay1us(void)
{
/* execute a delay */
// Clear update event flag
TIM6->SR = 0;
// Prescaler
TIM6->PSC = 0;
// Preload
TIM6->ARR = 84; // 336 for 4us
// Start timer
TIM6->CR1 |= TIM_CR1_CEN; // CEN bit
// Wait for delay to elapse
while(~(TIM6->SR & TIM_SR_UIF));
// Clear timer flag
TIM6->SR = 0;
}
while(1)
{
// Make pin high
GPIOE->ODR |= (1 << 2U);
delay1us();
// Make pin low
GPIOE->ODR &= ~(1 << 2U);
delay1us();
}