put display refreshing into an interrupt

This commit is contained in:
Lea 2023-08-29 20:36:01 +02:00
parent 8b302d2ae6
commit 1884d06102
Signed by: Lea
GPG key ID: 1BAFFE8347019C42
4 changed files with 26 additions and 7 deletions

View file

@ -12,7 +12,7 @@
"AVR.device.type": "atmega2560", "AVR.device.type": "atmega2560",
"AVR.device.frequency": 16000000, "AVR.device.frequency": 16000000,
"AVR.programmer.type": "wiring", "AVR.programmer.type": "wiring",
"AVR.programmer.port": "/dev/ttyACM0", "AVR.programmer.port": "/dev/ttyACM1",
"AVR.programmer.rate": 115200, "AVR.programmer.rate": 115200,
"AVR.programmer.arguments": [ "AVR.programmer.arguments": [
"-D" "-D"

View file

@ -6,6 +6,7 @@
#include "7segment.h" #include "7segment.h"
uint8_t display[4] = { 0, 0, 0, 0 }; uint8_t display[4] = { 0, 0, 0, 0 };
uint8_t current_digit = 0;
/** /**
* Display pins should be mapped like this: * Display pins should be mapped like this:
@ -28,8 +29,17 @@ void set_display_value(uint8_t digit, uint8_t data) {
display[digit] = 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 // Returns the amount of microseconds we blocked for
float update_display() { float update_display_all_sync() {
int sleep_time = 2000; int sleep_time = 2000;
for (int i = 0; i < 4; i++) { for (int i = 0; i < 4; i++) {
display_value(i, display[i]); display_value(i, display[i]);

View file

@ -4,6 +4,10 @@ void display_value(uint8_t digit, uint8_t data);
void set_display_value(uint8_t digit, uint8_t data); void set_display_value(uint8_t digit, uint8_t data);
float update_display(); void update_next_digit();
void update_display_digit(int digit);
float update_display_all_sync();
void show_float(float value); void show_float(float value);

13
main.c
View file

@ -10,7 +10,7 @@
#include "lib/7segment.h" #include "lib/7segment.h"
#include "lib/7segment_4digits.h" #include "lib/7segment_4digits.h"
int timer_start = 0xffff - (F_CPU / 1024 / 1000); int tick_timer_start = 0xffff - (F_CPU / 1024 / 1000); // 1ms
volatile uint32_t ticks = 0; volatile uint32_t ticks = 0;
volatile bool counting = false; volatile bool counting = false;
volatile bool beeping = false; volatile bool beeping = false;
@ -18,13 +18,15 @@ volatile bool beeping = false;
void start_timer() { void start_timer() {
// Sets the Clock Select. See table on page 157 the Atmel datasheet. // Sets the Clock Select. See table on page 157 the Atmel datasheet.
TCCR1B |= _BV(CS10) | _BV(CS12); // clk/1024 TCCR1B |= _BV(CS10) | _BV(CS12); // clk/1024
TCCR2B |= _BV(CS22); // clk/64
// Set the TOIE (Timer Overflow Interrupt Enable) bit // Set the TOIE (Timer Overflow Interrupt Enable) bit
// on TIMSK1 (Timer 1 Interrupt Mask Register). // on TIMSK1 (Timer 1 Interrupt Mask Register).
TIMSK1 |= _BV(TOIE1); TIMSK1 |= _BV(TOIE1);
TIMSK2 |= _BV(TOIE2);
// Sets the current timer value // Sets the current timer value
TCNT1 = timer_start; TCNT1 = tick_timer_start;
// Enable interrupts // Enable interrupts
sei(); sei();
@ -38,7 +40,7 @@ void main() {
// ISR is used to handle interrupts. TIMER1_OVF_vect // ISR is used to handle interrupts. TIMER1_OVF_vect
// is triggered whenever timer 1 (16 bit) overflows. // is triggered whenever timer 1 (16 bit) overflows.
ISR(TIMER1_OVF_vect) { ISR(TIMER1_OVF_vect) {
TCNT1 = timer_start; // TODO figure out how to get the correct number TCNT1 = tick_timer_start;
if (counting) ticks += 1; if (counting) ticks += 1;
if (bit_is_set(PIND, PIND0)) { if (bit_is_set(PIND, PIND0)) {
@ -48,6 +50,10 @@ ISR(TIMER1_OVF_vect) {
} }
} }
ISR(TIMER2_OVF_vect) {
update_next_digit();
}
void prog_7segment() { void prog_7segment() {
DDRF = 0b11111111; DDRF = 0b11111111;
DDRK = 0b00000000; DDRK = 0b00000000;
@ -92,7 +98,6 @@ void prog_7segment_4digit() {
while(1) { while(1) {
show_float(ticks / 1000.0f); show_float(ticks / 1000.0f);
float sleep_time = update_display();
if (!pressed_pause && bit_is_clear(PINK, PINK7)) { if (!pressed_pause && bit_is_clear(PINK, PINK7)) {
beep(); beep();