# #use delay(clock=4000000) wrong output at leds

Status
Not open for further replies.

#### maoterk

##### Newbie level 5
Hi, i'm new at programming and i tried this easy code for flashing leds with dspic33e usb starter kit, but it seems to take around 5 seconds to flash rather than 1
Any suggestions?
Code:
#include <33EP512MU810.h>
#use delay(clock=40000000)
#byte PORTD=0xF81
void main() {

set_tris_d(0x00);

WHILE (TRUE){
output_high(PIN_D2);
delay_ms(1000);
output_low(PIN_D2);
delay_ms(1000);
}}

#### betwixt

##### Super Moderator
Staff member
I'm not sure which compiler you are using but two possibilities come to mind...

Is the clock configured properly - for example are you using an internal 8MHz clock instead of your external 40MHz one? That would account for being five times slower than expected.

Is there a maximum value for the delay_ms() function that you could be exceeding?

Brian.

#### maoterk

##### Newbie level 5
I am using ccs compiler and the internal oscillator. I am not sure about the maximum delay value

thanks for the response

#### maoterk

##### Newbie level 5
If i use #use delay(clock=8000000) it works correctly. So can't i use a faster clock with the internal oscillator?

thanks

#### xenos

##### Full Member level 4
Well, the oscillator documentation says that:
The primary oscillator and Internal FRC oscillator sources can optionally use an on-chip PLL to
obtain higher operating speeds.

On other microchip devices I have successfuly turn PLL on internal OSC, but these devices seems to have a startup limitation that may be a problem.
Check paragraph: "7.7.1 Input Clock Limitation at Start-up for PLL Mode"
It seems to me that you may not change PLL on the running oscillator and you should temporarily swap to an other one (eg to an external XT).

maoterk

### maoterk

Points: 2

#### Gorgon

##### Full Member level 6
Hi, i'm new at programming and i tried this easy code for flashing leds with dspic33e usb starter kit, but it seems to take around 5 seconds to flash rather than 1
Any suggestions?
Code:
#include <33EP512MU810.h>
#use delay(clock=40000000)
#byte PORTD=0xF81
void main() {

set_tris_d(0x00);

WHILE (TRUE){
output_high(PIN_D2);
delay_ms(1000);
output_low(PIN_D2);
delay_ms(1000);
}}

Since you have set the delay clock parameter to 40 000 000, it is natural that the speed is 1/5 of what you expect, if your actual CPU clock is 8MHz.

The delay(clock) parameter is not adjusting the CPU clock, it's only used as a base value for the compiler to compute the number of loops that is needed for the correct software delay.

maoterk

Points: 2