Closed. This question is off-topic。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
                        
                        4年前关闭。
                                                                                            
                
        
我想尝试通过直接切换输出,使用带有带有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

10-04 14:36