Ver código fonte

Berechnung der Auflösung funktioniert jetzt korrekt

Heinrich Blatt 7 meses atrás
pai
commit
0a6dffb608

+ 1 - 1
src/battery_data/battery.c

@@ -91,7 +91,7 @@ static void batteryslots_read_state(uint8_t slot) {
     if (battery_slots[slot].set_current >= 0) {
         // read channel 1 (current reading on charge)
         bare_voltage = read_adc_channel(slot, 1);
-        battery_slots[slot].measurement.current = bare_voltage*10;
+        battery_slots[slot].measurement.current = bare_voltage*10/1000; // current comes in microvolts
 #ifdef DEBUG_TRACE_CTRL
         printf("Slot %d voltage: %d mV and %d mA (dac shunt)\n", slot, battery_slots[slot].measurement.voltage, battery_slots[slot].measurement.current);
 #endif

+ 3 - 2
src/peripherals/adc/adc.c

@@ -20,18 +20,19 @@ uint16_t read_adc_channel(uint8_t slot, uint8_t channel) {
         
         case ADC_STATE_CONFIGURE:
             adc_params.channel = channel;
-            adc_params.resolution = 12;
             adc_params.continuous = ADC_MEASUREMENT_IS_CONTINUOUS;
             if (channel == 0 || channel == 3) {
                 // voltage measurement
                 // -> we can measure directly
                 adc_params.gain = 1;
                 adc_params.resolution = 12;
+                adc_params.factor = 1;
             } else {
                 // current measurement
                 // -> maximum gain, max resolution
                 adc_params.gain = 8;
                 adc_params.resolution = 16;
+                adc_params.factor = 1000; // get microvolts
             }
             //printf("Config: Memory address of batteries: %p\n", &batteries[0]);
             adc_hal.configure(slot, &adc_params);
@@ -54,7 +55,7 @@ uint16_t read_adc_channel(uint8_t slot, uint8_t channel) {
         case ADC_STATE_READ:
             adc_voltage = adc_hal.read_voltage(slot, &adc_params);
 #ifdef DEBUG_ADC
-            printf("[ADC] ADC reading completed. Slot %d Channel %d is %d mV.\n", slot, channel, adc_voltage);
+            printf("[ADC] ADC reading completed. Slot %d Channel %d is %d \n", slot, channel, adc_voltage);
 #endif
             adc_state = ADC_STATE_DONE;
 

+ 6 - 7
src/peripherals/adc/adc_hal.c

@@ -193,21 +193,20 @@ static int16_t read_adc_raw_data(uint8_t slot_id, ADC_Params *params) {
     uint8_t lsb = controllerRxPackage.packet[1];
     uint8_t config_adc_byte = controllerRxPackage.packet[2];
 
+    uint32_t max_adc_val = 0;
+
     switch (params->resolution) {
         case 12: // 12-bit
             raw_adc = ((msb & 0b00001111) << 8) | lsb;
-            if (raw_adc > 2047)
-                raw_adc -= 4096;
+            max_adc_val = 4096;
             break;
         case 14: // 14-bit
             raw_adc = ((msb & 0b00111111) << 8) | lsb;
-            if (raw_adc > 16384/2)
-                raw_adc -= 16384;
+            max_adc_val = 16384;
             break;
         case 16: // 16-bit
             raw_adc = ((msb & 0b11111111) << 8) | lsb;
-            if (raw_adc > 65535/2)
-                raw_adc -= 65536;
+            max_adc_val = 65536;
             break;
         default:
             //printf("Error: Unknown ADC Resolution!\n");
@@ -217,7 +216,7 @@ static int16_t read_adc_raw_data(uint8_t slot_id, ADC_Params *params) {
 #ifdef DEBUG_ADC
     printf("Raw ADC: %d, MSB: 0x%02X (%d) LSB: 0x%02X (%d) Config: 0x%02X\n", raw_adc, msb, msb, lsb, lsb, config_adc_byte);
 #endif
-    return raw_adc / params->gain;
+    return raw_adc * params->factor * 2048 / (max_adc_val/2) / params->gain;
 }
 
 ADC_Interface adc_hal= {

+ 1 - 0
src/peripherals/adc/adc_interface.h

@@ -11,6 +11,7 @@ typedef struct {
     uint8_t resolution;
     bool continuous;
     uint8_t gain;
+    uint16_t factor;
 } ADC_Params;
 
 // 14.04: added pointer for ADC_Params