Эх сурвалжийг харах

Small fixes. Need for corrections.

Vladimir N. Shilov 1 жил өмнө
parent
commit
c701e40545
2 өөрчлөгдсөн 19 нэмэгдсэн , 15 устгасан
  1. 12 12
      inc/board.h
  2. 7 3
      src/main.c

+ 12 - 12
inc/board.h

@@ -15,21 +15,21 @@
 #define ADC_CHNLI ADC1_CHANNEL_4
 #define ADC_CHNLI ADC1_CHANNEL_4
 #define ADC_SCHTI ADC1_SCHMITTTRIG_CHANNEL4
 #define ADC_SCHTI ADC1_SCHMITTTRIG_CHANNEL4
 #define ADC_SMPLS 64
 #define ADC_SMPLS 64
-#define ADC_RES   1024U
+#define ADC_RES   1023UL
 // Supply voltage in mili volts
 // Supply voltage in mili volts
-#define ADC_VREF  3335U
+#define ADC_VREF  3030UL
 // Voltage channel
 // Voltage channel
-#define VOLTAGE_MULT_TOP  15
-#define VOLTAGE_MULT_BOT  100
-#define VOLT_MUL_K        100U
-#define VOLTAGE_MUL       (uint16_t)(VOLT_MUL_K * (VOLTAGE_MULT_BOT * ADC_VREF) / (VOLTAGE_MULT_TOP * ADC_RES))
+#define VOLTAGE_MULT_TOP  15UL
+#define VOLTAGE_MULT_BOT  100UL
+#define VOLT_MUL_K        100UL
+#define VOLT_MUL_DIV      (VOLTAGE_MULT_TOP * ADC_RES)
+#define VOLTAGE_MUL       (uint16_t)((VOLT_MUL_K * (VOLTAGE_MULT_BOT * ADC_VREF) + (VOLT_MUL_DIV/2)) / VOLT_MUL_DIV)
 // Current channel
 // Current channel
 #define CURREN_SHUNT      (1/0.01)
 #define CURREN_SHUNT      (1/0.01)
-#define CURRENT_MULT_TOP  15
-#define CURRENT_MULT_BOT  1
-#define CUR_MUL_K         100U
-#define CURRENT_MUL       (uint16_t)(CUR_MUL_K * (CURRENT_MULT_BOT * ADC_VREF * CURREN_SHUNT) / (CURRENT_MULT_TOP * ADC_RES))
-// shunt resistance in mili Ohms
-#define ADC_SHUNT 10
+#define CURRENT_MULT_TOP  15UL
+#define CURRENT_MULT_BOT  1UL
+#define CUR_MUL_K         100UL
+#define CUR_MUL_DIV       (CURRENT_MULT_TOP * ADC_RES)
+#define CURRENT_MUL       (uint16_t)((CUR_MUL_K * (CURRENT_MULT_BOT * ADC_VREF * CURREN_SHUNT) + (CUR_MUL_DIV/2)) / CUR_MUL_DIV)
 
 
 #endif /* __BOARD_H */
 #endif /* __BOARD_H */

+ 7 - 3
src/main.c

@@ -52,13 +52,15 @@ void main(void)
   boardInit();
   boardInit();
   Delay(1000);
   Delay(1000);
 
 
+  int8_t i;
+  uint32_t tbuf;
   /* Infinite loop */
   /* Infinite loop */
   while (1) {
   while (1) {
-    wfi();
+    //wfi();
+    Delay(100);
     if (BufferIndex >= ADC_SMPLS) {
     if (BufferIndex >= ADC_SMPLS) {
       BufferIndex = 0;
       BufferIndex = 0;
-      int8_t i;
-      uint32_t tbuf = 0;
+      tbuf = 0;
       for (i=0; i<ADC_SMPLS; i++) {
       for (i=0; i<ADC_SMPLS; i++) {
         tbuf += ConversionBuffer[i];  
         tbuf += ConversionBuffer[i];  
       }
       }
@@ -70,6 +72,7 @@ void main(void)
 
 
         tbuf /= ADC_SMPLS;
         tbuf /= ADC_SMPLS;
         tbuf *= VOLTAGE_MUL;
         tbuf *= VOLTAGE_MUL;
+        tbuf += VOLT_MUL_K/2;
         tbuf /= VOLT_MUL_K;
         tbuf /= VOLT_MUL_K;
         Voltage = tbuf;
         Voltage = tbuf;
       } else {
       } else {
@@ -79,6 +82,7 @@ void main(void)
 
 
         tbuf /= ADC_SMPLS;
         tbuf /= ADC_SMPLS;
         tbuf *= CURRENT_MUL;
         tbuf *= CURRENT_MUL;
+        tbuf += CUR_MUL_K/2;
         tbuf /= CUR_MUL_K;
         tbuf /= CUR_MUL_K;
         Current = tbuf;
         Current = tbuf;
       }
       }