Selaa lähdekoodia

Fix for current requirements.

Vladimir N. Shilov 3 kuukautta sitten
vanhempi
commit
2982d5543f
3 muutettua tiedostoa jossa 62 lisäystä ja 34 poistoa
  1. 14 8
      inc/board.h
  2. 12 6
      src/board.c
  3. 36 20
      src/main.c

+ 14 - 8
inc/board.h

@@ -14,8 +14,8 @@
 /* PWM Timers for 50 Hz */
 #define TIM1_PSC            (24 - 1)
 #define TIM1_ARR            (20000 - 1)
-#define SERVO_INIT_VAL      1000
-#define SERVO_OPEN_VAL      2000
+#define SERVO_INIT_VAL      2000
+#define SERVO_OPEN_VAL      1000
 
 #define TIM3_PSC            (24000 - 1)
 #define TIM3_ARR            (100 - 1)
@@ -25,12 +25,18 @@
 #define Select_Port   GPIOA
 #define Photo_Pin     GPIO_PIN_7
 #define Photo_Port    GPIOB
-#define Servo_1_Pin   GPIO_PIN_8
-#define Servo_1_Port  GPIOA
-#define Servo_2_Pin   GPIO_PIN_3
-#define Servo_2_Port  GPIOB
-#define Servo_1_CHNL  TIM1->CCR1
-#define Servo_2_CHNL  TIM1->CCR2
+#define Servo_1_Pin   GPIO_PIN_3
+#define Servo_1_Port  GPIOB
+#define Servo_1_CHNL  TIM1->CCR2
+#define Servo_1_ON    GPIO_SetPinMode(Servo_1_Port, Servo_1_Pin, GPIO_MODE_AFF)
+#define Servo_1_OFF   GPIO_SetPinMode(Servo_1_Port, Servo_1_Pin, GPIO_MODE_IN)
+#ifdef USE_SERVO_2
+  #define Servo_2_Pin   GPIO_PIN_8
+  #define Servo_2_Port  GPIOA
+  #define Servo_2_CHNL  TIM1->CCR1
+  #define Servo_2_ON    GPIO_SetPinMode(Servo_2_Port, Servo_2_Pin, GPIO_MODE_AFF)
+  #define Servo_2_OFF   GPIO_SetPinMode(Servo_2_Port, Servo_2_Pin, GPIO_MODE_IN)
+#endif
 
 /* Variables */
 

+ 12 - 6
src/board.c

