以 ATmega 16 為例,ATmega 16 總共有 4組Pin 腳(PA、PB、PC、PD),本例使用 PA 為例,控制點亮 LED。

 

電路:

Register 主要會用到 DDRA、PORTA,控制方法如下:

 

DDRA:

DDRA 為 8 bit的 Register,其對應到 PA0 到 PA7,當DDRA當中的 Bit 設為 1 時該接腳為 Output ,設為 0 時該接腳為 Input

PORTA:

PORTA 為 8 bit的 Register,其對應到 PA0 到 PA7,當該接腳 DDRA Register 設為 1 (該接腳為 Output),寫入 1 時,該接腳為 High ,寫入0 時該接腳為 Low

程式:

#include <avr/io.h>

int main(void){

  DDRA=1<<PA0; //將 PA0 設為 Output

  PORTA = 0<<PA0;//將 PA0 設為 Low (輸出高電位),此行程式碼可以省略

  while(1);

  return 0;

}

 

arrow
arrow
    全站熱搜

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