This commit is contained in:
Олег Бородин
2024-08-26 21:00:46 +02:00
parent aee7e32bf0
commit 35b79f0cda
5 changed files with 249 additions and 170 deletions

View File

@@ -52,9 +52,9 @@ void button_reset(void) {
button.strokes_ended = false;
}
#define BUTTON_TIME_PRESSED 100
#define BUTTON_TIME_RELEASED 100
#define BUTTON_RELEASED_TIME 1500
#define BUTTON_TIME_PRESSED 50
#define BUTTON_TIME_RELEASED 50
#define BUTTON_RELEASED_TIME 500
void button_handle(void) {
if (!button.strokes_ended) {
@@ -78,6 +78,7 @@ void button_handle(void) {
button.released_time = 0;
}
}
}
if (button.push_counter > 0) {
button.released_time++;
if (button.released_time > BUTTON_RELEASED_TIME) {
@@ -86,5 +87,4 @@ void button_handle(void) {
} else {
button.released_time = 0;
}
}
}

405
contr.c
View File

@@ -30,31 +30,43 @@
#define CONTR_BAND_40M 0x04
#define CONTR_BAND_80M 0x05
#define CONR_VCCARR_SIZE 4
#define CONR_PWRARR_SIZE 16
#define CONTR_DELAY_OFF 0
#define CONTR_DELAY_100MS 22
#define CONTR_DELAY_200MS 44
#define CONTR_DELAY_300MS 66
#define CONTR_DELAY_500MS 110
#define CONR_FREQARR_SIZE 32
#define CONR_VCCARR_SIZE 4
#define CONR_PWRARR_SIZE 32
#define CONR_DISPSTR_SIZE 20
typedef struct {
uint8_t band;
uint16_t freq;
uint16_t cnt;
uint16_t vcc;
uint16_t vccs[CONR_VCCARR_SIZE];
size_t vccp;
float vcc;
uint16_t vccr;
uint16_t freq_arr[CONR_FREQARR_SIZE];
size_t freq_pos;
uint16_t fwd1s[CONR_PWRARR_SIZE];
uint16_t fwd2s[CONR_PWRARR_SIZE];
size_t fwd1p;
size_t fwd2p;
uint16_t fwd;
uint16_t fwd_arr[CONR_PWRARR_SIZE];
size_t fwd_pos;
uint16_t fwd_awg;
uint16_t rev1s[CONR_PWRARR_SIZE];
uint16_t rev2s[CONR_PWRARR_SIZE];
size_t rev1p;
size_t rev2p;
uint16_t rev;
uint16_t rev_arr[CONR_PWRARR_SIZE];
size_t rev_pos;
uint16_t rev_awg;
uint16_t swr_arr[CONR_PWRARR_SIZE];
size_t swr_pos;
uint16_t swr_awg;
char disp_str[CONR_DISPSTR_SIZE];
volatile uint16_t osc;
uint16_t tx_delay;
uint16_t tx_cnt;
} contr_t;
contr_t contr;
@@ -63,102 +75,195 @@ void contr_switch_band(void);
void contr_set_band(uint8_t band);
void contr_write_band(void);
void contr_read_band(void);
char* contr_get_bandname();
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.vcc = 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.fwd_pos = 0;
contr.fwd_awg = 0;
for (size_t i = 0; i < CONR_PWRARR_SIZE; i++) {
contr.fwd_arr[i] = 0;
}
contr.rev_pos = 0;
contr.rev_awg = 0;
for (size_t i = 0; i < CONR_PWRARR_SIZE; i++) {
contr.rev_arr[i] = 0;
}
contr.swr_pos = 0;
contr.swr_awg = 0;
for (size_t i = 0; i < CONR_PWRARR_SIZE; i++) {
contr.swr_arr[i] = 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;
contr.disp_str[0] = '\0';
contr.tx_delay = CONTR_DELAY_200MS;
contr.tx_cnt = 0;
}
void contr_setup(void) {
contr_read_band();
}
#define CONTR_VCC_SCALE 270.0F
#define CONTR_VCC_SCALE 270
void contr_measure_vcc(void) {
contr.vccr = adc_read(ACD_CHANELL3);
contr.vcc = adc_read(ACD_CHANELL3) / CONTR_VCC_SCALE;
}
void contr_calc_vcc(void) {
contr.vcc = lroundf((float)contr.vccr / CONTR_VCC_SCALE) / 10.0F;
}
float contr_get_vcc(void) {
uint16_t 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);
}
}
#define CONTR_FWD_SCALE 39
#define CONTR_REV_SCALE 39
void contr_calc_fwd(void) {
uint32_t summ = 0;
for (size_t i = 0; i < CONR_PWRARR_SIZE; i++) {
summ += contr.fwd2s[i];
void contr_measure_swr(void) {
uint16_t fwd = adc_read(ACD_CHANELL1) / CONTR_FWD_SCALE;
uint16_t rev = adc_read(ACD_CHANELL0) / CONTR_REV_SCALE;
contr.fwd_arr[contr.fwd_pos++] = fwd;
if ((contr.fwd_pos) > CONR_PWRARR_SIZE) {
contr.fwd_pos = 0;
}
summ /= CONR_PWRARR_SIZE;
contr.fwd = summ / CONTR_FWD_SCALE;
contr.rev_arr[contr.rev_pos++] = rev;
if ((contr.rev_pos) > CONR_PWRARR_SIZE) {
contr.rev_pos = 0;
}
}
void contr_calc_swr(void) {
uint32_t fwd_awg = 0;
for (size_t i = 0; i < CONR_PWRARR_SIZE; i++) {
fwd_awg += contr.fwd_arr[i];
}
fwd_awg = (fwd_awg + CONR_PWRARR_SIZE - 1) / CONR_PWRARR_SIZE;
contr.fwd_awg = (uint16_t)(fwd_awg);
uint32_t rev_awg = 0;
for (size_t i = 0; i < CONR_PWRARR_SIZE; i++) {
rev_awg += contr.rev_arr[i];
}
rev_awg = (rev_awg + CONR_PWRARR_SIZE - 1) / CONR_PWRARR_SIZE;
contr.rev_awg = (uint16_t)(rev_awg);
uint32_t pwr_diff = (fwd_awg - rev_awg);
uint32_t swr = 0;
if (pwr_diff != 0) {
swr = ((fwd_awg + rev_awg) * 100) / pwr_diff;
}
contr.swr_arr[contr.swr_pos++] = swr;
if ((contr.swr_pos) > CONR_PWRARR_SIZE) {
contr.swr_pos = 0;
}
uint32_t swr_awg = 0;
for (size_t i = 0; i < CONR_PWRARR_SIZE; i++) {
swr_awg += contr.swr_arr[i];
}
swr_awg /= CONR_PWRARR_SIZE;
contr.swr_awg = (uint16_t)(swr_awg);
}
uint16_t contr_get_fwd(void) {
return contr.fwd;
return contr.fwd_awg;
}
#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;
return contr.rev_awg;
}
uint16_t contr_get_swr(void) {
return contr.swr_awg;
}
#define CONTR_FREQ_SCALE 11
void contr_measure_freq(void) {
uint16_t osc = contr.osc;
contr.osc = 0;
contr.freq_arr[contr.freq_pos++] = osc;
if ((contr.freq_pos) > CONR_FREQARR_SIZE) {
contr.freq_pos = 0;
}
uint32_t tmp = 0;
for (size_t i = 0; i < CONR_FREQARR_SIZE; i++) {
tmp += contr.freq_arr[i];
}
contr.freq = tmp / CONR_FREQARR_SIZE / CONTR_FREQ_SCALE;
}
uint16_t contr_get_freq(void) {
return contr.freq;
}
void contr_tx_handle(void) {
if (ptt_is_pressed()) {
contr.tx_cnt = contr.tx_delay;
buzzer_on();
}
if (contr.tx_cnt > 0) {
contr.tx_cnt--;
} else if (contr.tx_cnt == 0) {
buzzer_off();
}
}
void contr_switch_delay(void) {
switch (contr.tx_delay) {
case CONTR_DELAY_100MS:
contr_set_band(CONTR_DELAY_200MS);
break;
case CONTR_DELAY_200MS:
contr_set_band(CONTR_DELAY_300MS);
break;
case CONTR_DELAY_300MS:
contr_set_band(CONTR_DELAY_500MS);
break;
case CONTR_DELAY_500MS:
contr_set_band(CONTR_DELAY_100MS);
break;
default:
break;
}
}
char* contr_get_delayname(void) {
switch (contr.tx_delay) {
case CONTR_DELAY_100MS:
return "100ms";
break;
case CONTR_DELAY_200MS:
return "200ms";
break;
case CONTR_DELAY_300MS:
return "300ms";
break;
case CONTR_DELAY_500MS:
return "500ms";
break;
default:
break;
}
return " 0ms";
}
ISR(TIMER0_OVF_vect) {
button_handle();
contr_measure_vcc();
contr_measure_swr();
contr_tx_handle();
contr_calc_swr();
uart_handle();
}
/*
ISR(TIMER1_OVF_vect) {
contr.osc++;
REG_SETUP(TCNT1, TIMER1_PRESIZE);
}
*/
void contr_switch_band(void) {
switch (contr.band) {
@@ -215,34 +320,17 @@ void contr_set_band(uint8_t band) {
atten_off();
}
char* contr_get_bandname() {
char* bandstr = NULL;
switch (contr.band) {
case CONTR_BAND_10M:
bandstr = "10m";
break;
case CONTR_BAND_20M:
bandstr = "20m";
break;
case CONTR_BAND_40M:
bandstr = "40m";
break;
case CONTR_BAND_80M:
bandstr = "80m";
break;
case CONTR_BAND_OFF:
bandstr = "OFF";
break;
}
return bandstr;
}
#define CONTR_KEY_SWDELAY 2
#define CONTR_KEY_SWBAND 3
#define CONTR_KEY_BUZZER 4
#define CONTR_KEY_FUN 5
void contr_button_eval(void) {
switch (button_get()) {
case CONTR_KEY_SWDELAY:
contr_switch_delay();
break;
case CONTR_KEY_SWBAND:
contr_switch_band();
break;
@@ -268,9 +356,8 @@ void contr_show_band(void) {
}
void contr_show_vcc(void) {
char buffer[12] = { '\0' };
sprintf(buffer, "%4.1fv", contr_get_vcc());
disp_string(3, 11, buffer);
sprintf(contr.disp_str, "%3.1fv", (float)contr_get_vcc() / 10);
disp_string(3, 11, contr.disp_str);
}
void contr_show_ptt(void) {
@@ -281,60 +368,30 @@ 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) {
contr.cnt++;
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' };
uint16_t freq = contr_get_freq();
sprintf(buffer, "%5uM", freq);
disp_string(0, 0, buffer);
sprintf(contr.disp_str, "%2uM", freq);
disp_string(0, 0, contr.disp_str);
}
void contr_show_delay(void) {
disp_string(0, 0, contr_get_delayname());
}
void contr_show_fwd(void) {
char buffer[12] = { '\0' };
sprintf(buffer, "F %7uw", contr_get_fwd());
disp_string(1, 2, buffer);
sprintf(contr.disp_str, "F=%4.1f", (float)contr_get_fwd() / 100.0F);
disp_string(1, 0, contr.disp_str);
}
void contr_show_rev(void) {
char buffer[12] = { '\0' };
sprintf(buffer, "R %7uw", contr_get_rev());
disp_string(2, 2, buffer);
sprintf(contr.disp_str, "R=%4.1f", (float)contr_get_rev() / 100.F);
disp_string(1, 8, contr.disp_str);
}
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);
sprintf(contr.disp_str, "S=%2.2f", (float)contr_get_swr() / 100.0F);
disp_string(2, 0, contr.disp_str);
}
void contr_main(void) {
@@ -342,16 +399,15 @@ void contr_main(void) {
uint16_t counter = 0;
while (true) {
contr_show_ptt();
contr_show_delay();
contr_button_eval();
//contr_calc_vcc();
//contr_show_fwd();
//contr_show_rev();
contr_show_swr();
contr_show_vcc();
contr_show_fwd();
contr_show_rev();
contr_show_swr();
contr_show_band();
contr_show_freq();
counter++;
_delay_ms(100);
_delay_ms(50);
}
}
@@ -365,3 +421,28 @@ void contr_read_band(void) {
eeprom_read_bytes(CONTR_BAND_EEPROM_ADDR, &contr.band, sizeof(contr.band));
}
char* contr_get_bandname() {
char* bandstr = NULL;
switch (contr.band) {
case CONTR_BAND_10M:
bandstr = "10m";
break;
case CONTR_BAND_20M:
bandstr = "20m";
break;
case CONTR_BAND_40M:
bandstr = "40m";
break;
case CONTR_BAND_80M:
bandstr = "80m";
break;
case CONTR_BAND_OFF:
bandstr = "OFF";
break;
default:
bandstr = "UNK";
break;
}
return bandstr;
}

1
main.c
View File

@@ -28,7 +28,6 @@ int main(void) {
adc_init();
timer0_init();
timer1_init();
atten_init();
ampl_init();

View File

@@ -21,7 +21,7 @@ void timer0_init(void) {
REG_SETDOWN_BIT(TCCR0A, WGM00);
/* TCCR0B */
/* Set clock to 1/64 */
/* Set prescaler */
REG_SETUP_BIT(TCCR0B, CS02);
REG_SETDOWN_BIT(TCCR0B, CS01);
REG_SETDOWN_BIT(TCCR0B, CS00);

View File

@@ -1,8 +1,7 @@
#ifndef CONTR_INIT_H_QWERTY
#define CONTR_INIT_H_QWERTY
//#define TIMER0_PRESIZE 128
#define TIMER1_PRESIZE 65500 //65532 // 65536
#define TIMER1_PRESIZE 65528 //65532 // 65536
void timer0_init(void);
void timer1_init(void);