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

delay_us() problem

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



Joined: 30 May 2005
Posts: 10
Location: India

View user's profile Send private message Visit poster's website

delay_us() problem
PostPosted: Sun Feb 04, 2007 12:18 pm     Reply with quote

Hello,
i'm running a 16F876A chip at 20Mhz

I am trying to drive a hobby servo, using the following code(from elsewhere in this forum) to generate 1ms to 2ms timings.

Code:

#include <16F876A.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#define quar_mov 100

#int_TIMER0           // This function is called every time
void servo_isr()      // the RTCC (timer0) overflows (255->0).
{
int b=10;
output_high(PIN_C0);
delay_ms(1);
delay_us(quar_mov*b);
output_low(PIN_C0);
}


void main()
{
set_timer0(0);
setup_timer_0( RTCC_INTERNAL | RTCC_DIV_256 );
enable_interrupts(INT_TIMER0);
enable_interrupts(GLOBAL);
while(1);
}



I need to keep updating the value of "b" from a scale (0 to 10) to generate 1ms to 2ms delays.

According to the manual,
Syntax: delay_us (time)
Parameters: time - a variable 0-255 or a constant 0-65535

if I key in delay_us(100*10), it gives a perfect 1ms delay.
But delay_us(quar_mov*b); gives me less than 1ms.

Please note that delay_us(quar_mov*10) also gives a perfect 1ms delay.

It's the int b which is not getting loaded in.

Is there any work around this?
Thank you.
_________________
**ice**
Machinegrid.com :: Robots at Work!!
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Feb 04, 2007 12:22 pm     Reply with quote

Here are two functions for a long delay with a variable parameter (16-bit).

long_delay_us():
http://www.ccsinfo.com/forum/viewtopic.php?t=16656

long_delay_ms():
http://www.ccsinfo.com/forum/viewtopic.php?t=375
ice



Joined: 30 May 2005
Posts: 10
Location: India

View user's profile Send private message Visit poster's website

Thank you
PostPosted: Sun Feb 04, 2007 1:12 pm     Reply with quote

Thank you for your help,PCM.
It now works.

My servo update in now at 70Hz ,I 'm not able to go lower than that(RTCC_DIV_256).It's slow, but it gets it's position right.


In case anyone finds it useful, hobby servo control code:

Code:

#include <16F876A.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)


void long_delay_ms(long count)
{
long k;

for(k = 0; k <count>0).
{
int16 sum;           
int16 b=20;
output_high(PIN_C0);
sum=400+(b*20);             //change value of b from 0 to 20(1ms to 2ms)
long_delay_ms(sum);        // 400 for 1ms,,600 for 1.5ms,800 for 2ms
output_low(PIN_C0); 
}



void main()
{
set_timer0(0);
setup_timer_0( RTCC_INTERNAL | RTCC_DIV_256 );
enable_interrupts(INT_TIMER0);
enable_interrupts(GLOBAL);
while(1);
}
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