|
|
View previous topic :: View next topic |
Author |
Message |
ice
Joined: 30 May 2005 Posts: 10 Location: India
|
delay_us() problem |
Posted: Sun Feb 04, 2007 12:18 pm |
|
|
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
|
|
|
ice
Joined: 30 May 2005 Posts: 10 Location: India
|
Thank you |
Posted: Sun Feb 04, 2007 1:12 pm |
|
|
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);
}
|
|
|
|
|
|
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
|