96 lines
2.1 KiB
C
96 lines
2.1 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 timer_start = 0xffff - (F_CPU / 1024 / 1000);
|
|
volatile uint32_t ticks = 0;
|
|
|
|
// https://gist.github.com/Wollw/2381139
|
|
void start_timer() {
|
|
// Sets the Clock Select. See table on page 157 the Atmel datasheet.
|
|
TCCR1B |= _BV(CS10) | _BV(CS12); // clk/1024
|
|
|
|
// Set the TOIE (Timer Overflow Interrupt Enable) bit
|
|
// on TIMSK1 (Timer 1 Interrupt Mask Register).
|
|
TIMSK1 |= _BV(TOIE1);
|
|
|
|
// Sets the current timer value
|
|
TCNT1 = timer_start;
|
|
|
|
// Enable interrupts
|
|
sei();
|
|
}
|
|
|
|
void main() {
|
|
start_timer();
|
|
prog_7segment_4digit();
|
|
}
|
|
|
|
// ISR is used to handle interrupts. TIMER1_OVF_vect
|
|
// is triggered whenever timer 1 (16 bit) overflows.
|
|
ISR(TIMER1_OVF_vect) {
|
|
TCNT1 = timer_start; // TODO figure out how to get the correct number
|
|
ticks += 1;
|
|
}
|
|
|
|
void prog_7segment() {
|
|
DDRF = 0b11111111;
|
|
DDRK = 0b00000000;
|
|
PORTK = 0b00000001; // enable pull-up resistor
|
|
|
|
uint8_t val = 0;
|
|
|
|
set_display(&PORTF, val, false);
|
|
|
|
while(1) {
|
|
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);
|
|
}
|
|
}
|
|
|
|
void prog_7segment_4digit() {
|
|
DDRF = 0b11111111;
|
|
DDRK = 0b00001111;
|
|
PORTK = 0b10000000; // set pull-up
|
|
|
|
float num = 0.0f;
|
|
bool running = false;
|
|
bool pressed = false;
|
|
|
|
while(1) {
|
|
show_float(ticks / 1000.0f);
|
|
float sleep_time = update_display();
|
|
|
|
if (!pressed && bit_is_clear(PINK, PINK7)) {
|
|
running = !running;
|
|
pressed = true;
|
|
} else if (pressed && bit_is_set(PINK, PINK7)) {
|
|
pressed = false;
|
|
}
|
|
|
|
if (running) {
|
|
num += sleep_time / 1000.0f / 1000.0f;
|
|
if (num >= 10000) num = 0.0f;
|
|
}
|
|
}
|
|
}
|