Compare commits

...

2 commits

Author SHA1 Message Date
Lea 7e5a1470aa
fancy 2023-11-03 09:37:39 +01:00
Lea 1d2187c8d9
code 2023-10-20 10:58:29 +02:00
7 changed files with 4 additions and 211 deletions

View file

@ -1,40 +0,0 @@
#include <avr/io.h>
#include <stdbool.h>
/**
* Display pins should be mapped like this:
* a b c d e f g DP
* 0 1 2 3 4 5 6 7
*
* d1 d2 d3 d4
* 0 1 2 3
*/
void set_display_raw(volatile uint8_t * header, uint8_t data) {
*header = data;
}
uint8_t digit_to_binary(bool period, uint8_t digit) {
uint8_t val = period << 7;
switch(digit) {
case 0: return val | 0x3f;
case 1: return val | 0x06;
case 2: return val | 0x5b;
case 3: return val | 0x4f;
case 4: return val | 0x66;
case 5: return val | 0x6d;
case 6: return val | 0x7d;
case 7: return val | 0x07;
case 8: return val | 0x7f;
case 9: return val | 0x6f;
// by default draw a dash
default: return 0b01000000;
}
}
void set_display(volatile uint8_t * header, uint8_t value, bool period) {
uint8_t byte = digit_to_binary(period, value);
set_display_raw(header, byte);
}

View file

@ -1,7 +0,0 @@
#include <avr/io.h>
void set_display_raw(volatile uint8_t * header, uint8_t data);
void set_display(volatile uint8_t * header, uint8_t value, bool period);
uint8_t digit_to_binary(bool period, uint8_t digit);

View file

@ -1,59 +0,0 @@
#include <avr/io.h>
#include <stdbool.h>
#include <util/delay.h>
#include <math.h>
#include "util.h"
#include "7segment.h"
uint8_t display[4] = { 0, 0, 0, 0 };
uint8_t current_digit = 0;
void display_value(uint8_t digit, uint8_t data) {
// The left part sets the last 4 bits high (turns the
// according digit off) except for the one we want to address.
// The right part makes sure the first 4 bits remain unchanged.
PORTK = (0b00001111 & ~(1 << digit)) | (PORTK & 0b11110000);
PORTF = data;
}
void set_display_value(uint8_t digit, uint8_t data) {
display[digit] = data;
}
void update_next_digit() {
update_display_digit(current_digit++);
if (current_digit >= 4) current_digit = 0;
}
void update_display_digit(int digit) {
display_value(digit, display[digit]);
}
// Returns the amount of microseconds we blocked for
float update_display_all_sync() {
int sleep_time = 2000;
for (int i = 0; i < 4; i++) {
display_value(i, display[i]);
_delay_us(sleep_time);
}
return sleep_time * 4;
}
void show_float(float value) {
if (value >= 10000 || value < 0) {
for (int i = 0; i < 4; i++) {
set_display_value(i, 0b01000000); // minus character
}
return;
}
int digits = num_digits((int)value);
int full_number = value * (int)roundf(powf(10, 4 - digits));
for (int i = 0; i < 4; i++) {
bool show_period = digits == i + 1;
int digit = get_digit_at_position(full_number, 3 - i);
set_display_value(i, digit_to_binary(show_period, digit));
}
}

View file

@ -1,13 +0,0 @@
#include <avr/io.h>
void display_value(uint8_t digit, uint8_t data);
void set_display_value(uint8_t digit, uint8_t data);
void update_next_digit();
void update_display_digit(int digit);
float update_display_all_sync();
void show_float(float value);

View file

@ -1,31 +0,0 @@
int num_digits(int num) {
if (num < 0) num = -num;
if (num >= 10) return num_digits(num / 10) + 1;
return 1;
}
int get_digit_at_position(int number, int index) {
if (index < 0) {
return -1;
}
int divisor = 1;
int tempNumber = number;
while (tempNumber >= 10) {
tempNumber /= 10;
divisor *= 10;
}
if (index >= divisor) {
return -1;
}
for (int i = 0; i < index; i++) {
number /= 10;
}
int digit = number % 10;
return digit;
}

View file

@ -1,3 +0,0 @@
int num_digits(int num);
int get_digit_at_position(int number, int index);

62
main.c
View file

@ -1,29 +1,19 @@
#define __AVR_ATmega2560__ #define __AVR_ATmega2560__
#define F_CPU 16000000UL #define F_CPU 16000000UL
#define TICK_BEEP false
#include <avr/io.h> #include <avr/io.h>
#include <util/delay.h> #include <util/delay.h>
#include <stdbool.h> #include <stdbool.h>
#include <avr/interrupt.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 int tick_timer_start = 0xffff - (F_CPU / 1024); // 1 second
volatile uint32_t ticks = 0;
volatile beep_time = 0;
volatile bool counting = false;
void start_timer() { void start_timer() {
// Sets the Clock Select. See table on page 157 the Atmel datasheet.
TCCR1B |= _BV(CS10) | _BV(CS12); // clk/1024 TCCR1B |= _BV(CS10) | _BV(CS12); // clk/1024
TCCR2B |= _BV(CS22); // clk/64
// Set the TOIE (Timer Overflow Interrupt Enable) bit // Set the TOIE (Timer Overflow Interrupt Enable) bit
// on TIMSK1 (Timer 1 Interrupt Mask Register). // on TIMSK1 (Timer 1 Interrupt Mask Register).
TIMSK1 |= _BV(TOIE1); TIMSK1 |= _BV(TOIE1);
TIMSK2 |= _BV(TOIE2);
// Sets the current timer value // Sets the current timer value
TCNT1 = tick_timer_start; TCNT1 = tick_timer_start;
@ -36,56 +26,12 @@ void start_timer() {
// is triggered whenever timer 1 (16 bit) overflows. // is triggered whenever timer 1 (16 bit) overflows.
ISR(TIMER1_OVF_vect) { ISR(TIMER1_OVF_vect) {
TCNT1 = tick_timer_start; TCNT1 = tick_timer_start;
if (counting) { PORTB ^= 0b10000000;
ticks += 1;
if (TICK_BEEP && ticks % 1000 == 0 && !beep_time) beep_time = 10;
}
if (bit_is_set(PIND, PIND0)) {
PORTD &= 0b11111110;
} else if (beep_time > 0) {
PORTD |= 0b00000001;
beep_time--;
}
}
ISR(TIMER2_OVF_vect) {
update_next_digit();
}
void beep() {
beep_time = 50;
} }
void main() { void main() {
DDRF = 0b11111111; DDRB = 0b10000000;
DDRK = 0b00001111;
DDRD = 0b00000001;
PORTK = 0b11000000; // set pull-up
start_timer(); start_timer();
while(1);
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();
pressed_clear = true;
} else pressed_clear = false;
}
} }