CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Using Variables for delays

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
AlboKid
Guest







Using Variables for delays
PostPosted: Sun Dec 01, 2002 8:54 pm     Reply with quote

I want to use delays that vary every second or so.


example)
do
{
output_high(PIN_B5);
high_value = (1440 - (28*count));
delay_us(high_value);
low_value = 20000 - high_value;
output_low(PIN_B5);
delay_us(low_value);
} while (something)
count++;

i just want to know if i can declare some variable to control the lows and highs, such as high_value or low_value so that it can control a steering servo for a autonmous race car.

thanks
___________________________
This message was ported from CCS's old forum
Original Post ID: 9667
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

Re: Using Variables for delays
PostPosted: Mon Dec 02, 2002 1:35 am     Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group