//! [callback_funcs] void tc_callback_to_change_duty_cycle( struct tc_module *const module_inst) { static uint16_t i = 0; i += 128; tc_set_compare_value(module_inst, TC_COMPARE_CAPTURE_CHANNEL_0, i + 1); }
/*! * @brief Implements a delay of the length of the argument in econds * * @param[in] NULL * * @param[out] NULL * * @return NULL * */ void tc4_wait_for_msec (uint32_t msec) { /* Set the compare value */ tc_set_compare_value(&tc4_instance, TC_COMPARE_CAPTURE_CHANNEL_0, (msec *500)); /* start counting */ tc_start_counter(&tc4_instance); /* delay until required time is elapsed */ while (!tc4_callback_flag); /* stop the counter */ tc_stop_counter(&tc4_instance); /* reset the interrupt flag */ tc4_callback_flag = false; }
void Adafruit_ZeroTimer::setCompare(uint8_t channum, uint32_t compare) { if (channum > 1) return; // set the configuration if (_countersize == TC_COUNTER_SIZE_8BIT) { config_tc.counter_8_bit.compare_capture_channel[channum] = compare; } else if (_countersize == TC_COUNTER_SIZE_16BIT) { config_tc.counter_16_bit.compare_capture_channel[channum] = compare; } else if (_countersize == TC_COUNTER_SIZE_32BIT) { config_tc.counter_32_bit.compare_capture_channel[channum] = compare; } // set it live tc_set_compare_value(&tc_instance, (tc_compare_capture_channel)channum, compare); }
void us_ticker_set_interrupt(timestamp_t timestamp) { uint32_t cur_time; int32_t delta; cur_time = us_ticker_read(); delta = (int32_t)((uint32_t)timestamp - cur_time); if (delta < 0) { /* Event already occurred in past */ us_ticker_irq_handler(); return; } NVIC_DisableIRQ(TICKER_COUNTER_IRQn); NVIC_SetVector(TICKER_COUNTER_IRQn, (uint32_t)TICKER_COUNTER_Handlr); /* Enable the callback */ tc_enable_callback(&us_ticker_module, TC_CALLBACK_CC_CHANNEL0); tc_set_compare_value(&us_ticker_module, TC_COMPARE_CAPTURE_CHANNEL_0, (uint32_t)timestamp); NVIC_EnableIRQ(TICKER_COUNTER_IRQn); }
/*! \brief to load compare value in channel compare register */ void tmr_write_cmpreg(uint16_t compare_value) { tc_set_compare_value(&module_inst, TC_COMPARE_CAPTURE_CHANNEL_0, (uint32_t)compare_value); }
/* Timer 0 Initialization */ void timer_init(void) { struct tc_config conf_tc; struct tc_events conf_tc_events = {.generate_event_on_compare_channel[0] = 1}; tc_get_config_defaults(&conf_tc); conf_tc.clock_source = GCLK_GENERATOR_0; conf_tc.wave_generation = TC_WAVE_GENERATION_MATCH_FREQ; conf_tc.counter_16_bit.compare_capture_channel[0] = 0xFFFF; tc_init(&tc_inst, TC0, &conf_tc); tc_enable_events(&tc_inst, &conf_tc_events); tc_enable(&tc_inst); tc_stop_counter(&tc_inst); /* Enable TC0 match/capture channel 0 interrupt */ TC0->COUNT16.INTENSET.bit.MC0 = 1; /* Enable TC0 module interrupt */ NVIC_EnableIRQ(TC0_IRQn); } /* DAC Initialization */ void dac_initialize(void) { struct dac_config conf_dac; struct dac_events conf_dac_events = {.on_event_start_conversion = 1}; dac_get_config_defaults(&conf_dac); conf_dac.clock_source = GCLK_GENERATOR_3; conf_dac.reference = DAC_REFERENCE_INT1V; dac_init(&dac_inst, DAC, &conf_dac); dac_enable_events(&dac_inst, &conf_dac_events); dac_enable(&dac_inst); } /* Event System Initialization */ void evsys_init(void) { struct events_resource conf_event_resource; struct events_config conf_event; events_get_config_defaults(&conf_event); conf_event.edge_detect = EVENTS_EDGE_DETECT_NONE; conf_event.path = EVENTS_PATH_ASYNCHRONOUS; conf_event.generator = EVSYS_ID_GEN_TC0_MCX_0; events_allocate(&conf_event_resource, &conf_event); events_attach_user(&conf_event_resource, EVSYS_ID_USER_DAC_START); } /* Initialize the selected waveform buffer with output data */ void buffer_init(void) { #if WAVE_MODE==SINE_WAVE for (i = 0; i < DEGREES_PER_CYCLE; i++) { sine_wave_buf[i] = (uint16_t)(500 + (500*sin((double)i*DEGREE))); } #elif WAVE_MODE==SAW_TOOTH_WAVE for (i = 0; i < 256; i++) { sawtooth_wave_buf[i] = i*4; } #elif WAVE_MODE==TRIANGLE_WAVE for (i = 0; i < 128; i++) { triangle_wave_buf[i] = i*8; } for (i = 128; i < 256; i++) { triangle_wave_buf[i] = 1023 - (i*8); } #endif } /* Main function */ int main(void) { system_init(); timer_init(); dac_initialize(); evsys_init(); buffer_init(); /* Set the TC0 compare value corresponding to specified frequency */ #if WAVE_MODE==SINE_WAVE tc_set_compare_value(&tc_inst, 0, \ system_gclk_gen_get_hz(GCLK_GENERATOR_0)/(FREQUENCY*360)); #else tc_set_compare_value(&tc_inst, 0, \ system_gclk_gen_get_hz(GCLK_GENERATOR_0)/(FREQUENCY*256)); #endif /* Start TC0 timer */ tc_start_counter(&tc_inst); /* Enable global interrupt */ system_interrupt_enable_global(); while (true) { } }