[General] Logic Behind Delay Function

Status
Not open for further replies.

SAMO6

Newbie level 5
Joined
Nov 14, 2016
Messages
10
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
139
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?
 

Just use MikroC. There are all libraries implemented already. Same simple as arduino.
 
Reactions: SAMO6

    SAMO6

    Points: 2
    Helpful Answer Positive Rating
Hi,

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.

Klaus
 

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.

Brian.
 
Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…