以下内容来自官方网站下载,未经任何更改
/*!
LTC6804-1 Multicell Battery Monitor
@verbatim
The LTC6804 is a 3rd generation multicell battery stack
monitor that measures up to 12 series connected battery
cells with a total measurement error of less than 1.2mV. The
cell measurement range of 0V to 5V makes the LTC6804
suitable for most battery chemistries. All 12 cell voltages
can be captured in 290uS, and lower data acquisition rates
can be selected for high noise reduction.
Using the LTC6804-1, multiple devices are connected in
a daisy-chain with one host processor connection for all
devices.
@endverbatim
REVISION HISTORY
$Revision: 1000 $
$Date: 2013-12-13
Copyright (c) 2013, Linear Technology Corp.(LTC)
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
The views and conclusions contained in the software and documentation are those
of the authors and should not be interpreted as representing official policies,
either expressed or implied, of Linear Technology Corp.
The Linear Technology Linduino is not affiliated with the official Arduino team.
However, the Linduino is only possible because of the Arduino team's commitment
to the open-source community. Please, visit http://www.arduino.cc and
http://store.arduino.cc , and consider a purchase that will help fund their
ongoing work.
Copyright 2013 Linear Technology Corp. (LTC)
***********************************************************/
//! @defgroup LTC68041 LTC6804-1: Multicell Battery Monitor
/*! @file
@ingroup LTC68041
Library for LTC6804-1 Multicell Battery Monitor
*/
#include <stdint.h>
#include <Arduino.h>
#include "Linduino.h"
#include "LT_SPI.h"
#include "LTC68041.h"
#include <SPI.h>
/*!
6804 conversion command variables.
*/
uint8_t ADCV[2]; //!< Cell Voltage conversion command.
uint8_t ADAX[2]; //!< GPIO conversion command.
/*!
\brief This function will initialize all 6804 variables and the SPI port.
This function will initialize the Linduino to communicate with the LTC6804 with a 1MHz SPI clock.
The Function also intializes the ADCV and ADAX commands to convert all cell and GPIO voltages in
the Normal ADC mode.
*/
void LTC6804_initialize()
{
quikeval_SPI_connect();
spi_enable(SPI_CLOCK_DIV16); // This will set the Linduino to have a 1MHz Clock
set_adc(MD_NORMAL,DCP_DISABLED,CELL_CH_ALL,AUX_CH_ALL);
}
/*!*******************************************************************************************************************
\brief Maps global ADC control variables to the appropriate control bytes for each of the different ADC commands
@param[in] uint8_t MD The adc conversion mode
@param[in] uint8_t DCP Controls if Discharge is permitted during cell conversions
@param[in] uint8_t CH Determines which cells are measured during an ADC conversion command
@param[in] uint8_t CHG Determines which GPIO channels are measured during Auxiliary conversion command
Command Code:
-------------
|command | 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
|-----------|-------|-------|-------|-------|-------|-------|-------|-------|-------|-------|-------|-------|-------|-------|-------|-------|
|ADCV: | 0 | 0 | 0 | 0 | 0 | 0 | 1 | MD[1] | MD[2] | 1 | 1 | DCP | 0 | CH[2] | CH[1] | CH[0] |
|ADAX: | 0 | 0 | 0 | 0 | 0 | 1 | 0 | MD[1] | MD[2] | 1 | 1 | DCP | 0 | CHG[2]| CHG[1]| CHG[0]|
******************************************************************************************************************/
void set_adc(uint8_t MD, //ADC Mode
uint8_t DCP, //Discharge Permit
uint8_t CH, //Cell Channels to be measured
uint8_t CHG //GPIO Channels to be measured
)
{
uint8_t md_bits;
md_bits = (MD & 0x02) >> 1;
ADCV[0] = md_bits + 0x02;
md_bits = (MD & 0x01) << 7;
ADCV[1] = md_bits + 0x60 + (DCP<<4) + CH;
md_bits = (MD & 0x02) >> 1;
ADAX[0] = md_bits + 0x04;
md_bits = (MD & 0x01) << 7;
ADAX[1] = md_bits + 0x60 + CHG ;
}
/*!*********************************************************************************************
\brief Starts cell voltage conversion
Starts ADC conversions of the LTC6804 Cpin inputs.
The type of ADC conversion executed can be changed by setting the associated global variables:
|Variable|Function |
|--------|----------------------------------------------|
| MD | Determines the filter corner of the ADC |
| CH | Determines which cell channels are converted |
| DCP | Determines if Discharge is Permitted |
Command Code:
-------------
|CMD[0:1] | 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
|-----------|-------|-------|-------|-------|-------|-------|-------|-------|-------|-------|-------|-------|-------|-------|-------|-------|
|ADCV: | 0 | 0 | 0 | 0 | 0 | 0 | 1 | MD[1] | MD[2] | 1 | 1 | DCP | 0 | CH[2] | CH[1] | CH[0] |
***********************************************************************************************/
void LTC6804_adcv()
{
uint8_t cmd[4];
uint16_t cmd_pec;
//1
cmd[0] = ADCV[0];
cmd[1] = ADCV[1];
//2
cmd_pec = pec15_calc(2, ADCV);
cmd[2] = (uint8_t)(cmd_pec >> 8);
cmd[3] = (uint8_t)(cmd_pec);
//3
wakeup_idle (); //This will guarantee that the LTC6804 isoSPI port is awake. This command can be removed.
//4
output_low(LTC6804_CS);
spi_write_array(4,cmd);
output_high(LTC6804_CS);
}
/*
LTC6804_adcv Function sequence:
1. Load adcv command into cmd array
2. Calculate adcv cmd PEC and load pec into cmd array
3. wakeup isoSPI port, this step can be removed if isoSPI status is previously guaranteed
4. send broadcast adcv command to LTC6804 daisy chain
*/
/*!******************************************************************************************************
\brief Start an GPIO Conversion
Starts an ADC conversions of the LTC6804 GPIO inputs.
The type of ADC conversion executed can be changed by setting the associated global variables:
|Variable|Function |
|--------|----------------------------------------------|
| MD | Determines the filter corner of the ADC |
| CHG | Determines which GPIO channels are converted |
Command Code:
-------------
|CMD[0:1] | 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
|-----------|-------|-------|-------|-------|-------|-------|-------|-------|-------|-------|-------|-------|-------|-------|-------|-------|
|ADAX: | 0 | 0 | 0 | 0 | 0 | 1 | 0 | MD[1] | MD[2] | 1 | 1 | DCP | 0 | CHG[2]| CHG[1]| CHG[0]|
*********************************************************************************************************/
void LTC6804_adax()
{
uint8_t cmd[4];
uint16_t cmd_pec;
cmd[0] = ADAX[0];
cmd[1] = ADAX[1];
cmd_pec = pec15_calc(2, ADAX);
cmd[2] = (uint8_t)(cmd_pec >> 8);
cmd[3] = (uint8_t)(cmd_pec);
wakeup_idle (); //This will guarantee that the LTC6804 isoSPI port is awake. This command can be removed.
output_low(LTC6804_CS);
spi_write_array(4,cmd);
output_high(LTC6804_CS);
}
/*
LTC6804_adax Function sequence:
1. Load adax command into cmd array
2. Calculate adax cmd PEC and load pec into cmd array
3. wakeup isoSPI port, this step can be removed if isoSPI status is previously guaranteed
4. send broadcast adax command to LTC6804 daisy chain
*/
/***********************************************//**
\brief Reads and parses the LTC6804 cell voltage registers.
The function is used to read the cell codes of the LTC6804.
This function will send the requested read commands parse the data
and store the cell voltages in cell_codes variable.
@param[in] uint8_t reg; This controls which cell voltage register is read back.
0: Read back all Cell registers
1: Read back cell group A
2: Read back cell group B
3: Read back cell group C
4: Read back cell group D
@param[in] uint8_t total_ic; This is the number of ICs in the daisy chain(-1 only)
@param[out] uint16_t cell_codes[]; An array of the parsed cell cod