|
|
View previous topic :: View next topic |
Author |
Message |
jptalledo Guest
|
Optimize 2 servo control program |
Posted: Wed Oct 27, 2004 6:37 am |
|
|
Hi everyone
I will post my code to control 2 servos using timer0 and two variables : Posservo0 and Posservo1 to control the pulse for each servomotor
please if someone could help me to optimize the code I will be very pleased.
Thanks
#include <16F876A.h>
#device adc=8
#use delay(clock=20000000)
#fuses NOWDT,HS, PUT, NOPROTECT, NODEBUG, NOBROWNOUT, NOLVP, NOCPD, NOWRT
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
#include "C:\Documents and Settings\Jean Paul\My Documents\servo.h"
#include <stdio.h>
#include <string.h>
#define PinServo0 PIN_B4
#define PinServo1 PIN_B5
int PosServo0;
int PosServo1;
int flag;
int i = 0;
int j;
#int_TIMER0
TIMER0_isr()
{
output_high(PinServo0);
output_high(PinServo1);
delay_us(800);
flag=0;
i=0;
j=0;
while(flag==0)
{
delay_us(5);
i=i+1;
if (i==PosServo0)
{
output_low(PinServo0);
j=j+1;
}
if (i==PosServo1)
{
output_low(PinServo1);
j=j+1;
}
if (j==2)
flag=1;
}
}
void main()
{
byte Sync = 0; // 0xFF
byte ServoNo = 0; // 1 - 255
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_spi(FALSE);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_128);
enable_interrupts(INT_TIMER0);
enable_interrupts(GLOBAL);
PosServo0 = 127; // default servo pos
output_low(PinServo0);
while (TRUE)
{
if (kbhit())
{
Sync = getc();
if (Sync==0xFF)
{
PosServo0= getc();
PosServo1= getc(); // 0-255
}
}
}
} |
|
|
Trampas
Joined: 04 Sep 2004 Posts: 89 Location: NC
|
|
Posted: Wed Oct 27, 2004 11:04 am |
|
|
First look at the fast I/O capabilities of CCS.
Second consider redesigning program, that is having a delay function in a ISR may not be the best method.
Second where possible instead of using ints for variables like flag, use a bit. This may take less time for if(flag==0).
Of course the biggest thing is look at the assembly generated and play with the code.
Trampas |
|
|
|
|
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
|