Browse Source

update for overflow handling

terry 2 years ago
parent
commit
5926f982e4
8 changed files with 238 additions and 96 deletions
  1. 1 1
      Core/Inc/main.h
  2. 1 0
      Core/Inc/usart.h
  3. 1 1
      Core/Src/adc.c
  4. 165 60
      Core/Src/main.c
  5. 11 4
      Core/Src/stm32f1xx_it.c
  6. 11 0
      Core/Src/usart.c
  7. 47 29
      MDK-ARM/iocollect.uvguix.terry
  8. 1 1
      MDK-ARM/iocollect.uvoptx

+ 1 - 1
Core/Inc/main.h

@@ -47,7 +47,7 @@ extern "C" {
 
 /* Exported macro ------------------------------------------------------------*/
 /* USER CODE BEGIN EM */
-extern uint8_t receive_buffer[300];  // 定义接收缓存,这里假设最多可以接收100个字节
+extern uint8_t receive_buffer[100];  // 定义接收缓存,这里假设最多可以接收100个字节
 extern uint8_t flag ;
 
 extern uint16_t Adc2_CalibrationValue ;

+ 1 - 0
Core/Inc/usart.h

@@ -45,6 +45,7 @@ void MX_USART3_UART_Init(void);
 
 /* USER CODE BEGIN Prototypes */
 void update_baudrate(uint16_t temp);
+
 /* USER CODE END Prototypes */
 
 #ifdef __cplusplus

+ 1 - 1
Core/Src/adc.c

@@ -189,7 +189,7 @@ void GetADCResults(ADC_HandleTypeDef* hadc)
     HAL_ADC_Start(&hadc1);
     HAL_ADC_PollForConversion(&hadc1, 100);
     uint16_t adc1_raw = HAL_ADC_GetValue(&hadc1);
-    float temperature = CalculateTemperature(adc1_raw, 3.3) * 10 + 10;
+    float temperature = CalculateTemperature(adc1_raw, 3.3) * 10 - 40;
     ApplyFilter((uint16_t)temperature, &adc1_filtered, adc1_raw_buffer, &adc1_raw_buffer_index);
     
     HAL_ADC_Start(&hadc1);

+ 165 - 60
Core/Src/main.c

@@ -52,7 +52,7 @@ int fputc(int ch, FILE *f)
 /* Private variables ---------------------------------------------------------*/
 
 /* USER CODE BEGIN PV */
-uint8_t data_to_send[300];
+uint8_t data_to_send[100];
 uint16_t CRC_value = 0;
 uint16_t initial_address = 0x00C1;
 uint32_t System_version  = 0x23051100;
@@ -116,6 +116,9 @@ int main(void)
   /* USER CODE BEGIN WHILE */
   while (1)
   {
+		__HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_ORE); // 清除接收溢出标志位
+		__HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_RXNE); // 清除接收中断标志位
+
 #if Modbus
     gpioaStatus = GetPaInputStatus();
     gpiobStatus = GetPbOutputStatus();
@@ -131,8 +134,13 @@ int main(void)
             CRC_value = calculate_crc(data_to_send,5);
             data_to_send[5] = (uint8_t)(CRC_value & 0xFF);
             data_to_send[6] = (uint8_t)(CRC_value >> 8);
