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
dbgarasiya
Senior II

you have to add TIM1->EGR , which is for update count

and then get value in

TIMx->CNT ,which will increment as per millisecons

dbgarasiya
Senior II

.

Vivek yadav1
Associate III

I am Trying to generate 1 sec time delay. So i write a code. In which as per my calculation. LED must Toggle at every 1 sec.

Can Any one guide me Where am i wrong ?

 
// This header file contains memory mapping of all peripherial
#include "stm32f10x.h"
 
 
void toggleLED()
{
	GPIOC->ODR ^= GPIO_ODR_ODR13;
}
 
int main()
{
FLASH->ACR |=  FLASH_ACR_LATENCY_0;
// by default HSI is selected which is 8 Mhz
RCC->CR |= RCC_CR_HSION;
while(!RCC_CR_HSIRDY);
// HSI divide by 8 i.e frequancy = 8 Mhz/8 = 1 Mhz
RCC->CFGR |= RCC_CFGR_SW_HSI | RCC_CFGR_HPRE_DIV8 | RCC_CFGR_MCO_HSI;
// Port C and Timer 1 is selected
RCC->APB2ENR |= RCC_APB2ENR_IOPCEN | RCC_APB2ENR_TIM1EN;
/*	GPIO Block	*/
// Pin13,10 Mhz, Analog 	
GPIOC->CRH |= GPIO_CRH_MODE13_0 ;
							
	
	
/*		Timer Block	*/	
TIM1->CR1 |=  TIM_CR1_ARPE	|
							TIM_CR1_CMS_0 |
							TIM_CR1_DIR		|
							TIM_CR1_CEN ;
	
TIM1->EGR |= TIM_EGR_UG; 
TIM1->PSC = 0;// count at every 1 pules
TIM1->ARR = 1000 - 1;
//	Tout = (ARR+1)*(PSC+1)/Fclk
// Tout = 1000*1/1 Mhz
// Tout = 1/1000 sec == 1 milli Sec
uint16_t cnt =0;
 
// After configuraing Timer i am expected to toggle the LED at every 1 sec.
while(1)
{
	if(TIM1->CNT == 999)
		cnt++;
 
	if(cnt == 1000)
	{
		cnt =0;
		toggleLED();
	}
 
}	
 
}

I am getting 3 sec delay

dbgarasiya
Senior II

Try this code

RCC->APB2ENR |= (1<<0);  // Enable clock set as per your controller datasheet

TIM1->PSC = 54000; // My timer clock is 54 Mhz : 54 MHZ/ 54000 == 1 ms cnt

TIM1->EGR = TIM_EGR_UG;

TIM1->CR1 = TIM_CR1_CEN;

you will get ms delay .. i have not used TIM1->ARR , here

didn't work for me

dbgarasiya
Senior II

what is the issue coming ?

is that showing differnt value . or counter does not starts at all ?

#include "stm32f10x.h"
 
#define BIT(x) (1<<x)
void init()
{
	RCC->CR   |= RCC_CR_HSION;//enable HSI 8 MHz RC Oscillator
	RCC->CFGR |= RCC_CFGR_PLLSRC_HSI_Div2; // 8/2 = 4 MHz
	RCC->CFGR |= RCC_CFGR_PLLMULL9; // 4*9 = 36 Mhz
	RCC->CFGR |= RCC_CFGR_SW_PLL; // PLL selected as system clock
	while(!RCC_CFGR_SWS_PLL); // Wait for PLL to be ready
	
	RCC->APB2ENR |= (1<<0);  // Enable clock set as per your controller datasheet
	RCC->APB2ENR |=  RCC_APB2ENR_IOPCEN;//Port C enable
	RCC->APB2ENR |= RCC_APB2ENR_TIM1EN;//Timer1 Enable
	TIM1->PSC = 36000; // My timer clock is 36 Mhz : 36 MHZ/ 36000 == 1 ms cnt
 
	TIM1->EGR = TIM_EGR_UG;
 
	TIM1->CR1 = TIM_CR1_CEN;
 
}
 
void configurePins()
{
    // Turn on GPIO C 
    RCC->APB2ENR |= BIT(4);
    // Configure PC13 as an output
    GPIOC->CRH |= BIT(20);
    GPIOC->CRH &= ~(BIT(23) | BIT(22) | BIT(21) );
}
 
int main()
{
	init();
	configurePins();
		
	TIM1->ARR = 1001;
	uint16_t cnt =0;
 
	while(1)
	{
 
		if(TIM1->CNT == 1000 )
		{
			cnt++;		
		}
		else if(cnt == 1000)
		{
			GPIOC->ODR ^= BIT(13);
			cnt = 0;
		}
	}//End-while
 
	
}
*

I wrote this code.

Piranha
Chief II

Milliseconds - if you're beginner, look at what others have done before you. Implement a software system tick counter and make delay function wait on that. https://www.keil.com/pack/doc/CMSIS/Core/html/group__SysTick__gr.html

Microseconds - a sane code rarely needs such a delays. Forget it for now.

Nanoseconds - there doesn't exist such a thing as a precise nanosecond delays based on software. Only hardware can do it and implementation will always be specific to particular use. Therefore forget it at all.

S.Ma
Principal

Very precise output timings seems now possible with STM32 having HiResolutionTimer. The learning curve might be steep as it's equivalent to a 4GHz timer.