Vladimir N. Shilov 3 месяцев назад
Родитель
Сommit
016404a387
4 измененных файлов с 66 добавлено и 98 удалено
  1. 8 6
      inc/board.h
  2. 2 14
      inc/main.h
  3. 15 30
      src/board.c
  4. 41 48
      src/main.c

+ 8 - 6
inc/board.h

@@ -6,12 +6,10 @@
 /* Includes */
 #include "main.h"
 #include "gpio.h"
-#include "xprintf.h"
+//#include "xprintf.h"
 
 /* Type Defs */
 /* Exported macros */
-#define LED_ON()     GPIO_PIN_SET(GPIOB, GPIO_PIN_0)
-#define LED_OFF()      GPIO_PIN_RES(GPIOB, GPIO_PIN_0)
 /* Constants */
 /* PWM Timers for 50 Hz */
 #define TIM1_PSC            (24 - 1)
@@ -23,12 +21,16 @@
 #define TIM3_ARR            (100 - 1)
 
 /* Defines */
+#define Select_Pin    GPIO_PIN_3
+#define Select_Port   GPIOA
 #define Photo_Pin     GPIO_PIN_7
 #define Photo_Port    GPIOB
-#define Servo_2_Pin   GPIO_PIN_8
-#define Servo_2_Port  GPIOA
-#define Servo_1_Pin   GPIO_PIN_11
+#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
 
 /* Variables */
 

+ 2 - 14
inc/main.h

@@ -42,20 +42,8 @@ typedef enum {
 } onoff_t;
 
 typedef struct t_flag {
-  uint32_t RTC_IRQ:     1;
-  uint32_t SPI_TX_End:  1;
-  uint32_t I2C_TX_End:  1;
-  uint32_t I2C_RX_End:  1;
-  uint32_t I2C_TX_Err:  1;
-  uint32_t I2C_RX_Err:  1;
-  uint32_t BME280:      1;
-  uint32_t Blink_1:     1;
-  uint32_t Blink_2:     1;
-  uint32_t Blink_3:     1;
-  uint32_t Blink_4:     1;
-  uint32_t Blink_5:     1;
-  uint32_t Now_Day:     1;
-  uint32_t _reserv:    19;
+  uint32_t ONE_CHANNEL:     1;
+  uint32_t _reserv:        31;
 } flag_t;
 extern volatile flag_t Flag;
 

+ 15 - 30
src/board.c

@@ -48,7 +48,7 @@ void Board_Init(void)
   ADC_Init();
 
   TIM1_Init();
-  TIM3_Init();
+  //TIM3_Init();
 
   //IWDG_Init();
 }
@@ -87,30 +87,24 @@ 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|Servo_2_Pin), GPIO_AF_2);
-  GPIO_SetPinOutputType(Servo_1_Port, (Servo_1_Pin|Servo_2_Pin), GPIO_OTYPE_PP);
-  GPIO_SetPinPull(Servo_1_Port, (Servo_1_Pin|Servo_2_Pin), GPIO_PUPDR_NO);
-  GPIO_SetPinSpeed(Servo_1_Port, (Servo_1_Pin|Servo_2_Pin), GPIO_OSPEED_LW);
-  GPIO_SetPinMode(Servo_1_Port, (Servo_1_Pin|Servo_2_Pin), GPIO_MODE_AFF);
-
-  GPIO_SetAFPin_0_7(GPIOB, GPIO_PIN_3, GPIO_AF_1);
-  GPIO_SetPinOutputType(GPIOB, GPIO_PIN_3, GPIO_OTYPE_PP);
-  GPIO_SetPinPull(GPIOB, GPIO_PIN_3, GPIO_PUPDR_NO);
-  GPIO_SetPinSpeed(GPIOB, GPIO_PIN_3, GPIO_OSPEED_LW);
-  GPIO_SetPinMode(GPIOB, GPIO_PIN_3, GPIO_MODE_AFF);
+  GPIO_SetAFPin_8_15(Servo_1_Port, Servo_1_Pin, GPIO_AF_2);
+  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);
+  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);
 
   /* Photo_Pin: analog in, pull none */
   GPIO_SetPinPull(Photo_Port, Photo_Pin, GPIO_PUPDR_NO);
   GPIO_SetPinMode(Photo_Port, Photo_Pin, GPIO_MODE_ANL);
 
-  /* Test out Pin A4 */
-  GPIO_SetPinMode(GPIOA, GPIO_PIN_4, GPIO_MODE_OUT);
-  //GPIO_SetPinOutputType(GPIOA, GPIO_PIN_4, GPIO_OTYPE_PP);
-  //GPIO_SetPinSpeed(GPIOA, GPIO_PIN_4, GPIO_OSPEED_LW);
-  //GPIO_SetPinPull(GPIOA, GPIO_PIN_4, GPIO_PUPDR_NO);
-
-  /* Test in Pin A5 */
-  GPIO_SetPinPull(GPIOA, GPIO_PIN_5, GPIO_PUPDR_UP);
+  /* Num of chnls select pin */
+  GPIO_SetPinPull(Select_Port, Select_Pin, GPIO_PUPDR_UP);
 }
 
 /**
@@ -130,8 +124,6 @@ static void ADC_Init(void)
   //ADC synchronous clock derived from AHB clock divided by 2
   ADC1->CFGR2 |= ADC_CFGR2_CKMODE_0;
 
-  /* Poll for ADC channel configuration ready */
-  //while ((ADC1->ISR & ADC_ISR_CCRDY) == 0) {};
   /* Clear flag ADC channel configuration ready */
   ADC1->ISR |= ADC_ISR_CCRDY;
 