-            HAL_UART_Transmit(&huart1, data_to_send, 7, 9); // 发送数据
-            flag = 0; // 将标志位重新置为0
+						HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 7, 9);
+						if (status == HAL_OK) {
+								// 发送成功
+								flag = 0; // 将标志位重新置为0
+						} else {
+								HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
+						}
         }
 
         else if(receive_buffer[0] == (uint8_t)initial_address && receive_buffer[1] == 0x03 && receive_buffer[2] == 0x00 && receive_buffer[3] == 0x02 && receive_buffer[4] == 0x00 && receive_buffer[5] == 0x01){
@@ -144,8 +152,13 @@ int main(void)
             CRC_value = calculate_crc(data_to_send,5);
             data_to_send[5] = (uint8_t)(CRC_value & 0xFF);
             data_to_send[6] = (uint8_t)(CRC_value >> 8);
-            HAL_UART_Transmit(&huart1, data_to_send, 7, 9); // 发送数据
-            flag = 0; // 将标志位重新置为0
+						HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 7, 9);
+						if (status == HAL_OK) {
+								// 发送成功
+								flag = 0; // 将标志位重新置为0
+						} else {
+								HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
+						}
         }
         
         else if(receive_buffer[0] == (uint8_t)initial_address && receive_buffer[1] == 0x03 && receive_buffer[2] == 0x00 && receive_buffer[3] == 0x03 && receive_buffer[4] == 0x00 && receive_buffer[5] == 0x01){
@@ -157,8 +170,13 @@ int main(void)
             CRC_value = calculate_crc(data_to_send,5);
             data_to_send[5] = (uint8_t)(CRC_value & 0xFF);
             data_to_send[6] = (uint8_t)(CRC_value >> 8);
-            HAL_UART_Transmit(&huart1, data_to_send, 7, 7); // 发送数据
-            flag = 0; // 将标志位重新置为0
+						HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 7, 9);
+						if (status == HAL_OK) {
+								// 发送成功
+								flag = 0; // 将标志位重新置为0
+						} else {
+								HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
+						}
         }
         
         else if(receive_buffer[0] == (uint8_t)initial_address && receive_buffer[1] == 0x03 && receive_buffer[2] == 0x00 && receive_buffer[3] == 0x04 && receive_buffer[4] == 0x00 && receive_buffer[5] == 0x01){
@@ -170,8 +188,13 @@ int main(void)
             CRC_value = calculate_crc(data_to_send,5);
             data_to_send[5] = (uint8_t)(CRC_value & 0xFF);
             data_to_send[6] = (uint8_t)(CRC_value >> 8);
-            HAL_UART_Transmit(&huart1, data_to_send, 7, 7); // 发送数据
-            flag = 0; // 将标志位重新置为0
+						HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 7, 9);
+						if (status == HAL_OK) {
+								// 发送成功
+								flag = 0; // 将标志位重新置为0
+						} else {
+								HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
+						}
         }
         
         else if(receive_buffer[0] == (uint8_t)initial_address && receive_buffer[1] == 0x03 && receive_buffer[2] == 0x00 && receive_buffer[3] == 0x05 && receive_buffer[4] == 0x00 && receive_buffer[5] == 0x01){
@@ -183,8 +206,13 @@ int main(void)
             CRC_value = calculate_crc(data_to_send,5);
             data_to_send[5] = (uint8_t)(CRC_value & 0xFF);
             data_to_send[6] = (uint8_t)(CRC_value >> 8);
-            HAL_UART_Transmit(&huart1, data_to_send, 7, 7); // 发送数据
-            flag = 0; // 将标志位重新置为0
+						HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 7, 9);
+						if (status == HAL_OK) {
+								// 发送成功
+								flag = 0; // 将标志位重新置为0
+						} else {
+								HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
+						}
         }
         
         else if(receive_buffer[0] == (uint8_t)initial_address && receive_buffer[1] == 0x06 && receive_buffer[2] == 0x00 && receive_buffer[3] == 0x0A && receive_buffer[4] == 0x00 && receive_buffer[5] == 0x01){
@@ -200,8 +228,13 @@ int main(void)
             CRC_value = calculate_crc(data_to_send,5);
             data_to_send[5] = (uint8_t)(CRC_value & 0xFF);
             data_to_send[6] = (uint8_t)(CRC_value >> 8);
-            HAL_UART_Transmit(&huart1, data_to_send, 7, 7); // 发送数据
-            flag = 0; // 将标志位重新置为0
+						HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 7, 9);
+						if (status == HAL_OK) {
+								// 发送成功
+								flag = 0; // 将标志位重新置为0
+						} else {
+								HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
+						}
         }
         
         else if(receive_buffer[0] == (uint8_t)initial_address && receive_buffer[1] == 0x06 && receive_buffer[2] == 0x00 && receive_buffer[3] == 0x0B && receive_buffer[4] == 0x00 && receive_buffer[5] == 0x01){
@@ -217,8 +250,13 @@ int main(void)
             CRC_value = calculate_crc(data_to_send,5);
             data_to_send[5] = (uint8_t)(CRC_value & 0xFF);
             data_to_send[6] = (uint8_t)(CRC_value >> 8);
-            HAL_UART_Transmit(&huart1, data_to_send, 7, 7); // 发送数据
-            flag = 0; // 将标志位重新置为0
+						HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 7, 9);
+						if (status == HAL_OK) {
+								// 发送成功
+								flag = 0; // 将标志位重新置为0
+						} else {
+								HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
+						}
         }
         
         else if(receive_buffer[0] == (uint8_t)initial_address && receive_buffer[1] == 0x06 && receive_buffer[2] == 0x00 && receive_buffer[3] == 0xAA && receive_buffer[4] == 0x00 ){
@@ -232,8 +270,13 @@ int main(void)
             CRC_value = calculate_crc(data_to_send,5);
             data_to_send[5] = (uint8_t)(CRC_value & 0xFF);
             data_to_send[6] = (uint8_t)(CRC_value >> 8);
-            HAL_UART_Transmit(&huart1, data_to_send, 7, 7); // 发送数据
-            flag = 0; // 将标志位重新置为0
+						HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 7, 9);
+						if (status == HAL_OK) {
+								// 发送成功
+								flag = 0; // 将标志位重新置为0
+						} else {
+								HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
+						}
         }
         
         else if(receive_buffer[0] == (uint8_t)initial_address && receive_buffer[1] == 0x03 && receive_buffer[2] == 0x00 && receive_buffer[3] == 0xBB && receive_buffer[4] == 0x00 && receive_buffer[5] == 0x02){
@@ -247,8 +290,13 @@ int main(void)
             CRC_value = calculate_crc(data_to_send,7);
             data_to_send[7] = (uint8_t)(CRC_value & 0xFF);
             data_to_send[8] = (uint8_t)(CRC_value >> 8);
-            HAL_UART_Transmit(&huart1, data_to_send, 9, 9); // 发送数据
-            flag = 0; // 将标志位重新置为0
+						HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 9, 11);
+						if (status == HAL_OK) {
+								// 发送成功
+								flag = 0; // 将标志位重新置为0
+						} else {
+								HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
+						}
         }
         
         else if(receive_buffer[0] == (uint8_t)initial_address && receive_buffer[1] == 0x06 && receive_buffer[2] == 0x00 && receive_buffer[3] == 0xCC && receive_buffer[4] == 0xA5 && receive_buffer[5] == 0x5A){
@@ -268,8 +316,13 @@ int main(void)
             CRC_value = calculate_crc(data_to_send,5);
             data_to_send[5] = (uint8_t)(CRC_value & 0xFF);
             data_to_send[6] = (uint8_t)(CRC_value >> 8);
-            HAL_UART_Transmit(&huart1, data_to_send, 7, 7); // 发送数据
-            flag = 0; // 将标志位重新置为0
+						HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 7, 9);
+						if (status == HAL_OK) {
+								// 发送成功
+								flag = 0; // 将标志位重新置为0
+						} else {
+								HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
+						}
         }
         
         else if(receive_buffer[0] == (uint8_t)initial_address && receive_buffer[1] == 0x03 && receive_buffer[2] == 0x00 && receive_buffer[3] == 0x01 && receive_buffer[4] == 0x00 && receive_buffer[5] == 0x02 ){
@@ -283,8 +336,13 @@ int main(void)
             CRC_value = calculate_crc(data_to_send,7);
             data_to_send[7] = (uint8_t)(CRC_value & 0xFF);
             data_to_send[8] = (uint8_t)(CRC_value >> 8);
-            HAL_UART_Transmit(&huart1, data_to_send, 9, 8); // 发送数据
-            flag = 0; // 将标志位重新置为0
+						HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 9, 11);
+						if (status == HAL_OK) {
+								// 发送成功
+								flag = 0; // 将标志位重新置为0
+						} else {
+								HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
+						}
         }
         
         else if(receive_buffer[0] == (uint8_t)initial_address && receive_buffer[1] == 0x03 && receive_buffer[2] == 0x00 && receive_buffer[3] == 0x01 && receive_buffer[4] == 0x00 && receive_buffer[5] == 0x03 ){
@@ -300,8 +358,13 @@ int main(void)
             CRC_value = calculate_crc(data_to_send,9);
             data_to_send[9] = (uint8_t)(CRC_value & 0xFF);
             data_to_send[10] = (uint8_t)(CRC_value >> 8);
-            HAL_UART_Transmit(&huart1, data_to_send, 11, 10); // 发送数据
-            flag = 0; // 将标志位重新置为0
+						HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 11, 14);
+						if (status == HAL_OK) {
+								// 发送成功
+								flag = 0; // 将标志位重新置为0
+						} else {
+								HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
+						}
         }
         
         else if(receive_buffer[0] == (uint8_t)initial_address && receive_buffer[1] == 0x03 && receive_buffer[2] == 0x00 && receive_buffer[3] == 0x01 && receive_buffer[4] == 0x00 && receive_buffer[5] == 0x04 ){
@@ -319,8 +382,13 @@ int main(void)
             CRC_value = calculate_crc(data_to_send,11);
             data_to_send[11] = (uint8_t)(CRC_value & 0xFF);
             data_to_send[12] = (uint8_t)(CRC_value >> 8);
-            HAL_UART_Transmit(&huart1, data_to_send, 13, 12); // 发送数据
-            flag = 0; // 将标志位重新置为0
+						HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 13, 15);
+						if (status == HAL_OK) {
+								// 发送成功
+								flag = 0; // 将标志位重新置为0
+						} else {
+								HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
+						}
         }
         
         else if(receive_buffer[0] == (uint8_t)initial_address && receive_buffer[1] == 0x03 && receive_buffer[2] == 0x00 && receive_buffer[3] == 0x01 && receive_buffer[4] == 0x00 && receive_buffer[5] == 0x05 ){
@@ -340,8 +408,13 @@ int main(void)
             CRC_value = calculate_crc(data_to_send,13);
             data_to_send[13] = (uint8_t)(CRC_value & 0xFF);
             data_to_send[14] = (uint8_t)(CRC_value >> 8);
-            HAL_UART_Transmit(&huart1, data_to_send, 15, 14); // 发送数据
-            flag = 0; // 将标志位重新置为0
+						HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 15, 16);
+						if (status == HAL_OK) {
+								// 发送成功
+								flag = 0; // 将标志位重新置为0
+						} else {
+								HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
+						}
         }
         
         else if(receive_buffer[0] == (uint8_t)initial_address && receive_buffer[1] == 0x03 && receive_buffer[2] == 0x00 && receive_buffer[3] == 0x02 && receive_buffer[4] == 0x00 && receive_buffer[5] == 0x02 ){
@@ -355,8 +428,13 @@ int main(void)
             CRC_value = calculate_crc(data_to_send,7);
             data_to_send[7] = (uint8_t)(CRC_value & 0xFF);
             data_to_send[8] = (uint8_t)(CRC_value >> 8);
-            HAL_UART_Transmit(&huart1, data_to_send, 9, 8); // 发送数据
-            flag = 0; // 将标志位重新置为0
+						HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 9, 11);
+						if (status == HAL_OK) {
+								// 发送成功
+								flag = 0; // 将标志位重新置为0
+						} else {
+								HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
+						}
         }
         
         else if(receive_buffer[0] == (uint8_t)initial_address && receive_buffer[1] == 0x03 && receive_buffer[2] == 0x00 && receive_buffer[3] == 0x02 && receive_buffer[4] == 0x00 && receive_buffer[5] == 0x03 ){
@@ -372,8 +450,13 @@ int main(void)
             CRC_value = calculate_crc(data_to_send,9);
             data_to_send[9] = (uint8_t)(CRC_value & 0xFF);
             data_to_send[10] = (uint8_t)(CRC_value >> 8);
-            HAL_UART_Transmit(&huart1, data_to_send, 11, 10); // 发送数据
-            flag = 0; // 将标志位重新置为0
+						HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 11, 14);
+						if (status == HAL_OK) {
+								// 发送成功
+								flag = 0; // 将标志位重新置为0
+						} else {
+								HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
+						}
         }
         
         else if(receive_buffer[0] == (uint8_t)initial_address && receive_buffer[1] == 0x03 && receive_buffer[2] == 0x00 && receive_buffer[3] == 0x02 && receive_buffer[4] == 0x00 && receive_buffer[5] == 0x04 ){
@@ -391,8 +474,13 @@ int main(void)
             CRC_value = calculate_crc(data_to_send,11);
             data_to_send[11] = (uint8_t)(CRC_value & 0xFF);
             data_to_send[12] = (uint8_t)(CRC_value >> 8);
-            HAL_UART_Transmit(&huart1, data_to_send, 13, 12); // 发送数据
-            flag = 0; // 将标志位重新置为0
+						HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 13, 15);
+						if (status == HAL_OK) {
+								// 发送成功
+								flag = 0; // 将标志位重新置为0
+						} else {
+								HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
+						}
         }
         
         else if(receive_buffer[0] == (uint8_t)initial_address && receive_buffer[1] == 0x03 && receive_buffer[2] == 0x00 && receive_buffer[3] == 0x03 && receive_buffer[4] == 0x00 && receive_buffer[5] == 0x02 ){
@@ -406,8 +494,13 @@ int main(void)
             CRC_value = calculate_crc(data_to_send,7);
             data_to_send[7] = (uint8_t)(CRC_value & 0xFF);
             data_to_send[8] = (uint8_t)(CRC_value >> 8);
-            HAL_UART_Transmit(&huart1, data_to_send, 9, 8); // 发送数据
-            flag = 0; // 将标志位重新置为0
+						HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 9, 11);
+						if (status == HAL_OK) {
+								// 发送成功
+								flag = 0; // 将标志位重新置为0
+						} else {
+								HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
+						}
         }
         
         else if(receive_buffer[0] == (uint8_t)initial_address && receive_buffer[1] == 0x03 && receive_buffer[2] == 0x00 && receive_buffer[3] == 0x03 && receive_buffer[4] == 0x00 && receive_buffer[5] == 0x03 ){
@@ -423,8 +516,13 @@ int main(void)
             CRC_value = calculate_crc(data_to_send,9);
             data_to_send[9] = (uint8_t)(CRC_value & 0xFF);
             data_to_send[10] = (uint8_t)(CRC_value >> 8);
-            HAL_UART_Transmit(&huart1, data_to_send, 11, 10); // 发送数据
-            flag = 0; // 将标志位重新置为0
+						HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 11, 14);
+						if (status == HAL_OK) {
+								// 发送成功
+								flag = 0; // 将标志位重新置为0
+						} else {
+								HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
+						}
         }
         
         else if(receive_buffer[0] == (uint8_t)initial_address && receive_buffer[1] == 0x03 && receive_buffer[2] == 0x00 && receive_buffer[3] == 0x04 && receive_buffer[4] == 0x00 && receive_buffer[5] == 0x02 ){
@@ -438,8 +536,13 @@ int main(void)
             CRC_value = calculate_crc(data_to_send,7);
             data_to_send[7] = (uint8_t)(CRC_value & 0xFF);
             data_to_send[8] = (uint8_t)(CRC_value >> 8);
-            HAL_UART_Transmit(&huart1, data_to_send, 9, 8); // 发送数据
-            flag = 0; // 将标志位重新置为0
+						HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 9, 11);
+						if (status == HAL_OK) {
+								// 发送成功
+								flag = 0; // 将标志位重新置为0
+						} else {
+								HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
+						}
         }
         
         else if(receive_buffer[0] == (uint8_t)initial_address && receive_buffer[1] == 0x06 && receive_buffer[2] == 0x00 && receive_buffer[3] == 0x0C && receive_buffer[4] == 0x00 ){
@@ -455,8 +558,13 @@ int main(void)
             CRC_value = calculate_crc(data_to_send,6);
             data_to_send[6] = (uint8_t)(CRC_value & 0xFF);
             data_to_send[7] = (uint8_t)(CRC_value >> 8);
-            HAL_UART_Transmit(&huart1, data_to_send, 8, 8); // 发送数据
-            flag = 0; // 将标志位重新置为0
+						HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 8, 9);
+						if (status == HAL_OK) {
+								// 发送成功
+								flag = 0; // 将标志位重新置为0
+						} else {
+								HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
+						}
         }
         
         else if(receive_buffer[0] == 0xFA && receive_buffer[1] == 0x03 && receive_buffer[2] == 0x00 && receive_buffer[3] == 0xAA && receive_buffer[4] == 0x00  && receive_buffer[5] == 0x01 ){
@@ -468,29 +576,26 @@ int main(void)
             CRC_value = calculate_crc(data_to_send,5);
             data_to_send[5] = (uint8_t)(CRC_value & 0xFF); 
             data_to_send[6] = (uint8_t)(CRC_value >> 8);
-            HAL_UART_Transmit(&huart1, data_to_send, 7, 7); // 发送数据
-            flag = 0; // 将标志位重新置为0
-        }
+						HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 7, 9);
+						if (status == HAL_OK) {
+								// 发送成功
+								flag = 0; // 将标志位重新置为0
+						} else {
+								HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
+						}
+        }
+				
+		memset(receive_buffer,0,sizeof(receive_buffer));
+		read_new_address_from_flash(ADDR_FLASH_PAGE_61); 
+		__HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_ORE); // 清除接收溢出标志位
+		__HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_RXNE); // 清除接收中断标志位
     }
 
     HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
 		HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_SET);
 		HAL_Delay(1);
 		HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET);
