Sonsivri
 
*
Welcome, Guest. Please login or register.
Did you miss your activation email?
April 25, 2024, 05:28:47 17:28


Login with username, password and session length


Pages: [1]
Print
Author Topic: STM2F1, coocox a _delay_us  (Read 3444 times)
0 Members and 2 Guests are viewing this topic.
karri
Junior Member
**
Offline Offline

Posts: 39

Thank You
-Given: 14
-Receive: 3


« on: January 14, 2016, 09:59:32 09:59 »

Hi, I am not sure, If I did not asked before, but forum search has not found anything.

I am new in CooCox and ARM, I worked with AVRs before. What I miss in CooCox is "delay.h" lib similar to AVR GCC. I hnow, that I can use SysTimer for milisecond delays, but what are you using for micro second delays? (something like _delay_us)

My current solution is:
Code:
void _delay_us(uint16_t nTime)
{
   TIM3->CNT = 0;
   while ( (TIM3->CNT) < nTime);
}
where TIM3 si set for us clock
Code:
void Timer3_Init(void)
{
    // 1 MHz clock
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
    TIM_TimeBaseStructure.TIM_Prescaler = 21 ;  // I have 22MHz clock
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
    //TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Down;
    TIM_TimeBaseStructure.TIM_Period = 0xFFFF;
    TIM_TimeBaseStructure.TIM_ClockDivision = 0;
    TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
    // Set one shot
    TIM_SelectOnePulseMode(TIM3, TIM_OPMode_Single);
    /* TIM2 enable counter */
    TIM_Cmd(TIM3, ENABLE);
}
It is clear, that it is not accurate for very short delays. But my typical delays are longer than 10us.
Is this OK, or you have some better solution?
Thank you
Logged
sam_des
Senior Member
****
Offline Offline

Posts: 253

Thank You
-Given: 124
-Receive: 146


« Reply #1 on: February 02, 2016, 04:23:38 04:23 »

Good old Software delay with manually counting instruction cycles is only way.

For trial & error method, Use a simple for() loop in separate function, see how much delay it generates on scope & adjust it until you get desired result. Just know that each compiler or even each version of same compiler may implement your code differently. So you have to check every time you change compiler or version.

If you want more precision, get down to writing a ASM routine. Your routine will be portable at least across different versions of compiler without issues.

In any case you will have to adjust your parameters as per core clock you're using. Also any interrupts occurring during execution of routine will affect actual delay, unless you toggle them.

_sam_des
Logged

Never be afraid to do something new. Remember Amateurs built the Ark, Professionals built the Titanic !
Pages: [1]
Print
Jump to:  


DISCLAIMER
WE DONT HOST ANY ILLEGAL FILES ON THE SERVER
USE CONTACT US TO REPORT ILLEGAL FILES
ADMINISTRATORS CANNOT BE HELD RESPONSIBLE FOR USERS POSTS AND LINKS

... Copyright © 2003-2999 Sonsivri.to ...
Powered by SMF 1.1.18 | SMF © 2006-2009, Simple Machines LLC | HarzeM Dilber MC