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.

i am having an issue using __delay_ms()

Status
Not open for further replies.

Iawia

Member
Hi,

USING:

pic12F508
MPLAB X v8.8
compiler HI-tech c


apparently, the macro __delay_ms(time) will not compile when 'time' is a declared variable. I have tried included source and header files like 'delay.h', and 'delay.c' from other libraries without any luck.

I would like to vary the delays with a variable. Is this possible?
Thanks.
 
I had the same recently. It looks like only integers can be used on HTC ..

In MikroC there is the Vdelay function, which does allow variables to be used.

If it can be done with HTC, it would be useful.

Dave
 
Yes... I found that even constants will throw errors ( software errors )... __delay_us(200); doesn't give the desired delay but __delay_us(180); does....

I make two functions that call "__delay_us();" one for Ms and one for Us... It works for me..
 
So what's the deal with this???

I feel like this should be a simple thing to have the __delay_ms() run on variable delays, but it isn't very simple. I have been trying to research other functions like DelayMs(). Does anyone have any info on this? I'm not sure how to obtain this function.

Would some code such as a for loop or do-while do the trick? How accurate would this timing be?

int i = 0;
do{
__delay_ms(1);
i++;
}while(i<delay_value)
 
Functions, like everything, are written by people... Some people think they are the best programmers in the world.... Microsoft is full of these people.. Hi-tech will have one or two...

Hence!! I write most of my own routines.... Then if they don't work as expected ( this is very normal ), I only have me to blame....

write this:-

Code:
void delayMs(int x)
   {
   while(x--)
      __delay_us(1000);
   }

if that doesn't work (it should do) re-write the delayUs () routine.
 
Last edited:
Thanks Ian,

This is what i have managed to come up with.
I am capturing tenths of a ms for better accuracy, but still I am unsure on exactly how accurate this really is.
(with this example i am realizing finally how precious memory is with the 12F508, the 'while' taking up less program space than 'for')

Code:
void DelayMs(int tenths_of_ms){	// Delay routine in 1/10 of a millisec

	while(tenths_of_ms--)
	__delay_us(100);
}

The compiler is finally happy and that's good. My only thoughts are that having the variable there as a part of the function would mess accuracy up so much that it would defeat the purpose of the function itself, the reason why it is so hard to find.

cheers, -t
 
Last edited:
if you are interested, It is Vdelay_ms() in MikroC, I've used it but it is not as accurate as Delay_ms.
 
Last edited:
The while() loop takes 6 clock cycles.. so for an xtal of 4mhz ( 1μS instruction time ) you would use a constant of 94

Code:
void DelayMs(int tenths_of_ms){	// Delay routine in 1/10 of a millisec
 
	while(tenths_of_ms--)
	__delay_us(94);
}

for other xtals you need to adjust accordingly..

For complete acuracy, use timer0 and pre-load the timer with 5 and every timeout it will be exactly 250μS with a 4mhz xtal ( you'll need to set the prescaler to 1:1 using the WDT setting )
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top