Control two servo with two potentiometer

Thread Starter

mr_l

Joined Aug 20, 2011
12
Hello!
I'm trying to write code to control two servo with two potentiometers
Devices that I use is: microcontoller atmega88, breadboard, two servo Futaba S3003 and Elsy AVRUSB500 programmers.
Everything is properly connected but my code is not working as it should.
What happens when I try to control the servo is that only one potentiometer controls both servos.
Can someone tell me what is wrong in my code and how to solve this problem??
Thanks in advance here is my cod:

Rich (BB code):
#include<avr/io.h>
#include<util/delay.h>
#include<avr/interrupt.h>

void ADC_init(void);
void Tim_init(void);

unsigned int ADC_read(unsigned char);

int main(void)
{

    Tim_init(); // Initialization of Timer1

    DDRB |= 0b00000111;// Set PB1 and PB2 as outputs
    TCCR1A |= (1<<COM1A1);  //  PWM on port B1 - non-inverted compare mode 2
    TCCR1A |= (1<<COM1B1);   //  PWM on port B2 - non-inverted compare mode 2
    
    TCNT1 = 0;

    OCR1A = 1500;
    OCR1B = 1500;

    TIMSK1 |= (1<<OCIE1A);
    TIMSK1 |= (1<<OCIE1B);

    ADC_init();    // Initialization of ADC    


     while (1)
     {
    
     if (TIFR1 & (1 << OCF1A)) 
        {
           OCR1A = ADC_read(0);
        }
      
        if (TIFR1 & (1 << OCF1B)) 
        {
           OCR1B = ADC_read(1);
        }
      
      }

    
}

void Tim_init(void)
{

    TCCR1A = 0;     // disable all PWM on Timer1 
    ICR1 = 19999;   // frequency  20ms

    
    TCCR1A = (1<<WGM11);  //  Timer 1 for Fast PWM mode via ICR1, with no prescaling
    TCCR1B = (1<<WGM13) | (1<<WGM12) | (1 << CS10) ;
    
}

void ADC_init(void)        // Initialization of ADC
{
    ADMUX=(1<<REFS0);    // AVcc with external capacitor  AREF
    ADCSRA=(1<<ADEN)|(1<<ADPS2)|(0<<ADPS1)|(0<<ADPS0);    // Enable ADC and set Prescaler division factor as 128
}
 
unsigned int ADC_read(unsigned char ch)
{
    ch= ch & 0b00000011;        // Set chanel 1 or 2
    ADMUX |= ch;                // selecting channel
 
    ADCSRA|=(1<<ADSC);            // start conversion
    while(!(ADCSRA & (1<<ADIF)));    // waiting for ADIF, conversion complete
    ADCSRA|=(1<<ADIF);            // clearing of ADIF
 
    return ((ADCL|(ADCH<<8))*8);
}
 
Last edited by a moderator:

BillO

Joined Nov 24, 2008
990
Hmmm, your cod looks a little fishy....:p

That being said it may be that I'm not familiar with some of the syntax used here. This kinda looks like C, but can you point me to an on-line language reference for this particular version?
 

stahta01

Joined Jun 9, 2011
133
The below line implies ch should be 1 or 2 instead of 0 and 1 like you are using. You need to determine the right answer.
Rich (BB code):
ch= ch & 0b00000011;        // Set chanel 1 or 2
Tim S.
 

BillO

Joined Nov 24, 2008
990
Is this right?
Rich (BB code):
     TCCR1A |= (1<<COM1A1);  //  PWM on port B1 - non-inverted compare mode 2
     TCCR1A |= (1<<COM1B1);   //  PWM on port B2 - non-inverted compare mode 2

Maybe it should be this?
Rich (BB code):
     TCCR1A |= (1<<COM1A1);  //  PWM on port B1 - non-inverted compare mode 2
     TCCR1B |= (1<<COM1B1);   //  PWM on port B2 - non-inverted compare mode 2
 
Top