-		
-		
-		static uint32_t last_receive_time = 0; // 上一次接收到数据的时间
-    uint32_t current_time = HAL_GetTick(); // 当前时间	
-		if (HAL_UART_GetError(&huart1) != HAL_UART_ERROR_NONE) // 如果发生了错误
-    {
-        NVIC_SystemReset(); // 重启设备
-    }
-    if (current_time - last_receive_time > 1000) // 如果1秒钟没有接收到数据
-    {
-        NVIC_SystemReset(); // 重启设备
-    }
-    last_receive_time = current_time; // 更新上一次接收到数据的时间
-		
+
 		#endif
     /* USER CODE END WHILE */
 

+ 11 - 4
Core/Src/stm32f1xx_it.c

@@ -44,7 +44,7 @@
 
 /* Private variables ---------------------------------------------------------*/
 /* USER CODE BEGIN PV */
-uint8_t receive_buffer[300];  // 定义接收缓存,这里假设最多可以接收100个字节
+uint8_t receive_buffer[100];  // 定义接收缓存,这里假设最多可以接收100个字节
 uint8_t flag = 0;
 
 uint8_t gpioaStatus = 0;
@@ -228,13 +228,13 @@ void TIM2_IRQHandler(void)
 void USART1_IRQHandler(void)
 {
   /* USER CODE BEGIN USART1_IRQn 0 */
-    static int index = 0; // 用于记录存储到数组中的位置
+		static uint8_t index = 0; // 用于记录存储到数组中的位置
     HAL_UART_Receive(&huart1, &receive_buffer[index], 1, 1); // 读取接收到的数据
-    if (index < 8) // 如果还没有接收到全部8个字节
+    if (index < 8) // 如果还没有接收到全部数据
     {
         index++; // 将记录位置的变量加1
     }
-    if (index == 8) // 如果已经接收到了全部8个字节
+    if (index == 8) // 如果已经接收到了全部数据
     {
         index = 0; // 将记录位置的变量重新置为0
         flag = 1; // 将标志位置为1
@@ -242,6 +242,13 @@ void USART1_IRQHandler(void)
   /* USER CODE END USART1_IRQn 0 */
   HAL_UART_IRQHandler(&huart1);
   /* USER CODE BEGIN USART1_IRQn 1 */
+    if (__HAL_UART_GET_FLAG(&huart1, UART_FLAG_ORE) == SET) // 如果发生了接收溢出
+    {
+        __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_RXNE); // 清除接收中断标志位
+        __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_ORE); // 清除接收溢出标志位
+        index = 0; // 重新开始接收数据
+        HAL_UART_Receive_IT(&huart1, receive_buffer, 1); // 重新开启串口接收中断
+    }
 
   /* USER CODE END USART1_IRQn 1 */
 }