@@ -87,17 +87,18 @@ void SystemClock_Config(void)
 static void GPIO_Init(void)
 {
   /* Servo_1_Pin, Servo_2_Pin - Servos control, Alt PP out, middle speed */
-  GPIO_SetAFPin_8_15(Servo_1_Port, Servo_1_Pin, GPIO_AF_2);
+  GPIO_SetAFPin_0_7(Servo_1_Port, Servo_1_Pin, GPIO_AF_1);
   GPIO_SetPinOutputType(Servo_1_Port, Servo_1_Pin, GPIO_OTYPE_PP);
   GPIO_SetPinPull(Servo_1_Port, Servo_1_Pin, GPIO_PUPDR_NO);
   GPIO_SetPinSpeed(Servo_1_Port, Servo_1_Pin, GPIO_OSPEED_LW);
   GPIO_SetPinMode(Servo_1_Port, Servo_1_Pin, GPIO_MODE_AFF);
-
-  GPIO_SetAFPin_0_7(Servo_2_Port, Servo_2_Pin, GPIO_AF_1);
+#ifdef USE_SERVO_2
+  GPIO_SetAFPin_8_15(Servo_2_Port, Servo_2_Pin, GPIO_AF_2);
   GPIO_SetPinOutputType(Servo_2_Port, Servo_2_Pin, GPIO_OTYPE_PP);
   GPIO_SetPinPull(Servo_2_Port, Servo_2_Pin, GPIO_PUPDR_NO);
   GPIO_SetPinSpeed(Servo_2_Port, Servo_2_Pin, GPIO_OSPEED_LW);
   GPIO_SetPinMode(Servo_2_Port, Servo_2_Pin, GPIO_MODE_AFF);
+#endif
 
   /* Photo_Pin: analog in, pull none */
   GPIO_SetPinPull(Photo_Port, Photo_Pin, GPIO_PUPDR_NO);
@@ -173,13 +174,18 @@ static void TIM1_Init(void)
   TIM1->CCR2 = SERVO_INIT_VAL;
   TIM1->CCR4 = SERVO_INIT_VAL;
   // pwm mode 1 for chanels
-  TIM1->CCMR1 = (TIM_CCMR1_OC1M_1 | TIM_CCMR1_OC1M_2 | TIM_CCMR1_OC1PE);
-  TIM1->CCMR1 |= (TIM_CCMR1_OC2M_1 | TIM_CCMR1_OC2M_2 | TIM_CCMR1_OC2PE);
+  TIM1->CCMR1 = (TIM_CCMR1_OC2M_1 | TIM_CCMR1_OC2M_2 | TIM_CCMR1_OC2PE);
+#ifdef USE_SERVO_2
+  TIM1->CCMR1 |= (TIM_CCMR1_OC1M_1 | TIM_CCMR1_OC1M_2 | TIM_CCMR1_OC1PE);
+#endif
   //TIM1->SR |= TIM_SR_UIF;
   TIM1->BDTR = TIM_BDTR_MOE; // enable main output
   TIM1->EGR = TIM_EGR_UG; // force timer update
   /* TIM1 CC_EnableChannel */
-  TIM1->CCER = (TIM_CCER_CC1E | TIM_CCER_CC1P | TIM_CCER_CC2E | TIM_CCER_CC2P);
+  TIM1->CCER = (TIM_CCER_CC2E | TIM_CCER_CC2P);
+#ifdef USE_SERVO_2
+  TIM1->CCER |= (TIM_CCER_CC1E | TIM_CCER_CC1P);
+#endif
   /* Set the trigger output 2 (TRGO2) used for ADC synchronization */
   TIM1->CR2 |= TIM_CR2_MMS2_1; // update event
   /* TIM_EnableCounter */

+ 36 - 20
src/main.c

@@ -32,9 +32,10 @@ volatile flag_t Flag = {0};
 volatile uint32_t photo_Value;
 
 /* Private function prototypes -----------------------------------------------*/
+/*
 static void servo_open(void);
 static void servo_close(void);
-
+*/
 /* Private user code ---------------------------------------------------------*/
 
 /**
@@ -46,17 +47,21 @@ int main(void)
   /* Initialize onBoard Hardware */
   Board_Init();
 
-  delay_ms(500);
-  servo_open();
-  delay_ms(1000);
-  servo_close();
-  delay_ms(1000);
+  Servo_1_ON;
+  Servo_1_CHNL = SERVO_INIT_VAL;
+  Servo_1_OFF;
+
+#ifdef USE_SERVO_2
+  Servo_2_ON;
+  Servo_2_CHNL = SERVO_INIT_VAL;
+  Servo_2_OFF;
 
   if ((Select_Port->IDR & Select_Pin) == 0) {
     Flag.ONE_CHANNEL = 1;
   } else {
     Flag.ONE_CHANNEL = 0;
   }
+#endif
 
   /* if light is on, wait for off */
   while (photo_Value <= PHOTO_LEVEL_OFF) {
@@ -73,30 +78,39 @@ int main(void)
       __WFI();
     }
     /* open servo 1 */
+    Servo_1_ON;
     Servo_1_CHNL = SERVO_OPEN_VAL;
+    Servo_1_OFF;
+
     /* wait for light off */
     while (photo_Value <= PHOTO_LEVEL_OFF) {
       //IWDG->KR = 0xAAAA;
       __WFI();
     }
     /* close servo 1 */
+    Servo_1_ON;
     Servo_1_CHNL = SERVO_INIT_VAL;
+    Servo_1_OFF;
     
-    if (Flag.ONE_CHANNEL != 0) {
-      /* wait for light on */
-      while (photo_Value > PHOTO_LEVEL_ON) {
-        //IWDG->KR = 0xAAAA;
-        __WFI();
-      }
-      Servo_2_CHNL = SERVO_OPEN_VAL;
-      /* wait for light off */
-      while (photo_Value <= PHOTO_LEVEL_OFF) {
-        //IWDG->KR = 0xAAAA;
-        __WFI();
-      }
-      /* close servo 2 */
-      Servo_2_CHNL = SERVO_INIT_VAL;
+#ifdef USE_SERVO_2
+    /* wait for light on */
+    while (photo_Value > PHOTO_LEVEL_ON) {
+      //IWDG->KR = 0xAAAA;
+      __WFI();
+    }
+    Servo_2_ON;
+    Servo_2_CHNL = SERVO_OPEN_VAL;
+    Servo_2_OFF;
+    /* wait for light off */
+    while (photo_Value <= PHOTO_LEVEL_OFF) {
+      //IWDG->KR = 0xAAAA;
+      __WFI();
     }
+    /* close servo 2 */
+    Servo_2_ON;
+    Servo_2_CHNL = SERVO_INIT_VAL;
+    Servo_2_OFF;
+#endif
 
     /* end of life */
     //while (1) {
@@ -105,6 +119,7 @@ int main(void)
   }
 } /* End of mine() */
 
+/*
 static void servo_open(void) {
   int i;
   for (i=SERVO_INIT_VAL; i <= SERVO_OPEN_VAL; i+=50) {
@@ -122,6 +137,7 @@ static void servo_close(void) {
     delay_ms(40);
   }
 }
+*/
 
 /*
 static void uart_init(uint32_t baud)