Skip to content

Instantly share code, notes, and snippets.

@ilsubyeega
Created April 18, 2024 06:20
Show Gist options
  • Save ilsubyeega/9845f979b629722a6e3221a167219b8b to your computer and use it in GitHub Desktop.
Save ilsubyeega/9845f979b629722a6e3221a167219b8b to your computer and use it in GitHub Desktop.
#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];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment