microcontrolling/main.c

92 lines
2.1 KiB
C
Raw Normal View History

2023-08-25 07:33:25 +00:00
#define __AVR_ATmega2560__
#define F_CPU 16000000UL
#define TICK_BEEP false
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
int tick_timer_start = 0xffff - (F_CPU / 1024 / 1000); // 1ms
2023-08-29 16:52:13 +00:00
volatile uint32_t ticks = 0;
2023-08-29 18:54:39 +00:00
volatile beep_time = 0;
2023-08-29 17:11:48 +00:00
volatile bool counting = 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
TCCR2B |= _BV(CS22); // clk/64
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);
TIMSK2 |= _BV(TOIE2);
2023-08-29 08:58:36 +00:00
// Sets the current timer value
TCNT1 = tick_timer_start;
2023-08-29 08:58:36 +00:00
// 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;
2023-08-30 14:08:44 +00:00
if (counting) {
ticks += 1;
if (TICK_BEEP && ticks % 1000 == 0 && !beep_time) beep_time = 10;
}
2023-08-29 18:18:30 +00:00
if (bit_is_set(PIND, PIND0)) {
PORTD &= 0b11111110;
2023-08-29 18:54:39 +00:00
} else if (beep_time > 0) {
2023-08-29 18:18:30 +00:00
PORTD |= 0b00000001;
2023-08-29 18:54:39 +00:00
beep_time--;
2023-08-29 18:18:30 +00:00
}
2023-08-29 08:58:36 +00:00
}
ISR(TIMER2_OVF_vect) {
update_next_digit();
}
2023-08-29 18:18:30 +00:00
void beep() {
2023-08-29 18:54:39 +00:00
beep_time = 50;
2023-08-29 18:18:30 +00:00
}
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 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
2023-08-29 18:49:46 +00:00
start_timer();
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
2023-08-29 17:11:48 +00:00
if (!pressed_pause && bit_is_clear(PINK, PINK7)) {
counting = !counting;
pressed_pause = true;
2023-08-29 18:40:50 +00:00
beep();
2023-08-29 17:11:48 +00:00
} 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)) {
ticks = 0;
counting = false;
2023-08-29 18:40:50 +00:00
2023-08-29 18:54:39 +00:00
if (!pressed_clear) beep();
2023-08-29 18:18:30 +00:00
pressed_clear = true;
} else pressed_clear = false;
2023-08-27 19:01:23 +00:00
}
}