쓰잘데기 없는 나의 블로그

AVR 스위치 제어 소스 코드

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

void Delay_us(unsigned char time_us) 
{ register unsigned char i;

  for(i = 0; i < time_us; i++)   // 4 cycle +
    { asm volatile(" PUSH  R0 ");  // 2 cycle +
      asm volatile(" POP   R0 ");  // 2 cycle +
      //asm volatile(" PUSH  R0 ");  // 2 cycle +
      //asm volatile(" POP   R0 ");  // 2 cycle +
      //asm volatile(" PUSH  R0 ");  // 2 cycle +
      //asm volatile(" POP   R0 ");  // 2 cycle = 16 cycle = 1 us for 16MHz
    }
}

void Delay_ms(unsigned int time_ms) 
{ register unsigned int i;

  for(i = 0; i < time_ms; i++)
    {
  Delay_us(125);
      //Delay_us(250);
      //Delay_us(250);
      //Delay_us(250);
    }
}

int main(void){
 DDRB = 0x01;
 DDRC = 0x00;

unsigned char key;
 for(;;)
        {
                // 0,1번만 사용하고 나머지는 마스크처리
                // 클릭하면 '0'이 입력된다.
                key = PINC & 0x03;
                switch(key)
                {
                        // 1번 버튼 클릭
                        case 0x01:
                                //outp(0xff, PORTD);  // PORTD D0 ON
        PORTB = 0xFF;
                                Delay_ms(500);
                                //outp(0x00, PORTD);  // PORTD ALL OFF
        PORTB = 0x00;
                                Delay_ms(500);
                                break;

                        // 0번 버튼 클릭
                        case 0x02:
                                //outp(0xff, PORTD);  // PORTD D0 ON
        PORTB = 0xFF;
                                Delay_ms(500);
                                //outp(0x00, PORTD);  // PORTD ALL OFF
        PORTB = 0x00;
                                Delay_ms(500);
                                break;
                        default :
                                //outp(0xff, PORTD);  // PORTD D0 ON
        PORTB = 0xFF;
                                Delay_ms(2000);
                                //outp(0x00, PORTD);  // PORTD ALL OFF
        PORTB = 0x00;
                                Delay_ms(2000);
                }
        }
}