Ver Fonte

Added ADC1 and TIM1 configuration.

Volodymyr Shylov há 11 meses atrás
pai
commit
37aaed1a2d
5 ficheiros alterados com 46 adições e 26 exclusões
  1. 1 0
      .vscode/c_cpp_properties.json
  2. 5 0
      .vscode/settings.json
  3. 1 1
      Makefile
  4. 1 1
      inc/board.h
  5. 38 24
      src/main.c

+ 1 - 0
.vscode/c_cpp_properties.json

@@ -6,6 +6,7 @@
             "defines": [
                 "STM8S003=",
                 "USE_STDPERIPH_DRIVER=",
+                "__ICCSTM8__=",
                 "_IAR_="
             ],
             "includePath": [

+ 5 - 0
.vscode/settings.json

@@ -0,0 +1,5 @@
+{
+  "makefile.extensionOutputFolder": "./.vscode",
+  "C_Cpp.errorSquiggles": "disabled",
+  "commentTranslate.multiLineMerge": true
+}

+ 1 - 1
Makefile

@@ -51,7 +51,7 @@ APP_OBJECTS = $(notdir $(patsubst %.c,%.o,$(wildcard $(APP_SRC)/*.c)))
 LIB_OBJECTS = $(notdir $(patsubst %.c,%.o,$(wildcard $(LIB_SRC)/*.c)))
 
 # STM8S Peripheral driver object files
-PERIPH_OBJECTS = stm8s_adc1.o
+#PERIPH_OBJECTS = stm8s_adc1.o
 #PERIPH_OBJECTS += stm8s_awu.o
 #PERIPH_OBJECTS += stm8s_beep.o
 #PERIPH_OBJECTS += stm8s_clk.o

+ 1 - 1
inc/board.h

@@ -11,7 +11,7 @@
 #define ADC_PINU  GPIO_PIN_3
 #define ADC_CHNLU ADC1_CHANNEL_4
 #define ADC_SCHTU ADC1_SCHMITTTRIG_CHANNEL4
-#define ADC_PORTU GPIOC
+#define ADC_PORTI  GPIOC
 #define ADC_PINI  GPIO_PIN_4
 #define ADC_CHNLI ADC1_CHANNEL_2
 #define ADC_SCHTI ADC1_SCHMITTTRIG_CHANNEL2

+ 38 - 24
src/main.c

@@ -124,41 +124,55 @@ static void boardInit(void) {
   //GPIOB->ODR |= (GPIO_PIN_4 | GPIO_PIN_5);
   //GPIOB->CR2 |= (GPIO_PIN_4 | GPIO_PIN_5);
 
-  /** Configure ADC */
-  /* De-Init ADC peripheral*/
-  ADC1_DeInit();
-
-  /* Init ADC1 peripheral */
-  ADC1_Init(ADC1_CONVERSIONMODE_SINGLE, ADC_CHNLU, ADC1_PRESSEL_FCPU_D12, \
-            ADC1_EXTTRIG_TIM, ENABLE, ADC1_ALIGN_RIGHT, ADC_SCHTU, DISABLE);
-
-  /* Enable EOC interrupt */
-  ADC1_ITConfig(ADC1_IT_EOCIE, ENABLE);
-
+  /** Configure ADC1 peripheral */
+  /* Configure the data alignment */
+  ADC1->CR2 |= (uint8_t)(ADC1_CR2_ALIGN);
+  /* Set the single conversion mode */
+  ADC1->CR1 &= (uint8_t)(~ADC1_CR1_CONT);
+  /* Clear the ADC1 channels */
+  ADC1->CSR &= (uint8_t)(~ADC1_CSR_CH);
+  /* Select the ADC1 channel */
+  ADC1->CSR |= (uint8_t)(ADC_CHNLU);
+  /* Clear the SPSEL bits */
+  ADC1->CR1 &= (uint8_t)(~ADC1_CR1_SPSEL);
+  /* Select the prescaler division factor according to ADC1_PrescalerSelection values */
+  ADC1->CR1 |= (uint8_t)(ADC1_PRESSEL_FCPU_D2);
+  /* Enable the external Trigger */
+  ADC1->CR2 |= (uint8_t)(ADC1_CR2_EXTTRIG);
+  /* 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;
+  /* Enable the ADC1 EOC interrupt */
+  ADC1->CSR |= (uint8_t)ADC1_IT_EOCIE;
   /*Start Conversion */
-  ADC1_StartConversion();
-
-  /** Configure TIM1 */
-  //TIM1_DeInit();
+  ADC1->CR1 |= ADC1_CR1_ADON;
 
-  /* Time Base configuration */
-  /*
+  /** Configure TIM1 peripheral. Time Base configuration:
   Timer period - 3,125 ms
   TIM1_Period = 50
   TIM1_Prescaler = 1000
   TIM1_CounterMode = TIM1_COUNTERMODE_UP
   TIM1_RepetitionCounter = 0
   */
-  //TIM1_TimeBaseInit(999, TIM1_COUNTERMODE_UP, 49, 0);
-
+  /* Set the Autoreload value */
+  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);
+  /* Select the Counter Mode */
+  TIM1->CR1 = 0x0;
+  /* Set the Repetition Counter value */
+  TIM1->RCR = 0;
   /* Trigrer configuration */
-  //TIM1_SelectOutputTrigger(TIM1_TRGOSOURCE_UPDATE);
-
+  TIM1->CR2 = (uint8_t)TIM1_TRGOSOURCE_UPDATE;
   /* Update Interrupt Enable */
-  ////TIM1_ITConfig(TIM1_IT_UPDATE, ENABLE);
-
+  //TIM1->IER |= (uint8_t)TIM1_IT_UPDATE;
   /* Enable TIM1 */
-  //TIM1_Cmd(ENABLE);
+  TIM1->CR1 |= TIM1_CR1_CEN;
 
   /**
   TIM4 configuration: