Now working with INA233
Some checks failed
Test compiling project / test (push) Failing after 1m37s

This commit is contained in:
2024-07-30 23:15:01 +02:00
parent 0ff5604419
commit 3c1f66d7d0
5 changed files with 136 additions and 77 deletions

View File

@@ -1,5 +1,37 @@
#include "INA233.h"
uint16_t get_word(uint8_t address, uint8_t reg) {
uint8_t data[2];
int16_t rawVoltage;
// Request data from the PMBus device
Wire.beginTransmission(address);
Wire.write(reg);
Wire.endTransmission();
Wire.requestFrom(address, 2);
if (Wire.available() == 2) {
data[0] = Wire.read();
data[1] = Wire.read();
} else {
// return 0 on error
return 0.0;
}
// Combine the two bytes into a single 16-bit value
return (data[1] << 8) | data[0];
}
void sendWord(uint8_t deviceAddress, uint8_t registerAddress, uint16_t value) {
Wire.beginTransmission(deviceAddress);
Wire.write(registerAddress); // Send the register address
Wire.write((uint8_t)value); // Send the low byte first
Wire.write((uint8_t)(value >> 8)); // Send the high byte next
Wire.endTransmission();
}
INA233::INA233(const uint8_t address, TwoWire *wire)
{
_address = address;
@@ -52,31 +84,69 @@ float INA233::getBusVoltage() {
}
float INA233::getShuntVoltage() {
uint8_t data[2];
int16_t rawVoltage;
// Request data from the PMBus device
Wire.beginTransmission(INA233::_address);
Wire.write(REGISTER_READ_VSHUNT);
Wire.endTransmission();
Wire.requestFrom(INA233::_address, 2);
if (Wire.available() == 2) {
data[0] = Wire.read();
data[1] = Wire.read();
} else {
// return 0 on error
return 0.0;
}
// Combine the two bytes into a single 16-bit value
rawVoltage = (data[1] << 8) | data[0];
// convert it to volts
uint16_t rawVoltage = get_word(INA233::_address, REGISTER_READ_VSHUNT);
float voltage = rawVoltage * pow(10,-4);
return voltage;
}
float INA233::getShuntVoltage_mV() {
return getShuntVoltage() * 1000;
}
void INA233::setAveragingMode(AveragingMode avg_mode) {
uint8_t avg_bits = avg_mode;
uint16_t old_conf = get_word(INA233::_address, REGISTER_CONFIGURATION);
// Mask to clear bits 9, 10, and 11 (positions 9 to 11) in the original register
uint16_t clear_mask = ~(0x7 << 9);
old_conf &= clear_mask;
// Prepare new bits: extract 3 bits from new_bits and shift them to position 9
// 0x7 ensures only the last 3 bits are considered
uint16_t set_bits = (avg_bits & 0x7) << 9;
// Set the new bits in the register
old_conf |= set_bits;
sendWord(INA233::_address, REGISTER_CONFIGURATION, old_conf);
};
void INA233::setBusVoltageConversionTime(ConversionTime conversion_time) {
uint8_t conv_bits = conversion_time;
uint16_t old_conf = get_word(INA233::_address, REGISTER_CONFIGURATION);
// Mask to clear bits 9, 10, and 11 (positions 6 to 8) in the original register
uint16_t clear_mask = ~(0x7 << 6);
old_conf &= clear_mask;
// Prepare new bits: extract 3 bits from new_bits and shift them to position 6
// 0x7 ensures only the last 3 bits are considered
uint16_t set_bits = (conv_bits & 0x7) << 6;
// Set the new bits in the register
old_conf |= set_bits;
sendWord(INA233::_address, REGISTER_CONFIGURATION, old_conf);
};
void INA233::setShuntVoltageConversionTime(ConversionTime conversion_time) {
uint8_t conv_bits = conversion_time;
uint16_t old_conf = get_word(INA233::_address, REGISTER_CONFIGURATION);
// Mask to clear bits 9, 10, and 11 (positions 3 to 5) in the original register
uint16_t clear_mask = ~(0x7 << 3);
old_conf &= clear_mask;
// Prepare new bits: extract 3 bits from new_bits and shift them to position 3
// 0x7 ensures only the last 3 bits are considered
uint16_t set_bits = (conv_bits & 0x7) << 3;
// Set the new bits in the register
old_conf |= set_bits;
sendWord(INA233::_address, REGISTER_CONFIGURATION, old_conf);
};
uint16_t INA233::getConfigRegister() {
return get_word(INA233::_address, REGISTER_CONFIGURATION);
}

View File

@@ -5,15 +5,46 @@
#define REGISTER_READ_VIN 0x88
#define REGISTER_READ_VSHUNT 0xd1
#define REGISTER_READ_VSHUNT 0xD1
#define REGISTER_CONFIGURATION 0xD0
enum AveragingMode {
averages_1 = B000,
averages_4 = B001,
averages_16 = B010,
averages_64 = B011,
averages_128 = B100,
averages_256 = B101,
averages_512 = B110,
averages_1024 = B111
};
enum ConversionTime {
conversion_time_140uS = B000,
conversion_time_204uS = B001,
conversion_time_332us = B010,
conversion_time_588uS = B011,
conversion_time_1100uS = B100,
conversion_time_2116uS = B101,
conversion_time_4156uS = B110,
conversion_time_8244uS = B111
};
class INA233{
public:
INA233(uint8_t addr, TwoWire *wire = &Wire);
bool begin(const uint8_t sda, const uint8_t scl);
float getBusVoltage(void);
float getShuntVoltage_mV(void);
float getShuntVoltage(void);
void setAveragingMode(AveragingMode);
void setBusVoltageConversionTime(ConversionTime);
void setShuntVoltageConversionTime(ConversionTime);
uint16_t getConfigRegister();
bool isConnected(void);
private:

View File

@@ -1,46 +0,0 @@
This directory is intended for project specific (private) libraries.
PlatformIO will compile them to static libraries and link into executable file.
The source code of each library should be placed in a an own separate directory
("lib/your_library_name/[here are source files]").
For example, see a structure of the following two libraries `Foo` and `Bar`:
|--lib
| |
| |--Bar
| | |--docs
| | |--examples
| | |--src
| | |- Bar.c
| | |- Bar.h
| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html
| |
| |--Foo
| | |- Foo.c
| | |- Foo.h
| |
| |- README --> THIS FILE
|
|- platformio.ini
|--src
|- main.c
and a contents of `src/main.c`:
```
#include <Foo.h>
#include <Bar.h>
int main (void)
{
...
}
```
PlatformIO Library Dependency Finder will find automatically dependent
libraries scanning project source files.
More information about PlatformIO Library Dependency Finder
- https://docs.platformio.org/page/librarymanager/ldf.html

Submodule lib/infinityPV_INA233_Arduino-library deleted from bc68e03c01

View File

@@ -177,17 +177,24 @@ void display_task(void* parameter)
display_percentage(water_data.percentage);
} else {
Log.verbose("Error detected");
// We have an error, display error code for 3 seconds and then water level for 3 seconds
if (voltage_low) {
display_error_code(1);
delay(3000);
} else if (voltage_high) {
display_error_code(2);
delay(3000);
} else if (current_low) {
display_error_code(3);
delay(3000);
} else if (current_high) {
display_error_code(4);
delay(3000);
} else {
delay(3000);
}
delay(3000);
display_percentage(water_data.percentage);
}
}
@@ -342,6 +349,8 @@ void setup()
pinMode(LED_4, OUTPUT);
pinMode(LED_5, OUTPUT);
pinMode(LED_RED, OUTPUT);
display_error_code(31);
delay(5000);
display_error_code(17);
Log.verbose("Beginning SPIFFS");
@@ -358,15 +367,11 @@ void setup()
ina_sensor.setShuntVoltageConversionTime(7);
ina_sensor.setAverage(4);
#else
//ina_sensor.setMaxCurrentShunt(0.02, 4, false);
//ina_sensor.setBusVoltageConversionTime(7);
//ina_sensor.setShuntVoltageConversionTime(7);
//ina_sensor.setAverage(4);
ina_sensor.setShuntVoltageConversionTime(conversion_time_8244uS);
ina_sensor.setBusVoltageConversionTime(conversion_time_8244uS);
ina_sensor.setAveragingMode(averages_128);
#endif
display_error_code(21);
display_error_code(22);
/////////////////////////////// ROUTES ///////////////////////////////