4 digit 7 segment jumpscare

This commit is contained in:
Lea 2023-08-27 21:01:23 +02:00
parent 5e36740992
commit 479d0f6df0
Signed by: Lea
GPG key ID: 1BAFFE8347019C42
7 changed files with 133 additions and 16 deletions

View file

@ -6,29 +6,33 @@
* a b c d e f g DP * a b c d e f g DP
* 0 1 2 3 4 5 6 7 * 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) { void set_display_raw(volatile uint8_t * header, uint8_t data) {
*header = data; *header = data;
} }
void set_display(volatile uint8_t * header, uint8_t value, bool period) { uint8_t digit_to_binary(uint8_t input, uint8_t digit) {
uint8_t byte = period << 7; switch(digit) {
case 0: return input | 0x3f;
switch(value) { case 1: return input | 0x06;
case 0: byte |= 0x3f; break; case 2: return input | 0x5b;
case 1: byte |= 0x06; break; case 3: return input | 0x4f;
case 2: byte |= 0x5b; break; case 4: return input | 0x66;
case 3: byte |= 0x4f; break; case 5: return input | 0x6d;
case 4: byte |= 0x66; break; case 6: return input | 0x7d;
case 5: byte |= 0x6d; break; case 7: return input | 0x07;
case 6: byte |= 0x7d; break; case 8: return input | 0x7f;
case 7: byte |= 0x07; break; case 9: return input | 0x6f;
case 8: byte |= 0x7f; break; // by default only draw the period if it was provided in the input
case 9: byte |= 0x6f; break; default: return input & (uint8_t) 0x10000000;
default: byte &= 0x10000000; break;
} }
}
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); set_display_raw(header, byte);
} }

View file

@ -3,3 +3,5 @@
void set_display_raw(volatile uint8_t * header, uint8_t data); void set_display_raw(volatile uint8_t * header, uint8_t data);
void set_display(volatile uint8_t * header, uint8_t value, bool period); void set_display(volatile uint8_t * header, uint8_t value, bool period);
uint8_t digit_to_binary(uint8_t in, uint8_t digit);

47
lib/7segment_4digits.c Normal file
View file

@ -0,0 +1,47 @@
#include <avr/io.h>
#include <stdbool.h>
#include <util/delay.h>
#include <math.h>
#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));
}
}

9
lib/7segment_4digits.h Normal file
View file

@ -0,0 +1,9 @@
#include <avr/io.h>
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);

31
lib/util.c Normal file
View file

@ -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;
}

3
lib/util.h Normal file
View file

@ -0,0 +1,3 @@
int num_digits(int num);
int get_digit_at_position(int number, int index);

21
main.c
View file

@ -5,9 +5,15 @@
#include <avr/io.h> #include <avr/io.h>
#include <util/delay.h> #include <util/delay.h>
#include <stdbool.h> #include <stdbool.h>
#include "lib/util.h"
#include "lib/7segment.h" #include "lib/7segment.h"
#include "lib/7segment_4digits.h"
void main() { void main() {
prog_7segment_4digit();
}
void prog_7segment() {
DDRF = 0b11111111; DDRF = 0b11111111;
DDRK = 0b00000000; DDRK = 0b00000000;
PORTK = 0b00000001; // enable pull-up resistor PORTK = 0b00000001; // enable pull-up resistor
@ -32,3 +38,18 @@ void main() {
set_display(&PORTF, val, false); 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;
}
}