Hi all
I am trying to control multple servos via flex sensors. however i have succesffuly developed a normal one where a delay is used but as i try to get the second working it all just goes wrong. so iv found that i have to work with timers and interrupts to make 5 servos work simultaniously. i have created this code to run one servo at the start but its not working, im getting no reaction from the servo.
code:
Moderators note : used code tags
I am trying to control multple servos via flex sensors. however i have succesffuly developed a normal one where a delay is used but as i try to get the second working it all just goes wrong. so iv found that i have to work with timers and interrupts to make 5 servos work simultaniously. i have created this code to run one servo at the start but its not working, im getting no reaction from the servo.
code:
C:
unsigned int Adcval;
unsigned int Adcval2;
unsigned int i,a, count;
char value = 0;
int on_time ;//= 150; //On-Time for the PWM signal
//int count; //count gets incremented for every timer overlap
int pot_value;
void interrupt ()
{
if(TMR0IF_bit==1) // Timer has overflown
{
TMR0 = 252; /*Load the timer Value, (Note: Timervalue is 101 instaed of 100 as the
TImer0 needs two instruction Cycles to start incrementing TMR0 */
TMR0IF_bit=0; // Clear timer interrupt flag
count++;
}
if (count >= on_time)
{
PORTB.F0=1; // complement the value for blinking the LEDs
}
if (count >= (on_time+(200-on_time)))
{
PORTB.F0=0;
count=0;
}
}
void main()
{
TRISA.F2 = 1; //analogue IP servo1
TRISB = 0x00; // PORTB as Ouput Port
ADC_init();
GIE_BIT=1; // TURNING ON GLOBAL INTERRUPT
PEIE_BIT=1;// TURNING ON PERIPHERL INTERRUPT BIT
OPTION_REG = 0b00000100; // Timer0 with external freq and 32 as prescaler
TMR0=251; // Load the time value for 1us delayValue can be between 0-256 only
INTCON.TMR0IE = 1; //Enable timer interrupt bit in PIE1 register
while(1)
{
pot_value = (ADC_Read(2))*0.039;
on_time = (170-pot_value);
}
}
Last edited by a moderator: