using clock() function time.h

Showing results for 
Show  only  | Search instead for 
Did you mean: 

using clock() function time.h

Contributor III


I am trying to use time.h file clock() function.

Here is my code. I am getting -1 as return value for clock() fucntion. 

What is going wrong here, can someone explain

#include "derivative.h" /* include peripheral declarations */
#include <time.h>
#include "MPC577xC.h"
void delay(int milliseconds);
extern void xcptn_xmpl(void);

void delay(int milliseconds)
clock_t ticks1 = clock(), ticks2 = ticks1;
clock_t target = milliseconds * CLOCKS_PER_SEC;
while ( ( ticks2 - ticks1) < target)
ticks2 = clock();

int main(void)
volatile int counter = 0;
int x;

   xcptn_xmpl (); /* Configure and Enable Interrupts */

   SIU.PCR[114].R= 0x0200;
   SIU.GPDO[114].B.PDOn = 0x0;
      SIU.GPDO[114].R = 1;
      SIU.GPDO[114].R = 0;


Labels (1)
1 Reply

NXP Employee
NXP Employee


Return code -1 is correct, because the microcontroller does not have any resources, how to return you correct time.


This is the clock function description:

This function returns the calling process' current CPU time. If the CPU time is not available or cannot be represented, clock returns the value (clock_t)(-1).


and this is the time function description:

The time function returns the current calendar time as a value of type time_t. If the argument result is not a null pointer, the calendar time value is also stored in *result. If the current calendar time is not available, the value (time_t)(-1) is returned.


So you cannot simply use these functions. The way, how to use these functions exists, but it will be pretty difficult and I do not have any experience with it. Please look at the follow  URL: