microcontrolling/main.c

113 lines
2.5 KiB
C
Raw Normal View History

2023-08-25 07:33:25 +00:00
#define __AVR_ATmega2560__
#define F_CPU 16000000UL
2023-08-26 17:08:09 +00:00
#define DISPLAY PORTF
2023-08-25 07:33:25 +00:00
#include <avr/io.h>
#include <util/delay.h>
2023-08-26 17:08:09 +00:00
#include <stdbool.h>
2023-08-29 08:58:36 +00:00
#include <avr/interrupt.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-08-29 16:52:13 +00:00
int timer_start = 0xffff - (F_CPU / 1024 / 1000);
volatile uint32_t ticks = 0;
2023-08-29 17:11:48 +00:00
volatile bool counting = false;
2023-08-29 18:18:30 +00:00
volatile bool beeping = false;
2023-08-29 08:58:36 +00:00
void start_timer() {
// Sets the Clock Select. See table on page 157 the Atmel datasheet.
2023-08-29 16:52:13 +00:00
TCCR1B |= _BV(CS10) | _BV(CS12); // clk/1024
2023-08-29 08:58:36 +00:00
// Set the TOIE (Timer Overflow Interrupt Enable) bit
// on TIMSK1 (Timer 1 Interrupt Mask Register).
TIMSK1 |= _BV(TOIE1);
// Sets the current timer value
2023-08-29 16:52:13 +00:00
TCNT1 = timer_start;
2023-08-29 08:58:36 +00:00
// Enable interrupts
sei();
}
2023-08-25 07:33:25 +00:00
void main() {
2023-08-29 08:58:36 +00:00
start_timer();
2023-08-27 19:01:23 +00:00
prog_7segment_4digit();
}
2023-08-29 08:58:36 +00:00
// ISR is used to handle interrupts. TIMER1_OVF_vect
// is triggered whenever timer 1 (16 bit) overflows.
ISR(TIMER1_OVF_vect) {
2023-08-29 16:52:13 +00:00
TCNT1 = timer_start; // TODO figure out how to get the correct number
2023-08-29 17:11:48 +00:00
if (counting) ticks += 1;
2023-08-29 18:18:30 +00:00
if (bit_is_set(PIND, PIND0)) {
PORTD &= 0b11111110;
} else if (beeping) {
PORTD |= 0b00000001;
}
2023-08-29 08:58:36 +00:00
}
2023-08-27 19:01:23 +00:00
void prog_7segment() {
2023-08-26 15:31:29 +00:00
DDRF = 0b11111111;
2023-08-26 17:08:09 +00:00
DDRK = 0b00000000;
PORTK = 0b00000001; // enable pull-up resistor
2023-08-26 15:31:29 +00:00
2023-08-26 17:08:09 +00:00
uint8_t val = 0;
set_display(&PORTF, val, false);
2023-08-25 07:33:25 +00:00
while(1) {
2023-08-26 17:08:09 +00:00
loop_until_bit_is_clear(PINK, PINK0);
if (val >= 9) {
val = 0xff; // will overflow to 0 when incremented
}
set_display(&PORTF, ++val, true);
// debounce
_delay_ms(200);
loop_until_bit_is_set(PINK, PINK0);
set_display(&PORTF, val, false);
2023-08-25 07:33:25 +00:00
}
}
2023-08-27 19:01:23 +00:00
2023-08-29 18:18:30 +00:00
void beep() {
beeping = true;
_delay_ms(50);
beeping = false;
}
2023-08-27 19:01:23 +00:00
void prog_7segment_4digit() {
2023-08-29 07:53:00 +00:00
DDRF = 0b11111111;
DDRK = 0b00001111;
2023-08-29 18:18:30 +00:00
DDRD = 0b00000001;
2023-08-29 17:11:48 +00:00
PORTK = 0b11000000; // set pull-up
2023-08-27 19:01:23 +00:00
float num = 0.0f;
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-08-29 16:52:13 +00:00
show_float(ticks / 1000.0f);
2023-08-29 07:53:00 +00:00
float sleep_time = update_display();
2023-08-29 17:11:48 +00:00
if (!pressed_pause && bit_is_clear(PINK, PINK7)) {
2023-08-29 18:18:30 +00:00
beep();
2023-08-29 17:11:48 +00:00
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-08-29 17:11:48 +00:00
if (bit_is_clear(PINK, PINK6)) {
2023-08-29 18:18:30 +00:00
if (!pressed_clear) beep(50);
2023-08-29 17:11:48 +00:00
ticks = 0;
counting = false;
2023-08-29 18:18:30 +00:00
pressed_clear = true;
} else pressed_clear = false;
2023-08-27 19:01:23 +00:00
}
}