From 479d0f6df0ad16585a5552a0b8ec938a7383ed4a Mon Sep 17 00:00:00 2001 From: Lea Date: Sun, 27 Aug 2023 21:01:23 +0200 Subject: [PATCH] 4 digit 7 segment jumpscare --- lib/7segment.c | 36 ++++++++++++++++++-------------- lib/7segment.h | 2 ++ lib/7segment_4digits.c | 47 ++++++++++++++++++++++++++++++++++++++++++ lib/7segment_4digits.h | 9 ++++++++ lib/util.c | 31 ++++++++++++++++++++++++++++ lib/util.h | 3 +++ main.c | 21 +++++++++++++++++++ 7 files changed, 133 insertions(+), 16 deletions(-) create mode 100644 lib/7segment_4digits.c create mode 100644 lib/7segment_4digits.h create mode 100644 lib/util.c create mode 100644 lib/util.h diff --git a/lib/7segment.c b/lib/7segment.c index 4914908..1e99749 100644 --- a/lib/7segment.c +++ b/lib/7segment.c @@ -6,29 +6,33 @@ * a b c d e f g DP * 0 1 2 3 4 5 6 7 * - * TODO pass register as parameter + * d1 d2 d3 d4 + * 1 2 3 4 */ 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; +uint8_t digit_to_binary(uint8_t input, uint8_t digit) { + switch(digit) { + case 0: return input | 0x3f; + case 1: return input | 0x06; + case 2: return input | 0x5b; + case 3: return input | 0x4f; + case 4: return input | 0x66; + case 5: return input | 0x6d; + case 6: return input | 0x7d; + case 7: return input | 0x07; + case 8: return input | 0x7f; + case 9: return input | 0x6f; + // by default only draw the period if it was provided in the input + default: return input & (uint8_t) 0x10000000; } +} + +void set_display(volatile uint8_t * header, uint8_t value, bool period) { + uint8_t byte = digit_to_binary(period << 7, value); set_display_raw(header, byte); } diff --git a/lib/7segment.h b/lib/7segment.h index b7f65c2..7a816ec 100644 --- a/lib/7segment.h +++ b/lib/7segment.h @@ -3,3 +3,5 @@ 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(uint8_t in, uint8_t digit); diff --git a/lib/7segment_4digits.c b/lib/7segment_4digits.c new file mode 100644 index 0000000..e4d24ed --- /dev/null +++ b/lib/7segment_4digits.c @@ -0,0 +1,47 @@ +#include +#include +#include +#include +#include "util.h" +#include "7segment.h" + +uint8_t display[4] = { 0, 0, 0, 0 }; + +/** + * Display pins should be mapped like this: + * a b c d e f g DP + * 0 1 2 3 4 5 6 7 + * + * +*/ + +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_display() { + for (int i = 0; i < 4; i++) { + display_value(i, display[i]); + _delay_us(100); + } +} + +void show_float(float value) { + 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 << 7, digit)); + } +} diff --git a/lib/7segment_4digits.h b/lib/7segment_4digits.h new file mode 100644 index 0000000..ae54034 --- /dev/null +++ b/lib/7segment_4digits.h @@ -0,0 +1,9 @@ +#include + +void display_value(uint8_t digit, uint8_t data); + +void set_display_value(uint8_t digit, uint8_t data); + +void update_display(); + +void show_float(float value); diff --git a/lib/util.c b/lib/util.c new file mode 100644 index 0000000..6bcd929 --- /dev/null +++ b/lib/util.c @@ -0,0 +1,31 @@ +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 new file mode 100644 index 0000000..f0a7c25 --- /dev/null +++ b/lib/util.h @@ -0,0 +1,3 @@ +int num_digits(int num); + +int get_digit_at_position(int number, int index); diff --git a/main.c b/main.c index bcede98..51e7fb0 100644 --- a/main.c +++ b/main.c @@ -5,9 +5,15 @@ #include #include #include +#include "lib/util.h" #include "lib/7segment.h" +#include "lib/7segment_4digits.h" void main() { + prog_7segment_4digit(); +} + +void prog_7segment() { DDRF = 0b11111111; DDRK = 0b00000000; PORTK = 0b00000001; // enable pull-up resistor @@ -32,3 +38,18 @@ void main() { set_display(&PORTF, val, false); } } + +void prog_7segment_4digit() { + DDRF = 0b11111111; + DDRK = 0b00001111; + + float num = 0.0f; + + while(1) { + show_float(num); + update_display(); + + num += 0.001f; + if (num >= 10000) num = 0.0f; + } +}