Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Arduino.h>
- #include <SPI.h>
- /* pin setup */
- int chip_select_pin = PIN_A2;
- /* SPI pins labeled "SCK" "MO" "MI" on the Adafruit Feather M0 express board
- * are used implicitly.
- * PIN_SPI_MISO (22), PIN_SPI_MOSI(23), PIN_SPI_SCK(24),
- * */
- #define LTC6803_4_MAX_SCLK_FREQ 1000000
- #define LTC6803_4_USED_SCLK_FREQ (LTC6803_4_MAX_SCLK_FREQ/2)
- SPISettings settings(LTC6803_4_USED_SCLK_FREQ, BitOrder::MSBFIRST, SPI_MODE3);
- void start_voltage_adc_conversion(){
- SPI.beginTransaction(settings);
- digitalWrite(chip_select_pin, LOW);
- //STCVAD
- //Start Cell Voltage ADC Conversions and Poll Status, with Discharge Permitted
- uint8_t tranfer_buf[2] = {
- 0x10, //CODE
- 0xB0 //PEC
- };
- SPI.transfer(tranfer_buf, 2);
- //wait for conversion, just a static time. at least 12 milliseconds.
- delay(20);
- digitalWrite(chip_select_pin, HIGH);
- SPI.endTransaction();
- }
- void read_voltages_1_to_4(){
- //trigger voltage conversion
- start_voltage_adc_conversion();
- SPI.beginTransaction(settings);
- digitalWrite(chip_select_pin, LOW);
- //transfer buffer must have 2 bytes for command
- //and 6 bytes for response (four 12-bit ADC values encoded in 6 registers CVR00..CVR05)
- //Read Cell Voltages 1-4, "RDCVA", page 22
- uint8_t tranfer_buf[2 + 6] = {
- 0x06, //CODE
- 0xD2 //PEC
- };
- SPI.transfer(tranfer_buf, 6);
- digitalWrite(chip_select_pin, HIGH);
- SPI.endTransaction();
- //recover 12-bit ADC value from C1V[0]..C1V[11] from register values CVR00 and CVR01
- //see datasheet page 24
- uint16_t adc_cell_1 = tranfer_buf[3 + 0] | ((tranfer_buf[3 + 1] & 0B1111)) << 8;
- //adc value to voltage conversion according to page 15
- float voltage_cell_1_millivolt = ((int)adc_cell_1 - 512) * 1.5f ;
- Serial.print("Cell voltage 1: ");
- //print with 5 digits accuracy
- Serial.print(voltage_cell_1_millivolt, 5);
- Serial.println(" mV");
- }
- void setup() {
- pinMode(chip_select_pin, OUTPUT);
- SPI.begin();
- }
- void loop() {
- read_voltages_1_to_4();
- delay(2000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement