Kalibrierung wird im EEPROM gespeichert.

Nach Abschluss  der Kalibrierung wird das System neu gestartet um alle Daten zu laden.
This commit is contained in:
Carsten Keller 2024-06-09 17:58:16 +02:00
parent 15075e001d
commit 5eb047e240
Signed by: carsten
GPG Key ID: DF06343A3A9B8868
2 changed files with 42 additions and 8 deletions

View File

@ -11,6 +11,7 @@
#include <cinttypes> #include <cinttypes>
#include <cmath> #include <cmath>
#include <stm32g0xx.h> #include <stm32g0xx.h>
#include <stm32g0xx_ll_lpuart.h>
#include "STM32G071KBT6.hpp" #include "STM32G071KBT6.hpp"
#include "LED.hpp" #include "LED.hpp"
#include "DAC.hpp" #include "DAC.hpp"
@ -23,8 +24,11 @@
using namespace ElektronischeLast; using namespace ElektronischeLast;
static void do_icall(iADC& adc); #define EEPROM_ADDRESS_VOLTAGE 0U
static void do_ucall(iADC& dac); #define EEPROM_ADDRESS_CURRENT 4U
static void do_icall(iADC& adc, iI2C& eeprom);
static void do_ucall(iADC& dac, iI2C& eeprom);
uint16_t set_solltrom (CLI_OutFunction pfvOutFunction, char *acCommands[], uint16_t u16ArgCount); uint16_t set_solltrom (CLI_OutFunction pfvOutFunction, char *acCommands[], uint16_t u16ArgCount);
uint16_t set_dutyCyle (CLI_OutFunction pfvOutFunction, char *acCommands[], uint16_t u16ArgCount); uint16_t set_dutyCyle (CLI_OutFunction pfvOutFunction, char *acCommands[], uint16_t u16ArgCount);
@ -71,8 +75,8 @@ int main (void)
float voltage_gain = NAN; float voltage_gain = NAN;
float current_gain = NAN; float current_gain = NAN;
eeprom.read(0UL, 4UL, (uint8_t*)&voltage_gain); eeprom.read(EEPROM_ADDRESS_VOLTAGE, 4UL, (uint8_t*)&voltage_gain);
eeprom.read(4UL, 4UL, (uint8_t*)&current_gain); eeprom.read(EEPROM_ADDRESS_CURRENT, 4UL, (uint8_t*)&current_gain);
if(std::isnan(voltage_gain)) if(std::isnan(voltage_gain))
{ {
voltage_gain = 22.272125f; voltage_gain = 22.272125f;
@ -88,6 +92,8 @@ int main (void)
printf("\r\nElektronische Last\r\n"); printf("\r\nElektronische Last\r\n");
printf("- Initialisierung erfolgreich\r\n"); printf("- Initialisierung erfolgreich\r\n");
printf("- Verstärkung U: %f, Verstärkung I: %f\r\n", voltage_gain, current_gain);
printf("Elektronische Last>");
PIDController_Init(&pid); PIDController_Init(&pid);
@ -101,10 +107,14 @@ int main (void)
switch(modus) switch(modus)
{ {
case icall: case icall:
do_icall(adc); do_icall(adc, eeprom);
while(!LL_LPUART_IsActiveFlag_TC(LPUART1));
NVIC_SystemReset();
break; break;
case ucall: case ucall:
do_ucall(adc); do_ucall(adc, eeprom);
while(!LL_LPUART_IsActiveFlag_TC(LPUART1));
NVIC_SystemReset();
break; break;
case normal: case normal:
default: default:
@ -212,26 +222,28 @@ uint16_t set_icall(CLI_OutFunction pfvOutFunction, char *acCommands[], uint16_t
return 0U; return 0U;
} }
static void do_icall(iADC& adc) static void do_icall(iADC& adc, iI2C& eeprom)
{ {
strom += adc.get_current_raw(); strom += adc.get_current_raw();
spannung++; spannung++;
if(spannung == 1000UL) if(spannung == 1000UL)
{ {
float current_gain = 1.0f / ((float)strom / 1000.0f * 3.3f / 4096.0f); float current_gain = 1.0f / ((float)strom / 1000.0f * 3.3f / 4096.0f);
eeprom.write(EEPROM_ADDRESS_CURRENT, sizeof(float), (uint8_t*)&current_gain);
printf("\r\nStromverstärkung: %f", current_gain); printf("\r\nStromverstärkung: %f", current_gain);
modus = normal; modus = normal;
spannung = 0UL; spannung = 0UL;
strom = 0UL; strom = 0UL;
} }
} }
static void do_ucall(iADC& adc) static void do_ucall(iADC& adc, iI2C& eeprom)
{ {
spannung += adc.get_voltage_raw(); spannung += adc.get_voltage_raw();
strom++; strom++;
if(strom == 1000UL) if(strom == 1000UL)
{ {
float voltage_gain = 15.0f / ((float)spannung / 1000.0f * 3.3f / 4096.0f); float voltage_gain = 15.0f / ((float)spannung / 1000.0f * 3.3f / 4096.0f);
eeprom.write(EEPROM_ADDRESS_VOLTAGE, sizeof(float), (uint8_t*)&voltage_gain);
printf("\r\nSpannungsverstärkung: %f", voltage_gain); printf("\r\nSpannungsverstärkung: %f", voltage_gain);
modus = normal; modus = normal;
spannung = 0UL; spannung = 0UL;

View File

@ -95,6 +95,28 @@ namespace ElektronischeLast
} }
bool iI2C::write(std::uint32_t address, std::uint32_t length, std::uint8_t buffer[]) bool iI2C::write(std::uint32_t address, std::uint32_t length, std::uint8_t buffer[])
{ {
std::uint32_t transfered = 0UL;
LL_I2C_SetSlaveAddr(I2C2, this->device_address + ((address >= 256U) ? 2U : 0U));
LL_I2C_SetTransferRequest(I2C2, LL_I2C_REQUEST_WRITE);
LL_I2C_SetTransferSize(I2C2, 1U + length);
LL_I2C_EnableAutoEndMode(I2C2);
LL_I2C_GenerateStartCondition(I2C2);
while (transfered < (length + 1U))
{
if(LL_I2C_IsActiveFlag_TXIS(I2C2))
{
if(transfered == 0U)
{
LL_I2C_TransmitData8(I2C2, address & 0xFFU);
transfered++;
}
else
{
LL_I2C_TransmitData8(I2C2, buffer[transfered++ - 1U]);
}
}
}
return true; return true;
} }
} }