90 lines
2 KiB
C
90 lines
2 KiB
C
#define __AVR_ATmega2560__
|
|
#define F_CPU 16000000UL
|
|
#define DISPLAY PORTF
|
|
|
|
#include <avr/io.h>
|
|
#include <util/delay.h>
|
|
#include <stdbool.h>
|
|
#include <avr/interrupt.h>
|
|
#include "lib/util.h"
|
|
#include "lib/7segment.h"
|
|
#include "lib/7segment_4digits.h"
|
|
|
|
int tick_timer_start = 0xffff - (F_CPU / 1024 / 1000); // 1ms
|
|
volatile uint32_t ticks = 0;
|
|
volatile bool counting = false;
|
|
volatile bool beeping = false;
|
|
|
|
void start_timer() {
|
|
// Sets the Clock Select. See table on page 157 the Atmel datasheet.
|
|
TCCR1B |= _BV(CS10) | _BV(CS12); // clk/1024
|
|
TCCR2B |= _BV(CS22); // clk/64
|
|
|
|
// 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 (bit_is_set(PIND, PIND0)) {
|
|
PORTD &= 0b11111110;
|
|
} else if (beeping) {
|
|
PORTD |= 0b00000001;
|
|
}
|
|
}
|
|
|
|
ISR(TIMER2_OVF_vect) {
|
|
update_next_digit();
|
|
}
|
|
|
|
void beep() {
|
|
beeping = true;
|
|
_delay_ms(50);
|
|
beeping = false;
|
|
}
|
|
|
|
void main() {
|
|
DDRF = 0b11111111;
|
|
DDRK = 0b00001111;
|
|
DDRD = 0b00000001;
|
|
PORTK = 0b11000000; // set pull-up
|
|
|
|
start_timer();
|
|
|
|
float num = 0.0f;
|
|
bool pressed_pause = false;
|
|
bool pressed_clear = false;
|
|
|
|
while(1) {
|
|
show_float(ticks / 1000.0f);
|
|
|
|
if (!pressed_pause && bit_is_clear(PINK, PINK7)) {
|
|
counting = !counting;
|
|
pressed_pause = true;
|
|
beep();
|
|
} else if (pressed_pause && bit_is_set(PINK, PINK7)) {
|
|
pressed_pause = false;
|
|
}
|
|
|
|
if (bit_is_clear(PINK, PINK6)) {
|
|
ticks = 0;
|
|
counting = false;
|
|
|
|
if (!pressed_clear) beep(50);
|
|
pressed_clear = true;
|
|
} else pressed_clear = false;
|
|
}
|
|
}
|