Documente Academic
Documente Profesional
Documente Cultură
/**********************************
* Configured for Arduino UNO R3
* you will need to use external pull up resistors of
* 4.7k-ohm to pull the SDA and SCL lines up to 3.3v
**********************************/
/**********************************
* CONFIGURE I2C/SERIAL ON ARDUINO
**********************************/
/**********************************
* CONFIGURE SERIAL LIBRARY
**********************************/
//#include <SoftwareSerial.h>
//#include <Serial.h>
#include <Wire.h>
/**********************************
* DEFINE VARIABLES AND SMBus MAPPINGS
**********************************/
#define BATT_SMBUS_ADDR 0x0B ///< I2C address
#define BATT_SMBUS_ADDR_MIN 0x08 ///< lowest
possible address
#define BATT_SMBUS_ADDR_MAX 0x7F ///< highest
possible address
//BUS MAPPINGS FROM DEV.3DR
#define BATT_SMBUS_TEMP 0x08 ///< temperature
register
#define BATT_SMBUS_VOLTAGE 0x09 ///< voltage
register
#define BATT_SMBUS_REMAINING_CAPACITY 0x0f ///< predicted
remaining battery capacity as a percentage
#define BATT_SMBUS_FULL_CHARGE_CAPACITY 0x10 ///< capacity
when fully charged
#define BATT_SMBUS_DESIGN_CAPACITY 0x18 ///< design
capacity register
#define BATT_SMBUS_DESIGN_VOLTAGE 0x19 ///< design
voltage register
#define BATT_SMBUS_SERIALNUM 0x1c ///< serial
number register
#define BATT_SMBUS_MANUFACTURE_NAME 0x20 ///< manufacturer
name
#define BATT_SMBUS_MANUFACTURE_DATA 0x23 ///< manufacturer
data
#define BATT_SMBUS_MANUFACTURE_INFO 0x25 ///< cell voltage
register
#define BATT_SMBUS_CURRENT 0x2a ///< current
register
#define BATT_SMBUS_MEASUREMENT_INTERVAL_US (1000000 / 10) ///< time in
microseconds, measure at 10hz
#define BATT_SMBUS_TIMEOUT_US 10000000 ///< timeout
looking for battery 10seconds after startup
#define BATT_SMBUS_BUTTON_DEBOUNCE_MS 300 ///< button holds
longer than this time will cause a power off event
#define bufferLen 32
uint8_t i2cBuffer[bufferLen];
void setup()
{
Serial.flush();
while (!Serial) {
; //wait for Console port
to connect.
}
Serial.println("Console Initialized");
void scan()
{
byte i = 0;
for ( i= 0; i < 127; i++ )
{
Serial.print("Address: 0x");
Serial.print(i,HEX);
bool ack = i2c_start(i<<1 | I2C_WRITE);
if ( ack ) {
Serial.println(": OK");
Serial.flush();
}
else {
Serial.println(": -");
Serial.flush();
}
i2c_stop();
}
}
void loop()
{
uint8_t length_read = 0;
Serial.print("Voltage: ");
Serial.println((float)fetchWord(BATT_SMBUS_VOLTAGE)/1000);
Serial.print("Temp: ");
unsigned int tempk = fetchWord(BATT_SMBUS_TEMP);
Serial.println((float)tempk/10.0-273.15);
Serial.print("Chemistry ");
length_read = i2c_smbus_read_block(CELL_CHEM, i2cBuffer, bufferLen);
Serial.write(i2cBuffer, length_read);
Serial.println("");
// These aren't part of the standard, but work with some packs.
// They don't work with the Lenovo and Dell packs we've tested
Serial.print("Cell 1 Voltage: ");
Serial.println(fetchWord(CELL1_VOLTAGE));
Serial.print("Cell 2 Voltage: ");
Serial.println(fetchWord(CELL2_VOLTAGE));
Serial.print("Cell 3 Voltage: ");
Serial.println(fetchWord(CELL3_VOLTAGE));
Serial.print("Cell 4 Voltage: ");
Serial.println(fetchWord(CELL4_VOLTAGE));
Serial.println(".");
delay(5000);
}