Test of ADC Pin to battery level on Coragem device
Revision 4:1afe75848f10, committed 2020-02-13
- Comitter:
- brunnobbco
- Date:
- Thu Feb 13 19:48:14 2020 +0000
- Parent:
- 3:50c27e4261e2
- Commit message:
- Teste ADC Coragem
Changed in this revision
diff -r 50c27e4261e2 -r 1afe75848f10 main.cpp --- a/main.cpp Tue Feb 07 08:55:05 2017 +0000 +++ b/main.cpp Thu Feb 13 19:48:14 2020 +0000 @@ -1,98 +1,96 @@ #include "mbed.h" -//DigitalOut led1(LED1); -DigitalOut led1(P0_13); //nRF52840 PDK io -DigitalOut led2(P0_14); -DigitalOut led3(P0_15); -DigitalOut led4(P0_16); - +#define MIN_BATTERY_ADC_VALUE (int16_t)3071 //(min_voltage_on_analog_pin *4551) 2.48*4551 = 11295 +#define MAX_BATTERY_ADC_VALUE (int16_t)4095 //(max_voltage_on_analog_pin *4551) 3.38*4551 = 15391 -InterruptIn button1(P0_11);//(USER_BUTTON nRF52840 PDK); -InterruptIn button2(P0_12); -InterruptIn button3(P0_24); -InterruptIn button4(P0_25); - -void button1_pressed() -{ - led1 = led2 =led3 =led4 = 1; - led1 = 0; - -} +//AnalogIn analog_value(P0_2); +//DigitalOut led(P1_13); -void button1_released() -{ - led1 = led2 =led3 =led4 = 1; - //led1 = 0; -} - -void button2_pressed() -{ - led1 = led2 =led3 =led4 = 1; - led2 = 0; - -} - -void button2_released() -{ - led1 = led2 =led3 =led4 = 1; - //led1 = 0; -} - -void button3_pressed() -{ - led1 = led2 =led3 =led4 = 1; - led3 = 0; - // wait(0.1); +void config_adc(void){ + + //Configure SAADC singled-ended channel, Internal reference (0.6V) and 1/5 gain. + NRF_SAADC->CH[0].CONFIG = (SAADC_CH_CONFIG_GAIN_Gain1_5 << SAADC_CH_CONFIG_GAIN_Pos) | + (SAADC_CH_CONFIG_MODE_SE << SAADC_CH_CONFIG_MODE_Pos) | + (SAADC_CH_CONFIG_REFSEL_Internal << SAADC_CH_CONFIG_REFSEL_Pos) | + (SAADC_CH_CONFIG_RESN_Bypass << SAADC_CH_CONFIG_RESN_Pos) | + (SAADC_CH_CONFIG_RESP_Bypass << SAADC_CH_CONFIG_RESP_Pos) | + (SAADC_CH_CONFIG_TACQ_10us << SAADC_CH_CONFIG_TACQ_Pos); + + + // Configure the SAADC channel with VDD as a positive input, no negative input(single ended). + NRF_SAADC->CH[0].PSELP = SAADC_CH_PSELN_PSELN_AnalogInput0 << SAADC_CH_PSELN_PSELN_Pos ;//changed pin number to AIN7 + NRF_SAADC->CH[0].PSELN = SAADC_CH_PSELN_PSELN_NC << SAADC_CH_PSELN_PSELN_Pos; + + //Configure the SAADC resolution. + NRF_SAADC->RESOLUTION = SAADC_RESOLUTION_VAL_12bit << SAADC_RESOLUTION_VAL_Pos; + + // No automatic sampling, will trigger with TASKS_SAMPLE. + NRF_SAADC->SAMPLERATE = SAADC_SAMPLERATE_MODE_Task << SAADC_SAMPLERATE_MODE_Pos; + + //Enable SAADC (would capture analog pins if they were used in CH[0].PSELP) + NRF_SAADC->ENABLE = SAADC_ENABLE_ENABLE_Enabled << SAADC_ENABLE_ENABLE_Pos; } -void button3_released() -{ - led1 = led2 =led3 =led4 = 1; - //led1 = 0; -} +float battery_status(void){ + + int16_t result = 0; + float precise_result = 0; + + // Configure result to be put in RAM at the location of "result" variable. + NRF_SAADC->RESULT.MAXCNT = 1; + NRF_SAADC->RESULT.PTR = (uint32_t)&result; -void button4_pressed() -{ - led1 = led2 =led3 =led4 = 1; - led4 = 0; - -} - -void button4_released() -{ - led1 = led2 =led3 =led4 = 1; - //led1 = 0; -} + // Calibrate the SAADC (only needs to be done once in a while) + NRF_SAADC->TASKS_CALIBRATEOFFSET = 1; + while (NRF_SAADC->EVENTS_CALIBRATEDONE == 0); + NRF_SAADC->EVENTS_CALIBRATEDONE = 0; + + // Start the SAADC and wait for the started event. + NRF_SAADC->TASKS_START = 1; + while (NRF_SAADC->EVENTS_STARTED == 0); + NRF_SAADC->EVENTS_STARTED = 0; + + // Do a SAADC sample, will put the result in the configured RAM buffer. + NRF_SAADC->TASKS_SAMPLE = 1; + while (NRF_SAADC->EVENTS_END == 0); + NRF_SAADC->EVENTS_END = 0; + + precise_result = (float)(result/4551); + + // Stop the SAADC, since it's not used anymore. + NRF_SAADC->TASKS_STOP = 1; + while (NRF_SAADC->EVENTS_STOPPED == 0); + NRF_SAADC->EVENTS_STOPPED = 0; + NVIC_ClearPendingIRQ(SAADC_IRQn); + return precise_result; +} -// main() runs in its own thread in the OS -// (note the calls to Thread::wait below for delays) +int main() +{ -int main() { - led1 = led2 =led3 =led4 = 0; + config_adc(); + float result_1; - button1.fall(&button1_pressed); - button1.rise(&button1_released); + printf("\nAnalogIn example\n"); - button2.fall(&button2_pressed); - button2.rise(&button2_released); + while(1) { - button3.fall(&button3_pressed); - button3.rise(&button3_released); - - button4.fall(&button4_pressed); - button4.rise(&button4_released); +// meas_r = analog_value.read(); // Read the analog input value (value from 0.0 to 1.0 = full ADC conversion range) +// meas_v = meas_r * 3300; // Converts value in the 0V-3.3V range + + // Display values +// printf("measure = %f = %.0f mV\n", meas_r, meas_v); - - while (true) { - // led1 = !led1; - // wait(0.5); - // led2 = !led2; - // wait(0.5); - // led3 = !led3; - // wait(0.5); - // led4 = !led4; - // wait(0.5); + // LED is ON is the value is below 1V +// if (meas_v < 1000) { +// led = 1; // LED ON +// } else { +// led = 0; // LED OFF +// } +// printf("debug before bat_status routine"); + result_1=battery_status(); + printf("bat_lev: \n%f\n", result_1); + wait(1.0); // 1 second } } -
diff -r 50c27e4261e2 -r 1afe75848f10 mbed-os.lib --- a/mbed-os.lib Tue Feb 07 08:55:05 2017 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -https://github.com/ARMmbed/mbed-os/#269f58d75b752a4e67a6a2d8c5c698635ffd6752
diff -r 50c27e4261e2 -r 1afe75848f10 mbed_app.json --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed_app.json Thu Feb 13 19:48:14 2020 +0000 @@ -0,0 +1,31 @@ +{ + + "target_overrides": { + "K64F": { + "target.features_add": ["BLE"], + "target.extra_labels_add": ["CORDIO", "CORDIO_BLUENRG"] + }, + "NUCLEO_F401RE": { + "target.features_add": ["BLE"], + "target.extra_labels_add": ["CORDIO", "CORDIO_BLUENRG"] + }, + "DISCO_L475VG_IOT01A": { + "target.features_add": ["BLE"], + "target.extra_labels_add": ["CORDIO", "CORDIO_BLUENRG"] + }, + "NRF52840_DK": { + "target.features_add": ["BLE"], + "target.extra_labels_add": ["CORDIO", "CORDIO_LL", "SOFTDEVICE_N ONE", "NORDIC_CORDIO"], + "target.extra_labels_remove": ["SOFTDEVICE_COMMON", "SOFTDEVICE_S140_FULL", "NORDIC_SOFTDEVICE"], + "target.lf_clock_src": "NRF_LF_SRC_RC", + "target.lf_clock_rc_calib_timer_interval": 16, + "target.lf_clock_rc_calib_mode_config": 2 + + }, + "NRF52_DK": { + "target.features_add": ["BLE"], + "target.extra_labels_add": ["CORDIO", "CORDIO_LL", "SOFTDEVICE_NONE", "NORDIC_CORDIO"], + "target.extra_labels_remove": ["SOFTDEVICE_COMMON", "SOFTDEVICE_S132_FULL", "NORDIC_SOFTDEVICE"] + } + } +} \ No newline at end of file
diff -r 50c27e4261e2 -r 1afe75848f10 mbed_coragem.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed_coragem.lib Thu Feb 13 19:48:14 2020 +0000 @@ -0,0 +1,1 @@ +https://github.com/leiachewbacca/mbed_coragem.git/#b8dad0652c9cd2321126a590280a18a964e1eed0