Vladimir N. Shilov 1 yıl önce
ebeveyn
işleme
7aa3858b35
4 değiştirilmiş dosya ile 74 ekleme ve 3 silme
  1. 1 1
      cfg/halconf.h
  2. 2 2
      cfg/mcuconf.h
  3. 32 0
      lib/main.h
  4. 39 0
      main.c

+ 1 - 1
cfg/halconf.h

@@ -79,7 +79,7 @@
  * @brief   Enables the GPT subsystem.
  */
 #if !defined(HAL_USE_GPT) || defined(__DOXYGEN__)
-#define HAL_USE_GPT                         FALSE
+#define HAL_USE_GPT                         TRUE
 #endif
 
 /**

+ 2 - 2
cfg/mcuconf.h

@@ -48,7 +48,7 @@
 #define STM32_HPRE                          STM32_HPRE_DIV1
 #define STM32_PPRE1                         STM32_PPRE1_DIV2
 #define STM32_PPRE2                         STM32_PPRE2_DIV2
-#define STM32_ADCPRE                        STM32_ADCPRE_DIV4
+#define STM32_ADCPRE                        STM32_ADCPRE_DIV6
 #define STM32_USB_CLOCK_REQUIRED            FALSE
 #define STM32_USBPRE                        STM32_USBPRE_DIV1P5
 #define STM32_MCOSEL                        STM32_MCOSEL_NOCLOCK
@@ -87,7 +87,7 @@
 /*
  * GPT driver system settings.
  */
-#define STM32_GPT_USE_TIM1                  FALSE
+#define STM32_GPT_USE_TIM1                  TRUE
 #define STM32_GPT_USE_TIM2                  FALSE
 #define STM32_GPT_USE_TIM3                  FALSE
 #define STM32_GPT_USE_TIM4                  FALSE

+ 32 - 0
lib/main.h

@@ -20,6 +20,11 @@
 #define LCD_LINE_8  93
 #define LCD_LINE_9  103
 
+#define ADC_GRP1_NUM_CHANNELS   1
+#define ADC_GRP1_BUF_DEPTH      100
+#define ADC_CHANNEL_NUM         ADC_CHANNEL_IN9
+#define ADC_REF_VOLTAGE         3300
+
 /* Private variables */
 
 /*
@@ -36,6 +41,10 @@ static const SPIConfig spi1cfg = {
   .cr2              = 0U
 };
 
+static adcsample_t ADC_Data[ADC_GRP1_NUM_CHANNELS * ADC_GRP1_BUF_DEPTH];
+//static adcsample_t ADC_Data;
+static uint32_t Voltage;
+
 /* Private function prototypes */
 static void btn1_handler(const button_state_t);
 static void btn2_handler(const button_state_t);
@@ -51,5 +60,28 @@ static btn_hndlr bha[Button_Num] = {
 static void lcd_MainScreen(void);
 static void lcd_ClearMain(void);
 static uint8_t X_centered(const uint8_t len, const uint8_t pix);
+static void ADC_cb(ADCDriver *adcp);
+
+/* Perephireal */
+static const ADCConversionGroup adcgrpcfg1 = {
+  TRUE, // circular
+  ADC_GRP1_NUM_CHANNELS,
+  ADC_cb,
+  NULL,
+  0,                                  /* CR1 */
+  ADC_CR2_EXTTRIG,                    /* CR2, Timer 1 CC1 event */
+  ADC_SMPR1_SMP_AN10(ADC_SAMPLE_71P5),/* smpr1 */
+  0,                                  /* SMPR2 */
+  ADC_SQR1_NUM_CH(ADC_GRP1_NUM_CHANNELS),/* sqr1 */
+  0,                                  /* SQR2 */
+  ADC_SQR3_SQ1_N(ADC_CHANNEL_NUM)     /* sqr3 */
+};
+
+static const GPTConfig gptcfg1 = {
+  .frequency    =  1000000U,
+  .callback     =  NULL,
+  .cr2          =  TIM_CR2_MMS_2, /* MMS = 100 = TRGO on OC1REF. */
+  .dier         =  0U
+};
 
 #endif /* __MAIN_H__ */

+ 39 - 0
main.c

@@ -72,6 +72,18 @@ int main(void) {
    */
   chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
 
+  /* Activates the ADC1 driver and the temperature sensor. */
+  adcStart(&ADCD1, NULL);
+
+  /* Starting GPT3 driver, it is used for triggering the ADC. */
+  gptStart(&GPTD1, &gptcfg1);
+  GPTD1.tim->CCMR1 = 100; // 1Mhz/100=10kHz=100 us
+  GPTD1.tim->EGR = STM32_TIM_EGR_CC1G; //  Capture/Compare 1 generation
+  GPTD1.tim->CCMR1= (STM32_TIM_CCMR1_OC1CE | STM32_TIM_CCMR1_OC1M(3));
+  GPTD1.tim->CCER = STM32_TIM_CCER_CC1E;
+  //?GPTD1.tim->BDTR = STM32_TIM_BDTR_MOE;
+  adcStartConversion(&ADCD1, &adcgrpcfg1, &ADC_Data, ADC_GRP1_BUF_DEPTH); //?
+
   /*
    * Buttons
    */
@@ -109,6 +121,8 @@ int main(void) {
       chsnprintf(buf, 22, "T = %d.%03u °C     ", a, b);
       ST7735_WriteString(0, LCD_LINE_1, buf, LiberM_7x10, Yellow, Black);
     }
+    chsnprintf(buf, 22, "U = %u V", Voltage);
+    ST7735_WriteString(0, LCD_LINE_3, buf, LiberM_7x10, Orange, Black);
     chThdSleepMilliseconds(1000);
   }
 }
@@ -202,3 +216,28 @@ static uint8_t X_centered(const uint8_t len, const uint8_t pix) {
     return (uint8_t)((wdt - pl) / 2);
   }
 }
+
+/* in ADC_Data[] we heave measured data */
+static void ADC_cb(ADCDriver *adcp) {
+  (void)adcp;
+  int i;
+  uint64_t adc_buf = 0;
+
+  for (i=0; i<ADC_GRP1_BUF_DEPTH; i++) {
+    adc_buf += ADC_Data[i] * ADC_Data[i];
+  }
+
+  // adc_buf - sum of squares. SQRT:
+  uint32_t val = 0;
+  uint32_t step = 1;
+  while (step < adc_buf) {
+    adc_buf -= step;
+    step += 2;
+    val ++;
+  }
+  // transform ADC values to real voltage
+  val /= ADC_GRP1_BUF_DEPTH;
+  val *= ADC_REF_VOLTAGE;
+  val /= 4096;
+  Voltage = val;
+}