diff --git a/Makefile b/Makefile index d6e31ec..2331fcd 100644 --- a/Makefile +++ b/Makefile @@ -15,14 +15,15 @@ LDFLAGS += -Wl,--gc-sections LDFLAGS += -Wl,-u,vfprintf -lprintf_flt MAIN_OBJS += main.o -MAIN_OBJS += contr.o +MAIN_OBJS += contr_main.o +MAIN_OBJS += contr_relay.o +MAIN_OBJS += contr_init.o MAIN_OBJS += eeprom.o MAIN_OBJS += i2c.o MAIN_OBJS += disp.o MAIN_OBJS += fifo.o MAIN_OBJS += uart.o MAIN_OBJS += tool.o -MAIN_OBJS += timer.o MAIN_OBJS += temp.o diff --git a/contr.h b/contr.h deleted file mode 100644 index 86d1d3a..0000000 --- a/contr.h +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef CONTR_H_QWERTY -#define CONTR_H_QWERTY - -void contr_init(void); -void contr_setup(void); -void contr_main(void); - -#endif diff --git a/contr_init.c b/contr_init.c new file mode 100644 index 0000000..12b8c22 --- /dev/null +++ b/contr_init.c @@ -0,0 +1,48 @@ + +/* + * Copyright 2017 Oleg Borodin + */ + +#include +#include + +#include + +void contr_adc_init() { + /* Disable ADC */ + REG_SETDOWN_BIT(ADCSRA, ADEN); + /* Set reference*/ + REG_SETUP_BIT(ADMUX, REFS0); + REG_SETDOWN_BIT(ADMUX, REFS1); + /* Set result type */ + REG_SETUP_BIT(ADMUX, ADLAR); + /* Set freq prescale */ + REG_SETUP_BIT(ADCSRA, ADPS2); + REG_SETUP_BIT(ADCSRA, ADPS1); + REG_SETUP_BIT(ADCSRA, ADPS0); + /* Disable autoconversion */ + REG_SETDOWN_BIT(ADCSRA, ADATE); + /* Disable interrupt */ + REG_SETDOWN_BIT(ADCSRA, ADIE); + /* Enable ADC */ + REG_SETUP_BIT(ADCSRA, ADEN); +} + + +void contr_timer_init(void) { + /* Disable comparators */ + REG_SETDOWN_BIT(TCCR0A, COM0A1); + REG_SETDOWN_BIT(TCCR0A, COM0A0); + REG_SETDOWN_BIT(TCCR0A, COM0B1); + REG_SETDOWN_BIT(TCCR0A, COM0B0); + /* Set normal mode */ + REG_SETDOWN_BIT(TCCR0A, WGM01); + REG_SETDOWN_BIT(TCCR0A, WGM00); + /* Set clock to 1/64 */ + REG_SETDOWN_BIT(TCCR0B, CS02); + REG_SETUP_BIT(TCCR0B, CS01); + REG_SETUP_BIT(TCCR0B, CS00); + /* Enable timer interrupt */ + REG_SETUP_BIT(TIMSK0, TOIE0); + //REG_SETUP(TCNT0, TIMER_INITVAL); +} diff --git a/contr_init.h b/contr_init.h new file mode 100644 index 0000000..deaf913 --- /dev/null +++ b/contr_init.h @@ -0,0 +1,7 @@ +#ifndef CONTR_INIT_H_QWERTY +#define CONTR_INIT_H_QWERTY + +void contr_timer_init(void); +void contr_adc_init(void); + +#endif diff --git a/contr.c b/contr_main.c similarity index 63% rename from contr.c rename to contr_main.c index 6ed44c6..367bfa2 100644 --- a/contr.c +++ b/contr_main.c @@ -15,10 +15,11 @@ #include #include #include -#include #include -#include +#include +#include +#include #define CONTR_BAND_OFF 0x01 #define CONTR_BAND_10M 0x02 @@ -26,27 +27,6 @@ #define CONTR_BAND_40M 0x04 #define CONTR_BAND_80M 0x05 -#define VCC_SIZE 6 -#define FWD_SIZE 64 -#define REV_SIZE 64 - -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; - contr_t contr; void contr_key_init(void); @@ -56,44 +36,7 @@ void contr_key_reset(void); void contr_switch_band(void); void contr_set_band(uint8_t band); -void contr_txrelay_init(void); -void contr_txrelay_on(void); -void contr_txrelay_off(void); -void contr_txrelay_onoff(void); -void contr_relay10m_init(void); -void contr_relay10m_on(void); -void contr_relay10m_off(void); - -void contr_relay20m_init(void); -void contr_relay20m_on(void); -void contr_relay20m_off(void); - -void contr_relay40m_init(void); -void contr_relay40m_on(void); -void contr_relay40m_off(void); - -void contr_relay80m_init(void); -void contr_relay80m_on(void); -void contr_relay80m_off(void); - -void contr_buzzer_init(void); -void contr_buzzer_on(void); -void contr_buzzer_off(void); -void contr_buzzer_onoff(void); - -void contr_fan_init(void); -void contr_fan_on(void); -void contr_fan_off(void); -void contr_fan_onoff(void); -bool contr_fan_is_on(void); - -void contr_att_init(void); -void contr_att_on(void); -void contr_att_off(void); - -void contr_timer_init(void); -void contr_adc_init(void); void contr_vcc_measure(void); float contr_vcc_calc(void); @@ -107,23 +50,6 @@ float contr_rev_calc(void); void contr_write_band(void); void contr_read_band(void); -void contr_timer_init(void) { - /* Disable comparators */ - REG_SETDOWN_BIT(TCCR0A, COM0A1); - REG_SETDOWN_BIT(TCCR0A, COM0A0); - REG_SETDOWN_BIT(TCCR0A, COM0B1); - REG_SETDOWN_BIT(TCCR0A, COM0B0); - /* Set normal mode */ - REG_SETDOWN_BIT(TCCR0A, WGM01); - REG_SETDOWN_BIT(TCCR0A, WGM00); - /* Set clock to 1/64 */ - REG_SETDOWN_BIT(TCCR0B, CS02); - REG_SETUP_BIT(TCCR0B, CS01); - REG_SETUP_BIT(TCCR0B, CS00); - /* Enable timer interrupt */ - REG_SETUP_BIT(TIMSK0, TOIE0); - //REG_SETUP(TCNT0, TIMER_INITVAL); -} ISR(TIMER0_OVF_vect) { contr_key_handle(); @@ -136,7 +62,6 @@ ISR(TIMER0_OVF_vect) { while (!REG_BIT_ISUP(UCSR0A, UDRE0)); UDR0 = xchar; } - REG_SETUP(TCNT0, TIMER_INITVAL); } @@ -275,7 +200,6 @@ void contr_key_eval(void) { } } - void contr_show_logo(void) { char* dispstr = "Made by R2FDX"; disp_string(1, 1, dispstr); @@ -306,13 +230,12 @@ void contr_main(void) { if (contr.key_tap_counter) { sprintf(dispstr, "%2d", contr.key_tap_counter); - disp_string(3, 12, dispstr); + disp_string(2, 12, dispstr); } else { sprintf(dispstr, " "); - disp_string(3, 12, dispstr); + disp_string(2, 12, dispstr); } - if ((counter % 64) == 1) { contr_temp_measure(); } @@ -324,12 +247,22 @@ void contr_main(void) { sprintf(dispstr, "%3.2fV", contr_vcc_calc()); disp_string(0, 0, dispstr); - sprintf(dispstr, "%3.2f ", contr_fwd_calc()); + float fwd = contr_fwd_calc(); + sprintf(dispstr, "FWD %3.1fW ", fwd); disp_string(1, 2, dispstr); - sprintf(dispstr, "%3.2f ", contr_rev_calc()); + 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); } @@ -338,141 +271,6 @@ void contr_main(void) { } } -/* D4 PD4: Set input relay 5 output */ -void contr_txrelay_init(void) { - REG_SETUP_BIT(DDRD, PD4); -} -void contr_txrelay_on(void) { - REG_SETUP_BIT(PORTD, PD4); -} -void contr_txrelay_off(void) { - REG_SETDOWN_BIT(PORTD, PD4); -} -void contr_txrelay_onoff(void) { - if (REG_BIT_VALUE(PIND, PD4)) { - contr_txrelay_off(); - } else { - contr_txrelay_on(); - } -} -/* D6 PD6: Set 10M relay 1 output */ -void contr_relay10m_init(void) { - REG_SETUP_BIT(DDRD, PD6); -} -void contr_relay10m_on(void) { - REG_SETUP_BIT(PORTD, PD6); -} -void contr_relay10m_off(void) { - REG_SETDOWN_BIT(PORTD, PD6); -} - -/* D7 PD7: Set 20M relay 2 output */ -void contr_relay20m_init(void) { - REG_SETUP_BIT(DDRD, PD7); -} -void contr_relay20m_on(void) { - REG_SETUP_BIT(PORTD, PD7); -} -void contr_relay20m_off(void) { - REG_SETDOWN_BIT(PORTD, PD7); -} - -/* D8 PB0: Set 40M relay 3 output */ -void contr_relay40m_init(void) { - REG_SETUP_BIT(DDRB, PB0); -} -void contr_relay40m_on(void) { - REG_SETUP_BIT(PORTB, PB0); -} -void contr_relay40m_off(void) { - REG_SETDOWN_BIT(PORTB, PB0); -} - -/* D9 PB1: Set 80M relay 4 output */ -void contr_relay80m_init(void) { - REG_SETUP_BIT(DDRB, PB1); -} -void contr_relay80m_on(void) { - REG_SETUP_BIT(PORTB, PB1); -} -void contr_relay80m_off(void) { - REG_SETDOWN_BIT(PORTB, PB1); -} - -/* D10 PB2: Set buzzer output */ -void contr_buzzer_init(void) { - REG_SETUP_BIT(DDRB, PB2); -} -void contr_buzzer_on(void) { - REG_SETUP_BIT(PORTB, PB2); -} -void contr_buzzer_off(void) { - REG_SETDOWN_BIT(PORTB, PB2); -} -void contr_buzzer_onoff(void) { - if (REG_BIT_VALUE(PINB, PB2)) { - contr_buzzer_off(); - } else { - contr_buzzer_on(); - } -} - -/* D12 PB4: Set fan output */ -void contr_fan_init(void) { - REG_SETUP_BIT(DDRB, PB4); -} -void contr_fan_on(void) { - REG_SETUP_BIT(PORTB, PB4); -} -void contr_fan_off(void) { - REG_SETDOWN_BIT(PORTB, PB4); -} -void contr_fan_onoff(void) { - if (REG_BIT_VALUE(PINB, PB4)) { - contr_fan_off(); - } else { - contr_fan_on(); - } -} -bool contr_fan_is_on(void) { - if (REG_BIT_VALUE(PINB, PB4)) { - return true; - } - return false; -} - -/* D13 PB5: Set attenuator relay output */ -void contr_att_init(void) { - REG_SETUP_BIT(DDRB, PB5); -} -void contr_att_on(void) { - REG_SETUP_BIT(PORTB, PB5); -} -void contr_att_off(void) { - REG_SETDOWN_BIT(PORTB, PB5); -} - - -void contr_adc_init() { - /* Disable ADC */ - REG_SETDOWN_BIT(ADCSRA, ADEN); - /* Set reference*/ - REG_SETUP_BIT(ADMUX, REFS0); - REG_SETDOWN_BIT(ADMUX, REFS1); - /* Set result type */ - REG_SETUP_BIT(ADMUX, ADLAR); - /* Set freq prescale */ - REG_SETUP_BIT(ADCSRA, ADPS2); - REG_SETUP_BIT(ADCSRA, ADPS1); - REG_SETUP_BIT(ADCSRA, ADPS0); - /* Disable autoconversion */ - REG_SETDOWN_BIT(ADCSRA, ADATE); - /* Disable interrupt */ - REG_SETDOWN_BIT(ADCSRA, ADIE); - /* Enable ADC */ - REG_SETUP_BIT(ADCSRA, ADEN); -} - void contr_vcc_measure(void) { REG_SETUP_BIT(ADMUX, MUX0); REG_SETUP_BIT(ADMUX, MUX1); @@ -512,7 +310,7 @@ float contr_vcc_calc(void) { } void contr_fwd_measure(void) { - REG_SETDOWN_BIT(ADMUX, MUX0); + REG_SETUP_BIT(ADMUX, MUX0); REG_SETDOWN_BIT(ADMUX, MUX1); REG_SETDOWN_BIT(ADMUX, MUX2); REG_SETDOWN_BIT(ADMUX, MUX3); @@ -534,21 +332,15 @@ float contr_fwd_calc(void) { uint32_t fwd = 0; uint8_t n = 0; for (size_t i = 0; i < FWD_SIZE; i++) { - if (contr.fwd[i]) { - n++; - fwd += contr.fwd[i]; - } + fwd += contr.fwd[i]; } float xfwd = 0.0F; - if (n) { - fwd /= n; - xfwd = (float)fwd / 10.0F; - } + xfwd = (float)fwd / 3760.0F / FWD_SIZE; return xfwd; } void contr_rev_measure(void) { - REG_SETUP_BIT(ADMUX, MUX0); + REG_SETDOWN_BIT(ADMUX, MUX0); REG_SETDOWN_BIT(ADMUX, MUX1); REG_SETDOWN_BIT(ADMUX, MUX2); REG_SETDOWN_BIT(ADMUX, MUX3); @@ -568,18 +360,11 @@ void contr_rev_measure(void) { float contr_rev_calc(void) { uint32_t rev = 0; - uint8_t n = 0; for (size_t i = 0; i < REV_SIZE; i++) { - if (contr.rev[i]) { - n++; - rev += contr.rev[i]; - } + rev += contr.rev[i]; } float xrev = 0.0F; - if (n) { - rev /= n; - xrev = (float)rev / 10.0F; - } + xrev = (float)rev / 164.0F / REV_SIZE; return xrev; } diff --git a/contr_main.h b/contr_main.h new file mode 100644 index 0000000..a420305 --- /dev/null +++ b/contr_main.h @@ -0,0 +1,33 @@ +#ifndef CONTR_H_QWERTY +#define CONTR_H_QWERTY + +#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); +void contr_main(void); + +#endif diff --git a/contr_relay.c b/contr_relay.c new file mode 100644 index 0000000..f007208 --- /dev/null +++ b/contr_relay.c @@ -0,0 +1,131 @@ + + +#include +#include +#include +#include +#include + +#include +#include + +#include +//#include +//#include +//#include +//#include + +/* D4 PD4: Set input relay 5 output */ +void contr_txrelay_init(void) { + REG_SETUP_BIT(DDRD, PD4); +} +void contr_txrelay_on(void) { + REG_SETUP_BIT(PORTD, PD4); +} +void contr_txrelay_off(void) { + REG_SETDOWN_BIT(PORTD, PD4); +} +void contr_txrelay_onoff(void) { + if (REG_BIT_VALUE(PIND, PD4)) { + contr_txrelay_off(); + } else { + contr_txrelay_on(); + } +} +/* D6 PD6: Set 10M relay 1 output */ +void contr_relay10m_init(void) { + REG_SETUP_BIT(DDRD, PD6); +} +void contr_relay10m_on(void) { + REG_SETUP_BIT(PORTD, PD6); +} +void contr_relay10m_off(void) { + REG_SETDOWN_BIT(PORTD, PD6); +} + +/* D7 PD7: Set 20M relay 2 output */ +void contr_relay20m_init(void) { + REG_SETUP_BIT(DDRD, PD7); +} +void contr_relay20m_on(void) { + REG_SETUP_BIT(PORTD, PD7); +} +void contr_relay20m_off(void) { + REG_SETDOWN_BIT(PORTD, PD7); +} + +/* D8 PB0: Set 40M relay 3 output */ +void contr_relay40m_init(void) { + REG_SETUP_BIT(DDRB, PB0); +} +void contr_relay40m_on(void) { + REG_SETUP_BIT(PORTB, PB0); +} +void contr_relay40m_off(void) { + REG_SETDOWN_BIT(PORTB, PB0); +} + +/* D9 PB1: Set 80M relay 4 output */ +void contr_relay80m_init(void) { + REG_SETUP_BIT(DDRB, PB1); +} +void contr_relay80m_on(void) { + REG_SETUP_BIT(PORTB, PB1); +} +void contr_relay80m_off(void) { + REG_SETDOWN_BIT(PORTB, PB1); +} + +/* D10 PB2: Set buzzer output */ +void contr_buzzer_init(void) { + REG_SETUP_BIT(DDRB, PB2); +} +void contr_buzzer_on(void) { + REG_SETUP_BIT(PORTB, PB2); +} +void contr_buzzer_off(void) { + REG_SETDOWN_BIT(PORTB, PB2); +} +void contr_buzzer_onoff(void) { + if (REG_BIT_VALUE(PINB, PB2)) { + contr_buzzer_off(); + } else { + contr_buzzer_on(); + } +} + +/* D12 PB4: Set fan output */ +void contr_fan_init(void) { + REG_SETUP_BIT(DDRB, PB4); +} +void contr_fan_on(void) { + REG_SETUP_BIT(PORTB, PB4); +} +void contr_fan_off(void) { + REG_SETDOWN_BIT(PORTB, PB4); +} +void contr_fan_onoff(void) { + if (REG_BIT_VALUE(PINB, PB4)) { + contr_fan_off(); + } else { + contr_fan_on(); + } +} +bool contr_fan_is_on(void) { + if (REG_BIT_VALUE(PINB, PB4)) { + return true; + } + return false; +} + +/* D13 PB5: Set attenuator relay output */ +void contr_att_init(void) { + REG_SETUP_BIT(DDRB, PB5); +} +void contr_att_on(void) { + REG_SETUP_BIT(PORTB, PB5); +} +void contr_att_off(void) { + REG_SETDOWN_BIT(PORTB, PB5); +} + diff --git a/contr_relay.h b/contr_relay.h new file mode 100644 index 0000000..0ff2e32 --- /dev/null +++ b/contr_relay.h @@ -0,0 +1,41 @@ + +#ifndef CONTR_RELAY_H_QWERTY +#define CONTR_RELAY_H_QWERTY + +void contr_txrelay_init(void); +void contr_txrelay_on(void); +void contr_txrelay_off(void); +void contr_txrelay_onoff(void); + +void contr_relay10m_init(void); +void contr_relay10m_on(void); +void contr_relay10m_off(void); + +void contr_relay20m_init(void); +void contr_relay20m_on(void); +void contr_relay20m_off(void); + +void contr_relay40m_init(void); +void contr_relay40m_on(void); +void contr_relay40m_off(void); + +void contr_relay80m_init(void); +void contr_relay80m_on(void); +void contr_relay80m_off(void); + +void contr_buzzer_init(void); +void contr_buzzer_on(void); +void contr_buzzer_off(void); +void contr_buzzer_onoff(void); + +void contr_fan_init(void); +void contr_fan_on(void); +void contr_fan_off(void); +void contr_fan_onoff(void); +bool contr_fan_is_on(void); + +void contr_att_init(void); +void contr_att_on(void); +void contr_att_off(void); + +#endif diff --git a/main.c b/main.c index eb537b1..8356261 100644 --- a/main.c +++ b/main.c @@ -14,8 +14,8 @@ #include #include #include -#include -#include + +#include int main(void) { uart_init(); diff --git a/timer.c b/timer.c deleted file mode 100644 index 458ebf0..0000000 --- a/timer.c +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright 2017 Oleg Borodin - */ - -#include -#include -#include -#include -#include - -#include -#include -#include -#include - -#include -#include - - -void timer_init(void) { - /* Disable comparators */ - REG_SETDOWN_BIT(TCCR0A, COM0A1); - REG_SETDOWN_BIT(TCCR0A, COM0A0); - REG_SETDOWN_BIT(TCCR0A, COM0B1); - REG_SETDOWN_BIT(TCCR0A, COM0B0); - /* Set normal mode */ - REG_SETDOWN_BIT(TCCR0A, WGM01); - REG_SETDOWN_BIT(TCCR0A, WGM00); - /* Set clock to 1/64 */ - REG_SETDOWN_BIT(TCCR0B, CS02); - REG_SETUP_BIT(TCCR0B, CS01); - REG_SETUP_BIT(TCCR0B, CS00); - /* Enable timer interrupt */ - REG_SETUP_BIT(TIMSK0, TOIE0); - REG_SETUP(TCNT0, TIMER_INITVAL); -} diff --git a/timer.h b/timer.h deleted file mode 100644 index 3efb68f..0000000 --- a/timer.h +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef TIMER_H_QWERTY -#define TIMER_H_QWERTY - -#define TIMER_INITVAL 128 - -void timer_init(void); - -#endif