##// END OF EJS Templates
mkspec updates
mkspec updates

File last commit:

r30:62c112128e59 default
r55:c31e6b955f5b dev_alexis
Show More
ina226.c
114 lines | 3.4 KiB | text/x-c | CLexer
jeandet@pc-de-jeandet3.LAB-LPP.LOCAL
sync
r28 /*------------------------------------------------------------------------------
-- 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>
jeandet@pc-de-jeandet3.LAB-LPP.LOCAL
I2C library partially validated, ina226 library partially validated.
r30
int ina226open(INA226_t *dev, i2c_t i2cdev, uint16_t config, uint8_t A0, uint8_t A1, uint32_t shuntmOhm, uint32_t CurrentRangeuAmp)
jeandet@pc-de-jeandet3.LAB-LPP.LOCAL
sync
r28 {
if(dev != NULL)
{
dev->i2cdev=i2cdev;
dev->devAddress = INA226_I2C_ADDRESS | (A0 & 1) | ((A1<<1) & 2);
jeandet@pc-de-jeandet3.LAB-LPP.LOCAL
I2C library partially validated, ina226 library partially validated.
r30 printf("dev->devAddress = %x\n\r",dev->devAddress);
jeandet@pc-de-jeandet3.LAB-LPP.LOCAL
sync
r28 dev->shuntmOhm = shuntmOhm;
dev->CurrentRangeuAmp = CurrentRangeuAmp;
jeandet@PC-DE-JEANDET.lab-lpp.local
sync
r29 ina226setReg(dev,INA226_Configuration_Reg,0x8000);
jeandet@pc-de-jeandet3.LAB-LPP.LOCAL
I2C library partially validated, ina226 library partially validated.
r30 ina226setReg(dev,INA226_Configuration_Reg,config);
jeandet@PC-DE-JEANDET.lab-lpp.local
sync
r29 return ina226calibrate(dev,shuntmOhm,CurrentRangeuAmp);
jeandet@pc-de-jeandet3.LAB-LPP.LOCAL
sync
r28 }
return -1;
}
jeandet@PC-DE-JEANDET.lab-lpp.local
sync
r29 uint16_t ina226getID(INA226_t* dev)
jeandet@pc-de-jeandet3.LAB-LPP.LOCAL
sync
r28 {
if(dev != NULL)
{
jeandet@PC-DE-JEANDET.lab-lpp.local
sync
r29 uint16_t id=ina226getReg(dev,INA226_Die_ID_Reg);
return id;
jeandet@pc-de-jeandet3.LAB-LPP.LOCAL
sync
r28 }
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)));
jeandet@PC-DE-JEANDET.lab-lpp.local
sync
r29 return ina226setReg(dev,INA226_Calibration_Reg,(uint16_t)CAL);
jeandet@pc-de-jeandet3.LAB-LPP.LOCAL
sync
r28 }
uint32_t ina226getBusVoltage(INA226_t *dev)
{
jeandet@PC-DE-JEANDET.lab-lpp.local
sync
r29 uint32_t busVoltage= ina226getReg(dev,INA226_Bus_Voltage_Reg);
jeandet@pc-de-jeandet3.LAB-LPP.LOCAL
sync
r28 busVoltage*= 1250;
return busVoltage;
}
uint32_t ina226getPower(INA226_t *dev)
{
jeandet@PC-DE-JEANDET.lab-lpp.local
sync
r29 uint32_t power= ina226getReg(dev,INA226_Power_Reg);
power*= ((dev->CurrentRangeuAmp>>15)*25);
return power;
}
jeandet@pc-de-jeandet3.LAB-LPP.LOCAL
I2C library partially validated, ina226 library partially validated.
r30 int32_t ina226getCurrent(INA226_t *dev)
jeandet@PC-DE-JEANDET.lab-lpp.local
sync
r29 {
jeandet@pc-de-jeandet3.LAB-LPP.LOCAL
I2C library partially validated, ina226 library partially validated.
r30 int32_t current= ina226getReg(dev,INA226_Current_Reg);
current<<=16;
current>>=16;
current = current*(dev->CurrentRangeuAmp>>15);
jeandet@PC-DE-JEANDET.lab-lpp.local
sync
r29 return current;
}
uint16_t ina226getReg(INA226_t* dev,char reg)
{
if(dev != NULL)
{
char DATA[2];
DATA[0]=reg;
i2cwrite(dev->i2cdev,dev->devAddress,DATA,1);
i2cread(dev->i2cdev,dev->devAddress,DATA,2);
uint16_t val=DATA[0];
val=(val<<8)+DATA[1];
return val;
}
return -1;
jeandet@pc-de-jeandet3.LAB-LPP.LOCAL
sync
r28 }
jeandet@PC-DE-JEANDET.lab-lpp.local
sync
r29 int ina226setReg(INA226_t* dev,char reg,int16_t value)
{
if(dev != NULL)
{
char DATA[3];
DATA[0]=INA226_Calibration_Reg;
DATA[1]=(char)(0xff & (value>>8));
DATA[2]=(char)(0xff & value);
if(3==i2cwrite(dev->i2cdev,dev->devAddress,DATA,3))return 1;
}
return -1;
}
jeandet@pc-de-jeandet3.LAB-LPP.LOCAL
sync
r28