cancel
Showing results for 
Search instead for 
Did you mean: 

Need help !!! I want to make 3 delay functions which works guaranteed delay in nanoSecond, microSecond and milliSeconds using Timer.

Vivek yadav1
Associate III

What i am currently using to achieve my goal.

Few Details :

1: STM32F103C8 Blue Pill

2: CMISS core Library

3:Datasheet, Reference Manual and Programming Manual

I have attached two files. Which give you some idea that how I want to make These 3 functions. I want to understand at Register level.

I want to configure Registers by myself.

Thanks & Regards

@Vivek yadav​ 

20 REPLIES 20
oeliks
Senior

Well just stop timer change value of arr​, start timer? Or use 3 timers for this? Bluepill have 4 timers.

Ozone
Lead

I would suggest to use symbolic names (preprocessor constants) instead of "magic" numbers.

Hardly anyone want's to browse forth and back through the the reference manual while reading your code. This will include you in a few weeks ...

The stm32f1xx.h header file provided by ST would do no harm.

A nanoseconds delay would work down to the granularity of the counter used (clock, prescaler).

However, indeterminisms like interrupts or DMA transfers will introduce jitter significant in relation to the desired delay.

For milliseconds delays, you could use the SysTick timer as well.

Vivek yadav1
Associate III

@oeliks​ 

  • I think If i use only Timer then also it can be achieve.

Q.1 : How can we set our Controller to its Maximum Speed(max. 72Mhz)?

What value I have to change in my RCC Registers. I have a doubt because we have only 8 Mhz External oscillator.

Q.2: As I can See in datasheet that Timer1 has 16 bit Register Which means it can have maximum 65536[0 to 65535] and if we

calculate we get that This Register( TIMx_CNT ) will over Flow 122 times.

it means in 1 sec it is counting 122 times.

Am i right ????

@Ozone​  Thanks

I definitely use stm32f1xx.h header file.

  • I used HSE as system Clock.
  • What should I have to do to use SysTick timer. Is it a separate timer?

SysTick is a 24-bit timer in the core space (present in all Cortex M devices), with it's own fixed interrupt hander.

The CMSIS libs use to contain easy-to-use setup function.

BTW, the F10x MCUs had been covered by the now obsolete Standard Peripheral Library, which is still available for download.

Much simpler and straightforward then Cube/HAL, I would recommend to download and use the examples as template for your applications.

And even if it's only the sequence of peripheral initialization operations.

oeliks
Senior

If You cant calculate values of Your PLL manually try to use cubemx clock tool, it will do it for You. Its possible to set pll to 72mhz with just 8mhz hse in this device.

Almost all t​utorials with stm show how to set clocks.

72mhz=1.3838E-8 [s]

1.39E-8 * (122 * 65535) = time [s]

=)

dbgarasiya
Senior II

timer is works three main fundamentals , timer clock ,prescaler and period , once you understand this it is easy for you

dbgarasiya
Senior II

refrence manual

@dbgarasiya​ 

can You Check This ?

/*
*	@Beirf : This is my attempt to make my own delay funcation.
*					with my limited knowledge of ARM Cortex M3 Registers.
*					Delay Should be in milli Seconds
*					I am using PLL clock as System Clock at 72 Mhz.
*					
*
*/
void my_msDelay(uint32_t Delay)
{
	
	// Enable PLL Clock. and check Ready flag
		RCC->CR |= 1<<24;
		while(RCC->CR != (1<<25))
		{
			/*	Wait	*/
		}
	//Select PLL as SYSTEM clock
		RCC->CFGR |= 0x10 << 0;
	
	// Run at it maximal Value. which is 72 Mhz for STM32f103c8(Blue Pill)
		RCC->CFGR |= 0x0111 << 18;
	
	// Enable Timer 1
		RCC->APB2ENR |= 1<<11;
 
	// Enable Timer CEN bit
		TIM1->CR1 |= 1<<0;
		
	// Load Value in Prescaler
		TIM1->PSC = 1;
	
	//Now how would i calculate 1 sec time delay
		
		
}