ina226.c
115 lines
| 3.5 KiB
| text/x-c
|
CLexer
r71 | /*------------------------------------------------------------------------------ | |||
-- 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@member.fsf.org | ||||
-------------------------------------------------------------------------------*/ | ||||
#include <ina226.h> | ||||
#include <stdio.h> | ||||
#include <stddef.h> | ||||
int ina226open(INA226_t *dev, i2c_t i2cdev, uint16_t config, 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<<2) & 5); | ||||
printf("dev->devAddress = %x\n\r",dev->devAddress); | ||||
dev->shuntmOhm = shuntmOhm; | ||||
dev->CurrentRangeuAmp = CurrentRangeuAmp; | ||||
ina226setReg(dev,INA226_Configuration_Reg,0x8000); | ||||
ina226setReg(dev,INA226_Configuration_Reg,config); | ||||
return ina226calibrate(dev,shuntmOhm,CurrentRangeuAmp); | ||||
} | ||||
return -1; | ||||
} | ||||
uint16_t ina226getID(INA226_t* dev) | ||||
{ | ||||
if(dev != NULL) | ||||
{ | ||||
uint16_t id=ina226getReg(dev,INA226_Die_ID_Reg); | ||||
return id; | ||||
} | ||||
return -1; | ||||
} | ||||
int ina226calibrate(INA226_t* dev,uint32_t shuntmOhm, uint32_t CurrentRangeuAmp) | ||||
{ | ||||
dev->shuntmOhm = shuntmOhm; | ||||
dev->CurrentRangeuAmp = CurrentRangeuAmp; | ||||
r103 | // uint32_t CAL= (uint32_t)(5210000/(shuntmOhm*(CurrentRangeuAmp>>15))); | |||
float CAL=(5210000.0/((float)shuntmOhm*((float)CurrentRangeuAmp/32768.0))); | ||||
r71 | return ina226setReg(dev,INA226_Calibration_Reg,(uint16_t)CAL); | |||
} | ||||
uint32_t ina226getBusVoltage(INA226_t *dev) | ||||
{ | ||||
uint32_t busVoltage= ina226getReg(dev,INA226_Bus_Voltage_Reg); | ||||
busVoltage*= 1250; | ||||
return busVoltage; | ||||
} | ||||
uint32_t ina226getPower(INA226_t *dev) | ||||
{ | ||||
uint32_t power= ina226getReg(dev,INA226_Power_Reg); | ||||
power*= ((dev->CurrentRangeuAmp>>15)*25); | ||||
return power; | ||||
} | ||||
int32_t ina226getCurrent(INA226_t *dev) | ||||
{ | ||||
int32_t current= ina226getReg(dev,INA226_Current_Reg); | ||||
current<<=16; | ||||
current>>=16; | ||||
current = current*(dev->CurrentRangeuAmp>>15); | ||||
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; | ||||
} | ||||
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; | ||||
} | ||||