Compare commits

...

5 commits

Author SHA1 Message Date
Lea 0f7800d7e4
bleh 2024-01-07 22:28:24 +01:00
Lea 2ffe83f951
gbngfdbjn 2023-11-24 10:08:01 +01:00
Lea ec293656e2
dfgerdg 2023-11-17 09:36:44 +01:00
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 67 additions and 206 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);

122
main.c
View file

@ -1,29 +1,62 @@
#define __AVR_ATmega2560__
#define F_CPU 16000000UL
#define TICK_BEEP false
#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 beep_time = 0;
volatile bool counting = false;
volatile uint16_t tick_timer_start = 0xffff - (F_CPU / 1024 / 1000);
volatile bool speaker_disabled = true;
void start_timer() {
// Sets the Clock Select. See table on page 157 the Atmel datasheet.
uint16_t notes[][3] = {
{ 1000, 200, 30 },
{ 1200, 200, 30 },
{ 1500, 200, 30 },
{ 1600, 200, 30 },
{ 1500, 200, 30 },
{ 1200, 100, 10 },
{ 1000, 300, 100 },
};
void ADC_Setup() {
// AVCC with external capacitor at AREF pin
ADMUX = _BV(REFS0);
// Enable ADC, set prescaler to 8tick_timer_start
ADCSRA = _BV(ADEN) | _BV(ADPS0) | _BV(ADPS1);
// Perform dummy readout
ADCSRA |= _BV(ADSC); // Start conversion
while (ADCSRA & _BV(ADSC)); // Await result
(void) ADCW; // Read out result
}
uint16_t ADC_Read(uint8_t channel) {
// Select chosen channel
ADMUX = (ADMUX & ~(0x1F)) | (channel & 0x1F);
// Start conversion
ADCSRA |= _BV(ADSC);
// Await the result
while (ADCSRA & _BV(ADSC));
// Although it doesn't appear to be documented, ADCW holds the covnersion result
return ADCW;
}
uint16_t ADC_Read_Avg(uint8_t channel, uint8_t samples) {
uint32_t sum = 0;
for (int i = 0; i < samples; i++) {
sum += ADC_Read(channel);
}
return (uint16_t)(sum / samples);
}
void TIMER_Setup() {
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;
@ -36,56 +69,37 @@ void start_timer() {
// is triggered whenever timer 1 (16 bit) overflows.
ISR(TIMER1_OVF_vect) {
TCNT1 = tick_timer_start;
if (counting) {
ticks += 1;
if (TICK_BEEP && ticks % 1000 == 0 && !beep_time) beep_time = 10;
PORTB ^= 0b10000000;
if (speaker_disabled) PORTK &= 0b11111110;
else PORTK ^= 0b00000001;
}
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 play_note(uint16_t freq, uint8_t delay, uint8_t delay_silence) {
speaker_disabled = false;
tick_timer_start = 0xffff - (F_CPU / freq / 1000);
for (int i = 0; i < delay; i++) _delay_ms(1);
speaker_disabled = true;
for (int i = 0; i < delay_silence; i++) _delay_ms(1);
}
void main() {
DDRF = 0b11111111;
DDRK = 0b00001111;
DDRD = 0b00000001;
PORTK = 0b11000000; // set pull-up
DDRK = 0b00000001;
PORTB = 0b10000000; // Start with LED enabled
start_timer();
ADC_Setup();
TIMER_Setup();
float num = 0.0f;
bool pressed_pause = false;
bool pressed_clear = false;
//long rows = sizeof(notes) / sizeof(notes[0]);
while(1) {
show_float(ticks / 1000.0f);
while (true) {
//for (int i = 0; i < rows; i++) {
// play_note(notes[i][0], notes[i][1], notes[i][2]);
//}
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;
uint16_t value = ADC_Read_Avg(0, 4);
speaker_disabled = value == 0;
if (value < 100) value = 100; // Anything lower than this causes issues
tick_timer_start = 0xffff - (F_CPU / value / 1000);
}
}