Browse Source

Майже...

Vladimir N. Shilov 4 months ago
parent
commit
cc2be045a7
4 changed files with 23 additions and 5 deletions
  1. 1 0
      inc/stm32g0xx_it.h
  2. 9 3
      src/board.c
  3. 4 2
      src/main.c
  4. 9 0
      src/stm32g0xx_it.c

+ 1 - 0
inc/stm32g0xx_it.h

@@ -34,6 +34,7 @@ void HardFault_Handler(void);
 void SVC_Handler(void);
 void PendSV_Handler(void);
 void ADC1_IRQHandler(void);
+void TIM1_BRK_UP_TRG_COM_IRQHandler(void);
 
 #ifdef __cplusplus
 }

+ 9 - 3
src/board.c

@@ -49,7 +49,7 @@ void Board_Init(void)
   //GPIO_SetPinSpeed(GPIOA, GPIO_PIN_8, GPIO_OSPEED_VH);
   //GPIO_SetPinPull(GPIOA, GPIO_PIN_8, GPIO_PUPDR_NO);
 
-  //ADC_Init();
+  ADC_Init();
 
   TIM1_Init();
   TIM3_Init();
@@ -134,7 +134,8 @@ static void ADC_Init(void)
   ADC1->ISR |= ADC_ISR_CCRDY;
 
   /* External trigger - Tim3 */
-  ADC1->CFGR1 |= (ADC_CFGR1_EXTSEL_1 | ADC_CFGR1_EXTSEL_0 | ADC_CFGR1_EXTEN_0 | ADC_CFGR1_OVRMOD);
+  //ADC1->CFGR1 |= (ADC_CFGR1_EXTSEL_1 | ADC_CFGR1_EXTSEL_0 | ADC_CFGR1_EXTEN_0 | ADC_CFGR1_OVRMOD);
+  ADC1->CFGR1 = ADC_CFGR1_OVRMOD;
 
    /* Enable ADC internal voltage regulator */
    ADC1->CR |= ADC_CR_ADVREGEN; // ???
@@ -169,6 +170,9 @@ static void ADC_Init(void)
   */
 static void TIM1_Init(void)
 {
+  /* TIM1 interrupt Init */
+  NVIC_SetPriority(TIM1_BRK_UP_TRG_COM_IRQn, 0);
+  NVIC_EnableIRQ(TIM1_BRK_UP_TRG_COM_IRQn);
   /* target clock */
   TIM1->PSC = TIM1_PSC; // prescaler
   TIM1->ARR = TIM1_ARR; // auto reload value
@@ -184,6 +188,8 @@ static void TIM1_Init(void)
   TIM1->EGR = TIM_EGR_UG; // force timer update
   /* TIM1 CC_EnableChannel */
   TIM1->CCER = (TIM_CCER_CC1E | TIM_CCER_CC1P | TIM_CCER_CC4E | TIM_CCER_CC4P);
+  /* Enable interrupts */
+  TIM1->DIER = TIM_DIER_UIE;
   /* TIM_EnableCounter */
   TIM1->CR1 |= TIM_CR1_CEN;
 }
@@ -198,7 +204,7 @@ static void TIM3_Init(void)
   /* target clock */
   TIM3->PSC = TIM3_PSC; // prescaler
   TIM3->ARR = TIM3_ARR; // auto reload value
-  TIM3->CR1 = TIM_CR1_ARPE;
+  TIM3->CR1 = TIM_CR1_ARPE | TIM_CR1_DIR;
   // launch timer
   TIM3->EGR = TIM_EGR_UG; // force timer update
   /* Set the trigger output 2 (TRGO2) used for ADC synchronization */

+ 4 - 2
src/main.c

@@ -61,10 +61,10 @@ int main(void)
   uart_putc('0');
   xputc('\n');
   while (photo_Value <= PHOTO_LEVEL) {
-  //  IWDG->KR = 0xAAAA;
-  //  __WFI();
     delay_ms(1000);
     xprintf("photo_Value: %u\n", photo_Value);
+  //  IWDG->KR = 0xAAAA;
+    __WFI();
   }
   uart_putc('1');
 
@@ -73,6 +73,8 @@ int main(void)
   {
     /* wait for light on */
     while (photo_Value > PHOTO_LEVEL) {
+    delay_ms(1000);
+    xprintf("photo_Value: %u\n", photo_Value);
       IWDG->KR = 0xAAAA;
       __WFI();
     }

+ 9 - 0
src/stm32g0xx_it.c

@@ -94,4 +94,13 @@ void ADC1_IRQHandler(void)
   ADC1->ISR |= ADC_ISR_EOC;
 }
 
+void TIM1_BRK_UP_TRG_COM_IRQHandler(void)
+{
+  if ((TIM1->SR & TIM_SR_UIF) != 0) {
+    /* Update interrupt flag */
+    TIM1->SR &= ~TIM_SR_UIF;
+  }
+  ADC1->CR |= ADC_CR_ADSTART;
+}
+
 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/