Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Delay, 11 micro seconds using a For loop?

Status
Not open for further replies.
You question without providing additional info about the target micro controller, operating frequency and even compiler is completely useless.

Alex
 

If you're using mikroC, why not just use the delay routine:
Code:
delay_us(11); //11 microsecond delay

Just make sure you have the correct oscillator frequency set in mikroC (in your case, 20MHz).

Hope this helps.
Tahmid.
 

I guess you have nothing else you want to perform with your PIC micro. Wasting 11 usec in a loop is really wasting resources.
You should use a timer and compare for this. If the compare value is reached, you will get an interrupt.
A wait loop only works nicely if there is no chance for the program to be interrupted and waiting is really the only thing the micro does during than time.

Bob
 

this doesnt work for my case,
i intend to generate PWM cycle,

which is
1. STATE=ON,
2. Delay
3. State=OFF
4. Delay

@Bob:
can you please guide mehow to use timer? and interrupt,
i can use it as a backup option

thanks.
 
Last edited:

Can I see your code? That will help me find out where the error in the code lies, since your algorithm is fine (provided you return to step 1 after step 4 so that the process is repeated).

Hope this helps.
Tahmid.
 

void main()
{

int i;
TRISC = 0x00;
RC0_bit=1;
for(i=0; i<11; i++)
delay_us(1);


RC0_bit=0;
for(i=0; i<11; i++)
delay_us(1);
}


this doesnt work.....
 

You say it doesn't work. What does it do?

It looks like it should produce a single 11us, or slightly longer, positive pulse on RC0. Then it will pull RC0 down for 11us. Then the code ends so the uC will go to sleep.
It only happens once so it will be easy to miss. Is this what you want it to do?
 

Like mentioned by SherpaDoug, it is because your code doesn't repeat the action. In the algorithm above, for PWM to work, you need to go back to step 1 after step 4. You don't do that in the code. Change your code to:
Code:
void main()
{

int i;
TRISC = 0x00;
while(1){
RC0_bit=1;
for(i=0; i<11; i++)
delay_us(1);


RC0_bit=0;
for(i=0; i<11; i++)
delay_us(1);
}
}

or better still:
Code:
void main()
{

int i;
TRISC = 0x00;
while(1){
RC0_bit=1;
delay_us(11);


RC0_bit=0;
delay_us(11);
}
}

Hope this helps.
Tahmid.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top