cancel
Showing results for 
Search instead for 
Did you mean: 

The clocks each command takes

Arman Ilmak
Senior
Posted on April 27, 2018 at 19:51

Hi.

I wanted to know that for example how many clocks a while loop takes.

Do you have any ideas or any articles on this matter?

Note: this post was migrated and contained many threaded conversations, some content may be missing.
21 REPLIES 21
Posted on April 28, 2018 at 22:44

If you set the TIM to clock at 1 MHz you can observe intervals with 1us granularity by looking at the CNT value. You can interrupt at such rates.

start = TIM->CNT;

while((TIM->CNT - start) < 100); // Wait 100us

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on May 04, 2018 at 11:45

void delay_us(unsigned int d)

{

TIM2_Cmd(ENABLE);

while( ((TIM2->CNTRL)|((TIM2->CNTRL)<<8))<=d);

TIM2_Cmd(DISABLE);

TIM2->CNTRL=0;

TIM2->CNTRH=0;

}

void delay_ms(unsigned int d)

{

while(d){d--;delay_us(1000);}

}

I've written this code for my delay functions but they do not work.

The timer CNT counts each 1 us.

Posted on May 04, 2018 at 11:58

Run your led update function through a timer isr so it is not blocked by the 1wire routines.

Posted on May 04, 2018 at 17:33

I didn't understand what you mean.

Posted on May 04, 2018 at 17:44

I'm not advocating the repeated resetting and enabling/disabling the timer. I'm saying to let it free run and delta the current and start points.

If dealing with 8-bit values is most effective on the STM8 (not using, don't plan too), then perhaps decompose the delays into 200us or 100us pieces so the code is tighter and the opportunity for jitter reduced.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on May 04, 2018 at 18:00

You mean with these 8-bit MCUs I cant get a 1 us delay using the timer?

Posted on May 04, 2018 at 20:29

take some classes in embedded programming.

you are going down a path that can only hurt your learning in the long run.

Posted on May 04, 2018 at 21:25

If I had the possibility I would do that.But these classes will not be free and as a student, I can't afford the expenses.

Posted on May 04, 2018 at 21:34

There are some classes available for free. Look at edX and similar initiatives:

https://www.edx.org/course/embedded-systems-shape-the-world-microcontroller-inputoutput

 

also books are good way of learning and verifying the knowledge in practice.

Posted on May 04, 2018 at 22:43

'

I can't afford the expenses.'

it is a tough spot to be in, unfortunately.

with that said, you may want to focus a little bit on the basics before tackling issues outside of your skill set.

slowing down now often allows you to run faster later.