Skip to content

Instantly share code, notes, and snippets.

@ilsubyeega
Created April 18, 2024 06:20
Show Gist options
  • Select an option

  • Save ilsubyeega/9845f979b629722a6e3221a167219b8b to your computer and use it in GitHub Desktop.

Select an option

Save ilsubyeega/9845f979b629722a6e3221a167219b8b to your computer and use it in GitHub Desktop.

Revisions

  1. ilsubyeega created this gist Apr 18, 2024.
    44 changes: 44 additions & 0 deletions led4_scrolling_interupt.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,44 @@
    #include <xc.h>

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

    int count = 0;
    volatile int direction = 1;
    // 1 for going right side, -1 for going left side.
    int masks[] = { 0b11111000, 0b11110100, 0b11110010, 0b11110001 };

    char next_iteration();

    ISR(INT4_vect) { direction = 1; }
    ISR(INT5_vect) { direction = -1; }

    int main(void)
    {
    DDRD = 0x0f;
    DDRE = 0x00;

    PORTD = next_iteration();
    PORTE = 0xff;

    // enable global interrupt
    SREG |= 0b10000000; // <-- required
    EIMSK = 0b00110000; // <-- enable specific port. pin 4, 5
    EICRB = 0b000001010; // <-- active interrupt when low voltage is detected.

    while(1)
    {
    PORTD = next_iteration();
    _delay_ms(100);
    }
    }

    char next_iteration() {
    count += direction;

    if (count > 3) count = 0;
    if (count < 0) count = 3;

    return masks[count];
    }