diff --git a/Makefile b/Makefile index 5981e88..612759a 100644 --- a/Makefile +++ b/Makefile @@ -7,12 +7,12 @@ F_CPU = 16000000UL MCU = atmega328p CFLAGS += -DF_CPU=$(F_CPU) -mmcu=$(MCU) -CFLAGS += -I. -O2 --std=c11 +CFLAGS += -I. -Os --std=c11 CFLAGS += -ffunction-sections -fdata-sections LDFLAGS += -s -mmcu=$(MCU) LDFLAGS += -Wl,--gc-sections -LDFLAGS += -Wl,-u,vfprintf -lprintf_flt +LDFLAGS += -Wl,-u,vfprintf -lprintf_flt -lm MAIN_OBJS += main.o MAIN_OBJS += adc.o @@ -54,7 +54,7 @@ TTY_SPEED = 115200 BACKUP = $(MAIN_HEX).bak upload: $(MAIN_HEX) - $(AVRDUDE) -qq -c arduino -p ATMEGA328P -P $(TTY_PORT) -b $(TTY_SPEED) -U flash:w:$< + $(AVRDUDE) -qq -c arduino -p m328p -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/README.md b/README.md index 0fff135..5cfebf1 100644 --- a/README.md +++ b/README.md @@ -1,19 +1,41 @@ # Micro PA50 controller -set tx off; -while (ptt is on || have input power ) { - check freq; + + +``` +set amp off; +while () { + if (ptt is on || have input power) { + check temp; + if (temp is more min) { + set fan on; + } else { + set fan off; + } + if (temp is over) { + show temp warning; + continue; + } + check freq; // if auto? if freq in { set lpf for the freq if need; - calc vswr; - if (swr is good) { - set tx on; + calc power and vswr; + if (vswr is good) { + set amp on; } else { + set amp off; show wrong swr; } + if (power too hight) { + set amp off immed; + } } else { show wron freq; - set tx off; + set amp off; } + } else { + wait 100ms; + } } +``` diff --git a/adc.c b/adc.c index fc6ccd5..589b971 100644 --- a/adc.c +++ b/adc.c @@ -37,7 +37,7 @@ uint16_t adc_read(uint8_t channel) { uint16_t hval = (uint16_t)ADCH; hval = (hval << 8) & 0xFF00; lval = lval & 0x00FF; - return hval | lval; + return (hval | lval); } diff --git a/contr.c b/contr.c index 4b72f5f..2c98c55 100644 --- a/contr.c +++ b/contr.c @@ -7,6 +7,7 @@ #include #include #include +#include #include #include @@ -29,11 +30,31 @@ #define CONTR_BAND_40M 0x04 #define CONTR_BAND_80M 0x05 +#define CONR_VCCARR_SIZE 4 +#define CONR_PWRARR_SIZE 16 + typedef struct { uint8_t band; - uint16_t vcc; - uint16_t cnt; uint16_t freq; + uint16_t cnt; + + uint16_t vccs[CONR_VCCARR_SIZE]; + size_t vccp; + float vcc; + uint16_t vccr; + + uint16_t fwd1s[CONR_PWRARR_SIZE]; + uint16_t fwd2s[CONR_PWRARR_SIZE]; + size_t fwd1p; + size_t fwd2p; + uint16_t fwd; + + uint16_t rev1s[CONR_PWRARR_SIZE]; + uint16_t rev2s[CONR_PWRARR_SIZE]; + size_t rev1p; + size_t rev2p; + uint16_t rev; + } contr_t; contr_t contr; @@ -45,21 +66,99 @@ void contr_read_band(void); void contr_init(void) { contr.band = CONTR_BAND_OFF; + contr.vccp = 0; + for (size_t i = 0; i < CONR_VCCARR_SIZE; i++) contr.vccs[i] = 0; + + contr.fwd1p = 0; + contr.fwd2p = 0; + contr.fwd2p = 0; + for (size_t i = 0; i < CONR_PWRARR_SIZE; i++) contr.fwd1s[i] = 0; + for (size_t i = 0; i < CONR_PWRARR_SIZE; i++) contr.fwd2s[i] = 0; + contr.fwd = 0; + + contr.rev1p = 0; + contr.rev2p = 0; + contr.rev2p = 0; + for (size_t i = 0; i < CONR_PWRARR_SIZE; i++) contr.rev1s[i] = 0; + for (size_t i = 0; i < CONR_PWRARR_SIZE; i++) contr.rev2s[i] = 0; + contr.rev = 0; } void contr_setup(void) { contr_read_band(); } -void contr_measure_vcc() { - contr.vcc = adc_read(ACD_CHANELL3); +#define CONTR_VCC_SCALE 270.0F +void contr_measure_vcc(void) { + contr.vccr = adc_read(ACD_CHANELL3); } - -#define CONTR_VCC_SCALE 2700.0F -float contr_get_vcc() { - return (float)contr.vcc / CONTR_VCC_SCALE; +void contr_calc_vcc(void) { + contr.vcc = lroundf((float)contr.vccr / CONTR_VCC_SCALE) / 10.0F; } +float contr_get_vcc(void) { + return contr.vcc; +} + +#define CONTR_FWD_SCALE 370 +void contr_measure_fwd(void) { + contr.fwd1s[contr.fwd1p] = adc_read(ACD_CHANELL1); + contr.fwd1p++; + if ((contr.fwd1p) > CONR_PWRARR_SIZE) { + contr.fwd1p = 0; + if ((++(contr.fwd2p)) > CONR_PWRARR_SIZE) { + contr.fwd2p = 0; + } + uint32_t summ = 0; + for (size_t i = 0; i < CONR_PWRARR_SIZE; i++) { + summ += contr.fwd1s[i]; + } + contr.fwd2s[contr.fwd2p] = (uint16_t)(summ / CONR_PWRARR_SIZE); + } +} + +void contr_calc_fwd(void) { + uint32_t summ = 0; + for (size_t i = 0; i < CONR_PWRARR_SIZE; i++) { + summ += contr.fwd2s[i]; + } + summ /= CONR_PWRARR_SIZE; + contr.fwd = summ / CONTR_FWD_SCALE; +} + +uint16_t contr_get_fwd(void) { + return contr.fwd; +} + +#define CONTR_REV_SCALE 28 +void contr_measure_rev(void) { + contr.rev1s[contr.rev1p] = adc_read(ACD_CHANELL0); + contr.rev1p++; + if ((contr.rev1p) > CONR_PWRARR_SIZE) { + contr.rev1p = 0; + if ((++(contr.rev2p)) > CONR_PWRARR_SIZE) { + contr.rev2p = 0; + } + uint32_t summ = 0; + for (size_t i = 0; i < CONR_PWRARR_SIZE; i++) { + summ += contr.rev1s[i]; + } + contr.rev2s[contr.rev2p] = (summ / CONR_PWRARR_SIZE); + } +} + +void contr_calc_rev(void) { + uint32_t summ = 0; + for (size_t i = 0; i < CONR_PWRARR_SIZE; i++) { + summ += contr.rev2s[i]; + } + summ /= CONR_PWRARR_SIZE; + contr.rev = summ / CONTR_REV_SCALE; +} + +uint16_t contr_get_rev(void) { + return contr.rev; +} void contr_switch_band(void) { switch (contr.band) { @@ -81,26 +180,33 @@ void contr_switch_band(void) { } void contr_set_band(uint8_t band) { - relaytx_off(); + ampl_off(); contr.band = band; - relay80m_off(); - relay40m_off(); - relay20m_off(); - relay10m_off(); - switch (contr.band) { case CONTR_BAND_10M: - relay10m_on(); + filter80m_off(); + filter40m_off(); + filter20m_off(); + filter10m_on(); break; case CONTR_BAND_20M: - relay20m_on(); + filter80m_off(); + filter40m_off(); + filter10m_off(); + filter20m_on(); break; case CONTR_BAND_40M: - relay40m_on(); + filter80m_off(); + filter20m_off(); + filter10m_off(); + filter40m_on(); break; case CONTR_BAND_80M: - relay80m_on(); + filter40m_off(); + filter20m_off(); + filter10m_off(); + filter80m_on(); break; default: break; @@ -158,12 +264,12 @@ void contr_show_logo(void) { } void contr_show_band(void) { - disp_string(0, 13, contr_get_bandname(contr.band)); + disp_string(0, 13, contr_get_bandname()); } void contr_show_vcc(void) { char buffer[12] = { '\0' }; - sprintf(buffer, "%03.1fv", contr_get_vcc()); + sprintf(buffer, "%4.1fv", contr_get_vcc()); disp_string(3, 11, buffer); } @@ -176,10 +282,15 @@ void contr_show_ptt(void) { } ISR(TIMER0_OVF_vect) { + contr_measure_vcc(); + contr_measure_fwd(); + contr_measure_rev(); contr.freq = contr.cnt; contr.cnt = 0; button_handle(); uart_handle(); + contr_calc_fwd(); + contr_calc_rev(); } ISR(TIMER1_OVF_vect) { @@ -187,10 +298,43 @@ ISR(TIMER1_OVF_vect) { REG_SETUP(TCNT1, TIMER1_PRESIZE); } +#define CONTR_FREQ_SCALE 2 +uint16_t contr_get_freq(void) { + return contr.freq / CONTR_FREQ_SCALE; +} + void contr_show_freq(void) { char buffer[12] = { '\0' }; - sprintf(buffer, "%2.0fM", (float)contr.freq / 16.05F); - disp_string(0, 0, buffer); + uint16_t freq = contr_get_freq(); + + sprintf(buffer, "%5uM", freq); + disp_string(0, 0, buffer); +} + +void contr_show_fwd(void) { + char buffer[12] = { '\0' }; + sprintf(buffer, "F %7uw", contr_get_fwd()); + disp_string(1, 2, buffer); +} + +void contr_show_rev(void) { + char buffer[12] = { '\0' }; + sprintf(buffer, "R %7uw", contr_get_rev()); + disp_string(2, 2, buffer); +} + +void contr_show_swr(void) { + char buffer[12] = { '\0' }; + uint16_t fwd = contr_get_fwd(); + uint16_t rev = contr_get_rev(); + uint16_t diff = (fwd - rev); + + uint16_t swr = 0; + if (diff != 0) { + swr =((fwd + rev) * 100) / diff; + } + sprintf(buffer, " %u", swr); + disp_string(2, 2, buffer); } void contr_main(void) { @@ -199,7 +343,10 @@ void contr_main(void) { while (true) { contr_show_ptt(); contr_button_eval(); - contr_measure_vcc(); + //contr_calc_vcc(); + //contr_show_fwd(); + //contr_show_rev(); + contr_show_swr(); contr_show_vcc(); contr_show_band(); contr_show_freq(); @@ -208,11 +355,13 @@ void contr_main(void) { } } +#define CONTR_BAND_EEPROM_ADDR 0x00 + void contr_write_band(void) { - eeprom_write_bytes(0x00, &contr.band, sizeof(contr.band)); + eeprom_write_bytes(CONTR_BAND_EEPROM_ADDR, &contr.band, sizeof(contr.band)); } void contr_read_band(void) { - eeprom_read_bytes(0x00, &contr.band, sizeof(contr.band)); + eeprom_read_bytes(CONTR_BAND_EEPROM_ADDR, &contr.band, sizeof(contr.band)); } diff --git a/main.c b/main.c index 85a2ff9..52a4ea6 100644 --- a/main.c +++ b/main.c @@ -31,11 +31,11 @@ int main(void) { timer1_init(); atten_init(); - relaytx_init(); - relay10m_init(); - relay20m_init(); - relay40m_init(); - relay80m_init(); + ampl_init(); + filter10m_init(); + filter20m_init(); + filter40m_init(); + filter80m_init(); buzzer_init(); fan_init(); ptt_init(); @@ -43,12 +43,12 @@ int main(void) { button_init(); button_reset(); - relaytx_off(); + ampl_off(); atten_off(); - relay10m_off(); - relay20m_off(); - relay40m_off(); - relay80m_off(); + filter10m_off(); + filter20m_off(); + filter40m_off(); + filter80m_off(); buzzer_off(); fan_off(); diff --git a/relay.c b/relay.c index 5d2b49f..2aa9fbb 100644 --- a/relay.c +++ b/relay.c @@ -25,64 +25,64 @@ bool ptt_is_pressed(void) { } -/* D4 PD4: Set input relay 5 output */ -void relaytx_init(void) { +/* D4 PD4: Set input filter 5 output */ +void ampl_init(void) { REG_SETUP_BIT(DDRD, PD4); } -void relaytx_on(void) { +void ampl_on(void) { REG_SETUP_BIT(PORTD, PD4); } -void relaytx_off(void) { +void ampl_off(void) { REG_SETDOWN_BIT(PORTD, PD4); } -void relaytx_onoff(void) { +void ampl_onoff(void) { if (REG_BIT_VALUE(PIND, PD4)) { - relaytx_off(); + ampl_off(); } else { - relaytx_on(); + ampl_on(); } } -/* D6 PD6: Set 10M relay 1 output */ -void relay10m_init(void) { +/* D6 PD6: Set 10M filter 1 output */ +void filter10m_init(void) { REG_SETUP_BIT(DDRD, PD6); } -void relay10m_on(void) { +void filter10m_on(void) { REG_SETUP_BIT(PORTD, PD6); } -void relay10m_off(void) { +void filter10m_off(void) { REG_SETDOWN_BIT(PORTD, PD6); } -/* D7 PD7: Set 20M relay 2 output */ -void relay20m_init(void) { +/* D7 PD7: Set 20M filter 2 output */ +void filter20m_init(void) { REG_SETUP_BIT(DDRD, PD7); } -void relay20m_on(void) { +void filter20m_on(void) { REG_SETUP_BIT(PORTD, PD7); } -void relay20m_off(void) { +void filter20m_off(void) { REG_SETDOWN_BIT(PORTD, PD7); } -/* D8 PB0: Set 40M relay 3 output */ -void relay40m_init(void) { +/* D8 PB0: Set 40M filter 3 output */ +void filter40m_init(void) { REG_SETUP_BIT(DDRB, PB0); } -void relay40m_on(void) { +void filter40m_on(void) { REG_SETUP_BIT(PORTB, PB0); } -void relay40m_off(void) { +void filter40m_off(void) { REG_SETDOWN_BIT(PORTB, PB0); } -/* D9 PB1: Set 80M relay 4 output */ -void relay80m_init(void) { +/* D9 PB1: Set 80M filter 4 output */ +void filter80m_init(void) { REG_SETUP_BIT(DDRB, PB1); } -void relay80m_on(void) { +void filter80m_on(void) { REG_SETUP_BIT(PORTB, PB1); } -void relay80m_off(void) { +void filter80m_off(void) { REG_SETDOWN_BIT(PORTB, PB1); } @@ -128,7 +128,7 @@ bool fan_is_on(void) { return false; } -/* D13 PB5: Set attenuator relay output */ +/* D13 PB5: Set attenuator filter output */ void atten_init(void) { REG_SETUP_BIT(DDRB, PB5); } diff --git a/relay.h b/relay.h index 0027385..c6f93aa 100644 --- a/relay.h +++ b/relay.h @@ -5,26 +5,26 @@ void ptt_init(void); bool ptt_is_pressed(void); -void relaytx_init(void); -void relaytx_on(void); -void relaytx_off(void); -void relaytx_onoff(void); +void ampl_init(void); +void ampl_on(void); +void ampl_off(void); +void ampl_onoff(void); -void relay10m_init(void); -void relay10m_on(void); -void relay10m_off(void); +void filter10m_init(void); +void filter10m_on(void); +void filter10m_off(void); -void relay20m_init(void); -void relay20m_on(void); -void relay20m_off(void); +void filter20m_init(void); +void filter20m_on(void); +void filter20m_off(void); -void relay40m_init(void); -void relay40m_on(void); -void relay40m_off(void); +void filter40m_init(void); +void filter40m_on(void); +void filter40m_off(void); -void relay80m_init(void); -void relay80m_on(void); -void relay80m_off(void); +void filter80m_init(void); +void filter80m_on(void); +void filter80m_off(void); void buzzer_init(void); void buzzer_on(void); diff --git a/timer.c b/timer.c index 6621d79..2392395 100644 --- a/timer.c +++ b/timer.c @@ -22,10 +22,11 @@ void timer0_init(void) { /* TCCR0B */ /* Set clock to 1/64 */ - REG_SETDOWN_BIT(TCCR0B, CS02); - REG_SETUP_BIT(TCCR0B, CS01); - REG_SETUP_BIT(TCCR0B, CS00); + REG_SETUP_BIT(TCCR0B, CS02); + REG_SETDOWN_BIT(TCCR0B, CS01); + REG_SETDOWN_BIT(TCCR0B, CS00); + //REG_SETUP(TCNT0, TIMER0_PRESIZE); /* TIMSK0 */ /* Enable timer interrupt */ REG_SETUP_BIT(TIMSK0, TOIE0); diff --git a/timer.h b/timer.h index 1ae427c..44407e6 100644 --- a/timer.h +++ b/timer.h @@ -1,7 +1,8 @@ #ifndef CONTR_INIT_H_QWERTY #define CONTR_INIT_H_QWERTY -#define TIMER1_PRESIZE 65532 // 65536 +//#define TIMER0_PRESIZE 128 +#define TIMER1_PRESIZE 65500 //65532 // 65536 void timer0_init(void); void timer1_init(void);