diff --git a/lib/7segment.c b/lib/7segment.c new file mode 100644 index 0000000..4914908 --- /dev/null +++ b/lib/7segment.c @@ -0,0 +1,34 @@ +#include +#include + +/** + * Display pins should be mapped like this: + * a b c d e f g DP + * 0 1 2 3 4 5 6 7 + * + * TODO pass register as parameter +*/ + +void set_display_raw(volatile uint8_t * header, uint8_t data) { + *header = data; +} + +void set_display(volatile uint8_t * header, uint8_t value, bool period) { + uint8_t byte = period << 7; + + switch(value) { + case 0: byte |= 0x3f; break; + case 1: byte |= 0x06; break; + case 2: byte |= 0x5b; break; + case 3: byte |= 0x4f; break; + case 4: byte |= 0x66; break; + case 5: byte |= 0x6d; break; + case 6: byte |= 0x7d; break; + case 7: byte |= 0x07; break; + case 8: byte |= 0x7f; break; + case 9: byte |= 0x6f; break; + default: byte &= 0x10000000; break; + } + + set_display_raw(header, byte); +} diff --git a/lib/7segment.h b/lib/7segment.h new file mode 100644 index 0000000..b7f65c2 --- /dev/null +++ b/lib/7segment.h @@ -0,0 +1,5 @@ +#include + +void set_display_raw(volatile uint8_t * header, uint8_t data); + +void set_display(volatile uint8_t * header, uint8_t value, bool period); diff --git a/main.c b/main.c index 543ac52..f36dc20 100644 --- a/main.c +++ b/main.c @@ -1,8 +1,11 @@ #define __AVR_ATmega2560__ #define F_CPU 16000000UL +#define DISPLAY PORTF #include #include +#include +#include "lib/7segment.h" void main() { // DDRB = 0b10000000; @@ -15,12 +18,26 @@ void main() { // } DDRF = 0b11111111; - PORTF = 0b11111111; + DDRK = 0b00000000; + PORTK = 0b00000001; // enable pull-up resistor - uint8_t a = 0; + uint8_t val = 0; + + set_display(&PORTF, val, false); while(1) { - PORTF = a++; - _delay_ms(10); + loop_until_bit_is_clear(PINK, PINK0); + + if (val >= 9) { + val = 0xff; // will overflow to 0 when incremented + } + + set_display(&PORTF, ++val, true); + + // debounce + _delay_ms(200); + + loop_until_bit_is_set(PINK, PINK0); + set_display(&PORTF, val, false); } }