Initial rough copy

Dependencies:   BQ76PL536A mbed

Committer:
dmwahl
Date:
Sun Jul 25 18:23:02 2021 +0000
Revision:
2:6c664dcbd052
Parent:
1:af7a249bd2b2
Rough start

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dmwahl 2:6c664dcbd052 1 ///-----------------------------------------------------------------
dmwahl 2:6c664dcbd052 2 /// Description: BQ76PL536A Driver
dmwahl 2:6c664dcbd052 3 /// Author: David Wahl
dmwahl 2:6c664dcbd052 4 /// Date: 13-JUL-2021
dmwahl 2:6c664dcbd052 5 /// Notes: Initial release
dmwahl 2:6c664dcbd052 6 ///
dmwahl 2:6c664dcbd052 7 /// Revision History:
dmwahl 2:6c664dcbd052 8 /// Name: Date: Description:
dmwahl 2:6c664dcbd052 9 ///-----------------------------------------------------------------
dmwahl 2:6c664dcbd052 10
dmwahl 0:6e263c5ce85d 11 #include "mbed.h"
dmwahl 0:6e263c5ce85d 12 #include "bq76pl536a.h"
dmwahl 0:6e263c5ce85d 13
dmwahl 0:6e263c5ce85d 14 // a SPI sub-class that provides a constructed default
dmwahl 2:6c664dcbd052 15 /*class SPIPreInit : public SPI
dmwahl 0:6e263c5ce85d 16 {
dmwahl 0:6e263c5ce85d 17 public:
dmwahl 1:af7a249bd2b2 18 SPIPreInit(PinName mosi, PinName miso, PinName sclk, int freq, int bits, int mode) : SPI(mosi, miso, sclk)
dmwahl 1:af7a249bd2b2 19 {
dmwahl 0:6e263c5ce85d 20 frequency(freq);
dmwahl 0:6e263c5ce85d 21 format(bits, mode);
dmwahl 0:6e263c5ce85d 22 };
dmwahl 2:6c664dcbd052 23 };*/
dmwahl 0:6e263c5ce85d 24
dmwahl 1:af7a249bd2b2 25 //SPIPreInit spi(PA_7, PA_6, A1, 500000, 8, 1);
dmwahl 1:af7a249bd2b2 26 SPI spi(PA_7, PA_6, A1);
dmwahl 1:af7a249bd2b2 27
dmwahl 1:af7a249bd2b2 28 u8t attachedCells[] = {5};
dmwahl 1:af7a249bd2b2 29 u8t numDev = sizeof(attachedCells)/sizeof(attachedCells[0]);
dmwahl 1:af7a249bd2b2 30 u8t numCells = 0;
dmwahl 0:6e263c5ce85d 31
dmwahl 0:6e263c5ce85d 32 // (SPI object, PinName cs, u8t numDev, u8t attachedCells[], u16t cov[mV], u16t cuv[mV], u8t balanceTimeout[sec], bool balanceEnabled)
dmwahl 1:af7a249bd2b2 33 BQ76PL536A bq_pack(spi, PB_0, numDev, 4000, 3000, 5, true);
dmwahl 0:6e263c5ce85d 34
dmwahl 0:6e263c5ce85d 35 Serial pc(SERIAL_TX, SERIAL_RX);
dmwahl 2:6c664dcbd052 36 DigitalOut cs(D3); // Chip select for BQ76PL536A
dmwahl 0:6e263c5ce85d 37
dmwahl 0:6e263c5ce85d 38 int main()
dmwahl 0:6e263c5ce85d 39 {
dmwahl 1:af7a249bd2b2 40 //wait(1);
dmwahl 1:af7a249bd2b2 41 //spi.format(8,1);
dmwahl 1:af7a249bd2b2 42 //spi.frequency(50000);
dmwahl 0:6e263c5ce85d 43
dmwahl 1:af7a249bd2b2 44
dmwahl 1:af7a249bd2b2 45 for (int i = 0; i<numDev; i++) {
dmwahl 1:af7a249bd2b2 46 numCells +=attachedCells[i];
dmwahl 1:af7a249bd2b2 47 }
dmwahl 2:6c664dcbd052 48
dmwahl 2:6c664dcbd052 49 float cellVolts[5];
dmwahl 2:6c664dcbd052 50
dmwahl 0:6e263c5ce85d 51 pc.baud(115200);
dmwahl 1:af7a249bd2b2 52 pc.printf("Starting up...\r\n");
dmwahl 2:6c664dcbd052 53
dmwahl 2:6c664dcbd052 54 //wait_us(6);
dmwahl 2:6c664dcbd052 55 bq_pack.readAll(1);
dmwahl 2:6c664dcbd052 56 bq_pack.write(1,ADC_CONTROL_REG,bq_pack.bqPackData[ADC_CONTROL_REG]|5);
dmwahl 2:6c664dcbd052 57
dmwahl 1:af7a249bd2b2 58 //pc.printf("Status: %u\r\n",bq_pack.devStatus());
dmwahl 1:af7a249bd2b2 59 //pc.printf("COV: %u CUV: %u, numDev: %d numCells: %d\r\n", bq_pack.cov, bq_pack.cuv, bq_pack.numDev, numCells);
dmwahl 1:af7a249bd2b2 60 //pc.printf("%u %u %u %u %u\r\n", bq_pack.attachedCells[0], bq_pack.attachedCells[1], bq_pack.attachedCells[2], bq_pack.attachedCells[3], bq_pack.attachedCells[4]);
dmwahl 1:af7a249bd2b2 61 //pc.printf("%u %u %u %u\r\n", bq_pack.cellVolts[0], bq_pack.cellVolts[1], bq_pack.cellVolts[2], bq_pack.cellVolts[3]);
dmwahl 1:af7a249bd2b2 62
dmwahl 0:6e263c5ce85d 63 while(1) {
dmwahl 0:6e263c5ce85d 64 wait(1);
dmwahl 2:6c664dcbd052 65 bq_pack.adcConvert();
dmwahl 2:6c664dcbd052 66
dmwahl 2:6c664dcbd052 67 wait_us(500);
dmwahl 2:6c664dcbd052 68 bq_pack.readAll(1);
dmwahl 2:6c664dcbd052 69
dmwahl 2:6c664dcbd052 70 /*for (int i = 0; i <5; i++) {
dmwahl 2:6c664dcbd052 71 cellVolts[i] = bq_pack.cellVolts(bq_pack.bqPackData[VCELL1_L_REG+(i<<1)],bq_pack.bqPackData[VCELL1_L_REG+(i<<1)|1]);
dmwahl 2:6c664dcbd052 72 }
dmwahl 2:6c664dcbd052 73
dmwahl 2:6c664dcbd052 74 for (int i = 0; i <5; i++) {
dmwahl 2:6c664dcbd052 75 pc.printf("%.3f",cellVolts[i]);
dmwahl 2:6c664dcbd052 76 if (i!=4) {
dmwahl 2:6c664dcbd052 77 pc.printf(" ,");
dmwahl 2:6c664dcbd052 78 } else {
dmwahl 2:6c664dcbd052 79 pc.printf("\r\n");
dmwahl 2:6c664dcbd052 80 }
dmwahl 2:6c664dcbd052 81 }*/
dmwahl 2:6c664dcbd052 82
dmwahl 2:6c664dcbd052 83
dmwahl 2:6c664dcbd052 84
dmwahl 2:6c664dcbd052 85 pc.printf(" %.3f",bq_pack.cellVolts(bq_pack.bqPackData[VCELL1_L_REG],bq_pack.bqPackData[VCELL1_L_REG+1]));
dmwahl 2:6c664dcbd052 86 pc.printf(" %.3f",bq_pack.cellVolts(bq_pack.bqPackData[VCELL2_L_REG],bq_pack.bqPackData[VCELL2_L_REG+1]));
dmwahl 2:6c664dcbd052 87 pc.printf(" %.3f",bq_pack.cellVolts(bq_pack.bqPackData[VCELL3_L_REG],bq_pack.bqPackData[VCELL3_L_REG+1]));
dmwahl 2:6c664dcbd052 88 pc.printf(" %.3f",bq_pack.cellVolts(bq_pack.bqPackData[VCELL4_L_REG],bq_pack.bqPackData[VCELL4_L_REG+1]));
dmwahl 2:6c664dcbd052 89 pc.printf(" %.3f\r\n",bq_pack.cellVolts(bq_pack.bqPackData[VCELL5_L_REG],bq_pack.bqPackData[VCELL5_L_REG+1]));
dmwahl 1:af7a249bd2b2 90 //u8t addr_ctrl = bq_pack.readRegister(1, ADDRESS_CONTROL_REG);
dmwahl 1:af7a249bd2b2 91
dmwahl 0:6e263c5ce85d 92 }
dmwahl 0:6e263c5ce85d 93 }