Compare commits

...

5 commits

Author SHA1 Message Date
Lea cf04307ee5
oopsie daisy 2023-09-19 08:02:31 +02:00
Lea 9e74811115
remove unnecessary definition 2023-09-08 10:51:18 +02:00
Lea d304b2bc93
add comment for pinout 2023-09-08 10:49:26 +02:00
Lea 7ac3c0db18
remove unnecessary register write 2023-09-08 10:46:57 +02:00
Lea ccb18fd098
simplify code 2023-09-08 10:46:07 +02:00
5 changed files with 38 additions and 64 deletions

View file

@ -29,7 +29,7 @@ uint8_t digit_to_binary(bool period, uint8_t digit) {
case 8: return val | 0x7f; case 8: return val | 0x7f;
case 9: return val | 0x6f; case 9: return val | 0x6f;
// by default draw a dash // by default draw a dash
default: return 0x01000000; default: return 0b01000000;
} }
} }

View file

@ -30,14 +30,14 @@ void update_display_digit(int digit) {
display_value(digit, display[digit]); display_value(digit, display[digit]);
} }
// Returns the amount of microseconds we blocked for // Returns the amount of milliseconds we blocked for
float update_display_all_sync() { uint32_t update_display_all_sync() {
int sleep_time = 2000; uint32_t sleep_time_ms = 1;
for (int i = 0; i < 4; i++) { for (int i = 0; i < 4; i++) {
display_value(i, display[i]); update_display_digit(i);
_delay_us(sleep_time); _delay_ms(sleep_time_ms);
} }
return sleep_time * 4; return sleep_time_ms * 4;
} }
void show_float(float value) { void show_float(float value) {

View file

@ -8,6 +8,6 @@ void update_next_digit();
void update_display_digit(int digit); void update_display_digit(int digit);
float update_display_all_sync(); uint32_t update_display_all_sync();
void show_float(float value); void show_float(float value);

View file

@ -4,6 +4,7 @@ int num_digits(int num) {
return 1; return 1;
} }
// chatgpt generated
int get_digit_at_position(int number, int index) { int get_digit_at_position(int number, int index) {
if (index < 0) { if (index < 0) {
return -1; return -1;

85
main.c
View file

@ -1,90 +1,63 @@
#define __AVR_ATmega2560__ #define __AVR_ATmega2560__
#define F_CPU 16000000UL #define F_CPU 16000000UL
#define TICK_BEEP false
#include <avr/io.h> #include <avr/io.h>
#include <util/delay.h> #include <util/delay.h>
#include <stdbool.h> #include <stdbool.h>
#include <avr/interrupt.h>
#include "lib/util.h" #include "lib/util.h"
#include "lib/7segment.h" #include "lib/7segment.h"
#include "lib/7segment_4digits.h" #include "lib/7segment_4digits.h"
int tick_timer_start = 0xffff - (F_CPU / 1024 / 1000); // 1ms /**
volatile uint32_t ticks = 0; * O O O O O O O O
volatile beep_time = 0; * DDRF x x x x x x x x
volatile bool counting = false; * \-\-\-\-\-\-\-\--- 7 segment output
*
void start_timer() { * I I O O O O O O
// Sets the Clock Select. See table on page 157 the Atmel datasheet. * DDRK x x x x x x x x
TCCR1B |= _BV(CS10) | _BV(CS12); // clk/1024 * | | \-\-\-\--- digit selection output
TCCR2B |= _BV(CS22); // clk/64 * \-\--------------- Button input
*/
// 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;
// Enable interrupts
sei();
}
// ISR is used to handle interrupts. TIMER1_OVF_vect
// 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;
}
void main() { void main() {
DDRF = 0b11111111; DDRF = 0b11111111;
DDRK = 0b00001111; DDRK = 0b00001111;
DDRD = 0b00000001;
PORTK = 0b11000000; // set pull-up PORTK = 0b11000000; // set pull-up
start_timer(); uint32_t millis = 0;
bool counting = false;
float num = 0.0f;
bool pressed_pause = false; bool pressed_pause = false;
bool pressed_clear = false; bool pressed_clear = false;
while(1) { while(1) {
show_float(ticks / 1000.0f); // show_float() doesn't actually print to the display directly, it only updates
// a variable stored in memory. We call `update_display_all_sync()` periodically,
// which reads the stored variable and outputs it.
show_float(millis / 1000.0f);
// Cycles through all 4 displays, turns them on for a set amount of time
// while displaying the value set by `show_float()` earlier and returns
// the total amount waited.
uint32_t sleep_time = update_display_all_sync();
if (counting) {
// We can use the returned sleep_time to (inaccurately) count time.
millis += sleep_time;
}
// If the button connected to PIN7 is pressed, pause or unpause the counter.
if (!pressed_pause && bit_is_clear(PINK, PINK7)) { if (!pressed_pause && bit_is_clear(PINK, PINK7)) {
counting = !counting; counting = !counting;
pressed_pause = true; pressed_pause = true;
beep();
} else if (pressed_pause && bit_is_set(PINK, PINK7)) { } else if (pressed_pause && bit_is_set(PINK, PINK7)) {
pressed_pause = false; pressed_pause = false;
} }
// If the button connected to PIN6 is pressed, reset and pause the counter.
if (bit_is_clear(PINK, PINK6)) { if (bit_is_clear(PINK, PINK6)) {
ticks = 0; millis = 0;
counting = false; counting = false;
if (!pressed_clear) beep();
pressed_clear = true; pressed_clear = true;
} else pressed_clear = false; } else pressed_clear = false;
} }