48 lines
1.1 KiB
C
48 lines
1.1 KiB
C
|
#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));
|
||
|
}
|
||
|
}
|