Different Clock Speed on LPC1768 using LPCXpresso

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

Different Clock Speed on LPC1768 using LPCXpresso

Jump to solution
3,515 Views
cyrusvali
Contributor II

Hi there

I'm trying to program my LPC1768 board using LPCXpresso IDE

But i have a trouble building a code that uses delay function

I built a program with KEIL with CPU clock speed of 100MHz

the function delay for 1 ms is as follows:

void delayms (void) {

int i=25000;

while(i--);

}

This was built and ran perfectly using KEIL IDE

But when i built the same code on LPCXpresso i get 1.75ms of delay and not 1ms!

The system configuration file is identical on both IDEs (system_17xx.c) and Clock is set to 100MHz

What could be the cause of the problem?

Is there anything to be changed in the startup file? (cr_startup_lpc175x_6x.c)

Thank you

1 Solution
2,399 Views
rocketdawg2
Contributor II

it is only by luck that the value 25000 causes a delay of 1ms.

And that may change when changing compiler optimizations.

A loop of this kind is totally dependent upon the code that the compiler generates.

see this link.

What do you use to delay? | www.LPCware.com 

View solution in original post

13 Replies
2,399 Views
lpcxpresso_supp
NXP Employee
NXP Employee

In general, never rely on a software loop if you need a precise time delay. Use a hardware timer, or the SysTick timer. The LPC1769 LPCOpen examples (lpcopen_2_10_lpcxpresso_nxp_lpcxpresso_1769.zip archive) distributed with MCUXpresso has timer examples (e.g. periph_systick, periph_timer) to reference. These examples work with the LPC1768 provided the maximum core clock is honored.

Thanks and regards,

LPCXpresso Support

0 Kudos
2,399 Views
rons
Contributor II

It would be better to use the repetitive interrupt timer.

RIT_Delay_Ten_Nsec has a minimum delay of 10 nanoseconds.

--------------------------------------------------------------------

#include <stdint.h>

void RIT_Delay_Ten_Nsec(uint32_t ten_nsec)
{
rit_delay_done = 0;

/* The RIT count increments every 10ns */
RICOMPVAL = ten_nsec;
RICOUNTER = 0x00000000;

/* Enable timer */
RICTRL = RICTRL_RITEN | RICTRL_RITENCLR;

while (!rit_delay_done)
; /* do nothing */
}

--------------------------------------------------------------------

A software instruction loop in assembler is a second choice.

delay_100nsec has a minimum delay of 100 nanoseconds.

This assumes that the CPU clock is 100MHz. You can add

or subtract "nop" instructions to adjust the delay time in the loop.

--------------------------------------------------------------------

#include <stdint.h>

void delay_100nsec(uint32_t num_nsec)
{
__asm volatile ( "delay_100:" );
__asm volatile ( "nop" );
__asm volatile ( "nop" );
__asm volatile ( "subs r0, r0, #1" );
__asm volatile ( "bne delay_100" );
}

--------------------------------------------------------------------

0 Kudos
2,399 Views
kerryzhou
NXP TechSupport
NXP TechSupport

Hi Cyrus Vali,

    As you know, different IDE will generated difference asm code, that's why you get different time. It also relate to the optimization.

   If you want to get precise delay, I suggest you to use the hardware timer, that won't be relate to the IDE.

 

Wish it helps you!

Have a great day,
Kerry

 

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
----------------------------------------------------------------------------------------------------------------------

2,400 Views
rocketdawg2
Contributor II

it is only by luck that the value 25000 causes a delay of 1ms.

And that may change when changing compiler optimizations.

A loop of this kind is totally dependent upon the code that the compiler generates.

see this link.

What do you use to delay? | www.LPCware.com 

2,399 Views
cyrusvali
Contributor II

Thank you very much

Is there any way to check the exact clock speed?

Is there any function defined ?    

0 Kudos
2,399 Views
robertboys
Contributor IV

To confirm or determine the CPU clock speed using SWV try this:  www.keil.com/appnotes/docs/apnt_297.asp

SystemCoreClock is a calculated value and not measured.

I have seen a bug in the CMSIS system file cause this global variable to have the wrong value - not often though.

Bob

0 Kudos
2,399 Views
converse
Senior Contributor V

If you are using LPCOpen, the systen clock speed is normally set in the global variable SystemCoreClock.

However, that has nothing to do with your problem. Your delay loop is entirely dependent on the code generated by the compiler. There is no way to guarantee that your C code will compile into the same code, and thus take the same time, between different version of the same compiler, let alone a different compiler. Several accurate ways of defining a delay loop are listed in the thread that @DavidThedans provided.

2,399 Views
cyrusvali
Contributor II

I'm using LPC1768

is there any LPCOpen core for this chip?

all i could find is for lpc1788

Also i have a problem with debugging

why is Peripherals pane completely empty?

i'm connecting with Jlink to the board

i can build the code with no problem though

is there any useful guide to develop on LPCXpresso for LPC1768?

thanks

0 Kudos
2,399 Views
converse
Senior Contributor V

Use lpcopen for lpc1769. It is identical chip, just higher max speed. It is provided in lpcxpresso examples or can be found here

http://www.nxp.com/products/software-and-tools/hardware-development-tools/lpcxpresso-boards/lpcopen-... 

As as far as I remember, peripheral view is not supported with jlink, only with cmsis-dap probes, like lpc-link2. 

2,399 Views
cyrusvali
Contributor II

Thanks a lot

Can i download and use CMSIS packages for eclipse instructed in the below link?

Packs manager 

it supports Jlink AFAIK

0 Kudos
2,399 Views
converse
Senior Contributor V

No. Packs manager is not part of lpcxpresso. NXP do not support CMSIS. Only lpcopen.

2,399 Views
cyrusvali
Contributor II

Thanks

Can LPCXpresso compile assembly?

__asm  void wait()

{

    nop

    BX lr

}

what is wrong with this code?

it works on Keil

LPCXpresso says wrong syntax

0 Kudos
2,399 Views
kerryzhou
NXP TechSupport
NXP TechSupport

Hi Cyrus Vali,

   You can try this code in LPCXpresso IDE:

void wait(void)
{
      __ASM volatile ("nop");
      __ASM volatile ("BX lr");
}


Have a great day,
Kerry

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------