Hello Everyone,
Can anyone tell me, How to implement the delay function for PIC18F4550? I tried the simple for loop for LED blinking, But, LED is not blinking it is simply in ON state.
I used below mentioned simple for loop for delay generation.
Code:
void delay1()
{
unsigned int q;
for(q=0;q<10000;q++);
}
I want to know how can I generate delay of 100 milliseconds,10 microseconds using 20Mhz crystal frequency.
I also tried macros like __delay_us()__delay_ms(),etc. But, they are not working.
I also know I can use timer for delay generation, But it is complicated and I don't know how to do that, Can anyone please explain me about best method for delay generation for PIC18F4550?
If you need help with your code...please post the complete code, not only a snippet.
We don't see how your clock is configured
We don't see port configuration,
We don't see how you switch ON and OFF the Led.
...
And I doubt the delay() functions don't work. But we can't verify this with your given informations.
Most compilers have delay functions that calculate the values for you. Which compiler are you using?
The delay you posted is fine but it has two potential problems:
1. it creates the delay by using the time it takes to execute the instructions in the loop, that makes it dependent on the exact code used in the compiler and the clock divider ratio if you are using one. It can take trial and error to get the delay right.
2. it is a blocking routine, when you enter the delay loop the only way out is when the count reaches 10000. That may be OK in your application but during the delay the PIC can't do anything else.
A better method is to use the hardware timer registers and timer interrupts, that method can be used to alert you to the delay period finishing while your code does something else. It also gives more precise timing.