This commit is contained in:
2025-06-20 18:41:50 +02:00
parent a4aeaa5f91
commit d36f3b85b4
5 changed files with 153 additions and 36 deletions

86
software/src/main2.c Normal file
View File

@@ -0,0 +1,86 @@
#include <zephyr/kernel.h>
#include <zephyr/drivers/adc.h>
#include <zephyr/logging/log.h>
#include <zephyr/device.h>
#include <zephyr/devicetree.h>
#include <stdint.h>
LOG_MODULE_REGISTER(main2, LOG_LEVEL_DBG);
#define MOTOR_ADC_NODE DT_ALIAS(adc_motor_current)
#define VREF_ADC_NODE DT_ALIAS(adc_vref)
static const struct device * const adc_dev = DEVICE_DT_GET(DT_PARENT(MOTOR_ADC_NODE));
static const uint8_t motor_channel_id = DT_REG_ADDR(MOTOR_ADC_NODE);
static const uint8_t vref_channel_id = DT_REG_ADDR(VREF_ADC_NODE);
int main(void)
{
int err;
int16_t adc_raw_value;
LOG_INF("Starting ADC test with direct register setup...");
if (!device_is_ready(adc_dev)) {
LOG_ERR("ADC device is not ready");
return 0;
}
LOG_INF("Manually setting up ADC registers...");
uint32_t adc_base = DT_REG_ADDR(DT_NODELABEL(adc1));
volatile uint32_t *ADC_CR2 = (uint32_t *)(adc_base + 0x08);
volatile uint32_t *ADC_SMPR1 = (uint32_t *)(adc_base + 0x0C);
volatile uint32_t *ADC_SMPR2 = (uint32_t *)(adc_base + 0x10);
// Schritt 1: Internen VREFINT-Kanal einschalten
const uint32_t ADC_CR2_TSVREFE_BIT = 23;
*ADC_CR2 |= (1 << ADC_CR2_TSVREFE_BIT);
LOG_INF("VREFINT channel enabled via CR2 register.");
// Schritt 2: Lange Abtastzeiten für Stabilität setzen
*ADC_SMPR2 |= (0b111 << (3 * 9));
*ADC_SMPR1 |= (0b111 << (3 * (17 - 10)));
LOG_INF("Acquisition times set directly in SMPR registers.");
k_busy_wait(10);
while (1) {
int32_t motor_raw = 0;
int32_t vref_raw = 0;
struct adc_sequence sequence = {
.buffer = &adc_raw_value,
.buffer_size = sizeof(adc_raw_value),
.resolution = 12,
};
// Motor-Kanal lesen
sequence.channels = BIT(motor_channel_id);
if (adc_read(adc_dev, &sequence) == 0) {
motor_raw = adc_raw_value;
}
// VREF-Kanal lesen
sequence.channels = BIT(vref_channel_id);
if (adc_read(adc_dev, &sequence) == 0) {
vref_raw = adc_raw_value;
}
// VDD-Berechnung mit dem generischen, aber für Sie gut funktionierenden 1200mV-Wert
int32_t vdd_mv = (vref_raw > 0) ? (1200 * 4095 / vref_raw) : 0;
int32_t motor_mv = 0;
if (motor_raw > 0 && vdd_mv > 0) {
motor_mv = motor_raw;
err = adc_raw_to_millivolts(vdd_mv, ADC_GAIN_1, 12, &motor_mv);
}
LOG_INF("Motor Raw: %4d, Motor mV: %4d | VDD: %4d mV", motor_raw, motor_mv, vdd_mv);
k_sleep(K_MSEC(2000));
}
return 0;
}