Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Matthew Newberry
committed
Dec 19, 2011
0 parents
commit f05aa3a
Showing
4 changed files
with
136 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// | ||
// MAX1704.cpp | ||
// | ||
// | ||
// Created by Matthew Newberry on 12/18/11. | ||
// Copyright (c) 2011 __MyCompanyName__. All rights reserved. | ||
// | ||
|
||
#include "Arduino.h" | ||
#include "MAX1704.h" | ||
#include "Wire.h" | ||
|
||
float MAX1704::stateOfCharge(void){ | ||
|
||
int data[] = {}; | ||
readFrom(MAX1704_SOC,2,data); | ||
|
||
float fraction = data[1] / 256.0; | ||
float percentage = data[0] + fraction; | ||
|
||
return percentage; | ||
} | ||
|
||
void MAX1704::powerOnReset(){ | ||
|
||
performCommand(MAX1704_POWER_ON_RESET, 0x00); | ||
} | ||
|
||
void MAX1704::quickStart(){ | ||
|
||
performCommand(MAX1704_QUICK_START, 0x00); | ||
} | ||
|
||
void MAX1704::version(){ | ||
|
||
} | ||
|
||
void MAX1704::performCommand(byte address, int value){ | ||
|
||
Wire.beginTransmission(MAX1704_ADDR); | ||
Wire.write(MAX1704_COMMAND); | ||
Wire.write(address); | ||
Wire.write(value); | ||
|
||
Wire.endTransmission(); | ||
} | ||
|
||
void MAX1704::readFrom(byte address, int number, int* data){ | ||
|
||
Wire.beginTransmission(MAX1704_ADDR); | ||
Wire.write(address); | ||
Wire.endTransmission(); | ||
|
||
Wire.requestFrom(MAX1704_ADDR, number); | ||
|
||
int i = 0; | ||
while(Wire.available()){ | ||
|
||
data[i] = Wire.read(); | ||
i++; | ||
} | ||
|
||
Wire.endTransmission(); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// | ||
// MAX1704.h | ||
// | ||
// | ||
// Created by Matthew Newberry on 12/18/11. | ||
// Copyright (c) 2011 __MyCompanyName__. All rights reserved. | ||
// | ||
|
||
#include "Arduino.h" | ||
|
||
#ifndef _MAX1704_h | ||
#define _MAX1704_h | ||
|
||
#define MAX1704_ADDR 0x36 | ||
#define MAX1704_SOC 0x04 | ||
#define MAX1704_VERSION 0x08 | ||
#define MAX1704_POWER_ON_RESET 0x54 | ||
#define MAX1704_QUICK_START 0x40 | ||
#define MAX1704_CONFIG 0x0C | ||
#define MAX1704_COMMAND 0xFE | ||
|
||
class MAX1704{ | ||
|
||
public: | ||
float stateOfCharge(); | ||
void powerOnReset(); | ||
void quickStart(); | ||
void version(); | ||
|
||
private: | ||
void performCommand(byte address, int value); | ||
void readFrom(byte address, int number, int* data); | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/************************************************************************** | ||
* * | ||
* MAX1704* Driver for Arduino * | ||
* * | ||
*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU License. * | ||
* This program is distributed in the hope that it will be useful, * | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of * | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | ||
* GNU License V2 for more details. * | ||
* * | ||
***************************************************************************/ | ||
|
||
#include "Wire.h" | ||
#include "MAX1704.h" | ||
|
||
MAX1704 fuelGauge; | ||
|
||
void setup(){ | ||
|
||
Wire.begin(); | ||
Serial.begin(9600); | ||
|
||
fuelGauge.quickStart(); | ||
fuelGauge.powerOnReset(); | ||
} | ||
|
||
void loop(){ | ||
|
||
float charge = fuelGauge.stateOfCharge(); | ||
Serial.println(charge); | ||
delay(5000); | ||
} |
f05aa3a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Matt - would you add a connection diagram? You're linked from sparkfun's product 10617 and I bet there are people who want a quick diagram for connection to an Arduino (uno). thanks man!- ted