diff --git a/software/DataAcquisitionFromDevice/datalogger.py b/software/DataAcquisitionFromDevice/datalogger.py index ecf0e7c..862432d 100644 --- a/software/DataAcquisitionFromDevice/datalogger.py +++ b/software/DataAcquisitionFromDevice/datalogger.py @@ -1,5 +1,6 @@ from serial import Serial import datetime +import mcp3425 myDeviceId = '/dev/ttyUSB0' myBaudrate = 9600 @@ -10,17 +11,14 @@ logFile = open('data.log', 'w') logFile.write("Time,Counter\n") while True: - #dataIn = int(ord(ser.readline().strip())) dataIn = ser.readline().strip() - msb = dataIn[0] - lsb = dataIn[1] - voltage = ((dataIn[0] & 0xf) << 8) | lsb; + (msb, lsb) = (dataIn[0], dataIn[1]) + voltage = mcp3425.convert(msb, lsb, mcp3425.MCP3425_RESOLUTION.R14) now = datetime.datetime.now() + logFile.write("{0},{1}\n".format(now, dataIn)) logFile.flush() - print(dataIn) print(voltage) - diff --git a/software/DataAcquisitionFromDevice/mcp3425.py b/software/DataAcquisitionFromDevice/mcp3425.py new file mode 100644 index 0000000..02c50bf --- /dev/null +++ b/software/DataAcquisitionFromDevice/mcp3425.py @@ -0,0 +1,24 @@ +# based on https://ww1.microchip.com/downloads/en/DeviceDoc/22072b.pdf +# assumed that gain = 1 + +from enum import Enum + +class MCP3425_RESOLUTION(Enum): + R12 = 1 + R13 = 2 + R14 = 3 + +def convert(upperByte, lowerByte, resolution): + digitalToAnalog = lambda value, lsb, pga: (value * (lsb / pga)) + + digitalOutput = (upperByte << 8) | lowerByte; + + if resolution == MCP3425_RESOLUTION.R12: + return digitalToAnalog(digitalOutput, (1 * 0.01), 1) + + if resolution == MCP3425_RESOLUTION.R13: + return digitalToAnalog(digitalOutput, (250 * 0.000001), 1) + + if resolution == MCP3425_RESOLUTION.R14: + return digitalToAnalog(digitalOutput, (62.5 * 0.00001), 1) + diff --git a/software/Firmware/Src/VoltageSensorActualValue.c b/software/Firmware/Src/VoltageSensorActualValue.c index 79ec09c..82ec1d1 100644 --- a/software/Firmware/Src/VoltageSensorActualValue.c +++ b/software/Firmware/Src/VoltageSensorActualValue.c @@ -9,7 +9,6 @@ #include "PinoutConfiguration.h" #include "UserInterface.h" #include "stm8s_i2c.h" -#include "Logger.h" #define I2C_MASTER_ADDRESS 0x10 @@ -18,6 +17,16 @@ // is used to signalize communication direction (rx or tx) #define I2C_SLAVE_ADDRESS (0x68u << 1) +#define MCP3425_REG_BIT_READY (1 << 7) +#define MCP3425_REG_BIT_CONVERSION (1 << 4) +#define MCP3425_REG_BIT_SAMPLE_RATE_UPPER (1 << 3) +#define MCP3425_REG_BIT_SAMPLE_RATE_LOWER (1 << 2) +#define MCP3425_REG_BIT_GAIN_UPPER (1 << 1) +#define MCP3425_REG_BIT_GAIN_LOWER (1 << 0) + +#define MCP3425_CONFIGURATION (MCP3425_REG_BIT_READY | MCP3425_REG_BIT_SAMPLE_RATE_UPPER) +#define MCP3425_READ_MEASSUREMENT 0x10 + static void GPIO_setup(void); static void I2C_setup(void); static void write(uint8_t registerId); @@ -28,15 +37,14 @@ void VoltageSensorActualValue_Init() { GPIO_setup(); I2C_setup(); - - // select adc configuration and start measurement - write(0x00); } bool VoltageSensorActualValue_MeasureValue(VoltageSensorActualValue_MeasurementData_t *measurementData) { - write(0x10); + // select adc configuration and start measurement + write(MCP3425_CONFIGURATION); + *measurementData = read(0); // getRegisterValue should return false on timeout and this should be later propagated to GUI component @@ -53,6 +61,7 @@ void GPIO_setup(void) void I2C_setup(void) { + // TODO magic numbers I2C_DeInit(); I2C_Init(100000, I2C_MASTER_ADDRESS, @@ -105,6 +114,9 @@ static uint16_t read(uint8_t registerId) uint16_t registerLSB3 = I2C_ReceiveData(); while(!I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_RECEIVED)); + uint16_t registerLSB4 = I2C_ReceiveData(); + while(!I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_RECEIVED)); + I2C_AcknowledgeConfig(DISABLE); @@ -119,7 +131,6 @@ static uint16_t read(uint8_t registerId) Logger_Print( registerLSB3); */ -// printf("data: %d %d %d %d %d\r\n", registerMSB, registerLSB, registerLSB1, registerLSB2, registerLSB3); uint16_t registerValue = (registerMSB << 8) + registerLSB; return registerValue;