Selaa lähdekoodia

Added Current chanel. Need for testing.

Vladimir N. Shilov 11 kuukautta sitten
vanhempi
commit
a0d572c0eb
2 muutettua tiedostoa jossa 64 lisäystä ja 37 poistoa
  1. 19 16
      inc/board.h
  2. 45 21
      src/main.c

+ 19 - 16
inc/board.h

@@ -8,27 +8,30 @@
 
 /* ADC */
 #define ADC_PORTU GPIOD
-#define ADC_PINU  GPIO_PIN_3
-#define ADC_CHNLU ADC1_CHANNEL_4
-#define ADC_SCHTU ADC1_SCHMITTTRIG_CHANNEL4
-#define ADC_PORTI  GPIOC
-#define ADC_PINI  GPIO_PIN_4
-#define ADC_CHNLI ADC1_CHANNEL_2
-#define ADC_SCHTI ADC1_SCHMITTTRIG_CHANNEL2
-#define ADC_SMPLS 64
+#define ADC_PINU          GPIO_PIN_3
+#define ADC_CHNLU         ADC1_CHANNEL_4
+#define ADC_SCHTU         (uint8_t)ADC1_SCHMITTTRIG_CHANNEL4
+#define ADC_PORTI         GPIOC
+#define ADC_PINI          GPIO_PIN_4
+#define ADC_CHNLI         ADC1_CHANNEL_2
+#define ADC_SCHTI         (uint8_t)ADC1_SCHMITTTRIG_CHANNEL2
+#define ADC_SMPLS         64
 // Supply voltage in mili volts
-#define ADC_VREF  3333UL
-// Сопротивление резисторов делителя входного напряжения
+#define ADC_VREF          3333UL
+// Сопротивление резисторов делителя входного напряжения, кОм/100
 #define VOLTAGE_MULT_TOP  393UL
 #define VOLTAGE_MULT_BOT  110UL
-#define VOLT_MUL_MUL      100
+#define VOLT_MUL_MUL      100UL
 #define VOLTAGE_MUL_DIV   (VOLTAGE_MULT_BOT * 1023UL)
 #define VOLTAGE_MUL       (uint16_t)((VOLT_MUL_MUL * ADC_VREF * (VOLTAGE_MULT_TOP+VOLTAGE_MULT_BOT) + (VOLTAGE_MUL_DIV / 2)) / VOLTAGE_MUL_DIV)
-// Сопротивление резисторов ОУ усилителя сигнала шунта
-#define CURRENT_MULT_TOP  19000
-#define CURRENT_MULT_BOT  1200
-// shunt resistance in mili Ohms
-#define ADC_SHUNT 10
+// shunt resistance in Ohms
+#define CURRENT_SHUNT     (1/0.01)
+// Сопротивление резисторов ОУ усилителя сигнала шунта, кОм/100
+#define CURRENT_MULT_TOP  190UL
+#define CURRENT_MULT_BOT  12UL
+#define CURR_MUL_MUL      100UL
+#define CURRENT_MUL_DIV   (1023UL * (CURRENT_MULT_TOP + CURRENT_MULT_BOT))
+#define CURRENT_MUL       (uint16_t)(((CURR_MUL_MUL * ADC_VREF * CURRENT_SHUNT * CURRENT_MULT_BOT) + (CURRENT_MUL_DIV / 2)) / CURRENT_MUL_DIV)
 // Current commutator
 #define SWITCH_PORT GPIOD
 #define SWITCH_PIN  GPIO_PIN_6

+ 45 - 21
src/main.c

@@ -1,4 +1,4 @@
-/**
+/**current_mode
   ******************************************************************************
   * @file    Project/main.c
   * @author  "Volodymyr M. Shylov" <shilow@ukr.net>
@@ -27,11 +27,16 @@
 #define TIM4_PERIOD  (uint8_t)124
 
 /* Private typedef -----------------------------------------------------------*/
+typedef enum {
+  voltage_mode,
+  current_mode
+} measure_mode_t;
 /* Private constants ---------------------------------------------------------*/
 /* Private variables ---------------------------------------------------------*/
 __IO uint16_t ConversionBuffer[ADC_SMPLS];
 __IO uint8_t BufferIndex = 0;
