쓰잘데기 없는 나의 블로그

버튼으로 LED 깜빡임 제어

#include <avr/interrupt.h>


void DelayUs (unsigned char time_us) /* time delay for 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 = 8 cycle = 1 us for 8MHz
    }
}



void Delay_ms (unsigned int time_ms)  /* time delay for ms */
{
    register unsigned int i;

    for(i = 0; i < time_ms; i++)
    {
        DelayUs(250);
        DelayUs(250);
        DelayUs(250);
        DelayUs(250);
    }
}


ISR(TIMER1_COMPA_vect)
{
    PORTB = PORTB ^ 0x01;
}


void INT_initialize(){

    MCUCR = 0x0F;  // 모두 상승엣지

    GICR = 0xC0; // INT 0, INT1 을 사용

    GIFR = 0xC0; // clear interrupt flag : 11xx xxxx

    sei(); // global interrupt enable

}



//-----------------------------------------------------------------------------
// INT0 버튼 인터럽트
//-----------------------------------------------------------------------------
ISR(INT0_vect) /* INT0 interrupt function */
{
    if(OCR1A>2500) // 2500 밑으로는 더 이상 내려가지 않게 함 (스펙상 최소치 0)
    OCR1A -= 2500;

    loop: while(!(PIND & 0x40));
    Delay_ms(50);
    if(!(PIND & 0x40)) goto loop;
    //-------------------------------------------------------------------------
    // 인터럽트 클리어
    //-------------------------------------------------------------------------
    GIFR |= 0x40;
    
}
//-----------------------------------------------------------------------------
// INT1 버튼 인터럽트
//-----------------------------------------------------------------------------


ISR(INT1_vect) /* INT5 interrupt function */
{
    if(OCR1A<62500) // 62500 위로는 더 이상 올라기지 않게 함 (스펙상 최대치 65535)
    OCR1A += 2500;

    loop: while(!(PIND & 0x80));//loop: while(!(PINE & 0x20));
    Delay_ms(50);
    if(!(PIND & 0x80)) goto loop;
    GIFR |= 0x80;    
}


int main(void)
{

    DDRD = 0x00;
    DDRB = 0x01;//  0000 0001 -> PORTB의 0번핀을 출력용
    INT_initialize();
    
    TCCR1A = 0x00;
    TCCR1B = 0x0C;
    OCR1A = 32500;//

    TCNT1 = 0x0000;

    TIMSK = 0x10;
    TIFR = 0x3C;


    for(;;);

}