Permalink
Browse files

Initial commit

  • Loading branch information...
0 parents commit f05aa3ae4370a24cfc0e9e6731bdaca6b3938fa5 Matthew Newberry committed Dec 19, 2011
Showing with 136 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +65 −0 MAX1704.cpp
  3. +35 −0 MAX1704.h
  4. +35 −0 MAX1704.ino
1 .gitignore
@@ -0,0 +1 @@
+.DS_Store
65 MAX1704.cpp
@@ -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();
+}
+
35 MAX1704.h
@@ -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
35 MAX1704.ino
@@ -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);
+}

1 comment on commit f05aa3a

@tmarkson

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

Please sign in to comment.