diff --git a/DSTAT-temp/src/experiment.c b/DSTAT-temp/src/experiment.c index 53070d11ed2929c07ff2dca0e66ae87682e666db..d8a11e7dc362d6a9c7f88bb43a8c3a39e5b65a17 100644 --- a/DSTAT-temp/src/experiment.c +++ b/DSTAT-temp/src/experiment.c @@ -75,43 +75,52 @@ void send_data_int32(int32_t data){ udi_cdc_putc(((unsigned char *)(&data))[3]); } -void set_timer_period(uint32_t period, volatile void *tc) +uint16_t set_timer_period(uint32_t period, volatile void *tc) { + /** + * Sets a suitable timer source and sets period for a 16-bit timer + * + * @param period 32-bit period in CPU cycles + * @param *tc pointer to timer to set + * @return divider used. + */ uint16_t temp_div = ceil((double)period/65536); + uint16_t divider = 0; if (temp_div == 1) tc_write_clock_source(tc, TC_CLKSEL_DIV1_gc); else if (temp_div == 2){ tc_write_clock_source(tc, TC_CLKSEL_DIV2_gc); - period /= 2; + divider = 2; } else if (temp_div <= 4){ tc_write_clock_source(tc, TC_CLKSEL_DIV4_gc); - period /= 4; + divider = 4; } else if (temp_div <= 8){ tc_write_clock_source(tc,TC_CLKSEL_DIV8_gc); - period /= 8; + divider = 8; } else if (temp_div <= 64){ tc_write_clock_source(tc,TC_CLKSEL_DIV64_gc); - period /= 64; + divider = 64; } else if (temp_div <= 256){ tc_write_clock_source(tc,TC_CLKSEL_DIV256_gc); - period /= 256; + divider = 256; } else if (temp_div <= 1024){ tc_write_clock_source(tc,TC_CLKSEL_DIV1024_gc); - period /= 1024; + divider = 1024; } else{ printf("#Frequency/ADC rate is too low\n\r"); - return; + return 0; } + period /= divider; tc_write_period(tc, (uint16_t)period); - return; + return divider; } void pot_init(void){ diff --git a/DSTAT-temp/src/experiment.h b/DSTAT-temp/src/experiment.h index b52a58e66f10b967927eea76851150084f68dcbd..8b1ab536fe44ad9f463583f0c3f33f49d8889cf9 100644 --- a/DSTAT-temp/src/experiment.h +++ b/DSTAT-temp/src/experiment.h @@ -77,7 +77,7 @@ extern uint8_t autogain_enable; void send_data_uint16(uint16_t data); void send_data_int32(int32_t data); -void set_timer_period(uint32_t period, volatile void *tc); +uint16_t set_timer_period(uint32_t period, volatile void *tc); void pot_init(void); void pot_set_gain(void); void volt_exp_start(void);