본문 바로가기
IoT/아트멜 스튜디오와 아두이노로 배우는 ATmega328 프로그래밍

LED & button

by javapp 자바앱 2020. 5. 24.
728x90

1. LED 출력

#define F_CPU 16000000L

#include <avr/io.h>

#include <util/delay.h>

 

int main(void)

{

           DDRB = 0x0F; //0000 1111  출력할 곳 초기화

           DDRD = 0xF0; //1111 0000

 

          

           /* Replace with your application code */

           while (1)

           {

                      char p1; char p2;

                      for(int i = 0; i <8; i++){

                                 if(i >=4){

                                              p1 = 0x01 << (i-4);

                                              p2 = 0x80 >> (i-4);

                                 }else{

                                              p1 = 0x08 >> i;

                                              p2 = 0x10 << i;

                                 }

                                

                                

                                 PORTB = p1;  //LED 출력

                                 PORTD = p2;

                                

                                

                                 _delay_ms(1000);

                      }

           }

          

           return 1;

}

 

 


버튼 내장 풀업 저항 

 

#define F_CPU 16000000L

#include <util/delay.h>

#include <avr/io.h>

#include <avr/sfr_defs.h>

#include "UART.h"

 

 

 

void INIT_PORT(void)

{

           DDRB = 0x20;    // PB5 (디지털 13번 핀) 출력으로 설정

           PORTB = 0x00;   // LED는 꺼진 상태에서 시작

           DDRD = 0x00;    // 버튼 입력

           PORTD = 0x04;   // PD2 (디지털 2번 핀) 풀업 저항 사용

}

unsigned char button_pressed(void){

           if(bit_is_clear(PIND, PIND2)){

                      _delay_ms(10);

                      if(bit_is_clear(PIND, PIND2))

                                 return 1;

           }

           return 0;

}

int main(void)

{

           UART_INIT();      // UART 통신 초기화

           INIT_PORT();      // 포트 설정

           int state = 0;      // 현재 버튼의 상태

           while(1){

                      if(button_pressed()){ //디바운싱 적용
            

                                 if(state == 0) UART_transmit('*');

                                 state = 1;

                                 PORTB = 0x20; //출력

                      }

                      else{

                                 state = 0;

                                 PORTB = 0x00;

                      }

           }

}

 


버튼과 LED

 

#define F_CPU 16000000L

#include <util/delay.h>

#include <avr/io.h>

#include <avr/sfr_defs.h>

#include "UART.h"

 

//입력은 D11~ D8

//출력은 D7~D4

//1 간격으로 각각 입력핀에 연결된 버튼 On상태 확인 대응

//D11 PB3 (0000 1000)-> D7

void INIT_PORT(void)

{

       DDRB = 0x00;   // 버튼 입력

       PORTB = 0x0F; // (D11~ D8) 입력전 1인상태로 설정 0000 1111

       DDRD = 0xF0;   // LED 출력 초기화 , (D7~D4) 풀업 저항 사용

       PORTD = 0x00; // 

}

unsigned char button_pressed(void){

       if(bit_is_clear(PINB, PINB3)){ //디바운싱 적용, 버튼 2회 연속 검사하여 동일한 값인 경우를 비교

               _delay_ms(10);

               if(bit_is_clear(PINB, PINB3)) //눌려짐 확인

               return 4;

       }

       if(bit_is_clear(PINB, PINB2)){

               _delay_ms(10);

               if(bit_is_clear(PINB, PINB2))

               return 3;

       }

       if(bit_is_clear(PINB, PINB1)){

               _delay_ms(10);

               if(bit_is_clear(PINB, PINB1))

               return 2;

       }

       if(bit_is_clear(PINB, PINB0)){

               _delay_ms(10);

               if(bit_is_clear(PINB, PINB0))

               return 1;

       }

       return 0;

}

int main(void)

{

       UART_INIT();   // UART 통신 초기화

       INIT_PORT();   // 포트 설정

       //int state4 = 0;      // 현재 버튼의 상태

       //int state3 = 0;      // 현재 버튼의 상태

       //int state2 = 0;      // 현재 버튼의 상태

       //int state1 = 0;      // 현재 버튼의 상태

       while(1){

               _delay_ms(1000); //1

              

               if(button_pressed()== 4){

                      UART_transmit('O');UART_transmit(' ');

                     

                      PORTD = PORTD | 0x80; //세트

                     

               }else{

                      PORTD = PORTD & 0x7F; //클리어

                      UART_transmit('X');UART_transmit(' ');

               }

              

               if(button_pressed() == 3)

               {

                      UART_transmit('O');UART_transmit(' ');

                     

                      PORTD = PORTD | 0x40;

               }

               else{

                      PORTD = PORTD & 0xBF;

                      UART_transmit('X');UART_transmit(' ');

               }

              

               if(button_pressed() == 2){

                      UART_transmit('O');UART_transmit(' ');

                     

                      PORTD = PORTD | 0x20;

               }else{

                      PORTD = PORTD & 0xDF;

                      UART_transmit('X');UART_transmit(' ');

       }

              

               if(button_pressed() == 1){

                      UART_transmit('O');UART_transmit(' ');

                     

                      PORTD = PORTD | 0x10;

               }else{

                      PORTD = PORTD & 0xEF;

                      UART_transmit('X');UART_transmit(' ');

       }

               UART_transmit('\n');

       }

}

댓글