Exact time between loop

Discussion created by lpcware Employee on Jun 15, 2016
Latest reply on Jun 15, 2016 by lpcware
Content originally posted in LPCWare by Lithium on Sat May 21 15:35:06 MST 2011
Hi everyone,

I am having an problem, let me explain.
I am trying to build an quad copter, i need to reed 2 sensors, an acc and gyro.
To control the quad copter i need to use an DCM IMU, this well calculate the sensor data to amount degrees my quad copter is.
With that data i can make an pid to control it.

The problem is, the DCM IMU needs to run exact an amount of time, for example 50hz. So the time between every loop needs to be 20ms.
I used the example libary and used timer.c & timer.h.
To make the counter count every 10us i edited this:

    LPC_TIM0->PR  = pclk/1000000; /* set prescaler to get 1 M counts/sec */

    LPC_TIM0->MR0 = 10; /* Set up 10 mS interval 1000 * 10 = 10ms */
    LPC_TIM0->MR1 = 10; /* Set up 10 mS interval */

The counter needs to be 2000.
It could be that the loops runs at 23ms, to be sure my calculations are right i can use this:
G_Dt = timer/100000.0
This should always be 0.02.

I think the loop is not precise 20ms, how can i measure this?
The problem could be i2c, i need to read my data by i2c, this could have some delay in it.
Can i see how long it takes to run an i2c reading loop?
Is there also another way to make an loop that runs precise on 20ms?
To be sure the counter is always 2000 i made an if statement, used the debug mode to see if the counter is always 2000 when it enters the loop. It's always 2000.

Using an lpc EM 1769 board.
I hope i explained everything so you can understand what i mean.
If not please ask.