Now that we have the Battery Monitor Circuit (BMC) working, let's quickly look at a simple test program that demonstrates communication between an external device and the BMC. The program is named ROVsubBatteryTest_main.asm and the external device in use is also a PIC16F1937. In order to make it easier to initialize communication with the BMC at a specific time, the process is initiated via a push-button switch. The switch input is polled and upon being pressed, the main loop of the program calls the subroutine "GetBattery" four times (once for each cell). The program ensures that only a single four cell reading is performed per a single button press. Additionally, two LEDs are lit once the switch is pressed and turned off after the cell readings have been performed and the push-button is not longer pressed. One LED is controlled by the voltage of the push-button switch and the other via software. This just adds a visual indicator to help troubleshoot problems if the program is hanging up somewhere. The code where the LED is turned off can be placed anywhere in the program to aid in debugging when you are trying to narrow down the location of any problems. Once the battery reading is performed, the four cell voltages are transmitted out on the UART TX pin. This allows us to check the values saved by the external device (via a logic analyzer) and see if everything worked out correctly. All of the this is accomplished in the code shown below:
You may be wondering how calling "GetBattery" four times allows us to determine which cell values are being read and placed in the correct variables. This is accomplished with the variable named "cellCounter". "cellCounter" is initially zero and incremented everytime "GetBattery" is called. "GetBattery" checks the value of "cellCounter" and loads/sends the corresponding I2C command which tells the BMC to read a particular cell. After sending the proper command, another I2C transmission sequence is initiated and the cell value is received and stored in one of four variables named "cell1", "cell2", "cell3", or "cell4". After the value for "cell4" is stored away, "cellCounter" is reset for the next 4 cell sequence.
More details can be found in the source code file in the data-sheets section of this website or the Gitlab repository. I won't elaborate too much further here since this is only a test program and can be modified or rewritten to suit the needs of many different use cases.