// The voltage that a reading of 1 from `analogRead` actually represents JsVarFloat jshReadVRef() { #ifdef NRF52 nrf_saadc_channel_config_t config; config.acq_time = NRF_SAADC_ACQTIME_3US; config.gain = NRF_SAADC_GAIN1_6; // 1/6 of input volts config.mode = NRF_SAADC_MODE_SINGLE_ENDED; config.pin_p = NRF_SAADC_INPUT_VDD; config.pin_n = NRF_SAADC_INPUT_VDD; config.reference = NRF_SAADC_REFERENCE_INTERNAL; // 0.6v reference. config.resistor_p = NRF_SAADC_RESISTOR_DISABLED; config.resistor_n = NRF_SAADC_RESISTOR_DISABLED; // make reading nrf_saadc_enable(); nrf_saadc_resolution_set(NRF_SAADC_RESOLUTION_14BIT); nrf_saadc_channel_init(0, &config); return 6.0 * (nrf_analog_read() * 0.6 / 8192.0); #else const nrf_adc_config_t nrf_adc_config = { NRF_ADC_CONFIG_RES_10BIT, NRF_ADC_CONFIG_SCALING_INPUT_FULL_SCALE, NRF_ADC_CONFIG_REF_VBG }; // internal reference nrf_adc_configure( (nrf_adc_config_t *)&nrf_adc_config); return 1.2 / nrf_adc_convert_single(ADC_CONFIG_PSEL_AnalogInput0); #endif }
/**@brief Function for configuring ADC to do battery level conversion. */ static void adc_configure(void) { #ifdef NRF51 nrf_adc_config_t adc_config = NRF_ADC_CONFIG_DEFAULT; adc_config.scaling = NRF_ADC_CONFIG_SCALING_SUPPLY_ONE_THIRD; nrf_adc_configure(&adc_config); nrf_adc_int_enable(ADC_INTENSET_END_Msk); NVIC_EnableIRQ(ADC_IRQn); NVIC_SetPriority(ADC_IRQn, 3); nrf_adc_input_select(NRF_ADC_CONFIG_INPUT_DISABLED); #else // NRF52 ret_code_t err_code = nrf_drv_saadc_init(NULL, saadc_event_handler); APP_ERROR_CHECK(err_code); nrf_saadc_channel_config_t config = NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_VDD); err_code = nrf_drv_saadc_channel_init(0,&config); APP_ERROR_CHECK(err_code); err_code = nrf_drv_saadc_buffer_convert(&adc_buf[0],1); APP_ERROR_CHECK(err_code); err_code = nrf_drv_saadc_buffer_convert(&adc_buf[1],1); APP_ERROR_CHECK(err_code); #endif //NRF51 }
// The voltage that a reading of 1 from `analogRead` actually represents JsVarFloat jshReadVRef() { const nrf_adc_config_t nrf_adc_config = { NRF_ADC_CONFIG_RES_10BIT, NRF_ADC_CONFIG_SCALING_INPUT_FULL_SCALE, NRF_ADC_CONFIG_REF_VBG }; // internal reference nrf_adc_configure( (nrf_adc_config_t *)&nrf_adc_config); return 1.2 / nrf_adc_convert_single(ADC_CONFIG_PSEL_AnalogInput0); }
static void adc_config(void) { const nrf_adc_config_t nrf_adc_config = NRF_ADC_CONFIG_DEFAULT; nrf_adc_configure((nrf_adc_config_t *)&nrf_adc_config); nrf_adc_input_select(NRF_ADC_CONFIG_INPUT_4); nrf_adc_int_enable(ADC_INTENSET_END_Enabled << ADC_INTENSET_END_Pos); NVIC_SetPriority(ADC_IRQn, NRF_APP_PRIORITY_HIGH); NVIC_EnableIRQ(ADC_IRQn); }
/** * @brief ADC initialization. */ void adc_config(void) { const nrf_adc_config_t nrf_adc_config = NRF_ADC_CONFIG_DEFAULT; // Initialize and configure ADC nrf_adc_configure( (nrf_adc_config_t *)&nrf_adc_config); nrf_adc_input_select(NRF_ADC_CONFIG_INPUT_7); nrf_adc_int_enable(ADC_INTENSET_END_Enabled << ADC_INTENSET_END_Pos); NVIC_SetPriority(ADC_IRQn, NRF_APP_PRIORITY_HIGH); NVIC_EnableIRQ(ADC_IRQn); //printf("\n\rADC Configured Corectly\r\n"); }
/** * @brief ADC initialization. */ static void adc_initialize(void) { const nrf_adc_config_t nrf_adc_config = { NRF_ADC_CONFIG_RES_10BIT, NRF_ADC_CONFIG_SCALING_SUPPLY_ONE_THIRD, NRF_ADC_CONFIG_REF_VBG }; // Initialize and configure ADC nrf_adc_configure( (nrf_adc_config_t *)&nrf_adc_config); nrf_adc_int_enable(ADC_INTENSET_END_Enabled << ADC_INTENSET_END_Pos); NVIC_SetPriority(ADC_IRQn, APP_IRQ_PRIORITY_LOW); NVIC_EnableIRQ(ADC_IRQn); }
/// Returns a quickly-read analog value in the range 0-65535 int jshPinAnalogFast(Pin pin) { if (pinInfo[pin].analog == JSH_ANALOG_NONE) return 0; const nrf_adc_config_t nrf_adc_config = { NRF_ADC_CONFIG_RES_8BIT, // 8 bit for speed (hopefully!) NRF_ADC_CONFIG_SCALING_INPUT_FULL_SCALE, NRF_ADC_CONFIG_REF_VBG }; // internal reference nrf_adc_configure( (nrf_adc_config_t *)&nrf_adc_config); // sanity checks for nrf_adc_convert_single... assert(ADC_CONFIG_PSEL_AnalogInput0 == 1); assert(ADC_CONFIG_PSEL_AnalogInput1 == 2); assert(ADC_CONFIG_PSEL_AnalogInput2 == 4); // make reading return nrf_adc_convert_single(1 << (pinInfo[pin].analog & JSH_MASK_ANALOG_CH)) << 8; }
// Returns an analog value between 0 and 1 JsVarFloat jshPinAnalog(Pin pin) { if (pinInfo[pin].analog == JSH_ANALOG_NONE) return NAN; const nrf_adc_config_t nrf_adc_config = { NRF_ADC_CONFIG_RES_10BIT, NRF_ADC_CONFIG_SCALING_INPUT_FULL_SCALE, NRF_ADC_CONFIG_REF_VBG }; // internal reference nrf_adc_configure( (nrf_adc_config_t *)&nrf_adc_config); // sanity checks for nrf_adc_convert_single... assert(ADC_CONFIG_PSEL_AnalogInput0 == 1); assert(ADC_CONFIG_PSEL_AnalogInput1 == 2); assert(ADC_CONFIG_PSEL_AnalogInput2 == 4); // make reading return nrf_adc_convert_single(1 << (pinInfo[pin].analog & JSH_MASK_ANALOG_CH)) / 1024.0; }
/** * @brief ADC initialization. */ static void adc_config(void) { const nrf_adc_config_t nrf_adc_config = NRF_ADC_CONFIG_DEFAULT; // Initialize and configure ADC nrf_adc_configure( (nrf_adc_config_t *)&nrf_adc_config); #if defined(BOARD_BVMCN5102) nrf_adc_input_select(NRF_ADC_CONFIG_INPUT_3); #elif defined(BOARD_BLENANO) nrf_adc_input_select(NRF_ADC_CONFIG_INPUT_5); #else # error unknown board #endif nrf_adc_int_enable(ADC_INTENSET_END_Enabled << ADC_INTENSET_END_Pos); NVIC_SetPriority(ADC_IRQn, NRF_APP_PRIORITY_LOW); NVIC_EnableIRQ(ADC_IRQn); }
static void show_startup_screen(void) { char vbat_buff[BUFF_SZ+1] = {0}; nrf_adc_config_t adc_config = { .resolution = NRF_ADC_CONFIG_RES_10BIT, .scaling = NRF_ADC_CONFIG_SCALING_INPUT_FULL_SCALE, .reference = NRF_ADC_CONFIG_REF_VBG }; nrf_adc_configure(&adc_config); unsigned v = nrf_adc_convert_single(VBAT_SENSE_PIN); float vbat = vbat_adc_to_mv(v); glcd_print_str(0, 0, "waiting data ...", &g_font_Tahoma15x16, 1); snprintf(vbat_buff, BUFF_SZ, "Vbat=%.3f", vbat); glcd_print_str(0, 3, vbat_buff, &g_font_Tahoma15x16, 1); if (vbat < 3.2) { glcd_print_str(0, 6, "Low charge!", &g_font_Tahoma15x16, 1); nrf_delay_ms(1000); } nrf_delay_ms(1000); }
/// Returns a quickly-read analog value in the range 0-65535 int jshPinAnalogFast(Pin pin) { if (pinInfo[pin].analog == JSH_ANALOG_NONE) return 0; #ifdef NRF52 // sanity checks for channel assert(NRF_SAADC_INPUT_AIN0 == 1); assert(NRF_SAADC_INPUT_AIN1 == 2); assert(NRF_SAADC_INPUT_AIN2 == 3); nrf_saadc_input_t ain = 1 + (pinInfo[pin].analog & JSH_MASK_ANALOG_CH); nrf_saadc_channel_config_t config; config.acq_time = NRF_SAADC_ACQTIME_3US; config.gain = NRF_SAADC_GAIN1_4; // 1/4 of input volts config.mode = NRF_SAADC_MODE_SINGLE_ENDED; config.pin_p = ain; config.pin_n = ain; config.reference = NRF_SAADC_REFERENCE_VDD4; // VDD/4 as reference. config.resistor_p = NRF_SAADC_RESISTOR_DISABLED; config.resistor_n = NRF_SAADC_RESISTOR_DISABLED; // make reading nrf_saadc_enable(); nrf_saadc_resolution_set(NRF_SAADC_RESOLUTION_8BIT); nrf_saadc_channel_init(0, &config); return nrf_analog_read() << 8; #else const nrf_adc_config_t nrf_adc_config = { NRF_ADC_CONFIG_RES_8BIT, // 8 bit for speed (hopefully!) NRF_ADC_CONFIG_SCALING_INPUT_FULL_SCALE, NRF_ADC_CONFIG_REF_VBG }; // internal reference nrf_adc_configure( (nrf_adc_config_t *)&nrf_adc_config); // sanity checks for nrf_adc_convert_single... assert(ADC_CONFIG_PSEL_AnalogInput0 == 1); assert(ADC_CONFIG_PSEL_AnalogInput1 == 2); assert(ADC_CONFIG_PSEL_AnalogInput2 == 4); // make reading return nrf_adc_convert_single(1 << (pinInfo[pin].analog & JSH_MASK_ANALOG_CH)) << 8; #endif }
/**@brief Function for configuring ADC to do battery level conversion. */ static void adc_configure(void) { uint32_t err_code; nrf_adc_config_t adc_config = NRF_ADC_CONFIG_DEFAULT; // Configure ADC adc_config.reference = NRF_ADC_CONFIG_REF_VBG; adc_config.resolution = NRF_ADC_CONFIG_RES_8BIT; adc_config.scaling = NRF_ADC_CONFIG_SCALING_SUPPLY_ONE_THIRD; nrf_adc_configure(&adc_config); // Enable ADC interrupt nrf_adc_int_enable(ADC_INTENSET_END_Msk); err_code = sd_nvic_ClearPendingIRQ(ADC_IRQn); APP_ERROR_CHECK(err_code); err_code = sd_nvic_SetPriority(ADC_IRQn, NRF_APP_PRIORITY_LOW); APP_ERROR_CHECK(err_code); err_code = sd_nvic_EnableIRQ(ADC_IRQn); APP_ERROR_CHECK(err_code); }
void battery_init(void) { const nrf_adc_config_t nrf_adc_config = NRF_ADC_CONFIG; // Initialize and configure ADC nrf_adc_configure( (nrf_adc_config_t *)&nrf_adc_config); }