PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
Re: Using Variables for delays |
Posted: Mon Dec 02, 2002 1:35 am |
|
|
I guess your question really is, "How can I use a 16-bit
variable to do a delay with CCS, since CCS only allows
8-bit variables for delay_us() ?".
Of course, CCS lets you use a 16-bit constant, but you
need a variable, not a constant.
Below, there is a function which will accept a 16-bit variable
to do a delay for the specified time in microseconds.
I got this function from some sample code for Hitech C,
and converted it to CCS. The original code is here:
http://www.microchipc.com/sourcecode/#delay
I just did a rough test, using the sample program below,
and it appears to work. Possibly the delay parameter of
253 might have to be changed slightly, to fine tune the
overall loop delay for the CCS compiler. That would
require more detailed testing than I am able to do at
the moment.
Code: |
#include <16F877.H>
#fuses HS,NOWDT,NOPROTECT, PUT, BROWNOUT, NOLVP
#use delay(clock=8000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
//----------------------------------
void long_delay_us(long count)
{
char i;
i = (char)(count >> 8);
while(i >= 1)
{
i--;
delay_us(253);
restart_wdt();
}
delay_us((char)count);
}
//============================
main ()
{
long value;
long k;
value = 50000;
printf("Press Enter to start\n\r");
getc();
// Do a 10 second delay ( 200 * 50 ms = 10 seconds)
for(k = 0; k < 200; k++)
long_delay_us(value);
printf("Done\n\r");
while(1);
}
|
Note:
This routine is only needed for vs. 2 and vs. 3 compilers. Vs. 4 has this
feature built-in.
---
Edited to put the code in a code block.
___________________________
This message was ported from CCS's old forum
Original Post ID: 9670 |
|