簡體   English   中英

ATtiny85數字“開”輸出無法提供5 V電源

[英]ATtiny85 digital “on” output fails to deliver 5 V

我正在使用ATtiny85作為微控制器。 我試圖讀取兩個大約3 V的輸入,並為每個“接通”輸入(大於1 V)輸出5V。 我將PINB0和PINB1用於輸入,將PINB3和PINB4用於輸出。 問題是當PINB0和PINB1都打開時,我得到兩個5 V輸出,但是當其中之一打開時,我只得到2 V,我試圖解決這個問題,所以我得到5V輸出。

這是我的代碼:

#inlude <avr/io.h>
#include <stdint.h>

int main(void)
{
// set pin 0 to input (pi signal 0)
DDRB &= ~(1 << PINB0);
PORTB &= 0 << PINB0;

// set pin 1 to input (pi signal 1)
DDRB &= ~(1 << PINB1);
PORTB &= 0 << PINB1;

//set pin 3 to output of 0
DDRB |= 1 << PINB3;
PORTB &= 0 << PINB3;

//set pin 4 to output of 1
DDRB |= 1 << PINB4;
PORTB &= 0 << PINB4;

while (1)
{
    if (bit_is_clear(PINB, 0) && bit_is_clear(PINB, 1))
    {
        PORTB &= 0 << PINB3;    //output zero volts 
        PORTB &= 0 << PINB4;    //output zero volts
    }
    else if (bit_is_clear(PINB, 0) && !(bit_is_clear(PINB, 1)))
    {
        PORTB &= 0 << PINB3;    //output zero volts
        PORTB |= 1 << PINB4;    //output 5 volts
    }
    else if (!(bit_is_clear(PINB, 0)) && bit_is_clear(PINB, 1))
    {
        PORTB |= 1 << PINB3;    //output 5 volts
        PORTB &= 0 << PINB4;    //output zero volts
    }
    else
    {
        PORTB |= 1 << PINB3;    //output 5 volts
        PORTB |= 1 << PINB4;    //output 5 volts
    }
}
}

使用您發布的代碼,當僅設置一個輸入時,相應的輸出將在循環中快速切換為開和關,而不是保持導通,從而得到平均在高輸出和低輸出之間的輸出電壓。 發生這種情況的原因是,盡管您正確地將輸出設置為高,但是在清除之前或之后立即清除另一個輸出時,也將其設置為低。 例如,當只有引腳1為高電平時,可以在循環中運行以下代碼:

    PORTB &= 0 << PINB3;    //output zero volts
    PORTB |= 1 << PINB4;    //output 5 volts

由於將PINB3位(或任何其他數量)移位0會得到零,然后將其與PORTBPORTB ,第一行將清除PORTB 所有位,從而關閉兩個輸出 然后在下一行中,將引腳4重新打開。

同樣,當只有引腳0為高電平時,運行以下命令:

    PORTB |= 1 << PINB3;    //output 5 volts
    PORTB &= 0 << PINB4;    //output zero volts

在這種情況下,第一行打開引腳3,但第二行再次關閉兩個輸出

與其嘗試將0移至正確的位位置,不如嘗試將1移至位然后取反。 例如,要關閉引腳4:

    PORTB &= ~(1 << PINB4);

...並關閉引腳3:

    PORTB &= ~(1 << PINB3);

這樣,您與PORTB的值將設置為除您要清除的位以外的所有位,而不是未設置任何位的值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM