/** * \brief Application entry point. * * \return Unused (ANSI-C compatibility). */ int main(void) { uint8_t uc_key; /* Initialize the SAM system. */ sysclk_init(); board_init(); configure_console(); /* Output example information. */ puts(STRING_HEADER); g_afec0_sample_data = 0; g_afec1_sample_data = 0; g_max_digital = MAX_DIGITAL_12_BIT; set_afec_test(); while (1) { afec_start_software_conversion(AFEC0); delay_ms(g_delay_cnt); /* Check if the user enters a key. */ if (!uart_read(CONF_UART, &uc_key)) { /* Disable all afec interrupt. */ afec_disable_interrupt(AFEC0, AFEC_INTERRUPT_ALL); afec_disable_interrupt(AFEC1, AFEC_INTERRUPT_ALL); tc_stop(TC0, 0); set_afec_test(); } } }
// Module initialisation void AnalogInInit() { #if SAM3XA || SAM4S pmc_enable_periph_clk(ID_ADC); adc_init(ADC, SystemCoreClock, 2000000, ADC_STARTUP_TIME_12); // 2MHz clock adc_configure_timing(ADC, 3, ADC_SETTLING_TIME_3, 1); // Add transfer time adc_configure_trigger(ADC, ADC_TRIG_SW, 0); // Disable hardware trigger adc_disable_interrupt(ADC, 0xFFFFFFFF); // Disable all ADC interrupts adc_disable_all_channel(ADC); #elif SAM4E || SAME70 afec_enable(AFEC0); afec_enable(AFEC1); afec_config cfg; afec_get_config_defaults(&cfg); #if 0 // these are probably not needed, the defaults should be OK // cfg.afec_clock = 2000000UL; // reduce clock frequency // cfg.settling_time = AFEC_SETTLING_TIME_3; #endif while (afec_init(AFEC0, &cfg) != STATUS_OK) { (void)afec_get_latest_value(AFEC0); } while (afec_init(AFEC1, &cfg) != STATUS_OK) { (void)afec_get_latest_value(AFEC1); } afec_disable_interrupt(AFEC0, AFEC_INTERRUPT_ALL); afec_disable_interrupt(AFEC1, AFEC_INTERRUPT_ALL); afec_set_trigger(AFEC0, AFEC_TRIG_SW); afec_set_trigger(AFEC1, AFEC_TRIG_SW); #endif }
/** * \brief Application entry point. * * \return Unused (ANSI-C compatibility). */ int main(void) { uint8_t uc_key; /* Initialize the SAM system. */ sysclk_init(); board_init(); configure_console(); /* Output example information. */ puts(STRING_HEADER); g_afec_sample_data.value = 0; g_afec_sample_data.is_done = false; g_max_digital = MAX_DIGITAL_12_BIT; afec_enable(AFEC0); struct afec_config afec_cfg; afec_get_config_defaults(&afec_cfg); afec_init(AFEC0, &afec_cfg); struct afec_ch_config afec_ch_cfg; afec_ch_get_config_defaults(&afec_ch_cfg); afec_ch_set_config(AFEC0, AFEC_CHANNEL_POTENTIOMETER, &afec_ch_cfg); /* * Because the internal ADC offset is 0x800, it should cancel it and shift * down to 0. */ afec_channel_set_analog_offset(AFEC0, AFEC_CHANNEL_POTENTIOMETER, 0x800); afec_set_trigger(AFEC0, AFEC_TRIG_SW); /* Enable channel for potentiometer. */ afec_channel_enable(AFEC0, AFEC_CHANNEL_POTENTIOMETER); afec_set_callback(AFEC0, AFEC_INTERRUPT_DATA_READY, afec_data_ready, 1); display_menu(); afec_start_calibration(AFEC0); while((afec_get_interrupt_status(AFEC0) & AFEC_ISR_EOCAL) != AFEC_ISR_EOCAL); while (1) { afec_start_software_conversion(AFEC0); delay_ms(1000); /* Check if AFEC sample is done. */ if (g_afec_sample_data.is_done == true) { printf("Potentiometer Voltage: %4d mv.", (int)(g_afec_sample_data.value * VOLT_REF / g_max_digital)); puts("\r"); g_afec_sample_data.is_done = false; } /* Check if the user enters a key. */ if (!uart_read(CONF_UART, &uc_key)) { /* Disable all afec interrupt. */ afec_disable_interrupt(AFEC0, AFEC_INTERRUPT_ALL); set_afec_resolution(); afec_enable_interrupt(AFEC0, AFEC_INTERRUPT_DATA_READY); } } }
/** * \brief Callback function for AFEC conversion data is ready interrupt. */ static void afec_set_data_ready_flag(void) { is_data_ready = true; afec_disable_interrupt(AFEC0, AFEC_INTERRUPT_DATA_READY); tc_stop(TC0, 0); }
/** * \brief Callback function for AFEC enter compasion window interrupt. */ static void afec_set_comp_flag(void) { is_comp_event_flag = true; afec_disable_interrupt(AFEC0, AFEC_INTERRUPT_COMP_ERROR); }