2013-12-19 04:28 AM
Function works for TIM6 basic timer used as delay on STM32F303 , with suitable modifications can work on any STM8,32 variants
void TIMER_DELAY(uint16_t value){RCC->APB1ENR |= RCC_APB1Periph;
//value is 0x10 TIM6->ARR = value; // delay in ms TIM6->PSC = 36000-1; // APB1 = 36Mhz so eqn : CLK_CNT = (36000000)/36000,i.e. CLK_CNT = 1Khz TIM6->CR1 = 0x01; // ENaBle TIMER while(!((TIM6->SR & 0x01) == 0x01)); // Check for Update Event TIM6->CR1 &= ~(0x01); // Disable timer TIM6->SR &= ~(0x01); // Reset the update event}For eg. TIMER_DELAY(100) would provide me 100ms delay. #old-school-delay-loops2013-12-19 07:06 AM
TIM6->SR = ~(0x01); // Not RMW
Personally I find DWT_CYCCNT to be far easier, and providing sub-microsecond resolution.2013-12-19 08:59 AM
I would get laughed out of the office if I were to submit some code for review which had a spin in place delay loop for some number of milliseconds.
The timer peripheral is better off being used as a system resource as a one shot timer where modules can register for a call back. The module passes a time interval parameter when registering.2013-12-19 09:19 AM
That is the difference between professional engineers and the amateur!
We should encourage the amateurs to learn better styles of coding.2013-12-20 05:07 AM
Dear RocketDawg,
I guess it's very hard for u to read the title, so just to clarify , it was intended only for some basic knowledge.2013-12-20 05:09 AM
Dear sung.chen_chung,
FYI coders are not born professionalists(some exceptions)...it always starts from amateurs. And instead of showing criticism ,,, enlighten others