Go Down

Topic: How to Controll two servos with register? (Read 98 times) previous topic - next topic

Typhonn353

Nov 17, 2019, 06:11 pm Last Edit: Nov 17, 2019, 06:39 pm by Typhonn353
Hi,
i have to controll two servos without the functions. I have found a code example which tied to adjust for two servos. But it doesn't work. I use the pin 9 and 10.

Code: [Select]


void setup() {
  Serial.begin(9600);
 
  TCCR1A = 0;
  TCCR1B = 0;
 
  TCCR1A |= (1 << WGM10) | (1 << WGM11);
  TCCR1B |= (1 << WGM10) | (1 << WGM11);
 
  TCCR1B |= (1 << CS11);
 

  TCCR1A |= (1 << COM1A1);
  TCCR1B |= (1 << COM1B1);

  DDRB |= (1 << DDB1)|(1 << DDB2);
 
}

void loop() {
 
  for(int i = 0; i < 100; i++)
  {
    OCR1A = 90+i;
    Serial.println(90);
    OCR1B = 90+i;
    Serial.println(90);// Setzen des Impuls-
    delay(15);
  } 
}



blh64


johnwasser

Code: [Select]
  TCCR1A |= (1 << WGM10) | (1 << WGM11);
  TCCR1B |= (1 << WGM10) | (1 << WGM11);

 What you have written assumes that both bits are in both registers.  The WGM10 and WGM11 bits are only in one register each. 

Add some comments to indicate what you intended to do!  For example, what Waveform Generation Mode did you want to implement?  My guess is one of the PWM modes. If you REALLY wanted to set those two bits you would be sellecting Mode 3: PWM, Phase Correct, 10-bit.  I don't know why you would throw away 6 bits of resolution.


 Setting CS11 alone sets the clock prescale to 8 meaning a 2 MHz clock.  At 10 bits (1024 counts) you have a repeat rate close to 1 kHz.  A typical RC servo repeat rate is more like 20 Hz.  Can your servos take pulses at that speed?


If you use WGM 10 and set the ICR to 49999 you will get a 20 Hz repeat rate.


Servo pulses range from about 1000 microseconds to 2000 microseconds.  Your PWM values should be between 1000 and 2000 (clock period is 0.5 microseconds and each PWM count increases the number of clock counts by 2).  I don't know why you are using PWM values of 90 to 190.
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp

Robin2

@Typhonn353, if you require help with a program that uses registers it would be helpful if you include a comment on each line to say what it does so that it is not necessary for us to laboriously look everything up in the Atmega datasheet.

Those comments will also help you when you return to the code after a 2-week break :)

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

johnwasser

Code: [Select]
  TCCR1A |= (1 << WGM10) | (1 << WGM11);
  TCCR1B |= (1 << WGM10) | (1 << WGM11);

 What you have written assumes that both bits are in both registers.  The WGM10 and WGM11 bits are only in one register each. 

Add some comments to indicate what you intended to do!  For example, what Waveform Generation Mode did you want to implement?  My guess is one of the PWM modes. If you REALLY wanted to set those two bits you would be sellecting Mode 3: PWM, Phase Correct, 10-bit.  I don't know why you would throw away 6 bits of resolution.

 Setting CS11 alone sets the clock prescale to 8 meaning a 2 MHz clock.  At 10 bits (1024 counts) you have a repeat rate close to 1 kHz.  A typical RC servo repeat rate is more like 20 Hz.  Can your servos take pulses at that speed?

Servo pulses range from about 1000 microseconds to 2000 microseconds.  Your PWM values should be between 1000 and 2000 (clock period is 0.5 microseconds and each PWM count increases the number of clock counts by 2).  That's going to be hard to do when your highest PWM value is 1023.  I also don't know why you are using PWM values of 90 to 190.

If you use WGM 10 and set the ICR (TOP) to 49999 you will get a 20 Hz repeat rate.

Code: [Select]
void setup()
{
  Serial.begin(9600);


  // WGM = 10 (PWM, Phase Correct, TOP in ICR1)
  // CS = 2 (clock prescale of 8)
  // OCR1A abd OCR1B do normal PWM
  TCCR1A =  (1 << COM1A1) |  (1 << COM1B1) | (1 << WGM11);
  TCCR1B = (1 << WGM13) | (1 << CS11);
  ICR1 = 49999;


  DDRB |= (1 << DDB1) | (1 << DDB2);
}


void loop()
{


  for (int i = 1000; i < 2000; i += 10)
  {
    OCR1A = i;
    OCR1B = i;
    delay(15);
  }
}
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp

Go Up