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.
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..
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....
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.
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 )