Jelajahi Sumber

Fix for real life N2.

Vladimir N. Shilov 3 bulan lalu
induk
melakukan
4fe7500681
3 mengubah file dengan 26 tambahan dan 79 penghapusan
  1. 2 8
      inc/board.h
  2. 4 0
      src/board.c
  3. 20 71
      src/main.c

+ 2 - 8
inc/board.h

@@ -16,8 +16,9 @@
 /* PWM Timers for 50 Hz */
 #define TIM1_PSC            (24 - 1)
 #define TIM1_ARR            (20000 - 1)
-#define SERVO_INIT_VAL      2000
+#define SERVO_INIT_VAL      0
 #define SERVO_OPEN_VAL      1000
+#define SERVO_CLOSE_VAL     2000
 
 #define TIM3_PSC            (24000 - 1)
 #define TIM3_ARR            (100 - 1)
@@ -31,19 +32,12 @@
   #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)
 #endif
 #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 */

+ 4 - 0
src/board.c

@@ -3,6 +3,7 @@
 /* private defines */
 /* private variables */
 static volatile uint32_t TDelay;
+extern volatile uint32_t timeOut;
 
 /* private typedef */
 /* private functions */
@@ -255,4 +256,7 @@ void SysTick_Handler(void) {
   if (TDelay != 0) {
     TDelay --;
   }
+  if (timeOut != 0) {
+    timeOut --;
+  }
 }

+ 20 - 71
src/main.c

@@ -24,9 +24,11 @@
 /* Private define ------------------------------------------------------------*/
 #define PHOTO_LEVEL_ON  (100)
 #define PHOTO_LEVEL_OFF (1000)
+#define TIME_WAIT       1000
 
 /* Private macro -------------------------------------------------------------*/
 /* Private variables ---------------------------------------------------------*/
+volatile uint32_t timeOut;
 static const char msg1[] = {"russian warship go fuck yourself!\nfeel free to contact shilvn@gmail.com"};
 volatile flag_t Flag = {0};
 volatile uint32_t photo_Value;
@@ -48,23 +50,17 @@ int main(void)
   Board_Init();
 
 #ifdef USE_SERVO_1
-  Servo_1_ON;
+  Servo_1_CHNL = SERVO_CLOSE_VAL;
+  delay_ms(100);
   Servo_1_CHNL = SERVO_INIT_VAL;
-  Servo_1_OFF;
 #endif
 
 #ifdef USE_SERVO_2
-  Servo_2_ON;
+  Servo_2_CHNL = SERVO_CLOSE_VAL;
+  delay_ms(100);
   Servo_2_CHNL = SERVO_INIT_VAL;
-  Servo_2_OFF;
 #endif
-/*
-  if ((Select_Port->IDR & Select_Pin) == 0) {
-    Flag.ONE_CHANNEL = 1;
-  } else {
-    Flag.ONE_CHANNEL = 0;
-  }
-*/
+
   /* if light is on, wait for off */
   while (photo_Value <= PHOTO_LEVEL_OFF) {
     //IWDG->KR = 0xAAAA;
@@ -81,19 +77,20 @@ int main(void)
       __WFI();
     }
     /* open servo 1 */
-    Servo_1_ON;
     Servo_1_CHNL = SERVO_OPEN_VAL;
-    Servo_1_OFF;
 
     /* wait for light off */
+    timeOut = TIME_WAIT;
     while (photo_Value <= PHOTO_LEVEL_OFF) {
-      //IWDG->KR = 0xAAAA;
+      if (timeOut == 0) {
+        Servo_1_CHNL = SERVO_INIT_VAL;
+      }
       __WFI();
     }
     /* close servo 1 */
-    Servo_1_ON;
+    Servo_1_CHNL = SERVO_CLOSE_VAL;
+    delay_ms(100);
     Servo_1_CHNL = SERVO_INIT_VAL;
-    Servo_1_OFF;
 #endif
     
 #ifdef USE_SERVO_2
@@ -102,71 +99,23 @@ int main(void)
       //IWDG->KR = 0xAAAA;
       __WFI();
     }
-    Servo_2_ON;
     Servo_2_CHNL = SERVO_OPEN_VAL;
-    Servo_2_OFF;
     /* wait for light off */
+    timeOut = TIME_WAIT;
     while (photo_Value <= PHOTO_LEVEL_OFF) {
-      //IWDG->KR = 0xAAAA;
+      if (timeOut == 0) {
+        Servo_2_CHNL = SERVO_INIT_VAL;
+      }
       __WFI();
     }
-    /* close servo 2 */
-    Servo_2_ON;
+    /* close servo 1 */
+    Servo_2_CHNL = SERVO_CLOSE_VAL;
+    delay_ms(100);
     Servo_2_CHNL = SERVO_INIT_VAL;
-    Servo_2_OFF;
 #endif
-
-    /* end of life */
-    //while (1) {
-    //  __WFI();
-    //}
   }
 } /* End of mine() */
 
-/*
-static void servo_open(void) {
-  int i;
-  for (i=SERVO_INIT_VAL; i <= SERVO_OPEN_VAL; i+=50) {
-    Servo_1_CHNL = i;
-    Servo_2_CHNL = i;
-    delay_ms(40);
-  }
-}
-
-static void servo_close(void) {
-  int i;
-  for (i=SERVO_OPEN_VAL; i >= SERVO_INIT_VAL; i-=50) {
-    Servo_1_CHNL = i;
-    Servo_2_CHNL = i;
-    delay_ms(40);
-  }
-}
-*/
-
-/*
-static void uart_init(uint32_t baud)
-{
-  GPIO_SetPinMode(GPIOB, GPIO_PIN_6, GPIO_MODE_AFF);
-
-  RCC->APBENR2 |= RCC_APBENR2_USART1EN;
-
-  USART1->CR1 = USART_CR1_RE | USART_CR1_TE | USART_CR1_FIFOEN;
-  USART1->BRR = (SystemCoreClock / baud);
-  USART1->CR1 |= USART_CR1_UE;
-}
-
-static void uart_putc(char c)
-{
-  while (0 == (USART1->ISR & USART_ISR_TXE_TXFNF));
-  USART1->TDR = c;
-}
-
-static void uart_puts(char *s)
-{
-  while (*s)
-    uart_putc(*s++);
-}
-*/
 
 /**
   * @brief  This function is executed in case of error occurrence.