DEV Community

SUNG CHUL
SUNG CHUL

Posted on

Pattern Shift

#include <avr/io.h>
#define F_CPU 16000000L
#include <util/delay.h>
#include <avr/interrupt.h>

volatile int button_state = 0;

ISR(INT0_vect)
{
    button_state = (button_state + 1) % 2;
}

char up_pattern(char pattern)
{
    return((pattern << 1) | (pattern >> 7));
}

char down_pattern(char pattern)
{
    return((pattern >> 1) | (pattern << 7));
}

void INIT_INT0(void)
{
    EIMSK |= 1 << INT0;
    EICRA |= 1 << ISC01;
    sei();
}

int main(void)
{
    DDRB = 0xFF;
    DDRD = 0x00;                   

    INIT_INT0();

    char pattern = 0x01;
    PORTB = pattern;

    while (1) 
    {
        if(button_state == 1) 
            pattern = up_pattern(pattern);

        else 
            pattern = down_pattern(pattern);


        PORTB = pattern;
        _delay_ms(150);
    }
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)