# 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

#### mike50

##### New Member
I haven't checked the math, but it seems clear that the _delay_us macro is intended to give you delays in microseconds.

Mike

#### lmartinel

##### New Member
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

#### mike50

##### New Member
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.

Replies
0
Views
1K
Replies
20
Views
19K
Replies
4
Views
2K
Replies
14
Views
9K
Replies
17
Views
2K