Control two servo with two potentiometer

Discussion in 'Embedded Systems and Microcontrollers' started by mr_l, Nov 5, 2011.

  1. mr_l

    Thread Starter New Member

    Aug 20, 2011
    12
    0
    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:

    Code ( (Unknown Language)):
    1. #include<avr/io.h>
    2. #include<util/delay.h>
    3. #include<avr/interrupt.h>
    4.  
    5. void ADC_init(void);
    6. void Tim_init(void);
    7.  
    8. unsigned int ADC_read(unsigned char);
    9.  
    10. int main(void)
    11. {
    12.  
    13.     Tim_init(); // Initialization of Timer1
    14.  
    15.     DDRB |= 0b00000111;// Set PB1 and PB2 as outputs
    16.     TCCR1A |= (1<<COM1A1);  //  PWM on port B1 - non-inverted compare mode 2
    17.     TCCR1A |= (1<<COM1B1);   //  PWM on port B2 - non-inverted compare mode 2
    18.    
    19.     TCNT1 = 0;
    20.  
    21.     OCR1A = 1500;
    22.     OCR1B = 1500;
    23.  
    24.     TIMSK1 |= (1<<OCIE1A);
    25.     TIMSK1 |= (1<<OCIE1B);
    26.  
    27.     ADC_init();    // Initialization of ADC    
    28.  
    29.  
    30.      while (1)
    31.      {
    32.    
    33.      if (TIFR1 & (1 << OCF1A))
    34.         {
    35.            OCR1A = ADC_read(0);
    36.         }
    37.      
    38.         if (TIFR1 & (1 << OCF1B))
    39.         {
    40.            OCR1B = ADC_read(1);
    41.         }
    42.      
    43.       }
    44.  
    45.    
    46. }
    47.  
    48. void Tim_init(void)
    49. {
    50.  
    51.     TCCR1A = 0;     // disable all PWM on Timer1
    52.     ICR1 = 19999;   // frequency  20ms
    53.  
    54.    
    55.     TCCR1A = (1<<WGM11);  //  Timer 1 for Fast PWM mode via ICR1, with no prescaling
    56.     TCCR1B = (1<<WGM13) | (1<<WGM12) | (1 << CS10) ;
    57.    
    58. }
    59.  
    60. void ADC_init(void)        // Initialization of ADC
    61. {
    62.     ADMUX=(1<<REFS0);    // AVcc with external capacitor  AREF
    63.     ADCSRA=(1<<ADEN)|(1<<ADPS2)|(0<<ADPS1)|(0<<ADPS0);    // Enable ADC and set Prescaler division factor as 128
    64. }
    65.  
    66. unsigned int ADC_read(unsigned char ch)
    67. {
    68.     ch= ch & 0b00000011;        // Set chanel 1 or 2
    69.     ADMUX |= ch;                // selecting channel
    70.  
    71.     ADCSRA|=(1<<ADSC);            // start conversion
    72.     while(!(ADCSRA & (1<<ADIF)));    // waiting for ADIF, conversion complete
    73.     ADCSRA|=(1<<ADIF);            // clearing of ADIF
    74.  
    75.     return ((ADCL|(ADCH<<8))*8);
    76. }
     
    Last edited by a moderator: Nov 6, 2011
  2. BillO

    Well-Known Member

    Nov 24, 2008
    985
    136
    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?
     
  3. stahta01

    Member

    Jun 9, 2011
    133
    21
    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.
    Code ( (Unknown Language)):
    1.  
    2. ch= ch & 0b00000011;        // Set chanel 1 or 2
    3.  
    Tim S.
     
    mr_l likes this.
  4. BillO

    Well-Known Member

    Nov 24, 2008
    985
    136
    Is this right?
    Code ( (Unknown Language)):
    1.  
    2.      TCCR1A |= (1<<COM1A1);  //  PWM on port B1 - non-inverted compare mode 2
    3.      TCCR1A |= (1<<COM1B1);   //  PWM on port B2 - non-inverted compare mode 2
    4.  

    Maybe it should be this?
    Code ( (Unknown Language)):
    1.  
    2.      TCCR1A |= (1<<COM1A1);  //  PWM on port B1 - non-inverted compare mode 2
    3.      TCCR1B |= (1<<COM1B1);   //  PWM on port B2 - non-inverted compare mode 2
    4.  
     
Loading...