在 “ATmega16 以按鈕控制 LED” 一文中,如下按鈕電路中加入了 pull up resistor,由於不加入 pull up resistor ,即無法得知如下電路標示 1 之電位,其動作在按下按鈕後此點電位為低電位,否則為高電位,而ATmega16 單晶片中可以在內部設定 pull up resistor,即可無須在外部電路中加入 pull up resistor,此範例將外部 pull up resistor 電路移除,直接使用 ATmega16 內部pull up resistor。

 

 

電路:

 

以下介紹 pull up resistor 設定方法。

參考下表,將 DDxn 設為0 (該接腳為 Input),PORTxn設為 1,即可將該接腳設為內部 pull up resistor。

 

 

程式:

#include <avr/io.h>

 

int main(void){

   

DDRA=0x1; //設定 PA0 為 OUTPUT

   DDRC=0x00;//設定 PC0 為 INPUT 此行程式碼可以省略

   PORTC=0x1; //設定 PC0 為 pull up resistor

while(1){

if(PINC & 1) //當PINC AND 1 等於 False 時,按鈕按下

PORTA = 1<<PA0; //LED OFF

else

PORTA = 0<<PA0; //LED ON

}

return 0;

}

 

arrow
arrow
    全站熱搜

    iammic 發表在 痞客邦 留言(0) 人氣()