예제 #1
0
/** Read the input voltage, represented as a float in the range [0.0, 1.0]
 *
 * @param obj The analogin object
 * @return A floating value representing the current input voltage
 */
float analogin_read(analogin_t *obj)
{
    uint16_t reval;

    adc_regular_channel_config(obj->adc, 0, obj->channel, ADC_SAMPLETIME_7POINT5);

    adc_flag_clear(obj->adc, ADC_FLAG_EOC);
    /* start Conversion */
    adc_software_trigger_enable(obj->adc, ADC_REGULAR_CHANNEL);
    /* wait for conversion to complete */
    while (SET != adc_flag_get(obj->adc, ADC_FLAG_EOC)) {
    }
    /* ADC actual accuracy is 12 bits */
    reval = adc_regular_data_read(obj->adc);

    return (float)reval * (1.0f / (float)DEV_ADC_ACCURACY_12BIT);
}
예제 #2
0
/** Read the value from analogin pin, represented as an unsigned 16bit value
 *
 * @param obj The analogin object
 * @return An unsigned 16bit value representing the current input voltage
 */
uint16_t analogin_read_u16(analogin_t *obj)
{
    uint16_t reval;

    adc_regular_channel_config(obj->adc, 0, obj->channel, ADC_SAMPLETIME_7POINT5);

    adc_flag_clear(obj->adc, ADC_FLAG_EOC);
    /* start Conversion */
    adc_software_trigger_enable(obj->adc, ADC_REGULAR_CHANNEL);

    while (SET != adc_flag_get(obj->adc, ADC_FLAG_EOC)) {
    }
    /* ADC actual accuracy is 12 bits */
    reval = adc_regular_data_read(obj->adc);

    reval = DEV_ADC_PRECISION_12TO16(reval);

    return reval;
}
예제 #3
0
파일: analogin_api.c 프로젝트: pan-/mbed
/** Initialize the analogin peripheral
 *
 * Configures the pin used by analogin.
 * @param obj The analogin object to initialize
 * @param pin The analogin pin name
 */
void analogin_init(analogin_t *obj, PinName pin)
{
    uint32_t periph;

    MBED_ASSERT(obj);

    obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC);
    MBED_ASSERT(obj->adc != (ADCName)NC);

    uint32_t function = pinmap_function(pin, PinMap_ADC);
    MBED_ASSERT(function != (uint32_t)NC);

    obj->channel = GD_PIN_CHANNEL_GET(function);
    MBED_ASSERT(obj->channel <= ADC_CHANNEL_17);

    periph = obj->adc;
    /* save the pin for future use */
    obj->pin = pin;

    /* ADC clock enable and pin number reset */
    switch (periph) {
        case ADC0:
            rcu_periph_clock_enable(RCU_ADC0);
            break;

        case ADC1:
            rcu_periph_clock_enable(RCU_ADC1);
            /* reset pin number */
            pin = (PinName)(pin & AND_NUMBER);
            break;
    }

    /* ADC clock cannot be greater than 40M */
    rcu_adc_clock_config(RCU_CKADC_CKAPB2_DIV4);

    if ((ADC_CHANNEL_16 == obj->channel)) {
        /* ADC Vrefint enable */
        adc_tempsensor_vrefint_enable();
        /* set temperature sample flag */
        temperature_sample_flag = SET;
    }
    if ((ADC_CHANNEL_17 == obj->channel)) {
        /* ADC Vrefint enable */
        adc_tempsensor_vrefint_enable();
    }

    /* when pin >= ADC_TEMP, it indicates that the channel has no external pins */
    if (pin < ADC_TEMP) {
        pinmap_pinout(pin, PinMap_ADC);
    }

    /* ADC configuration */
    adc_special_function_config(obj->adc, ADC_SCAN_MODE, DISABLE);
    adc_special_function_config(obj->adc, ADC_CONTINUOUS_MODE, DISABLE);
    /* ADC trigger config */
    adc_external_trigger_source_config(obj->adc, ADC_REGULAR_CHANNEL, ADC0_1_EXTTRIG_REGULAR_NONE);
    /* ADC mode config */
    adc_mode_config(ADC_MODE_FREE);
    /* ADC data alignment config */
    adc_data_alignment_config(obj->adc, ADC_DATAALIGN_RIGHT);
    /* ADC channel length config */
    adc_channel_length_config(obj->adc, ADC_REGULAR_CHANNEL, 1);

    if (temperature_sample_flag == SET) {
        /* sample temperature needs more time */
        adc_regular_channel_config(obj->adc, 0, obj->channel, ADC_SAMPLETIME_239POINT5);
        /* clear temperature sample flag */
        temperature_sample_flag = RESET;
    } else {
        adc_regular_channel_config(obj->adc, 0, obj->channel, ADC_SAMPLETIME_28POINT5);
    }
    adc_external_trigger_config(obj->adc, ADC_REGULAR_CHANNEL, ENABLE);

    /* ADC enable */
    adc_enable(obj->adc);
    /* wait for ADC to stabilize */
    _delay(500);
    adc_calibration_enable(obj->adc);
}