使用avr控制多个舵机

Control multiple Servo using avr

我想通过直接切换输出来尝试使用带有 timer1 的 avr 来控制大约 20 个伺服系统(用于机器人)。 到目前为止,这就是我能想到的。但是当我尝试添加最后两个舵机时,所有舵机都失败了。我假设这是一个时间问题。但我不知道如何纠正这一点。我在网上搜索,但找不到任何具体答案。

如果有人能指出他们谈论控制多个伺服系统的良好来源,那将非常有帮助

int main(void)
{
    DDRB = 0xFF;
    DDRA = 0xFF;

    TCCR1A |= 1<<WGM11; 
    TCCR1B |= 1<<WGM12 | 1<<WGM13 | 1<<CS10;
    TIMSK |= 1 << OCIE1A;

    ICR1 = 19999;  // Clear on reaching this point. 
    uint16_t count = MIN_VAL;
    uint16_t count2 = 2000; 
    uint16_t c_rp = MIN_VAL;
    uint16_t c_rp2 = 1000; 
    uint16_t c_rp3 = MIN_VAL;



    sei();
    while(1)
    {
        // Control PORT B PINS 
        // TCNT1 counts upwards and on reaching count 
        // an then the pin goes low
        // SERVO 1
        if( (  PORTB & (1<<PIN) ) && TCNT1 >= count)
        {
            PORTB &= ~(1<<PIN);
            if (count < MAX_VAL)
                count  += INC_VAL; // CHANGING SERVO POSITION
            else
                count = MIN_VAL; 

        }

        // SERVO 2
        if( (  PORTB & (1<<PIN2) ) && TCNT1 >= count2)
        {
            PORTB &= ~(1<<PIN2); // CONSTANT SERVO POSITION

        }


        // Control PORT A PINS 
         // SERVO 3
        if( (  PORTA & (1<<RP) ) && TCNT1 >= c_rp)
        {
            PORTA &= ~(1<<RP);
            if (c_rp < MAX_VAL)
                c_rp  += INC_VAL;
            else
                c_rp = MIN_VAL; 

        }

          // SERVO 4
        if( (  PORTA & (1<<RP2) ) && TCNT1 >= c_rp2)
        {
            PORTA &= ~(1<<RP2);

        }

 }


ISR (TIMER1_COMPA_vect) // ISR called when TCNT1 equals ICR1
{
    PORTB |= (1<< PIN ) | (1<< PIN2) ;
    PORTA |= (1 << RP ) | (1 << RP2) | (1<< RP3);
}

通过这种无耻的自我推销,我至少可以得到 12 个。不过,它可能会为您指明正确的方向: https://github.com/pengumc/servocontroller/blob/ef4d7c69e96375feeab30072e7b7f7975616cc87/src/ServoControllerI2C.c