Created
January 21, 2014 13:29
-
-
Save niklasf/8540031 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <avr/io.h> | |
#include <avr/interrupt.h> | |
#include <util/delay.h> | |
void display_internal(uint8_t mask, uint8_t c) | |
{ | |
uint8_t bm = mask == 0x0F ? 0b00000000 : 0b00001000; | |
uint8_t sh = mask == 0x0F ? 0 : 4; | |
if ((c & mask) >> sh == 0x00) { | |
PORTK = 0b00000011 | bm; | |
PORTF = 0b11111111; | |
} | |
else if ((c & mask) >> sh == 0x01) { | |
PORTK = 0b00000000 | bm; | |
PORTF = 0b00000110; | |
} | |
else if ((c & mask) >> sh == 0x02) { | |
PORTK = 0b00000101 | bm; | |
PORTF = 0b11111011; | |
} | |
else if ((c & mask) >> sh == 0x03) { | |
PORTK = 0b00000100 | bm; | |
PORTF = 0b11111111; | |
} | |
else if ((c & mask) >> sh == 0x04) { | |
PORTK = 0b00000110 | bm; | |
PORTF = 0b11110110; | |
} | |
else if ((c & mask) >> sh == 0x05) { | |
PORTK = 0b00000110 | bm; | |
PORTF = 0b11111101; | |
} | |
else if ((c & mask) >> sh == 0x06) { | |
PORTK = 0b00000111 | bm; | |
PORTF = 0b11111101; | |
} | |
else if ((c & mask) >> sh == 0x07) { | |
PORTK = 0b00000000 | bm; | |
PORTF = 0b00000111; | |
} | |
else if ((c & mask) >> sh == 0x08) { | |
PORTK = 0b00000111 | bm; | |
PORTF = 0b11111111; | |
} | |
else if ((c & mask) >> sh == 0x09) { | |
PORTK = 0b00000110 | bm; | |
PORTF = 0b11111111; | |
} | |
else if ((c & mask) >> sh == 0x0A) { | |
PORTK = 0b00000111 | bm; | |
PORTF = 0b11110111; | |
} | |
else if ((c & mask) >> sh == 0x0B) { | |
PORTK = 0b00000111 | bm; | |
PORTF = 0b11111100; | |
} | |
else if ((c & mask) >> sh == 0x0C) { | |
PORTK = 0b00000011 | bm; | |
PORTF = 0b11111001; | |
} | |
else if ((c & mask) >> sh == 0x0D) { | |
PORTK = 0b00000101 | bm; | |
PORTF = 0b11111110; | |
} | |
else if ((c & mask) >> sh == 0x0E) { | |
PORTK = 0b00000111 | bm; | |
PORTF = 0b11111001; | |
} | |
else if ((c & mask) >> sh == 0x0F) { | |
PORTK = 0b00000111 | bm; | |
PORTF = 0b11110001; | |
} | |
} | |
ISR (INT3_vect) { | |
switch (PORTB) { | |
case 0b1101: | |
display_internal(0xF, 0x1); | |
break; | |
case 0b0111: | |
display_internal(0xF, 0x2); | |
break; | |
case 0b1011: | |
display_internal(0xF, 0x3); | |
break; | |
case 0b1110: | |
display_internal(0xF, 0xA); | |
break; | |
} | |
} | |
ISR (INT2_vect) { | |
switch (PORTB) { | |
case 0b1101: | |
display_internal(0xF, 0x4); | |
break; | |
case 0b0111: | |
display_internal(0xF, 0x5); | |
break; | |
case 0b1011: | |
display_internal(0xF, 0x6); | |
break; | |
case 0b1110: | |
display_internal(0xF, 0xB); | |
break; | |
} | |
} | |
ISR (INT1_vect) { | |
switch (PORTB) { | |
case 0b1101: | |
display_internal(0xF, 0x7); | |
break; | |
case 0b0111: | |
display_internal(0xF, 0x8); | |
break; | |
case 0b1011: | |
display_internal(0xF, 0x9); | |
break; | |
case 0b1110: | |
display_internal(0xF, 0xC); | |
break; | |
} | |
} | |
ISR (INT0_vect) { | |
switch (PORTB) { | |
case 0b1101: | |
display_internal(0xF, 0x0); | |
break; | |
case 0b0111: | |
display_internal(0xF, 0xF); | |
break; | |
case 0b1011: | |
display_internal(0xF, 0xE); | |
break; | |
case 0b1110: | |
display_internal(0xF, 0xD); | |
break; | |
} | |
} | |
int main() | |
{ | |
// 7 segment | |
DDRK = 0xFF; | |
DDRF = 0xFF; | |
// init keyboard | |
DDRB = 0xFF; | |
DDRD = 0x00; | |
// keyboard interrupts | |
cli(); | |
EIMSK |= (1 << INT0) | (1 << INT1) | (1 << INT2) | (1 << INT3); | |
EICRA |= (1 << ISC01) | (1 << ISC11) | (1 << ISC21) | (1 << ISC31); | |
sei(); | |
// sweep and display | |
while (1) { | |
PORTB = 0b0111; | |
for (int i = 0; i < 5000; i++); | |
PORTB = 0b1011; | |
for (int i = 0; i < 5000; i++); | |
PORTB = 0b1101; | |
for (int i = 0; i < 5000; i++); | |
PORTB = 0b1110; | |
for (int i = 0; i < 5000; i++); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment