cancel
Showing results for 
Search instead for 
Did you mean: 

Number of clock cycles per instruction

ivancolla9
Associate III
Posted on March 07, 2016 at 11:18

Good morning to everyone.

I am trying to figure out how to toggle periodically a digital output pin with a given frequency. The system clock is configure the internal oscillator, so it should be 16 MHz.

I set a timer with a prescaler of 101, so, if I understand correctly, it should count 1 every 100 cock tick.

Now, for example, I am trying to toggle a pin evry 20 ms. If I understand correctly, 16 MHz, should be 16000000 clock cycles per second. If I count 1 every 100, the timer should be 160000 every second. 

20 ms is 1/50 seconds, so 1600000/50 = 3200.

In my code, I execute various type of operation in an infinite cycle. When I have to evaluate if 20 ms are passed, I verify the STM counter. If 3200 counts are passed from before, I change the output.

The control implemented is something like this:

ULONG ulNow = STM.CNT.R;

ulElapsedTime = ulNow - ulWaveTimer;

if (ulElapsedTime  >= 3200)

/* Time passed. Change output and reset timers. */

The fact is that It seems that from one check and the others pass too many clock cycles, so the frequency obtained is not what I desire, but less. 

Is there a more smart way to implement an output that change with a desired frequency?

There is some fixed relation between the C code I write and the number of machine instructions that I obtain after compilation? 

Thank you,

Ivan

#clock-cycles-timer
5 REPLIES 5
Erwan YVIN
ST Employee
Posted on April 07, 2016 at 17:25

Hello Ivan ,

Sorry , for my late answer.

Have you solved your issue ?

Which device do you use ?

About STM, if you use EL, 

The smart way to implement an output at the desired frequency is to change the CMP register

CMP set to 3200 in hexa

Compare value for channel n. If the STM_CCRn[CEN] bit is set and the STM_CMPn register matches

the STM_CNT register, a channel interrupt request is generated and the STM_CIRn[CIF] bit is set.

and handle the associated vector number 

30 0x01E0 16 Match on channel 0 STM_0 STM_1

31 0x01F0 16 Match on channel 1 STM_0 STM_1

32 0x0200 16 Match on channel 2 STM_0 STM_1

33 0x0210 16 Match on channel 3 STM_0 STM_1

   Best regards

                   Erwan

ivancolla9
Associate III
Posted on May 03, 2016 at 17:36

Hello Erwan,

In the end I solved calling my function multiple times in a main cycle. The problem was that I had some complex calculations done by the micro and forced waits caused by the management of a display.

Having multiple calls ensures that I catch when the timer matches a certain value and so change the output pin value accordingly.

I also discovered that I can use the FlexPWM to generate waves with desired characteristics.

Thanks for the answer. I will try your solution and decide what is best for the application on which I am working.

Maposaagsky
Associate
Posted on March 07, 2018 at 19:36

The OP is using an SPC5 not an STM32

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on March 08, 2018 at 09:42

Yes , it is SPC5 Family

All the documents are located here :

http://www.st.com/en/automotive-microcontrollers/spc56el70l5.html

 

   Best regards

               Erwab