The code I gave you is very crude. It uses a loop that executes 10 NOPs=20 100 times to delay 1000 instruction cycles, but there are a lot of=20 cycles besides the NOPs that get executed, incrementing variables and=20 checking results, etc. It wasn't intended to be accurate, just a way to=20 see if the clock was even close. Try setting the configuration to HS or XT, with debugger off and see if=20 that helps. If not, you may have a bad crystal. Kerry geetansh wrote: > hey kerry i used your code and found some weird results > here i used both (ur code for delay and delay_ms(10)) n the result was 10= ms > delay loop gave a delay of approx. 6 seconds while delay_ms gave a delay = of > 2 seconds and after that i changed the clock freq in code to 20MHz(using > same 4MHz crystal) and here both delays gave a delay of 10seconds. > i think the crystal oscillator is definitely not giving 4MHz but why dela= y > loop and delay function are not giving same results? > > code used=20 > #include <16F877A.h> > #use delay(clock=3D20000000) > void main() > > { > int16 i,j; > while(1) > { > output_D(255); > for(i=3D0;i<10;i++) //gave a delay 6sec > { > for(j=3D0;j<100;j++) =20 > { > #asm > nop > nop > nop > nop > nop > nop > nop > nop > nop > nop > #endasm > } > }; > output_D(0); > delay_ms(10); // gave a delay of 2sec. > } > } > > > > Kerry Wentworth wrote: > =20 >> My somewhat outdated CCS compiler does delays using NOPs in a loop, not= =20 >> a timer. Also, the 877 has no internal oscillators. >> >> Since you don't enable interrupts (and they default to 'off'), it seems= =20 >> to me it can only be 2 things: >> >> 1) The debugger is using a LOT of time (seems unlikely) >> >> 2) The oscillator is not really running at 4MHz. >> >> Try running without the debugger (config=3D0F09), because that's easy. >> If that doesn't do it, write your own delay >> >> ms_delay(int d) // Only works with 4MHz clock!!! >> { >> for(i=3D0;i> { >> for(j=3D0;j<100;j++) >> { >> #asm >> nop >> nop >> nop >> nop >> nop >> nop >> nop >> nop >> nop >> nop >> #endasm >> } >> } >> } >> >> If that gives you the correct delay, the problem is the delay_ms()=20 >> routine, otherwise, it is the clock frequency. >> >> Kerry >> >> geetansh arora wrote: >> =20 >>> sorry i missed some details which might be important in solving this >>> problem. >>> >>> I started with my first project but it didn't worked so i decided to go >>> back >>> and blink leds,now the problem is when the delay between on off was se= t >>> 10ms the leds took 2.3sec(used a stopwatch n delay was approx. 2.3sec). >>> At first delay was set to 500ms but it took too much time to blink so >>> delay >>> was set to 10ms. >>> CCS compiler demo version was used for compiling , 4MHz crystal >>> oscillator >>> was used with 22pF capacitors and configuration bits 2F09 were entered = in >>> PICKIT 2 v2.61 (not used in the code) >>> >>> #include <16F877A.h> >>> #use delay(clock=3D4000000) >>> void main() >>> { >>> while(1) >>> { >>> output_D(255); >>> delay_ms(10); >>> output_D(0); >>> delay_ms(10); >>> } >>> } >>> please solve my problem guys. >>> thank you and sorry again for missing the details in previous post. >>> =20 --=20 http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist .