Update voltage measurement. Change ADC config setting.

Dependencies:   BLE_API mbed nRF51822 X_NUCLEO_IDB0XA1

Fork of BLE_BatteryLevel_ADC_170608 by Jun ADI

Committer:
Jun_adi
Date:
Mon Jul 24 02:05:26 2017 +0000
Revision:
22:ad0c52ec5bf9
Parent:
21:245543d2eb2c
Edit the "ADC read" code.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Rohit Grover 4:5b64235d1b85 1 /* mbed Microcontroller Library
rgrover1 12:4024aa3f72b0 2 * Copyright (c) 2006-2014 ARM Limited
Rohit Grover 4:5b64235d1b85 3 *
Rohit Grover 4:5b64235d1b85 4 * Licensed under the Apache License, Version 2.0 (the "License");
Rohit Grover 4:5b64235d1b85 5 * you may not use this file except in compliance with the License.
Rohit Grover 4:5b64235d1b85 6 * You may obtain a copy of the License at
Rohit Grover 4:5b64235d1b85 7 *
Rohit Grover 4:5b64235d1b85 8 * http://www.apache.org/licenses/LICENSE-2.0
Rohit Grover 4:5b64235d1b85 9 *
Rohit Grover 4:5b64235d1b85 10 * Unless required by applicable law or agreed to in writing, software
Rohit Grover 4:5b64235d1b85 11 * distributed under the License is distributed on an "AS IS" BASIS,
Rohit Grover 4:5b64235d1b85 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Rohit Grover 4:5b64235d1b85 13 * See the License for the specific language governing permissions and
Rohit Grover 4:5b64235d1b85 14 * limitations under the License.
Rohit Grover 4:5b64235d1b85 15 */
Rohit Grover 4:5b64235d1b85 16
Rohit Grover 4:5b64235d1b85 17 #include "mbed.h"
rgrover1 16:5cdd04cf1ed4 18 #include "BLE.h"
rgrover1 8:45beed07b093 19 #include "BatteryService.h"
Rohit Grover 4:5b64235d1b85 20
rgrover1 12:4024aa3f72b0 21 DigitalOut led1(LED1, 1);
rgrover1 12:4024aa3f72b0 22 Ticker t;
vcoubard 17:5afb0e5a48fc 23 BatteryService *batteryService = NULL;
Jun_adi 20:e7bc34a4a3db 24
Jun_adi 20:e7bc34a4a3db 25 //uint8_t batteryLevel = 50;
Jun_adi 21:245543d2eb2c 26 uint8_t batteryLevel = 0; // Change initial value of batteryLevel
Jun_adi 20:e7bc34a4a3db 27
Jun_adi 20:e7bc34a4a3db 28 //Pin setting for Analog input
Jun_adi 20:e7bc34a4a3db 29 AnalogIn analogin(P0_4);
Rohit Grover 4:5b64235d1b85 30
vcoubard 17:5afb0e5a48fc 31 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *disconnectionParams)
Rohit Grover 4:5b64235d1b85 32 {
vcoubard 17:5afb0e5a48fc 33 printf("Disconnected handle %u!\n\r", disconnectionParams->handle);
rgrover1 12:4024aa3f72b0 34 printf("Restarting the advertising process\n\r");
vcoubard 17:5afb0e5a48fc 35 BLE::Instance(BLE::DEFAULT_INSTANCE).gap().startAdvertising(); // restart advertising
Rohit Grover 4:5b64235d1b85 36 }
Rohit Grover 4:5b64235d1b85 37
rgrover1 12:4024aa3f72b0 38 void blink(void)
Rohit Grover 4:5b64235d1b85 39 {
rgrover1 12:4024aa3f72b0 40 led1 = !led1;
Rohit Grover 4:5b64235d1b85 41 }
Rohit Grover 4:5b64235d1b85 42
vcoubard 17:5afb0e5a48fc 43 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
vcoubard 17:5afb0e5a48fc 44 {
vcoubard 17:5afb0e5a48fc 45 BLE &ble = params->ble;
vcoubard 17:5afb0e5a48fc 46 ble_error_t error = params->error;
vcoubard 17:5afb0e5a48fc 47 Gap& gap = ble.gap();
vcoubard 17:5afb0e5a48fc 48
vcoubard 17:5afb0e5a48fc 49 if (error != BLE_ERROR_NONE) {
vcoubard 17:5afb0e5a48fc 50 return;
vcoubard 17:5afb0e5a48fc 51 }
vcoubard 17:5afb0e5a48fc 52
vcoubard 17:5afb0e5a48fc 53 gap.onDisconnection(disconnectionCallback);
vcoubard 17:5afb0e5a48fc 54
vcoubard 17:5afb0e5a48fc 55 batteryService = new BatteryService(ble, batteryLevel);
vcoubard 17:5afb0e5a48fc 56
vcoubard 17:5afb0e5a48fc 57 /* setup advertising */
vcoubard 17:5afb0e5a48fc 58 gap.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
vcoubard 17:5afb0e5a48fc 59 gap.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
vcoubard 17:5afb0e5a48fc 60 gap.setAdvertisingInterval(1000); /* 1000ms; in multiples of 0.625ms. */
vcoubard 17:5afb0e5a48fc 61 gap.startAdvertising();
vcoubard 17:5afb0e5a48fc 62 }
vcoubard 17:5afb0e5a48fc 63
Jun_adi 21:245543d2eb2c 64 void adc_init_setting() //ADC setting
Jun_adi 21:245543d2eb2c 65 {
Jun_adi 21:245543d2eb2c 66 NRF_ADC->ENABLE = ADC_ENABLE_ENABLE_Enabled;
Jun_adi 21:245543d2eb2c 67 NRF_ADC->CONFIG = (ADC_CONFIG_RES_10bit << ADC_CONFIG_RES_Pos) |
Jun_adi 21:245543d2eb2c 68 (ADC_CONFIG_INPSEL_AnalogInputOneThirdPrescaling << ADC_CONFIG_INPSEL_Pos) |
Jun_adi 21:245543d2eb2c 69 (ADC_CONFIG_REFSEL_VBG << ADC_CONFIG_REFSEL_Pos) |
Jun_adi 21:245543d2eb2c 70 (ADC_CONFIG_EXTREFSEL_None << ADC_CONFIG_EXTREFSEL_Pos);
Jun_adi 21:245543d2eb2c 71 }
Jun_adi 21:245543d2eb2c 72
Rohit Grover 4:5b64235d1b85 73 int main(void)
Rohit Grover 4:5b64235d1b85 74 {
Jun_adi 21:245543d2eb2c 75 adc_init_setting(); //ADC setting
rgrover1 12:4024aa3f72b0 76 t.attach(blink, 1.0f);
Rohit Grover 4:5b64235d1b85 77
rgrover1 12:4024aa3f72b0 78 printf("Initialising the nRF51822\n\r");
rgrover1 12:4024aa3f72b0 79
vcoubard 17:5afb0e5a48fc 80 BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE);
vcoubard 17:5afb0e5a48fc 81 ble.init(bleInitComplete);
rgrover1 13:60e095fe4b45 82
vcoubard 17:5afb0e5a48fc 83 /* SpinWait for initialization to complete. This is necessary because the
vcoubard 17:5afb0e5a48fc 84 * BLE object is used in the main loop below. */
vcoubard 17:5afb0e5a48fc 85 while (ble.hasInitialized() == false) { /* spin loop */ }
Rohit Grover 4:5b64235d1b85 86
Rohit Grover 4:5b64235d1b85 87 while (true) {
rgrover1 12:4024aa3f72b0 88 ble.waitForEvent(); // this will return upon any system event (such as an interrupt or a ticker wakeup)
Jun_adi 20:e7bc34a4a3db 89
Jun_adi 21:245543d2eb2c 90 //batteryLevel = analogin.read()*100;
Jun_adi 22:ad0c52ec5bf9 91
Jun_adi 22:ad0c52ec5bf9 92 //Read Anlog input voltage
Jun_adi 22:ad0c52ec5bf9 93 batteryLevel = analogin.read()* 1.2 * 3 * 10;
Jun_adi 20:e7bc34a4a3db 94 batteryService->updateBatteryLevel(batteryLevel);
Jun_adi 20:e7bc34a4a3db 95 }
Jun_adi 22:ad0c52ec5bf9 96
Jun_adi 20:e7bc34a4a3db 97 /*
rgrover1 12:4024aa3f72b0 98 // the magic battery processing
rgrover1 12:4024aa3f72b0 99 batteryLevel++;
rgrover1 12:4024aa3f72b0 100 if (batteryLevel > 100) {
rgrover1 12:4024aa3f72b0 101 batteryLevel = 20;
rgrover1 12:4024aa3f72b0 102 }
rgrover1 12:4024aa3f72b0 103
vcoubard 17:5afb0e5a48fc 104 batteryService->updateBatteryLevel(batteryLevel);
Jun_adi 22:ad0c52ec5bf9 105 }
Jun_adi 20:e7bc34a4a3db 106 */
Rohit Grover 4:5b64235d1b85 107 }