@@ -172,9 +164,6 @@ 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
@@ -186,17 +175,13 @@ static void TIM1_Init(void)
   // 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->CCMR2 = (TIM_CCMR2_OC4M_1 | TIM_CCMR2_OC4M_2 | TIM_CCMR2_OC4PE);
   //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_CC4E | TIM_CCER_CC4P);
-  TIM1->CCER |= (TIM_CCER_CC2E | TIM_CCER_CC2P);
+  TIM1->CCER = (TIM_CCER_CC1E | TIM_CCER_CC1P | TIM_CCER_CC2E | TIM_CCER_CC2P);
   /* Set the trigger output 2 (TRGO2) used for ADC synchronization */
   TIM1->CR2 |= TIM_CR2_MMS2_1; // update event
-  /* Enable interrupts */
-  //TIM1->DIER = TIM_DIER_UIE;
   /* TIM_EnableCounter */
   TIM1->CR1 |= TIM_CR1_CEN;
 }

+ 41 - 48
src/main.c

@@ -22,7 +22,8 @@
 /* Private includes ----------------------------------------------------------*/
 /* Private typedef -----------------------------------------------------------*/
 /* Private define ------------------------------------------------------------*/
-#define PHOTO_LEVEL (500)
+#define PHOTO_LEVEL_ON  (200)
+#define PHOTO_LEVEL_OFF (2000)
 
 /* Private macro -------------------------------------------------------------*/
 /* Private variables ---------------------------------------------------------*/
@@ -33,9 +34,6 @@ volatile uint32_t photo_Value;
 /* Private function prototypes -----------------------------------------------*/
 static void servo_open(void);
 static void servo_close(void);
-static void uart_puts(char *s);
-static void uart_putc(char c);
-static void uart_init(uint32_t baud);
 
 /* Private user code ---------------------------------------------------------*/
 
@@ -47,78 +45,71 @@ int main(void)
 {
   /* Initialize onBoard Hardware */
   Board_Init();
-  //uart_init(115200);
-  uart_puts("\033[2J\033[1;1H"); // clear screen
-  xdev_out(uart_putc);
-  xputs("Skid\n");
 
   delay_ms(500);
   servo_open();
-  delay_ms(500);
+  delay_ms(1000);
   servo_close();
   delay_ms(1000);
 
-  uart_putc('0');
-  xputc('\n');
-  while (photo_Value <= PHOTO_LEVEL) {
-    delay_ms(1000);
-    xprintf("photo_Value: %u\n", photo_Value);
-  //  IWDG->KR = 0xAAAA;
+  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;
     __WFI();
   }
-  uart_putc('1');
 
   /* Infinite loop */
   while (1)
   {
     /* wait for light on */
-    while (photo_Value > PHOTO_LEVEL) {
-    delay_ms(1000);
-    xprintf("photo_Value: %u\n", photo_Value);
-      IWDG->KR = 0xAAAA;
+    while (photo_Value > PHOTO_LEVEL_ON) {
+      //IWDG->KR = 0xAAAA;
       __WFI();
     }
-    uart_putc('2');
     /* open servo 1 */
-    TIM1->CCR1 = SERVO_OPEN_VAL;
+    Servo_1_CHNL = SERVO_OPEN_VAL;
     /* wait for light off */
-    while (photo_Value <= PHOTO_LEVEL) {
-      IWDG->KR = 0xAAAA;
+    while (photo_Value <= PHOTO_LEVEL_OFF) {
+      //IWDG->KR = 0xAAAA;
       __WFI();
     }
-    uart_putc('3');
     /* close servo 1 */
-    TIM1->CCR1 = SERVO_INIT_VAL;
+    Servo_1_CHNL = SERVO_INIT_VAL;
     
-    /* wait for light on */
-    while (photo_Value > PHOTO_LEVEL) {
-      IWDG->KR = 0xAAAA;
-      __WFI();
+    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;
     }
-    uart_putc('4');
-    /* open servo 2 */
-    TIM1->CCR4 = SERVO_OPEN_VAL;
-    /* wait for light off */
-    while (photo_Value <= PHOTO_LEVEL) {
-      IWDG->KR = 0xAAAA;
-      __WFI();
-    }
-    uart_putc('5');
-    /* close servo 2 */
-    TIM1->CCR4 = SERVO_INIT_VAL;
 
     /* end of life */
-    while (1) {
-      __WFI();
-    }
+    //while (1) {
+    //  __WFI();
+    //}
   }
 } /* End of mine() */
 
 static void servo_open(void) {
   int i;
   for (i=SERVO_INIT_VAL; i <= SERVO_OPEN_VAL; i+=50) {
-    TIM1->CCR1 = i;
-    TIM1->CCR4 = i;
+    Servo_1_CHNL = i;
+    Servo_2_CHNL = i;
     delay_ms(40);
   }
 }
@@ -126,12 +117,13 @@ static void servo_open(void) {
 static void servo_close(void) {
   int i;
   for (i=SERVO_OPEN_VAL; i >= SERVO_INIT_VAL; i-=50) {
-    TIM1->CCR1 = i;
-    TIM1->CCR4 = i;
+    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);
@@ -154,6 +146,7 @@ static void uart_puts(char *s)
   while (*s)
     uart_putc(*s++);
 }
+*/
 
 /**
   * @brief  This function is executed in case of error occurrence.