microcontrolling/main.c

65 lines
1.9 KiB
C
Raw Normal View History

2023-08-25 07:33:25 +00:00
#define __AVR_ATmega2560__
#define F_CPU 16000000UL
#include <avr/io.h>
#include <util/delay.h>
2023-08-26 17:08:09 +00:00
#include <stdbool.h>
2023-08-27 19:01:23 +00:00
#include "lib/util.h"
2023-08-26 17:08:09 +00:00
#include "lib/7segment.h"
2023-08-27 19:01:23 +00:00
#include "lib/7segment_4digits.h"
2023-08-25 07:33:25 +00:00
2023-09-08 08:49:26 +00:00
/**
* O O O O O O O O
* DDRF x x x x x x x x
* \-\-\-\-\-\-\-\--- 7 segment output
*
* I I O O O O O O
* DDRK x x x x x x x x
* | | \-\-\-\--- digit selection output
* \-\--------------- Button input
*/
2023-08-29 18:49:46 +00:00
void main() {
2023-08-29 07:53:00 +00:00
DDRF = 0b11111111;
DDRK = 0b00001111;
2023-08-29 17:11:48 +00:00
PORTK = 0b11000000; // set pull-up
2023-08-27 19:01:23 +00:00
2023-09-08 08:46:07 +00:00
uint32_t millis = 0;
bool counting = false;
2023-08-29 17:11:48 +00:00
bool pressed_pause = false;
2023-08-29 18:18:30 +00:00
bool pressed_clear = false;
2023-08-27 19:01:23 +00:00
while(1) {
2023-09-08 08:46:07 +00:00
// 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;
}
2023-08-29 07:53:00 +00:00
2023-09-08 08:46:07 +00:00
// If the button connected to PIN7 is pressed, pause or unpause the counter.
2023-08-29 17:11:48 +00:00
if (!pressed_pause && bit_is_clear(PINK, PINK7)) {
counting = !counting;
pressed_pause = true;
} else if (pressed_pause && bit_is_set(PINK, PINK7)) {
pressed_pause = false;
2023-08-29 07:53:00 +00:00
}
2023-08-27 19:01:23 +00:00
2023-09-08 08:46:07 +00:00
// If the button connected to PIN6 is pressed, reset and pause the counter.
2023-08-29 17:11:48 +00:00
if (bit_is_clear(PINK, PINK6)) {
2023-09-08 08:46:07 +00:00
millis = 0;
2023-08-29 17:11:48 +00:00
counting = false;
2023-08-29 18:40:50 +00:00
2023-08-29 18:18:30 +00:00
pressed_clear = true;
} else pressed_clear = false;
2023-08-27 19:01:23 +00:00
}
}