-static uint16_t Voltage;
+static uint16_t Voltage, Current;
+static measure_mode_t MeasureMode = voltage_mode;
 
 /* Private function prototypes -----------------------------------------------*/
 static void boardInit(void);
@@ -46,21 +51,39 @@ void main(void)
 
   /* Infinite loop */
   while (1) {
-    Delay(100);
+    wfi();
     if (BufferIndex >= ADC_SMPLS) {
       BufferIndex = 0;
       int8_t i;
-      uint32_t vbuf = 0;
+      uint32_t tbuf = 0;
       for (i=0; i<ADC_SMPLS; i++) {
-        vbuf += ConversionBuffer[i];  
+        tbuf += ConversionBuffer[i];  
       }
-      vbuf /= ADC_SMPLS;
-      vbuf *= VOLTAGE_MUL;
-      vbuf /= VOLT_MUL_MUL;
-      Voltage = vbuf;
+
+      if (MeasureMode == voltage_mode) {
+        MeasureMode = current_mode;
+        ADC1->CSR &= (uint8_t)(~ADC1_CSR_CH);
+        ADC1->CSR |= (uint8_t)(ADC_CHNLI);
+
+        tbuf /= ADC_SMPLS;
+        tbuf *= VOLTAGE_MUL;
+        tbuf /= VOLT_MUL_MUL;
+        Voltage = tbuf;
+      } else {
+        MeasureMode = voltage_mode;
+        ADC1->CSR &= (uint8_t)(~ADC1_CSR_CH);
+        ADC1->CSR |= (uint8_t)(ADC_CHNLU);
+
+        tbuf /= ADC_SMPLS;
+        tbuf *= CURRENT_MUL;
+        tbuf /= CURR_MUL_MUL;
+        Current = tbuf;
+      }
+
+      /* arrived new data, show it */
+      showV();
+      showC();
     }
-    showV();
-    showC();
   }
 
 }
@@ -77,10 +100,13 @@ static void showV(void) {
 }
 
 static void showC(void) {
- LedDigits[4] = (uint8_t)(VOLTAGE_MUL>>8 & 0xf );
- LedDigits[5] = (uint8_t)(VOLTAGE_MUL>>4 & 0xf);
- LedDigits[6] = (uint8_t)(VOLTAGE_MUL & 0xf);
- LedDigits[7] = (uint8_t)(VOLTAGE_MUL>>12);
+  uint16_t a = Current;
+  LedDigits[4] = a / 1000;
+  uint16_t b = a % 1000;
+  LedDigits[5] = b / 100;
+  uint8_t c = b % 100;
+  LedDigits[6] = c / 10;
+  LedDigits[7] = c % 10;
 }
 
 static void boardInit(void) {
@@ -158,12 +184,10 @@ static void boardInit(void) {
   /* Set the 'Internal TIM1 TRGO event' as external trigger */
   ADC1->CR2 &= (uint8_t)~(ADC1_CR2_EXTSEL);
   /* disables the ADC1 Schmitt Trigger on a selected channel(s) */
-  ADC1->TDRL |= (uint8_t)((uint8_t)0x01 << (uint8_t)ADC_SCHTU);
-  /* Enable the ADC1 peripheral */
-  ADC1->CR1 |= ADC1_CR1_ADON;
+  ADC1->TDRL |= (uint8_t)((1 << ADC_SCHTU) | (1 << ADC_SCHTI));
   /* Enable the ADC1 EOC interrupt */
   ADC1->CSR |= (uint8_t)ADC1_IT_EOCIE;
-  /*Start Conversion */
+  /* Enable the ADC1 peripheral */
   ADC1->CR1 |= ADC1_CR1_ADON;
 
   /** Configure TIM1 peripheral. Time Base configuration:
@@ -177,8 +201,8 @@ static void boardInit(void) {
   TIM1->ARRH = (uint8_t)(49 >> 8);
   TIM1->ARRL = (uint8_t)(49);
   /* Set the Prescaler value */
-  TIM1->PSCRH = (uint8_t)(999 >> 8);
-  TIM1->PSCRL = (uint8_t)(999);
+  TIM1->PSCRH = (uint8_t)(499 >> 8);
+  TIM1->PSCRL = (uint8_t)(499);
   /* Select the Counter Mode */
   TIM1->CR1 = 0x0;
   /* Set the Repetition Counter value */