/var/www/restricted/ssh/stm32/www/stm32circle/ STM CircleOS forum / How to make a delay (microseconds) method

Username:     
Password:     
             

Forum
  • Index
  •  » circleOS
  •  » How to make a delay (microseconds) method

# 1   2011-01-10 03:19:17 How to make a delay (microseconds) method

mikepo
Member
From: USA
Registered: 2010-12-03
Posts: 36

How to make a delay (microseconds) method

I am trying to implement the protocol to interact with a SHT11 humidity sensor.
As part of that I need a function to give a certain delay (in microseconds) in the CircleOS.
I think there is no built-in delay function in CircleOS, so I think I have to configure a timer and use that.
- which Timer(s) could I use that are not used by CircleOS and don't interfere with CircleOS
(I think TIM1 and maybe TIM2 are used by CircleOS?)

- I haven't done anything with timers yet, do you have some hints/examples how to get started?

Thanks,
Mike

Offline

 

# 2   2011-01-10 05:19:58 How to make a delay (microseconds) method

mikepo
Member
From: USA
Registered: 2010-12-03
Posts: 36

Re: How to make a delay (microseconds) method

I am still stuck on this.
Basically I am trying to user TIM5 to count up in increments of 1 us.
Here is the code I am trying to get to work.
I am calling "delay_init" from the Application_ini handler, but when I look at Timer5 in Ride7 it always stays at 0, none of the registers seem to be set, and it does not seem to count at all.

And when the code hits the "delay_us(1)" line, it just goes into an infinite loop, since the timer does not count.

I tried also with TIM4, which just caused the LCD display to flicker VERY fast, using TIM6 just hangs on the line calling "TIM_TimeBaseInit" ...

So somewhere there I am doing something wrong...

Code:

#define SYS_CLK 48000000    /* in this example we use SPEED_HIGH = 48 MHz */
#define DELAY_TIM_FREQUENCY 1000000 /* = 1MHZ -> timer runs in microseconds */


/* set TIM5 to run at DELAY_TIM_FREQUENCY */ 
void delay_init( void ) 
{


  /* Enable timer clock  - use TIMER5 */
  RCC_APB2PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE);
  

  /* Time base configuration */
  TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); 
  TIM_TimeBaseStructure.TIM_Prescaler = (SYS_CLK / DELAY_TIM_FREQUENCY) - 1;
  TIM_TimeBaseStructure.TIM_Period = UINT16_MAX; 
  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  TIM_TimeBaseInit(TIM5, &TIM_TimeBaseStructure);
 
 /* Enable counter */
  TIM_Cmd(TIM5, ENABLE);
}

/* wait busy loop, microseconds */
void delay_us( uint16_t uSecs ) 
{
  uint16_t start = TIM5->CNT;
  /* use 16 bit count wrap around */
  while((uint16_t)(TIM5->CNT - start) <= uSecs);
}

I am not using any interrupts, so do I need to do any NVIC initialization or something else to get the TIMER running?

Last edited by mikepo (2011-01-10 05:32:10)

Offline

 

# 3   2011-01-10 07:10:28 How to make a delay (microseconds) method

yrt
Administrator
From: Grenoble-France
Registered: 2008-06-11
Posts: 520
Website

Re: How to make a delay (microseconds) method

Hi Mike,

You don't see any registers modified because your timer is not configured . You written a little mistake in RCC configuration :

Code:

RCC_APB2PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE);

instead of

Code:

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE);

(TIM5 is well on the APB1 bus)

I also suggest to reset the CNT register at the beginning of the delay_us function.
The NVIC initialization is not necessary, when you don't use interrupts.

Yves

Offline

 

# 4   2011-01-10 13:58:40 How to make a delay (microseconds) method

mikepo
Member
From: USA
Registered: 2010-12-03
Posts: 36

Re: How to make a delay (microseconds) method

Thank you Yves,
that was a silly mistake I made!
I looked at the block diagram of the STM32 but I must have missed that.
The timer is now running.
Now I need to "just" get the protocol working to talk to the SHT11 sensor.

Offline

 

  • Index
  •  » circleOS
  •  » How to make a delay (microseconds) method

Board footer