Advertisement
emcniece

nightshade-energyshield.cpp

Nov 16th, 2014
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.18 KB | None | 0 0
  1. /*
  2.    NS_energyShield - Library for interfacing with NightShade Electronic's energyShield, rev 8.
  3.    Created by Aaron D. Liebold on August 8, 2014.
  4.    Released into the public domain.
  5.    
  6.    v1.0
  7. */
  8.  
  9. #include "nightshade-energyshield.h"
  10.  
  11.  
  12. NS_energyShield::NS_energyShield()
  13. {
  14.   //Wire.begin();   //Begins TwoWire interface
  15.   _address = 0b00110110;
  16. }
  17.  
  18. // Tell library if fuel gauge has non-default address
  19. void NS_energyShield::address(char address)
  20. {
  21.   _address = address;
  22. }
  23.  
  24. int NS_energyShield::voltage()
  25. {
  26.     byte voltageHB = 0x0;
  27.     byte voltageLB = 0x0;
  28.     int V;
  29.    
  30.     Wire.beginTransmission(_address);
  31.     Wire.write(0x0C);
  32.     Wire.endTransmission(false);
  33.     Wire.requestFrom(_address,2);
  34.     voltageHB = Wire.read();
  35.     voltageLB = Wire.read();
  36.    
  37.     V = (unsigned long) ((voltageHB << 5) + (voltageLB >> 3))*122/100;
  38.    
  39.     return V; // Returns voltage in mV
  40. }
  41.  
  42. int NS_energyShield::current()
  43. {
  44.     int Iraw;
  45.     byte currentHB = 0x0;
  46.     byte currentLB = 0x0;
  47.    
  48.     // Reads high and low chars of the register
  49.     Wire.beginTransmission(_address);
  50.     Wire.write(0x0E);
  51.     Wire.endTransmission(false);
  52.     Wire.requestFrom(_address,2);
  53.     currentHB = Wire.read();
  54.     currentLB = Wire.read();
  55.    
  56.     Iraw = (long) (((currentHB << 8) + currentLB) >> 4)*5/4; // Merges high and low bytes and applies 1.25 mA/unit conversion
  57.    
  58.     return Iraw; // Returns current in mA
  59. }
  60.  
  61. int NS_energyShield::percent()
  62. {
  63.     byte stateOfCharge;
  64.    
  65.     Wire.beginTransmission(_address);
  66.     Wire.write(0x02);
  67.     Wire.endTransmission(false);
  68.     Wire.requestFrom(_address,1);
  69.     stateOfCharge = Wire.read();
  70.    
  71.     return stateOfCharge; // Returns percent of battery charge in 0.5% increments (1/200)
  72. }
  73.  
  74.  
  75. int NS_energyShield::temperature()
  76. {
  77.     int T;
  78.     byte tempHB = 0x0;
  79.     byte tempLB = 0x0;
  80.    
  81.     // Reads high and low chars of the register
  82.     Wire.beginTransmission(_address);
  83.     Wire.write(0x0A);
  84.     Wire.endTransmission(false);
  85.     Wire.requestFrom(_address,2);
  86.     tempHB = Wire.read();
  87.     tempLB = Wire.read();
  88.    
  89.     T = tempHB*8 + tempLB/32; // Combines high and low bytes
  90.    
  91.     return T; // Returns temperature in increments of 0.125 degrees C
  92. }
  93.  
  94. int NS_energyShield::Vadp(int pin)
  95. {
  96.     int V = (unsigned long) analogRead(pin)*25000/1023;
  97.     return V;
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement