Beispiel #1
0
/**@brief Function for application main entry.
 */
int main(void)
{
    uint32_t err_code;
    bool erase_bonds;

    // Initialize.
    timers_init();
    buttons_leds_init(&erase_bonds);
    ble_stack_init();
    device_manager_init(erase_bonds);
    gap_params_init();
    advertising_init();
    services_init();
    conn_params_init();
    saadc_init();

    // Start execution.
    application_timers_start();
    err_code = ble_advertising_start(BLE_ADV_MODE_FAST);
    APP_ERROR_CHECK(err_code);

    // Enter main loop.
    for (;;)
    {
       			  // trigger next ADC conversion
        nrf_drv_saadc_sample_convert(0,&adc_sample);
        advdata.p_manuf_specific_data->data.p_data[0] = adc_sample/100 + '0';
        advdata.p_manuf_specific_data->data.p_data[1] = adc_sample%100/10 + '0';
        advdata.p_manuf_specific_data->data.p_data[2] = adc_sample%10 + '0';
        nrf_delay_ms(100);
        ble_advdata_set(&advdata, NULL);
    }
}
Beispiel #2
0
/**@brief Function for application main entry.
 */
int main(void)
{
    uint32_t err_code;
    
    // Initialize
    timers_init();
    ble_stack_init();
    bsp_module_init();
    scheduler_init();
    gap_params_init();
    advertising_init();
    services_init();
    conn_params_init();
    sec_params_init();
    
    adc_init();

    // Start execution
    timers_start();
    err_code = ble_advertising_start(BLE_ADV_MODE_FAST);
    APP_ERROR_CHECK(err_code);
    // Enter main loop
    
    for (;;)
    {
        if(start_adc_sampling == true)
        {
            nrf_drv_saadc_sample_convert(0, &adc_value);
            ble_android_characteristic_update(&m_android, &adc_value);
            start_adc_sampling = false;
        }
        app_sched_execute();
//        power_manage();
    }
}
Beispiel #3
0
uint16_t analogin_read_u16(analogin_t *obj)
{
    int16_t    adc_value;
    ret_code_t ret_code;
    
    ret_code = nrf_drv_saadc_sample_convert(obj->adc_pin, &adc_value);
    MBED_ASSERT(ret_code == NRF_SUCCESS);
    
    if (adc_value < 0)
    {
        // Even in the single ended mode measured value can be {-0}. Saturation for avoid casting to a big integer.
        return 0;
    }
    else
    {
        return (uint16_t) adc_value;
    }
}
Beispiel #4
0
/** 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)
{    
    MBED_ASSERT(obj);

    /* Only initialize SAADC on first pin. */
    static bool first_init = true;

    if (first_init) {

        first_init = false;

        /* Use configuration from sdk_config.h.
         * Default is: 
         *  - 12 bit.
         *  - No oversampling.
         *  - Priority 7 (lowest).
         *  - No low power mode.
         */
        nrf_drv_saadc_config_t adc_config = {
            .resolution         = (nrf_saadc_resolution_t)SAADC_CONFIG_RESOLUTION,
            .oversample         = (nrf_saadc_oversample_t)SAADC_CONFIG_OVERSAMPLE,
            .interrupt_priority = SAADC_CONFIG_IRQ_PRIORITY,
            .low_power_mode     = SAADC_CONFIG_LP_MODE
        };

        ret_code_t result = nrf_drv_saadc_init(&adc_config, analog_in_event_handler);
        MBED_ASSERT(result == NRF_SUCCESS);

        /* Register interrupt handler in vector table. */
        NVIC_SetVector(SAADC_IRQn, (uint32_t)SAADC_IRQHandler);
    }

    /* Use pinmap function to get associated channel. */
    uint32_t channel = pinmap_function(pin, PinMap_ADC);
    MBED_ASSERT(channel != (uint32_t) NC);

    /* Account for an off-by-one in Channel definition and Input definition. */
    nrf_saadc_input_t input = channel + 1;

    /* Configure channel and pin:
     *  - the 1/4 gain and VDD/4 makes the reference voltage VDD.
     */
    nrf_saadc_channel_config_t channel_config = {
        .resistor_p = NRF_SAADC_RESISTOR_DISABLED,
        .resistor_n = NRF_SAADC_RESISTOR_DISABLED,
        .gain       = NRF_SAADC_GAIN1_4,
        .reference  = NRF_SAADC_REFERENCE_VDD4,
        .acq_time   = NRF_SAADC_ACQTIME_10US,
        .mode       = NRF_SAADC_MODE_SINGLE_ENDED,
        .burst      = NRF_SAADC_BURST_DISABLED,
        .pin_p      = input,
        .pin_n      = NRF_SAADC_INPUT_DISABLED
    };

    ret_code_t result = nrf_drv_saadc_channel_init(channel, &channel_config);
    MBED_ASSERT(result == NRF_SUCCESS);

    /* Store channel in ADC object. */
    obj->channel = channel;
}


/** 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
 */
uint16_t analogin_read_u16(analogin_t *obj)
{    
    MBED_ASSERT(obj);

    /* Default return value is 0. */
    uint16_t retval = 0;
    
    /* Read single channel, blocking. */
    nrf_saadc_value_t value = { 0 };
    ret_code_t result = nrf_drv_saadc_sample_convert(obj->channel, &value);

    /* nrf_saadc_value_t is a signed integer. Only take the absolute value. */
    if ((result == NRF_SUCCESS) && (value > 0)) {

        /* Normalize 12 bit ADC value to 16 bit Mbed ADC range. */
        uint32_t normalized = value;
        retval = (normalized * ADC_16BIT_RANGE) / ADC_12BIT_RANGE;
    }

    return retval;
}