-1

我不会浪费你的时间,只是发布代码和解释

#define F_CPU 8000000UL

#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>

int main(void){
    sei(); //Enable interrupts 
    DDRB = (1 << PORTB3);//Set pin P3 as an output and other pins as inputs
    //PORTB = 0xff;
    DDRA = (1 << PORTA7);//Set pin A7 as an output and other pins as inputs
    //PORTA = (1 << PORTA7);
    TCCR0 = (1 << WGM00) | (1 << WGM01) | (1 << COM01);//Enable PWM, and configure Timer
    TIMSK = (1 <<  TOIE0);//Enabling an interrupt
    OCR0 = 255;//Setting comparison value for the Output compare unit
    TCCR0 |= (0b110 << CS00);//Selecting the clock as the falling edge on a certain pin

    while(1){
    /*
     * The portion of the code creates a square wave with a period of 39 us, which means that the falling edge occurs at a period of 78us, and since the output period of 
     * the PWM is 50Hz for a servo, that fits perfectly (1/(79*10^-6 * 256) ~ 50), but for some reason, the servo doesn't move...*/
       PORTA ^= (1<< PORT7);
       _delay_us(39);
    }
}

所以有什么问题??我真的没有示波器来测量频率,所以不要让我这样做,但我注意到的一件奇怪的事情是伺服电源线上的电压是 2.7V,而应该是 5V ,但是电源本身是提供5V的,只有当我将信号引脚连接到PWM引脚时才会发生这种情况,并且无论5V导轨是否连接到伺服器都会发生......关于什么问题的任何想法是??

4

1 回答 1

0

您的 PWM 输出具有 50% 的占空比,因此使用电压表测量时,有效端口输出电压从 5v 降低到 2.5v。假设您正在测量对地电压,如果将 5v 电源线连接到伺服器,则不会有任何区别,但如果未连接 PWM 信号,则会有所不同。

如果伺服是双向的,那么 50% 的占空比可能会使其保持静止 - 尝试不同的占空比,看起来好像您已经硬编码了 PWM 周期,每半个周期反转输出。尝试类似的东西

PORTA ^= (1<< PORT7);
_delay_us(28);
PORTA ^= (1<< PORT7);
_delay_us(50);
于 2016-06-19T13:17:00.837 回答