From da6d7e68ec3d303e78e83a1adf17f638a0d49269 Mon Sep 17 00:00:00 2001
From: WXK <287788329@qq.com>
Date: 星期二, 26 十二月 2023 15:45:30 +0800
Subject: [PATCH] 1

---
 Src/main.c |  221 ++++++++++++++++++++++++++++++-------------------------
 1 files changed, 121 insertions(+), 100 deletions(-)

diff --git a/Src/main.c b/Src/main.c
index 0d208be..1fce106 100644
--- a/Src/main.c
+++ b/Src/main.c
@@ -89,6 +89,7 @@
 
 /* Private user code ---------------------------------------------------------*/
 /* USER CODE BEGIN 0 */
+uint16_t Anchor_Vcc_time;
 uint32_t dev_id;
 uint16_t heartbeat_timer,poll_timer,sync_timer;
 uint8_t aRxBuffer[1],group_id;
@@ -161,10 +162,10 @@
 		}
 		if(bat_percent>99)
 		{
-			LED_LR_OFF;
+//			LED_LR_OFF;
 			LED_LG_ON;
 		}else{
-			LED_LR_ON;
+//			LED_LR_ON;
 			LED_LG_OFF;		
 		}		
 	}else{
@@ -173,7 +174,7 @@
             state5v=0;
             UsartDeInit();
         }
-		LED_LR_OFF;
+//		LED_LR_OFF;
 		LED_LG_OFF;
 //	}
 }
@@ -226,18 +227,21 @@
 	hardware_pici = STMFLASH_ReadHalfWord(FLASH_HARDWARE_VERSION_MAP+2);
 	hardware_type = STMFLASH_ReadHalfWord(FLASH_HARDWARE_VERSION_MAP+4);
 	//deca_sleep(1000);
-	g_com_map[VERSION] = (1<<8)|68;
-    g_com_map[DEV_ID]=0x6777;
+	g_com_map[VERSION] = (1<<8)|0;
+//    g_com_map[DEV_ID]=0x6777;
     #ifdef DEBUG_MODE
    // g_com_map[GROUP_ID]=21;
-    		g_com_map[DEV_ID]=0x6777;
+//    		g_com_map[DEV_ID]=0x6777;
     //	g_com_map[COM_INTERVAL]=100;
     //	g_com_map[MAX_REPORT_ANC_NUM]=1;
     //	g_com_map[NOMOVESLEEP_TIME]=10;
     #endif
-    g_com_map[GROUP_ID]=3;
+//    g_com_map[GROUP_ID]=33;
     g_com_map[COM_INTERVAL] = 1000;
+    if(g_com_map[COM_INTERVAL]<1000)
+    {g_com_map[COM_INTERVAL] = 1000;}
     active_flag = g_com_map[ACTIVE_INDEX];
+    g_com_map[POWER]=0;
     module_power = g_com_map[POWER];
     g_com_map[IMU_ENABLE] = 0;
 	imu_enable=g_com_map[IMU_ENABLE];
@@ -262,7 +266,7 @@
  //   g_com_map[COM_INTERVAL]=100;
 		if(g_com_map[COM_INTERVAL]==0)
 	{
-	  g_com_map[COM_INTERVAL]=100;
+	  g_com_map[COM_INTERVAL]=1000;
 	}
 	 if(module_power>67)
 		{module_power=67;}
@@ -276,7 +280,7 @@
     Error_Handler();
   }
 //g_com_map[NOMOVESLEEP_TIME]=5;
-    printf("固件版本: 免布线基站 V%d.%d. \r\n",g_com_map[VERSION]>>8,g_com_map[VERSION]&0xff);
+    printf("固件版本: LORA免布线基站 V%d.%d. \r\n",g_com_map[VERSION]>>8,g_com_map[VERSION]&0xff);
 	memcpy(g_com_map2,g_com_map,COM_MAP_SIZE);
 //	printf("DEVICE PAIRID: %d .\r\n",g_com_map[PAIR_ID]);
 //	printf("DEVICE ALARM DISTANCE: 1.%d 2.%d 3.%d .\r\n",g_com_map[ALARM_DISTANCE1],g_com_map[ALARM_DISTANCE2],g_com_map[ALARM_DISTANCE3]);
