diff --git a/Makefile b/Makefile index 0707a16..9b26a76 100644 --- a/Makefile +++ b/Makefile @@ -15,18 +15,18 @@ LDFLAGS += -Wl,--gc-sections LDFLAGS += -Wl,-u,vfprintf -lprintf_flt MAIN_OBJS += main.o -MAIN_OBJS += contr.o -MAIN_OBJS += relay.o -MAIN_OBJS += timer.o MAIN_OBJS += adc.o -MAIN_OBJS += eeprom.o -MAIN_OBJS += i2c.o +MAIN_OBJS += button.o +MAIN_OBJS += contr.o MAIN_OBJS += disp.o +MAIN_OBJS += eeprom.o MAIN_OBJS += fifo.o -MAIN_OBJS += uart.o -MAIN_OBJS += tool.o +MAIN_OBJS += i2c.o +MAIN_OBJS += relay.o MAIN_OBJS += temp.o - +MAIN_OBJS += timer.o +MAIN_OBJS += tool.o +MAIN_OBJS += uart.o MAIN_ELF = main.elf MAIN_HEX = main.hex @@ -54,7 +54,7 @@ TTY_SPEED = 115200 BACKUP = $(MAIN_HEX).bak upload: $(MAIN_HEX) - $(AVRDUDE) -c arduino -p ATMEGA328P -P $(TTY_PORT) -b $(TTY_SPEED) -U flash:w:$< + $(AVRDUDE) -qq -c arduino -p ATMEGA328P -P $(TTY_PORT) -b $(TTY_SPEED) -U flash:w:$< download: $(AVRDUDE) -F -V -c arduino -p m328p -P $(TTY_PORT) -b $(TTY_SPEED) -U flash:r:$(BACKUP):i diff --git a/adc.c b/adc.c index 6bf7a06..fc6ccd5 100644 --- a/adc.c +++ b/adc.c @@ -1,6 +1,6 @@ /* - * Copyright 2017 Oleg Borodin + * Copyright 2017-2024 Oleg Borodin */ #include diff --git a/contr.c b/contr.c index 078a3e6..be3385c 100644 --- a/contr.c +++ b/contr.c @@ -1,5 +1,5 @@ /* - * Copyright 2017 Oleg Borodin + * Copyright 2017-2024 Oleg Borodin */ #include @@ -19,7 +19,7 @@ #include #include #include - +#include #include @@ -29,105 +29,83 @@ #define CONTR_BAND_40M 0x04 #define CONTR_BAND_80M 0x05 -contr_t contr; +typedef struct { + uint8_t band; + bool button_was_pressed; + uint16_t button_time_counter; + uint16_t button_time_untap; + uint16_t button_tap_counter; + bool button_strokes_ended; +} contr_t; -void contr_key_init(void); -void contr_key_handle(void); -void contr_key_reset(void); +contr_t contr; void contr_switch_band(void); void contr_set_band(uint8_t band); - -void contr_vcc_measure(void); -float contr_vcc_calc(void); - -void contr_fwd_measure(void); -float contr_fwd_calc(void); - -void contr_rev_measure(void); -float contr_rev_calc(void); - void contr_write_band(void); void contr_read_band(void); - ISR(TIMER0_OVF_vect) { - contr_key_handle(); - contr_fwd_measure(); - contr_rev_measure(); - contr_vcc_measure(); - - volatile uint8_t xchar; - while ((xchar = fifo_getc(&uart_outbuf)) > 0) { - while (!REG_BIT_ISUP(UCSR0A, UDRE0)); - UDR0 = xchar; - } + button_handle(); + uart_handle(); } - void contr_init(void) { - contr.temp = 0.0F; contr.band = CONTR_BAND_OFF; - - contr.vcc_pos = 0; - for (size_t i = 0; i < VCC_SIZE; i++) { - contr.vcc[i] = 0; - } - contr.fwd_pos = 0; - for (size_t i = 0; i < FWD_SIZE; i++) { - contr.fwd[i] = 0; - } - contr.rev_pos = 0; - for (size_t i = 0; i < REV_SIZE; i++) { - contr.rev[i] = 0; - } - contr.key_was_pressed = false; - contr.key_time_counter = 0; - contr.key_tap_counter = 0; - contr.key_strokes_ended = false; } - void contr_setup(void) { contr_read_band(); } - void contr_switch_band(void) { + switch (contr.band) { + case CONTR_BAND_10M: + contr_set_band(CONTR_BAND_20M); + break; + case CONTR_BAND_20M: + contr_set_band(CONTR_BAND_40M); + break; + case CONTR_BAND_40M: + contr_set_band(CONTR_BAND_80M); + break; + case CONTR_BAND_80M: + contr_set_band(CONTR_BAND_10M); + break; + default: + break; + } +} + +void contr_set_band(uint8_t band) { relaytx_off(); - _delay_ms(100); + contr.band = band; + relay80m_off(); relay40m_off(); relay20m_off(); relay10m_off(); - _delay_ms(100); switch (contr.band) { case CONTR_BAND_10M: - relay20m_on(); - contr.band = CONTR_BAND_20M; + relay10m_on(); break; case CONTR_BAND_20M: - relay40m_on(); - contr.band = CONTR_BAND_40M; + relay20m_on(); break; case CONTR_BAND_40M: - relay80m_on(); - contr.band = CONTR_BAND_80M; + relay40m_on(); break; case CONTR_BAND_80M: - relay10m_on(); - contr.band = CONTR_BAND_10M; + relay80m_on(); break; default: break; } contr_write_band(); - _delay_ms(100); atten_on(); - _delay_ms(100); } -char* contr_get_bandname(uint8_t band) { +char* contr_get_bandname() { char* bandstr = NULL; switch (contr.band) { case CONTR_BAND_10M: @@ -149,165 +127,48 @@ char* contr_get_bandname(uint8_t band) { return bandstr; } -#define CONTR_KEY_SWBAND 2 +#define CONTR_KEY_SWBAND 3 #define CONTR_KEY_BUZZER 4 #define CONTR_KEY_FUN 5 -void contr_key_eval(void) { - if (contr.key_strokes_ended) { - switch (contr.key_tap_counter) { - case CONTR_KEY_SWBAND: - contr_key_reset(); - contr_switch_band(); - break; - case CONTR_KEY_BUZZER: - contr_key_reset(); - buzzer_onoff(); - break; - case CONTR_KEY_FUN: - contr_key_reset(); - fan_onoff(); - break; - default: - contr_key_reset(); - break; - } +void contr_button_eval(void) { + switch (button_get()) { + case CONTR_KEY_SWBAND: + contr_switch_band(); + break; + case CONTR_KEY_BUZZER: + buzzer_onoff(); + break; + case CONTR_KEY_FUN: + fan_onoff(); + break; } } void contr_show_logo(void) { + disp_clear(); char* dispstr = "Made by R2FDX"; disp_string(1, 1, dispstr); + _delay_ms(200); + disp_clear(); } -void contr_temp_measure(void) { - contr.temp = ds18b20_get_temp(); -} - -float contr_temp_calc(void) { - return contr.temp; +void contr_show_band(void) { + disp_string(0, 12, contr_get_bandname(contr.band)); } void contr_main(void) { - uint16_t counter = 0; - char dispstr[17] = { '\0' }; - char* bandstr = NULL; - - disp_clear(); contr_show_logo(); - _delay_ms(500); - disp_clear(); - - printf("READY>\n\r# "); + uint16_t counter = 0; while (true) { - contr_key_eval(); - - if (contr.key_tap_counter) { - sprintf(dispstr, "%2d", contr.key_tap_counter); - disp_string(2, 12, dispstr); - } else { - sprintf(dispstr, " "); - disp_string(2, 12, dispstr); - } - - if ((counter % 64) == 1) { - contr_temp_measure(); - } - - if ((counter % 4) == 1) { - - disp_string(0, 12, contr_get_bandname(contr.band)); - - sprintf(dispstr, "%3.2fV", contr_vcc_calc()); - disp_string(0, 0, dispstr); - - float fwd = contr_fwd_calc(); - sprintf(dispstr, "FWD %3.1fW ", fwd); - disp_string(1, 2, dispstr); - - float rev = contr_rev_calc(); - sprintf(dispstr, "REV %3.1fW ", rev); - disp_string(2, 2, dispstr); - - - float vswr = 0.0F; - if (fwd > 0.1F) { - vswr = (fwd + rev) / (fwd - rev); - } - sprintf(dispstr, "%3.1f ", vswr); - disp_string(3, 10, dispstr); - - sprintf(dispstr, "%3.2fC", contr_temp_calc()); - disp_string(3, 0, dispstr); - } + contr_button_eval(); + contr_show_band(); counter++; _delay_ms(100); } } -void contr_vcc_measure(void) { - if (++contr.vcc_pos > VCC_SIZE) { - contr.vcc_pos = 0; - } - contr.vcc[contr.vcc_pos] = adc_read(ACD_CHANELL3); -} - -#define CONTR_VCC_SCALE 2673.0F - -float contr_vcc_calc(void) { - uint32_t vcc = 0; - uint8_t n = 0; - for (size_t i = 0; i < VCC_SIZE; i++) { - if (contr.vcc[i]) { - n++; - vcc += contr.vcc[i]; - } - } - float xvcc = 0.0F; - if (n) { - vcc /= n; - xvcc = (float)vcc / CONTR_VCC_SCALE; - } - return xvcc; -} - -void contr_fwd_measure(void) { - if (++contr.fwd_pos > FWD_SIZE) { - contr.fwd_pos = 0; - } - contr.fwd[contr.fwd_pos] = adc_read(ACD_CHANELL1); -} - -float contr_fwd_calc(void) { - uint32_t fwd = 0; - uint8_t n = 0; - for (size_t i = 0; i < FWD_SIZE; i++) { - fwd += contr.fwd[i]; - } - float xfwd = 0.0F; - xfwd = (float)fwd / 3760.0F / FWD_SIZE; - return xfwd; -} - -void contr_rev_measure(void) { - if (++contr.rev_pos > REV_SIZE) { - contr.rev_pos = 0; - } - contr.rev[contr.rev_pos] = adc_read(ACD_CHANELL0);; -} - -float contr_rev_calc(void) { - uint32_t rev = 0; - for (size_t i = 0; i < REV_SIZE; i++) { - rev += contr.rev[i]; - } - float xrev = 0.0F; - xrev = (float)rev / 164.0F / REV_SIZE; - return xrev; -} - - void contr_write_band(void) { eeprom_write_bytes(0x00, &contr.band, sizeof(contr.band)); } @@ -316,53 +177,3 @@ void contr_read_band(void) { eeprom_read_bytes(0x00, &contr.band, sizeof(contr.band)); } - -#define CONTR_KEY_CNTINPRESS 100 -#define CONTR_KEY_CNTRELEASE 100 -#define CONTR_KEY_CNTUNTAP_READY 1200 -#define CONTR_KEY_CNTUNTAP_CLEAN 4000 - - -void contr_key_handle(void) { - if (!contr.key_was_pressed) { - if (button_is_pressed()) { - contr.key_time_counter++; - contr.key_time_untap++; - } - if (contr.key_time_counter > CONTR_KEY_CNTINPRESS) { - contr.key_time_counter = 0; - contr.key_was_pressed = true; - } - } else { - if (!button_is_pressed()) { - contr.key_time_counter++; - } - if (contr.key_time_counter > CONTR_KEY_CNTRELEASE) { - contr.key_time_counter = 0; - contr.key_was_pressed = false; - contr.key_tap_counter++; - contr.key_time_untap = 0; - } - } - if (contr.key_tap_counter > 0) { - contr.key_time_untap++; - if (contr.key_time_untap > CONTR_KEY_CNTUNTAP_READY) { - contr.key_strokes_ended = true; - } - if (contr.key_time_untap > CONTR_KEY_CNTUNTAP_CLEAN) { - contr.key_was_pressed = false; - contr.key_tap_counter = 0; - contr.key_time_untap = 0; - contr.key_strokes_ended = false; - } - } else { - contr.key_time_untap = 0; - } -} - -void contr_key_reset(void) { - contr.key_was_pressed = false; - contr.key_tap_counter = 0; - contr.key_time_untap = 0; - contr.key_strokes_ended = false; -} diff --git a/contr.h b/contr.h index a420305..1eb93a2 100644 --- a/contr.h +++ b/contr.h @@ -3,28 +3,6 @@ #include -#define VCC_SIZE 12 -#define FWD_SIZE 92 -#define REV_SIZE 92 - -typedef struct { - float temp; - uint8_t band; - bool key_was_pressed; - uint16_t key_time_counter; - uint16_t key_time_untap; - uint16_t key_tap_counter; - bool key_strokes_ended; - - uint16_t vcc[VCC_SIZE]; - size_t vcc_pos; - uint16_t fwd[FWD_SIZE]; - size_t fwd_pos; - uint16_t rev[REV_SIZE]; - size_t rev_pos; -} contr_t; - -extern contr_t contr; void contr_init(void); void contr_setup(void); diff --git a/fifo.c b/fifo.c index 6ee8405..e24bc16 100644 --- a/fifo.c +++ b/fifo.c @@ -1,6 +1,5 @@ /* - * Copyright 2017 Oleg Borodin - * + * Copyright 2017-2024 Oleg Borodin */ #include #include diff --git a/fifo.h b/fifo.h index c75fcde..b4fb489 100644 --- a/fifo.h +++ b/fifo.h @@ -1,5 +1,5 @@ /* - * Copyright 2017 Oleg Borodin + * Copyright 2017-2024 Oleg Borodin * */ #ifndef UART_H_IUI diff --git a/main.c b/main.c index c9891b9..415a014 100644 --- a/main.c +++ b/main.c @@ -10,6 +10,7 @@ #include #include +#include #include #include #include diff --git a/relay.c b/relay.c index da96d19..7208563 100644 --- a/relay.c +++ b/relay.c @@ -11,19 +11,6 @@ #include -void button_init(void) { - /* D3 PD3: Set key input */ - REG_SETDOWN_BIT(DDRD, PD3); - REG_SETUP_BIT(PORTD, PD3); -} - -bool button_is_pressed(void) { - if (REG_BIT_VALUE(PIND, PD3)) { - return true; - } - return false; -} - /* D4 PD4: Set input relay 5 output */ void relaytx_init(void) { REG_SETUP_BIT(DDRD, PD4); diff --git a/relay.h b/relay.h index 4e42c78..85f5a78 100644 --- a/relay.h +++ b/relay.h @@ -2,8 +2,6 @@ #ifndef CONTR_RELAY_H_QWERTY #define CONTR_RELAY_H_QWERTY -void button_init(void); -bool button_is_pressed(void); void relaytx_init(void); void relaytx_on(void); diff --git a/timer.c b/timer.c index 95659a8..687007c 100644 --- a/timer.c +++ b/timer.c @@ -1,6 +1,6 @@ /* - * Copyright 2017 Oleg Borodin + * Copyright 2017-2024 Oleg Borodin */ #include diff --git a/tool.c b/tool.c index 0396843..16677c2 100644 --- a/tool.c +++ b/tool.c @@ -1,5 +1,5 @@ /* - * Copyright 2017 Oleg Borodin + * Copyright 2017-2024 Oleg Borodin */ #include #include diff --git a/tool.h b/tool.h index 6916838..3c66565 100644 --- a/tool.h +++ b/tool.h @@ -1,5 +1,5 @@ /* - * Copyright 2017 Oleg Borodin + * Copyright 2017-2024 Oleg Borodin * */ #ifndef TOOLS_H_QWERTY diff --git a/uart.c b/uart.c index ab7cb49..0a9680a 100644 --- a/uart.c +++ b/uart.c @@ -1,5 +1,5 @@ /* - * Copyright 2017 Oleg Borodin + * Copyright 2017-2024 Oleg Borodin * */ #include @@ -60,3 +60,11 @@ void uart_init(void) { REG_SETDOWN_BIT(UCSR0B, UDRIE0); _delay_ms(20); } + +void uart_handle(void) { + volatile uint8_t xchar; + while ((xchar = fifo_getc(&uart_outbuf)) > 0) { + while (!REG_BIT_ISUP(UCSR0A, UDRE0)); + UDR0 = xchar; + } +} diff --git a/uart.h b/uart.h index 5d85a81..94fe00e 100644 --- a/uart.h +++ b/uart.h @@ -1,5 +1,5 @@ /* - * Copyright 2017 Oleg Borodin + * Copyright 2017-2024 Oleg Borodin * */ #ifndef UART_H_QWERTY @@ -13,6 +13,7 @@ extern FILE uart_stream; void uartio_init(void); void uart_init(void); +void uart_handle(void); int uart_putchar(char c, FILE * stream); int uart_getchar(FILE * stream);