battery_monitor_code

Dependencies:   MAX30003 USBDevice

Committer:
kidecha_rahul
Date:
Tue Oct 19 16:14:08 2021 +0000
Revision:
2:28250fbcad1e
Parent:
1:4510fb7c9621
battery_monitor_code

Who changed what in which revision?

UserRevisionLine numberNew contents of line
phonemacro 0:76206161b30f 1 /*******************************************************************************
phonemacro 0:76206161b30f 2 * Copyright (C) 2018 Maxim Integrated Products, Inc., All Rights Reserved.
phonemacro 0:76206161b30f 3 *
phonemacro 0:76206161b30f 4 * Permission is hereby granted, free of charge, to any person obtaining a
phonemacro 0:76206161b30f 5 * copy of this software and associated documentation files (the "Software"),
phonemacro 0:76206161b30f 6 * to deal in the Software without restriction, including without limitation
phonemacro 0:76206161b30f 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
phonemacro 0:76206161b30f 8 * and/or sell copies of the Software, and to permit persons to whom the
phonemacro 0:76206161b30f 9 * Software is furnished to do so, subject to the following conditions:
phonemacro 0:76206161b30f 10 *
phonemacro 0:76206161b30f 11 * The above copyright notice and this permission notice shall be included
phonemacro 0:76206161b30f 12 * in all copies or substantial portions of the Software.
phonemacro 0:76206161b30f 13 *
phonemacro 0:76206161b30f 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
phonemacro 0:76206161b30f 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
phonemacro 0:76206161b30f 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
phonemacro 0:76206161b30f 17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
phonemacro 0:76206161b30f 18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
phonemacro 0:76206161b30f 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
phonemacro 0:76206161b30f 20 * OTHER DEALINGS IN THE SOFTWARE.
phonemacro 0:76206161b30f 21 *
phonemacro 0:76206161b30f 22 * Except as contained in this notice, the name of Maxim Integrated
phonemacro 0:76206161b30f 23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
phonemacro 0:76206161b30f 24 * Products, Inc. Branding Policy.
phonemacro 0:76206161b30f 25 *
phonemacro 0:76206161b30f 26 * The mere transfer of this software does not imply any licenses
phonemacro 0:76206161b30f 27 * of trade secrets, proprietary technology, copyrights, patents,
phonemacro 0:76206161b30f 28 * trademarks, maskwork rights, or any other form of intellectual
phonemacro 0:76206161b30f 29 * property whatsoever. Maxim Integrated Products, Inc. retains all
phonemacro 0:76206161b30f 30 * ownership rights.
phonemacro 0:76206161b30f 31 *******************************************************************************
phonemacro 0:76206161b30f 32 */
phonemacro 0:76206161b30f 33 #include "mbed.h"
phonemacro 0:76206161b30f 34 #include "MAX14690.h"
phonemacro 0:76206161b30f 35 #include "max32630fthr.h"
phonemacro 0:76206161b30f 36 #include "USBSerial.h"
phonemacro 0:76206161b30f 37
phonemacro 0:76206161b30f 38 MAX32630FTHR pegasus(MAX32630FTHR::VIO_3V3);
phonemacro 0:76206161b30f 39
phonemacro 0:76206161b30f 40 DigitalOut rLED(LED1);
phonemacro 0:76206161b30f 41 DigitalOut gLED(LED2);
phonemacro 0:76206161b30f 42 DigitalOut bLED(LED3);
phonemacro 0:76206161b30f 43
kidecha_rahul 1:4510fb7c9621 44 //AnalogIn ain_0(AIN_0);
kidecha_rahul 1:4510fb7c9621 45 //AnalogIn ain_1(AIN_1);
kidecha_rahul 1:4510fb7c9621 46 AnalogIn ain_4(AIN_4);
kidecha_rahul 1:4510fb7c9621 47 AnalogIn ain_5(AIN_5);
kidecha_rahul 1:4510fb7c9621 48
kidecha_rahul 1:4510fb7c9621 49 const float battery_max = 4.20; //maximum voltage of battery
kidecha_rahul 1:4510fb7c9621 50 const float battery_min = 3.0; //minimum voltage of battery before shutdown
kidecha_rahul 1:4510fb7c9621 51 uint8_t output = 0;
kidecha_rahul 1:4510fb7c9621 52
kidecha_rahul 1:4510fb7c9621 53 //DigitalIn vref(P1_4);
kidecha_rahul 1:4510fb7c9621 54 //I2C i2cBus(I2C2_SDA, I2C2_SCL); // P5_7, P6_0
phonemacro 0:76206161b30f 55
kidecha_rahul 1:4510fb7c9621 56 //MAX14690 max14690(&i2cBus);
phonemacro 0:76206161b30f 57
phonemacro 0:76206161b30f 58 // main() runs in its own thread in the OS
phonemacro 0:76206161b30f 59 // (note the calls to Thread::wait below for delays)
phonemacro 0:76206161b30f 60 /**
phonemacro 0:76206161b30f 61 * @brief Sample main program for MAX14690
phonemacro 0:76206161b30f 62 * @version 1.0000.0
phonemacro 0:76206161b30f 63 *
phonemacro 0:76206161b30f 64 * @details Sample main program for MAX14690
phonemacro 0:76206161b30f 65 * The prints are sent to the terminal window (9600, 8n1).
phonemacro 0:76206161b30f 66 * The program initializeds the LED to blue-green.
phonemacro 0:76206161b30f 67 * If the program is successful at initializing the PMIC
phonemacro 0:76206161b30f 68 * then it prints out a successful message, and if not, then
phonemacro 0:76206161b30f 69 * it turns on the red LED and prints an error message.
phonemacro 0:76206161b30f 70 * To run the program, drag and drop the .bin file into the
phonemacro 0:76206161b30f 71 * DAPLINK folder. After it finishes flashing, cycle the power or
phonemacro 0:76206161b30f 72 * reset the Pegasus after flashing by pressing the button on
phonemacro 0:76206161b30f 73 * the Pegasus next to the battery connector or the button
phonemacro 0:76206161b30f 74 * on the MAXREFDES100HDK.
phonemacro 0:76206161b30f 75 */
phonemacro 0:76206161b30f 76 int main()
phonemacro 0:76206161b30f 77 {
phonemacro 0:76206161b30f 78 DigitalOut rLED(LED1, LED_OFF);
phonemacro 0:76206161b30f 79 DigitalOut gLED(LED2, LED_OFF);
phonemacro 0:76206161b30f 80 DigitalOut bLED(LED3, LED_OFF);
phonemacro 0:76206161b30f 81
kidecha_rahul 1:4510fb7c9621 82 Serial pc(P3_1, P3_0); // Use USB debug probe for serial link
kidecha_rahul 1:4510fb7c9621 83 pc.baud(115200); // Baud rate = 115200
kidecha_rahul 1:4510fb7c9621 84
phonemacro 0:76206161b30f 85 bLED = LED_ON;
phonemacro 0:76206161b30f 86 gLED = LED_ON;
phonemacro 0:76206161b30f 87
kidecha_rahul 1:4510fb7c9621 88 /*i2cBus.frequency(400000);
phonemacro 0:76206161b30f 89
phonemacro 0:76206161b30f 90 max14690.ldo2Millivolts = 3300;
phonemacro 0:76206161b30f 91 max14690.ldo3Millivolts = 3300;
phonemacro 0:76206161b30f 92 max14690.ldo2Mode = MAX14690::LDO_ENABLED;
phonemacro 0:76206161b30f 93 max14690.ldo3Mode = MAX14690::LDO_ENABLED;
kidecha_rahul 1:4510fb7c9621 94 max14690.monCfg = MAX14690::MON_BAT;
phonemacro 0:76206161b30f 95 if (max14690.init() == MAX14690_ERROR) {
kidecha_rahul 1:4510fb7c9621 96 pc.printf("Error initializing MAX14690.\r\n");
phonemacro 0:76206161b30f 97 bLED = LED_OFF;
phonemacro 0:76206161b30f 98 gLED = LED_OFF;
phonemacro 0:76206161b30f 99 rLED = LED_ON;
phonemacro 0:76206161b30f 100 } else {
kidecha_rahul 1:4510fb7c9621 101 pc.printf("MAX14690 PMIC initialization is succesful.\r\n");
kidecha_rahul 1:4510fb7c9621 102 gLED = LED_OFF;
kidecha_rahul 1:4510fb7c9621 103 }*/
kidecha_rahul 1:4510fb7c9621 104 while(1)
kidecha_rahul 1:4510fb7c9621 105 {
kidecha_rahul 1:4510fb7c9621 106 pc.printf("MAX14690 PMIC initialization is Done.\r\n");
kidecha_rahul 1:4510fb7c9621 107 // pc.printf("analog read 0 %3.4f\n", ain_4.read());
kidecha_rahul 1:4510fb7c9621 108 // pc.printf("percentage: %3.3f%%\n", ain_4.read() * 100.0f);
kidecha_rahul 1:4510fb7c9621 109 // pc.printf("normalized: 0x%04X \n", ain_4.read_u16());
kidecha_rahul 1:4510fb7c9621 110
kidecha_rahul 1:4510fb7c9621 111 unsigned short raw;
kidecha_rahul 1:4510fb7c9621 112 raw = ain_4.read_u16();
kidecha_rahul 1:4510fb7c9621 113 float vin;
kidecha_rahul 1:4510fb7c9621 114
kidecha_rahul 1:4510fb7c9621 115 vin = ((raw*5*1.2)/ 65535.0);
kidecha_rahul 1:4510fb7c9621 116 pc.printf("battery voltage is: %3.1f\n", vin);
kidecha_rahul 1:4510fb7c9621 117
kidecha_rahul 1:4510fb7c9621 118 output = ((vin - battery_min) / (battery_max - battery_min)) * 100;
kidecha_rahul 1:4510fb7c9621 119 if (output < 100)
kidecha_rahul 1:4510fb7c9621 120 pc.printf("percentage: %i\n",output);
kidecha_rahul 1:4510fb7c9621 121 else
kidecha_rahul 1:4510fb7c9621 122 pc.printf("percentage: 99\n");
kidecha_rahul 1:4510fb7c9621 123
kidecha_rahul 1:4510fb7c9621 124 wait(5);
kidecha_rahul 1:4510fb7c9621 125
kidecha_rahul 1:4510fb7c9621 126
kidecha_rahul 1:4510fb7c9621 127 }
phonemacro 0:76206161b30f 128 }
phonemacro 0:76206161b30f 129