@@ -360,10 +364,38 @@
 uint8_t tx_num;
 void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
 {
-    if(GPIO_Pin == GPIO_PIN_11)
+    if(GPIO_Pin == GPIO_PIN_3)
     {
         RadioIrqProcess();
     }
+}
+u16 poll_startcount;
+u16 Anchor_Vcc_time1;
+uint8_t TX_Anchor_Vcc[100]={0x55,0xAA,0x14,20,0,0/*标签id*/,0,0/*基站ID*/,0/*保留*/,};
+extern float bat_volt;
+void Send_Anchor_Vcc_Poll()
+{
+    uint8_t vcc_send;
+  if(Anchor_Vcc_time>=3600)
+  {     
+      Lora470_Init();
+      SX126xSetRfTxPower( 22 );//切换大发射功率
+      g_start_send_flag=0;
+      Anchor_Vcc_time=0;
+      memcpy(&TX_Anchor_Vcc[6],&g_com_map[DEV_ID],2);
+      memcpy(&TX_Anchor_Vcc[9],&g_com_map[VERSION],2);
+      vcc_send=(bat_volt-2.8)*100;
+      memcpy(&TX_Anchor_Vcc[11],&vcc_send,1);
+      memcpy(&TX_Anchor_Vcc[12],&g_com_map[POWER],1);
+      memcpy(&TX_Anchor_Vcc[13],&g_com_map[GROUP_ID],2);
+      memcpy(&TX_Anchor_Vcc[15],&g_com_map[DIST_OFFSET],2);
+//      memcpy(&TX_Anchor_Vcc[18],&g_com_map[DIST_OFFSET],2);
+      Radio.Send( TX_Anchor_Vcc, 2+20);
+      SX126xSetRfTxPower( 22 );//切换回小发射功率;
+      
+      Lora_Init();
+      
+  }
 }
 /* USER CODE END 0 */
 
@@ -396,10 +428,10 @@
   /* Initialize all configured peripherals */
   MX_GPIO_Init();
   MX_LPTIM1_Init();
+  MX_DMA_Init();
   MX_USART1_UART_Init();
   MX_SPI1_Init();
   MX_ADC_Init();
-  MX_DMA_Init();
   MX_IWDG_Init();
   /* USER CODE BEGIN 2 */
     LED_LG_ON;
@@ -419,15 +451,10 @@
      Error_Handler();
     }
     HAL_Delay(2000);
-//    Uwb_Lora_Switch(0,1);
+
     Lora_Init();
-//    RadioSleep();
+
     Delay_Ms(10);
-//    Uwb_Lora_Switch(0,0);
-//	mcu_sleep();
-	//LED0_BLINK;		
-//	SystemPower_Config();
-//HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);   
   /* USER CODE END 2 */
 
   /* Infinite loop */
@@ -437,34 +464,32 @@
     /* USER CODE END WHILE */
 
     /* USER CODE BEGIN 3 */
+      Send_Anchor_Vcc_Poll();
 		if(g_start_send_flag)
 	{ static uint16_t blink_count=0;
 		SystemClock_Config();
 		g_start_send_flag = 0;	
 		HAL_IWDG_Refresh(&hiwdg);
 
-//        Uwb_Lora_Switch(0,1);
-//        HAL_GPIO_WritePin(GPIOB, GPIO_PIN_2, GPIO_PIN_SET);
+
         memcpy(&TX_Buffer+3,&tx_num,1);
-//        HAL_GPIO_WritePin(GPIOB, RADIO_NSS_Pin, GPIO_PIN_RESET); 
-//        delay_us(100);
-//        HAL_GPIO_WritePin(GPIOB, RADIO_NSS_Pin, GPIO_PIN_SET); 
-//        delay_us(600);
-//            Lora_Init();
-//        printf("TX fasong\r\n");
+//        printf("Lora发送%d",tx_num);
+        HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, GPIO_PIN_SET);
+        poll_startcount=HAL_LPTIM_ReadCounter(&hlptim1);
+        poll_startcount=poll_startcount+557;//557代表17ms
+        if(poll_startcount>=32768)
+		{
+		poll_startcount -=32768;
+		}
 	    Radio.Send( &TX_Buffer, 4);
-//        delay_us(1000);
-////        RadioSleep();
-//        delay_us(1000);
+
         tx_num++;
-//                delay_ms(10);
-//        HAL_GPIO_WritePin(GPIOB, GPIO_PIN_2, GPIO_PIN_RESET);
-        delay_ms(9);
-//                Lora_Init();
-//        Uwb_Lora_Switch(1,0);
+        delay_ms(3);
+
 
 		if(bat_percent>15)
