我不太习惯用HAL,所以这里是如何通过直接访问定时器外设来做你想做的事情:
// SETUP STUFF:
// Enable the timer clock. I use the HAL for this
// as it adds the required startup delay. The code
// is pretty simple though.
__HAL_RCC_TIM1_CLK_ENABLE();
// Reset the control register. This gives us the
// default operation which is counting up with no
// divider.
TIM1->CR1 = 0;
// Set prescaler
TIM1->PSC = 16799;
// Will generate interrupt when this value is reached
TIM1->ARR = 4999;
// The PSC and ARR values are currently in the preload
// registers. To load them into the active registers we
// need an update event. We can do this manually as
// follows (or we could wait for the timer to expire).
TIM1->EGR |= TIM_EGR_UG;
// Timer is now ready to use.
// POLLING OPERATION:
// Next we setup the interrupts. We should first clear
// the update interrupt flag in case it has already been
// set.
TIM1->SR = ~TIM_SR_UIF;
// Then we can enable the update interrupt source
TIM1->DIER |= TIM_DIER_UIE;
// Note: we also need to setup the interrupt channel on
// the NVIC. Once that is done the isr will fire
// when the timer reaches 5000.
// We can now start the timer running...
TIM1->CR1 |= TIM_CR_CEN;
while ((GPIOA->IDR & GPIO_PIN_3) == 0x08)
{
Clk_h
DWT_Delay(200);
Clk_l
DWT_Delay(200);
}
// ...and stop the timer when we're done
TIM1->CR1 &= ~TIM_CR_CEN;
// Note if we want to repeat the polling loop again we should
// issue another TIM1->EGR |= TIM_EGR_UG event as this
// resets the timer to zero.
我不太习惯用HAL,所以这里是如何通过直接访问定时器外设来做你想做的事情:
// SETUP STUFF:
// Enable the timer clock. I use the HAL for this
// as it adds the required startup delay. The code
// is pretty simple though.
__HAL_RCC_TIM1_CLK_ENABLE();
// Reset the control register. This gives us the
// default operation which is counting up with no
// divider.
TIM1->CR1 = 0;
// Set prescaler
TIM1->PSC = 16799;
// Will generate interrupt when this value is reached
TIM1->ARR = 4999;
// The PSC and ARR values are currently in the preload
// registers. To load them into the active registers we
// need an update event. We can do this manually as
// follows (or we could wait for the timer to expire).
TIM1->EGR |= TIM_EGR_UG;
// Timer is now ready to use.
// POLLING OPERATION:
// Next we setup the interrupts. We should first clear
// the update interrupt flag in case it has already been
// set.
TIM1->SR = ~TIM_SR_UIF;
// Then we can enable the update interrupt source
TIM1->DIER |= TIM_DIER_UIE;
// Note: we also need to setup the interrupt channel on
// the NVIC. Once that is done the isr will fire
// when the timer reaches 5000.
// We can now start the timer running...
TIM1->CR1 |= TIM_CR_CEN;
while ((GPIOA->IDR & GPIO_PIN_3) == 0x08)
{
Clk_h
DWT_Delay(200);
Clk_l
DWT_Delay(200);
}
// ...and stop the timer when we're done
TIM1->CR1 &= ~TIM_CR_CEN;
// Note if we want to repeat the polling loop again we should
// issue another TIM1->EGR |= TIM_EGR_UG event as this
// resets the timer to zero.
举报