Exemplo n.º 1
0
/**
 * \brief AFEC(ADC) configuration.
 */
static void dsp_configure_adc(void)
{
	pmc_enable_periph_clk(ID_AFEC0);

	struct afec_config afec_cfg;
	struct afec_ch_config afec_ch_cfg;

	/* AFEC0 enable, init and configuration*/
	afec_enable(AFEC0);
	afec_get_config_defaults(&afec_cfg);
	afec_init(AFEC0, &afec_cfg);

	/* Set to Software trigger*/
	afec_set_trigger(AFEC0, AFEC_TRIG_SW);

	/* AFEC0 channals configuration*/
	afec_channel_enable(AFEC0,ADC_CHANNEL_POTENTIOMETER);
	afec_ch_get_config_defaults(&afec_ch_cfg);
	afec_ch_set_config(AFEC0, ADC_CHANNEL_POTENTIOMETER,&afec_ch_cfg);
	afec_ch_set_config(AFEC0, AFEC_TEMPERATURE_SENSOR, &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, ADC_CHANNEL_POTENTIOMETER, 0x800);
	afec_channel_set_analog_offset(AFEC0, AFEC_TEMPERATURE_SENSOR, 0x800);

	afec_channel_enable(AFEC0,AFEC_TEMPERATURE_SENSOR);
	afec_channel_enable(AFEC0,ADC_CHANNEL_MICROPHONE);

	struct afec_temp_sensor_config afec_temp_sensor_cfg;

	afec_temp_sensor_get_config_defaults(&afec_temp_sensor_cfg);
	afec_temp_sensor_cfg.rctc = true;
	afec_temp_sensor_set_config(AFEC0, &afec_temp_sensor_cfg);

	/* Perform an auto cab on the ADC Channel 4. */
	afec_start_calibration(AFEC0);
	while((afec_get_interrupt_status(AFEC0) & AFEC_ISR_EOCAL) !=
			AFEC_ISR_EOCAL);

	/* Enable potentiometer channel, disable microphone. */
	afec_channel_disable(AFEC0, ADC_CHANNEL_MICROPHONE);
	afec_channel_enable(AFEC0, AFEC_TEMPERATURE_SENSOR);
	afec_channel_enable(AFEC0, ADC_CHANNEL_MICROPHONE);

	/* Start the first conversion*/
	afec_start_software_conversion(AFEC0);
}
Exemplo n.º 2
0
// Enable or disable a channel. Use AnalogCheckReady to make sure the ADC is ready before calling this.
void AnalogInEnableChannel(AnalogChannelNumber channel, bool enable)
{
	if ((unsigned int)channel < numChannels)
	{
		if (enable)
		{
			activeChannels |= (1u << channel);
#if SAM3XA || SAM4S
			adc_enable_channel(ADC, GetAdcChannel(channel));
#if SAM4S
			adc_set_calibmode(ADC);										// auto calibrate at start of next sequence
#endif
			if (GetAdcChannel(channel) == ADC_TEMPERATURE_SENSOR)
			{
				adc_enable_ts(ADC);
			}
#elif SAM4E || SAME70
			afec_ch_config cfg;
			afec_ch_get_config_defaults(&cfg);
			afec_ch_set_config(GetAfec(channel), GetAfecChannel(channel), &cfg);
			afec_channel_set_analog_offset(GetAfec(channel), GetAfecChannel(channel), 2048);	// need this to get the full ADC range
			afec_channel_enable(GetAfec(channel), GetAfecChannel(channel));
#if SAM4E
			afec_start_calibration(GetAfec(channel));					// do automatic calibration
#endif
#endif
		}
		else
		{
			activeChannels &= ~(1u << channel);
#if SAM3XA || SAM4S
			adc_disable_channel(ADC, GetAdcChannel(channel));
			if (GetAdcChannel(channel) == ADC_TEMPERATURE_SENSOR)
			{
				adc_disable_ts(ADC);
			}
#elif SAM4E || SAME70
			afec_channel_disable(GetAfec(channel), GetAfecChannel(channel));
#endif
		}
	}
}