-		{	LED_TB_ON;
+		{	
+          LED_TB_ON;
 			Tag_App();
 			LED_TB_OFF;
 		}else{
@@ -472,7 +497,7 @@
 			Tag_App();
 			LED_TR_OFF;		
 		}
-//        Uwb_Lora_Switch(0,0);
+
 		//LED0_BLINK;		
 		IdleTask();	
 		if(waitusart_timer>0)
@@ -488,12 +513,12 @@
      {
         SCB->AIRCR = 0X05FA0000|(unsigned int)0x04;
      }
-         if(!HAL_GPIO_ReadPin(INPUT_5V_GPIO_Port,INPUT_5V_Pin))				
-                    HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);  
-//    #ifndef DEBUG_MODE	
-//    if(!HAL_GPIO_ReadPin(INPUT_5V_GPIO_Port,INPUT_5V_Pin))				
+//         if(!HAL_GPIO_ReadPin(INPUT_5V_GPIO_Port,INPUT_5V_Pin))				
 //                    HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);  
-//    #endif
+    #ifndef DEBUG_MODE	
+    if(!HAL_GPIO_ReadPin(INPUT_5V_GPIO_Port,INPUT_5V_Pin))				
+                    HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);  
+    #endif
     }
 // HAL_Delay(100);
     //  Get_Battary();
@@ -801,14 +826,22 @@
   HAL_GPIO_WritePin(GPIOB, BAT_MEAS_GND_Pin|PWR_CTL_Pin, GPIO_PIN_SET);
 
   /*Configure GPIO pin Output Level */
-  HAL_GPIO_WritePin(GPIOB, LED1_G_Pin|LED1_R_Pin|LED2_R_Pin|RADIO_nRESET_Pin
-                          |RADIO_NSS_Pin, GPIO_PIN_RESET);
+  HAL_GPIO_WritePin(GPIOB, GPIO_PIN_10|LED1_G_Pin|LED1_R_Pin|RADIO_nRESET_Pin
+                          |RADIO_NSS_Pin|GPIO_PIN_7, GPIO_PIN_RESET);
 
   /*Configure GPIO pin Output Level */
   HAL_GPIO_WritePin(GPIOA, DW_WKUP_Pin|DW_CTRL_Pin|LED2_G_Pin, GPIO_PIN_RESET);
-
-  /*Configure GPIO pins : PA0 RADIO_DIO1_Pin */
-  GPIO_InitStruct.Pin = GPIO_PIN_0|RADIO_DIO1_Pin;
+  //////////////////////////////////
+  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, GPIO_PIN_RESET);
+  //////////////////////////////////
+  GPIO_InitStruct.Pin = GPIO_PIN_15;
+  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
+  GPIO_InitStruct.Pull = GPIO_NOPULL;
+  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
+  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
+  
+  /*Configure GPIO pin : PA0 */
+  GPIO_InitStruct.Pin = GPIO_PIN_0;
   GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
   GPIO_InitStruct.Pull = GPIO_NOPULL;
   HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
@@ -840,20 +873,26 @@
   GPIO_InitStruct.Pull = GPIO_PULLUP;
   HAL_GPIO_Init(USER_KEY_GPIO_Port, &GPIO_InitStruct);
 
+  /*Configure GPIO pin : RADIO_BUSY_Pin */
+  GPIO_InitStruct.Pin = RADIO_BUSY_Pin;
+  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
+  GPIO_InitStruct.Pull = GPIO_NOPULL;
+  HAL_GPIO_Init(RADIO_BUSY_GPIO_Port, &GPIO_InitStruct);
+
+  /*Configure GPIO pins : PB10 LED1_G_Pin LED1_R_Pin RADIO_nRESET_Pin
+                           RADIO_NSS_Pin PB7 PWR_CTL_Pin */
+  GPIO_InitStruct.Pin = GPIO_PIN_10|LED1_G_Pin|LED1_R_Pin|RADIO_nRESET_Pin
+                          |RADIO_NSS_Pin|GPIO_PIN_7|PWR_CTL_Pin;
+  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
+  GPIO_InitStruct.Pull = GPIO_NOPULL;
+  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
+  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
+
   /*Configure GPIO pin : INPUT_5V_Pin */
   GPIO_InitStruct.Pin = INPUT_5V_Pin;
   GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
   GPIO_InitStruct.Pull = GPIO_PULLDOWN;
   HAL_GPIO_Init(INPUT_5V_GPIO_Port, &GPIO_InitStruct);
-
-  /*Configure GPIO pins : LED1_G_Pin LED1_R_Pin LED2_R_Pin RADIO_nRESET_Pin
-                           RADIO_NSS_Pin PWR_CTL_Pin */
-  GPIO_InitStruct.Pin = LED1_G_Pin|LED1_R_Pin|LED2_R_Pin|RADIO_nRESET_Pin
-                          |RADIO_NSS_Pin|PWR_CTL_Pin;
-  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
-  GPIO_InitStruct.Pull = GPIO_NOPULL;
-  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
-  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
 
   /*Configure GPIO pins : DW_WKUP_Pin DW_CTRL_Pin LED2_G_Pin */
   GPIO_InitStruct.Pin = DW_WKUP_Pin|DW_CTRL_Pin|LED2_G_Pin;
