-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathina226.c
78 lines (66 loc) · 2.72 KB
/
ina226.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
* INA226 - TI Current/Voltage/Power Monitor Code
* Copyright (C) 2021 Craig Peacock
*
* 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., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <unistd.h>
#include "i2c.h"
#include "ina226.h"
void ina226_init(uint32_t i2c_master_port, uint8_t i2c_slave_addr)
{
i2c_write_short(i2c_master_port, i2c_slave_addr, INA226_CFG_REG, 0x8000); // Reset
i2c_write_short(i2c_master_port, i2c_slave_addr, INA226_CFG_REG, 0x4527); // Average over 16 Samples
i2c_write_short(i2c_master_port, i2c_slave_addr, INA226_CAL_REG, 1024); // 1A, 0.100Ohm Resistor
printf("Manufacturer ID: 0x%04X\r\n",i2c_read_short(i2c_master_port, i2c_slave_addr, INA226_MANUFACTURER_ID));
printf("Die ID Register: 0x%04X\r\n",i2c_read_short(i2c_master_port, i2c_slave_addr, INA226_DIE_ID));
printf("Configuration Register: 0x%04X\r\n",i2c_read_short(i2c_master_port, i2c_slave_addr, INA226_CFG_REG));
printf("\r\n");
sleep(1);
}
float ina226_voltage(uint32_t i2c_master_port, uint8_t i2c_slave_addr)
{
uint16_t iBusVoltage;
float fBusVoltage;
iBusVoltage = i2c_read_short(i2c_master_port, i2c_slave_addr, INA226_BUS_VOLT_REG);
//printf("iBusVoltage = %04x\r\n", iBusVoltage);
fBusVoltage = (iBusVoltage) * 0.00125;
//printf("Bus Voltage = %.2fV, ", fBusVoltage);
return (fBusVoltage);
}
float ina226_current(uint32_t i2c_master_port, uint8_t i2c_slave_addr)
{
int16_t iCurrent;
float fCurrent;
iCurrent = i2c_read_short(i2c_master_port, i2c_slave_addr, INA226_CURRENT_REG);
// Internally Calculated as Current = ((ShuntVoltage * CalibrationRegister) / 2048)
fCurrent = iCurrent * 0.0005;
//printf("Current = %.3fA\r\n", fCurrent);
return (fCurrent);
}
float ina226_power(uint32_t i2c_master_port, uint8_t i2c_slave_addr)
{
int16_t iPower;
float fPower;
iPower = i2c_read_short(i2c_master_port, i2c_slave_addr, INA226_POWER_REG);
// The Power Register LSB is internally programmed to equal 25 times the programmed value of the Current_LSB
fPower = iPower * 0.0125;
//printf("Power = %.2fW\r\n", fPower);
return (fPower);
}