diff --git a/DSTAT/src/experiment.c b/DSTAT/src/experiment.c index 3da1d45b73b64f4a243ca261adca074b89ecc0fd..458cbc79cce2e096647033712e44cb7142f26cc6 100644 --- a/DSTAT/src/experiment.c +++ b/DSTAT/src/experiment.c @@ -38,7 +38,7 @@ volatile uint16_t tcf0period = 0; uint32_t skip_samples = 0; //Private function declarations -uint16_t set_timer_period(uint32_t period, volatile void *tc); +//uint16_t set_timer_period(uint32_t period, volatile void *tc); static void precond_rtc_callback(uint32_t time); static void porte_int0_lsv(void); static void tcf0_ovf_callback(void); diff --git a/DSTAT/src/experiment.h b/DSTAT/src/experiment.h index 7c171aea2be798167f29b4689a9807aecced69b7..5c4746979d887a18f3e5b6741638e9a7443c2b74 100644 --- a/DSTAT/src/experiment.h +++ b/DSTAT/src/experiment.h @@ -76,6 +76,7 @@ extern uint16_t g_gain; extern uint8_t g_short; extern uint8_t autogain_enable; +uint16_t set_timer_period(uint32_t period, volatile void *tc); void experiment_handler(char command); void pot_init(void); void pot_set_gain(void); diff --git a/DSTAT/src/main.c b/DSTAT/src/main.c index c7e5c687ef0e8ef2f30b44c0690450eeecb38ad3..d219490efd9907bd72a08894a8b6a1c302ab0b86 100644 --- a/DSTAT/src/main.c +++ b/DSTAT/src/main.c @@ -9,6 +9,7 @@ #include "asf.h" #include "settings.h" #include "tcs.h" +#include "shutter.h" #include <string.h> #include <math.h> #include <stdint.h> @@ -23,6 +24,8 @@ int8_t command_handler(char command){ * Calls functions in * @param command Command character input. */ + + double p1; switch (command){ case 'E': //Experiment options @@ -44,6 +47,23 @@ int8_t command_handler(char command){ printf("T%u.%u.%u.%u\n\r", tcs_data[0], tcs_data[1], tcs_data[2], tcs_data[3]); } break; + + case 'Z': //Test shutter + scanf("%lg",&p1); + shutter_cont(p1); + break; + + case 'z': + shutter_cont_stop(); + break; + + case '1': + shutter_close(); + break; + + case '2': + shutter_open(); + break; case 'V': //check version printf("V%u.%u\n\r", BOARD_VER_MAJOR, BOARD_VER_MINOR); @@ -110,7 +130,7 @@ int main(void){ } tcs_init(); - + shutter_init(); program_loop: while(getchar() != '!'); diff --git a/DSTAT/src/shutter.c b/DSTAT/src/shutter.c new file mode 100644 index 0000000000000000000000000000000000000000..1c8a4ac0cbacb47d001711f400ea84dd016d2ed7 --- /dev/null +++ b/DSTAT/src/shutter.c @@ -0,0 +1,71 @@ +// +// shutter.c +// dstat-firmware +// +// Created by Michael Dryden on 2016-01-27. +// Copyright © 2016 wheeler lab. All rights reserved. +// + +#include "shutter.h" +#include <stdint.h> +#include <tc.h> +#include <math.h> + +void shutter_init(void){ + ioport_set_pin_dir(SHUTTER_PIN, IOPORT_DIR_OUTPUT); + ioport_set_pin_level(SHUTTER_PIN, 0); +} + +void shutter_open(void){ + ioport_set_pin_level(SHUTTER_PIN, 1); +} + +void shutter_close(void){ + ioport_set_pin_level(SHUTTER_PIN, 0); +} + +uint8_t shutter_cont(double freq){ + tc_write_clock_source(&SHUTTER_TC, TC_CLKSEL_OFF_gc); + if (freq > 30 || freq < 0.23842) { + return 1; + } + + else{ + tc_enable(&SHUTTER_TC); + tc_set_wgm(&SHUTTER_TC, TC_WG_FRQ); + tc_enable_cc_channels(&SHUTTER_TC, TC_CCAEN); + + tc_write_clock_source(&SHUTTER_TC, TC_CLKSEL_DIV64_gc); + + uint16_t temp_div = ceil((1/(2*freq))*F_CPU/65536); + uint16_t divider = 0; + + if (temp_div <= 64){ + tc_write_clock_source(&SHUTTER_TC,TC_CLKSEL_DIV64_gc); + divider = 64; + } + else if (temp_div <= 256){ + tc_write_clock_source(&SHUTTER_TC,TC_CLKSEL_DIV256_gc); + divider = 256; + } + else if (temp_div <= 1024){ + tc_write_clock_source(&SHUTTER_TC,TC_CLKSEL_DIV1024_gc); + divider = 1024; + } + else{ + printf("#ERR: Frequency/ADC rate is too low\n\r"); + return 0; + } + + SHUTTER_TC.CCA = ((uint16_t)((F_CPU/divider)/(2*freq)))-1; //f=1/(2*(CCA+1)*f_clk) + + return 0; + } +} + +void shutter_cont_stop(void){ + tc_write_clock_source(&SHUTTER_TC, TC_CLKSEL_OFF_gc); + tc_set_wgm(&SHUTTER_TC, TC_WG_NORMAL); + tc_disable(&SHUTTER_TC); + ioport_set_pin_level(SHUTTER_PIN, 0); +} \ No newline at end of file diff --git a/DSTAT/src/shutter.h b/DSTAT/src/shutter.h new file mode 100644 index 0000000000000000000000000000000000000000..c1ba5dd7cd68749551c0508c0a80d74941d79690 --- /dev/null +++ b/DSTAT/src/shutter.h @@ -0,0 +1,24 @@ +// +// shutter.h +// dstat-firmware +// +// Created by Michael Dryden on 2016-01-27. +// Copyright © 2016 wheeler lab. All rights reserved. +// + +#ifndef shutter_h +#define shutter_h + +#include <stdio.h> +#include <ioport.h> + +#define SHUTTER_PIN IOPORT_CREATE_PIN(PORTE,0) +#define SHUTTER_TC TCE0 + +void shutter_init(void); +void shutter_open(void); +void shutter_close(void); +uint8_t shutter_cont(double freq); +void shutter_cont_stop(void); + +#endif /* shutter_h */