@@ -862,24 +901,18 @@
   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
   HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
 
-  /*Configure GPIO pin : RADIO_BUSY_Pin */
-  GPIO_InitStruct.Pin = RADIO_BUSY_Pin;
-  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
+  /*Configure GPIO pin : RADIO_DIO1_Pin */
+  GPIO_InitStruct.Pin = RADIO_DIO1_Pin;
+  GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
   GPIO_InitStruct.Pull = GPIO_NOPULL;
-  HAL_GPIO_Init(RADIO_BUSY_GPIO_Port, &GPIO_InitStruct);
-
-  /*Configure GPIO pin : PWR_ON_Pin */
-  GPIO_InitStruct.Pin = PWR_ON_Pin;
-  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
-  GPIO_InitStruct.Pull = GPIO_PULLUP;
-  HAL_GPIO_Init(PWR_ON_GPIO_Port, &GPIO_InitStruct);
+  HAL_GPIO_Init(RADIO_DIO1_GPIO_Port, &GPIO_InitStruct);
 
   /* EXTI interrupt init*/
   HAL_NVIC_SetPriority(EXTI0_1_IRQn, 1, 0);
   HAL_NVIC_EnableIRQ(EXTI0_1_IRQn);
 
-  HAL_NVIC_SetPriority(EXTI4_15_IRQn, 1, 0);
-  HAL_NVIC_EnableIRQ(EXTI4_15_IRQn);
+  HAL_NVIC_SetPriority(EXTI2_3_IRQn, 1, 0);
+  HAL_NVIC_EnableIRQ(EXTI2_3_IRQn);
 
 }
 
@@ -915,15 +948,15 @@
 {
 	static uint8_t i=0,lost_jumpcount=0;
 	i++;
-	
+	Anchor_Vcc_time++;
 	current_slotnum++;
     freqlost_count+=1/(float)tag_frequency;
     range_lost_time +=1/(float)tag_frequency;
-    
+//    g_start_send_flag=1;
     if(freqlost_count>FREQ_LOST_TIME)
     {
         tag_frequency = NOTAG_FREQ;
-        if(lost_jumpcount++>=0) //无测距情况下,每5秒发一次;
+        if(lost_jumpcount++>=10) //无测距情况下,每5秒发一次;
         {
             lost_jumpcount = 0;
             g_start_send_flag=1;
@@ -937,39 +970,27 @@
     }else{
         SetNextPollTime(5);
     }
-	nomove_count+=1/(float)tag_frequency;
-    if(motor_keeptime>0)
-	{	
-        nomove_count+=1/(float)tag_frequency;
-	}
-	if(!GET_USERKEY)
-	{
-		key_keeptime+=1/(float)tag_frequency;
-		if(key_keeptime>=KEY_KEEPRESET_TIME)
-		{
-            g_com_map[ACTIVE_INDEX]=!active_flag;
-            save_com_map_to_flash();
-            MOTOR_ON;
-            HAL_Delay(2000);
-			SCB->AIRCR = 0X05FA0000|(unsigned int)0x04; 
-		}
-	}else{
-		key_keeptime=0;
-	}
-//	if(nomove_count>STATIONARY_TIME)
+    
+//	nomove_count+=1/(float)tag_frequency;
+//    if(motor_keeptime>0)
+//	{	
+//        nomove_count+=1/(float)tag_frequency;
+//	}
+//	if(!GET_USERKEY)
 //	{
-//		stationary_flag = 1;
+//		key_keeptime+=1/(float)tag_frequency;
+//		if(key_keeptime>=KEY_KEEPRESET_TIME)
+//		{
+//            g_com_map[ACTIVE_INDEX]=!active_flag;
+//            save_com_map_to_flash();
+////            MOTOR_ON;
+//            HAL_Delay(2000);
+//			SCB->AIRCR = 0X05FA0000|(unsigned int)0x04; 
+//		}
 //	}else{
-//		stationary_flag = 0;
+//		key_keeptime=0;
 //	}
-//if(nomove_count>=nomovesleeptime-1&&imu_enable)
-//	{
-//		tx_near_msg[TAGSTATE] = tx_near_msg[TAGSTATE]|GOINGSLEEPBIT;
-//	}
-//	if(nomove_count>nomovesleeptime&&imu_enable)
-//	{
-//		mcu_sleep();
-//	}
+
 }
 /* USER CODE END 4 */
 

--
Gitblit v1.9.3