Hello,
Can we get precise delays with Nops()?
Nop() corresponds to TCY right? In case of PIC18F, TCY=4/CrystalFreq right? (4 cycles per instruction)
Im using a PIC18F2550 with a 4MHz crystal and I just want to blink an LED on RA0 pin. I want it to be ON for 1 second and OFF for the same amount of time.
So, once 4 cycles are needed to acomplish one instruction it gives: Nop() = (4/4Mhz) = 1us. Right? Then, I need 1.000.000 Nops.
However the result is that the LED is ON for much more that 1 second. About 2.5 seconds...
Here is the code Im using:
What am I missing?
Thanks in advance.
Can we get precise delays with Nops()?
Nop() corresponds to TCY right? In case of PIC18F, TCY=4/CrystalFreq right? (4 cycles per instruction)
Im using a PIC18F2550 with a 4MHz crystal and I just want to blink an LED on RA0 pin. I want it to be ON for 1 second and OFF for the same amount of time.
So, once 4 cycles are needed to acomplish one instruction it gives: Nop() = (4/4Mhz) = 1us. Right? Then, I need 1.000.000 Nops.
However the result is that the LED is ON for much more that 1 second. About 2.5 seconds...
Here is the code Im using:
Rich (BB code):
#include <p18f2550.h>
void delay(unsigned int N){
unsigned int i;
for(i=0; i < N; i++) Nop();
}
void main(void)
{
unsigned int i;
//WDTCONbits.SWDTEN = 0;
TRISA = 0x11111110;
LATA = 0;
while(1)
{
LATA = 1;
for(i = 0; i < 50; i++)
delay(20000);
LATA = 0;
for(i = 0; i < 50; i++)
delay(20000);
}
}
Thanks in advance.