diff --git a/lib/7segment.c b/lib/7segment.c deleted file mode 100644 index 4c4b9d3..0000000 --- a/lib/7segment.c +++ /dev/null @@ -1,40 +0,0 @@ -#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 - * - * d1 d2 d3 d4 - * 0 1 2 3 -*/ - -void set_display_raw(volatile uint8_t * header, uint8_t data) { - *header = data; -} - -uint8_t digit_to_binary(bool period, uint8_t digit) { - uint8_t val = period << 7; - - switch(digit) { - case 0: return val | 0x3f; - case 1: return val | 0x06; - case 2: return val | 0x5b; - case 3: return val | 0x4f; - case 4: return val | 0x66; - case 5: return val | 0x6d; - case 6: return val | 0x7d; - case 7: return val | 0x07; - case 8: return val | 0x7f; - case 9: return val | 0x6f; - // by default draw a dash - default: return 0b01000000; - } -} - -void set_display(volatile uint8_t * header, uint8_t value, bool period) { - uint8_t byte = digit_to_binary(period, value); - - set_display_raw(header, byte); -} diff --git a/lib/7segment.h b/lib/7segment.h deleted file mode 100644 index 17a220a..0000000 --- a/lib/7segment.h +++ /dev/null @@ -1,7 +0,0 @@ -#include - -void set_display_raw(volatile uint8_t * header, uint8_t data); - -void set_display(volatile uint8_t * header, uint8_t value, bool period); - -uint8_t digit_to_binary(bool period, uint8_t digit); diff --git a/lib/7segment_4digits.c b/lib/7segment_4digits.c deleted file mode 100644 index f174ab1..0000000 --- a/lib/7segment_4digits.c +++ /dev/null @@ -1,59 +0,0 @@ -#include -#include -#include -#include -#include "util.h" -#include "7segment.h" - -uint8_t display[4] = { 0, 0, 0, 0 }; -uint8_t current_digit = 0; - -void display_value(uint8_t digit, uint8_t data) { - // The left part sets the last 4 bits high (turns the - // according digit off) except for the one we want to address. - // The right part makes sure the first 4 bits remain unchanged. - PORTK = (0b00001111 & ~(1 << digit)) | (PORTK & 0b11110000); - - PORTF = data; -} - -void set_display_value(uint8_t digit, uint8_t data) { - display[digit] = data; -} - -void update_next_digit() { - update_display_digit(current_digit++); - if (current_digit >= 4) current_digit = 0; -} - -void update_display_digit(int digit) { - display_value(digit, display[digit]); -} - -// Returns the amount of microseconds we blocked for -float update_display_all_sync() { - int sleep_time = 2000; - for (int i = 0; i < 4; i++) { - display_value(i, display[i]); - _delay_us(sleep_time); - } - return sleep_time * 4; -} - -void show_float(float value) { - if (value >= 10000 || value < 0) { - for (int i = 0; i < 4; i++) { - set_display_value(i, 0b01000000); // minus character - } - return; - } - - int digits = num_digits((int)value); - int full_number = value * (int)roundf(powf(10, 4 - digits)); - - for (int i = 0; i < 4; i++) { - bool show_period = digits == i + 1; - int digit = get_digit_at_position(full_number, 3 - i); - set_display_value(i, digit_to_binary(show_period, digit)); - } -} diff --git a/lib/7segment_4digits.h b/lib/7segment_4digits.h deleted file mode 100644 index 90e08d6..0000000 --- a/lib/7segment_4digits.h +++ /dev/null @@ -1,13 +0,0 @@ -#include - -void display_value(uint8_t digit, uint8_t data); - -void set_display_value(uint8_t digit, uint8_t data); - -void update_next_digit(); - -void update_display_digit(int digit); - -float update_display_all_sync(); - -void show_float(float value); diff --git a/lib/util.c b/lib/util.c deleted file mode 100644 index 6bcd929..0000000 --- a/lib/util.c +++ /dev/null @@ -1,31 +0,0 @@ -int num_digits(int num) { - if (num < 0) num = -num; - if (num >= 10) return num_digits(num / 10) + 1; - return 1; -} - -int get_digit_at_position(int number, int index) { - if (index < 0) { - return -1; - } - - int divisor = 1; - int tempNumber = number; - while (tempNumber >= 10) { - tempNumber /= 10; - divisor *= 10; - } - - if (index >= divisor) { - return -1; - } - - for (int i = 0; i < index; i++) { - number /= 10; - } - - int digit = number % 10; - - return digit; -} - diff --git a/lib/util.h b/lib/util.h deleted file mode 100644 index f0a7c25..0000000 --- a/lib/util.h +++ /dev/null @@ -1,3 +0,0 @@ -int num_digits(int num); - -int get_digit_at_position(int number, int index); diff --git a/main.c b/main.c index 104406d..f0269d9 100644 --- a/main.c +++ b/main.c @@ -1,29 +1,19 @@ #define __AVR_ATmega2560__ #define F_CPU 16000000UL -#define TICK_BEEP false #include #include #include #include -#include "lib/util.h" -#include "lib/7segment.h" -#include "lib/7segment_4digits.h" -int tick_timer_start = 0xffff - (F_CPU / 1024 / 1000); // 1ms -volatile uint32_t ticks = 0; -volatile beep_time = 0; -volatile bool counting = false; +int tick_timer_start = 0xffff - (F_CPU / 1024); // 1 second void start_timer() { - // Sets the Clock Select. See table on page 157 the Atmel datasheet. TCCR1B |= _BV(CS10) | _BV(CS12); // clk/1024 - TCCR2B |= _BV(CS22); // clk/64 // Set the TOIE (Timer Overflow Interrupt Enable) bit // on TIMSK1 (Timer 1 Interrupt Mask Register). TIMSK1 |= _BV(TOIE1); - TIMSK2 |= _BV(TOIE2); // Sets the current timer value TCNT1 = tick_timer_start; @@ -36,56 +26,13 @@ void start_timer() { // is triggered whenever timer 1 (16 bit) overflows. ISR(TIMER1_OVF_vect) { TCNT1 = tick_timer_start; - if (counting) { - ticks += 1; - if (TICK_BEEP && ticks % 1000 == 0 && !beep_time) beep_time = 10; - } - - if (bit_is_set(PIND, PIND0)) { - PORTD &= 0b11111110; - } else if (beep_time > 0) { - PORTD |= 0b00000001; - beep_time--; - } -} - -ISR(TIMER2_OVF_vect) { - update_next_digit(); -} - -void beep() { - beep_time = 50; + if (PORTB & 0b10000000) PORTB &= 0b01111111; + else PORTB |= 0b10000000; } void main() { - DDRF = 0b11111111; - DDRK = 0b00001111; - DDRD = 0b00000001; - PORTK = 0b11000000; // set pull-up + DDRB = 0b10000000; start_timer(); - - float num = 0.0f; - bool pressed_pause = false; - bool pressed_clear = false; - - while(1) { - show_float(ticks / 1000.0f); - - if (!pressed_pause && bit_is_clear(PINK, PINK7)) { - counting = !counting; - pressed_pause = true; - beep(); - } else if (pressed_pause && bit_is_set(PINK, PINK7)) { - pressed_pause = false; - } - - if (bit_is_clear(PINK, PINK6)) { - ticks = 0; - counting = false; - - if (!pressed_clear) beep(); - pressed_clear = true; - } else pressed_clear = false; - } + while(1); }