Просмотр исходного кода

ADC read from Controller Board Logic

namrota ghosh 6 месяцев назад
Родитель
Сommit
d1534d7de5
1 измененных файлов с 51 добавлено и 0 удалено
  1. 51 0
      src/adc_peripheral/adc_singleConversion.c

+ 51 - 0
src/adc_peripheral/adc_singleConversion.c

@@ -0,0 +1,51 @@
+#include "ti_msp_dl_config.h"
+#include "src/battery_data/battery.h"
+
+
+/* ADC variables:
+ * bare_voltage gets the ADC value from the memory register
+ * checkADC boolean flag 
+ */
+
+volatile uint16_t bare_voltage;
+bool checkADC = false;
+
+void processADC(){
+    uint16_t gADCvoltage= 0;
+    int16_t gADCcurrent= 0;
+    //Start the ADC Sampling:
+    DL_ADC12_startConversion(ADC_Controller_INST);
+
+    /* Sleep until the ADC sampling and conversion is finished, then reset the boolean */
+    while(checkADC == false){
+        __WFI();
+    }
+
+    bare_voltage = DL_ADC12_getMemResult(ADC_Controller_INST, DL_ADC12_MEM_IDX_0);
+
+    // if any invalid value then ignore
+    if (bare_voltage == 0xffff) {
+        // the voltage reading is invalid -> ignore this cycle.
+        return;
+    }
+
+    gADCvoltage = bare_voltage*(56+100)/56; // Because of the voltage divider
+    gADCcurrent= bare_voltage*10/1000;
+
+    
+
+    checkADC = false;
+    DL_ADC12_enableConversions(ADC_Controller_INST);
+}
+
+
+void ADC_Controller_INST_IRQHandler(void)
+{
+    switch (DL_ADC12_getPendingInterrupt(ADC_Controller_INST)) {
+        case DL_ADC12_IIDX_MEM0_RESULT_LOADED:
+            checkADC = true;
+            break;
+        default:
+            break;
+    }
+}