+ 11 - 0
Core/Src/usart.c

@@ -217,4 +217,15 @@ void update_baudrate(uint16_t temp) {
         change_baudrate(9600);
     }
 }
+void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
+{
+		__HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_ORE); // 헌뇜쌈澗淚놔깃羚貫
+		__HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_RXNE); // 헌뇜쌈澗櫓뙤깃羚貫
+		
+}
+void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
+{
+		__HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_ORE); // 헌뇜쌈澗淚놔깃羚貫
+		__HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_RXNE); // 헌뇜쌈澗櫓뙤깃羚貫
+}
 /* USER CODE END 1 */

File diff suppressed because it is too large
+ 47 - 29
MDK-ARM/iocollect.uvguix.terry


+ 1 - 1
MDK-ARM/iocollect.uvoptx

@@ -140,7 +140,7 @@
         <SetRegEntry>
           <Number>0</Number>
           <Key>JL2CM3</Key>
-          <Name>-U69618568 -O78 -S2 -ZTIFSpeedSel5000 -A0 -C0 -JU1 -JI127.0.0.1 -JP0 -RST0 -N00("ARM CoreSight SW-DP") -D00(1BA01477) -L00(0) -TO18 -TC10000000 -TP21 -TDS8007 -TDT0 -TDC1F -TIEFFFFFFFF -TIP8 -TB1 -TFE0 -FO11 -FD20000000 -FC1000 -FN1 -FF0STM32F10x_512.FLM -FS08000000 -FL080000 -FP0($$Device:STM32F103RE$Flash\STM32F10x_512.FLM)</Name>
+          <Name>-U69618568 -O78 -S2 -ZTIFSpeedSel5000 -A0 -C0 -JU1 -JI127.0.0.1 -JP0 -RST0 -N00("ARM CoreSight SW-DP") -D00(1BA01477) -L00(0) -TO18 -TC10000000 -TP21 -TDS8007 -TDT0 -TDC1F -TIEFFFFFFFF -TIP8 -TB1 -TFE0 -FO31 -FD20000000 -FC1000 -FN1 -FF0STM32F10x_512.FLM -FS08000000 -FL080000 -FP0($$Device:STM32F103RE$Flash\STM32F10x_512.FLM)</Name>
         </SetRegEntry>
         <SetRegEntry>
           <Number>0</Number>