##// END OF EJS Templates
sync
sync

File last commit:

r28:de7a3b4fc572 default
r28:de7a3b4fc572 default
Show More
ina226.c
92 lines | 2.8 KiB | text/x-c | CLexer
/*------------------------------------------------------------------------------
-- This file is a part of the libuc, microcontroler library
-- Copyright (C) 2012, Alexis Jeandet
--
-- This program is free software; you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation; either version 3 of the License, or
-- (at your option) any later version.
--
-- 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 General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with this program; if not, write to the Free Software
-- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-------------------------------------------------------------------------------
-- Author : Alexis Jeandet
-- Mail : alexis.jeandet@gmail.com
-------------------------------------------------------------------------------*/
#include <ina226.h>
#include <stdio.h>
#include <stddef.h>
int ina226open(INA226_t *dev, i2c_t i2cdev, uint8_t A0, uint8_t A1, uint32_t shuntmOhm, uint32_t CurrentRangeuAmp)
{
if(dev != NULL)
{
dev->i2cdev=i2cdev;
dev->devAddress = INA226_I2C_ADDRESS | (A0 & 1) | ((A1<<1) & 2);
dev->shuntmOhm = shuntmOhm;
dev->CurrentRangeuAmp = CurrentRangeuAmp;
return 1;
}
return -1;
}
uint8_t ina226getID(INA226_t* dev)
{
if(dev != NULL)
{
char DATA[]={INA226_Die_ID_Reg};
i2cwrite(dev->i2cdev,dev->devAddress,DATA,1);
i2cread(dev->i2cdev,dev->devAddress,DATA,1);
return DATA[0];
}
return -1;
}
int ina226calibrate(INA226_t* dev,uint32_t shuntmOhm, uint32_t CurrentRangeuAmp)
{
dev->shuntmOhm = shuntmOhm;
dev->CurrentRangeuAmp = CurrentRangeuAmp;
uint32_t CAL= (uint32_t)(5210000/(shuntmOhm*(CurrentRangeuAmp>>15)));
char CMD[3];
CMD[0]=INA226_Calibration_Reg;
CMD[1]=(char)(0xff & (CAL>>8));
CMD[2]=(char)(0xff & CAL);
i2cwrite(dev->i2cdev,dev->devAddress,CMD,3);
}
uint32_t ina226getBusVoltage(INA226_t *dev)
{
char CMD[2];
CMD[0]=INA226_Bus_Voltage_Reg;
i2cwrite(dev->i2cdev,dev->devAddress,CMD,1);
i2cread(dev->i2cdev,dev->devAddress,CMD,2);
uint32_t busVoltage= CMD[0];
busVoltage = (busVoltage<<8) + CMD[1];
busVoltage*= 1250;
return busVoltage;
}
uint32_t ina226getPower(INA226_t *dev)
{
char CMD[2];
CMD[0]=INA226_Power_Reg;
i2cwrite(dev->i2cdev,dev->devAddress,CMD,1);
i2cread(dev->i2cdev,dev->devAddress,CMD,2);
uint32_t power= CMD[0];
power = (power<<8) + CMD[1];
power*= ((CurrentRangeuAmp>>15)*25);
return busVoltage;
}