Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Delay C-code for Pic 16F690

Status
Not open for further replies.

lmartinel

New Member
I am using the following code for Time Delays when programming the Pic 16F690. However, I am not totally understanding how it works to provide the time delay. Can anyone of you be so kind and explain it to me in detail how it works?

// Using Internal Clock of 8 Mhz
#define FOSC 8000000L

// Delay Function
#define _delay_us(x) { unsigned char us; \
us = (x)/(12000000/FOSC)|1; \
while(--us != 0) continue; }

void _delay_ms(unsigned int ms)
{
unsigned char i;
do {
i = 4;
do {
_delay_us(164);
} while(--i);
} while(--ms);
}



void main(void)
{

//Other code written here

_delay_ms(10);

//Other code written here


}

If possible is there a way to modify such code to provide time delays in micro seconds. Thank you
 
I haven't checked the math, but it seems clear that the _delay_us macro is intended to give you delays in microseconds.

Mike
 
Can you provide a simple delay routing with the _delay_us macro. Thank you
Perhaps you don't understand. Just use the _delay_us macro that you have.

Just use it in this way:

Code:
// Using Internal Clock of 8 Mhz
#define FOSC 8000000L

// Delay Function
#define _delay_us(x) { unsigned char us; \
us = (x)/(12000000/FOSC)|1; \
while(--us != 0) continue; }

void _delay_ms(unsigned int ms)
{
unsigned char i;
do {
i = 4;
do {
_delay_us(164);
} while(--i);
} while(--ms);
}

void main(void)
{

_delay_ms(10);

//Other code written here

[COLOR="Red"]// to delay by 20 microseconds

_delay_us(20);[/COLOR]

}
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top