Skip to content
The Chat Archive
gpt-3.5-turbo-0301· OpenAI WildChat-4.8M (AI2)

实验3:并行通信实验,实验板为stm32f407,给出设计方法及具体的实现代码 内容:使用两块实验板,连接必要的GPIO接口,实现并行数据传输。设计实现方案并实现。 要求:…

  1. User

    实验3:并行通信实验,实验板为stm32f407,给出设计方法及具体的实现代码 内容:使用两块实验板,连接必要的GPIO接口,实现并行数据传输。设计实现方案并实现。

    要求:

    使用握手信号以保证数据可靠传送 支持查询和中断方式 能够展示数据传送的过程和结果 接收设备的LED显示能够跟随发送设备上拨码开关的状态和变化 鼓励设计其它展示方式 计算数据传送的速度

  2. Assistant

    实验过程:

    1.确定GPIO接口连接

    发送设备连接5个GPIO口,用于数据和握手信号的传输,以及一个拨码开关GPIO口,用于控制LED的开关;接收设备连接5个GPIO口,用于数据和握手信号的传输,以及一个LED GPIO口,用于显示状态。

    2.设计并实现UART协议

    在发送设备中,通过程序设计实现UART协议,设计和发送数据格式和数据帧。

    3.实现并行数据传输

    在发送设备中,设计并实现并行数据传输功能,包括本地存储数据、判断握手信号、发送数据。在接收设备中,设计并实现并行数据接收功能,包括接收数据、判断握手信号、存储数据。同时,在每次数据传输结束后,通过握手信号保证数据可靠传送。

    4.实现查询和中断方式

    在主循环中,不断查询是否有数据需要发送;在中断服务函数中,接收到数据后立即进行处理。

    5.实现LED显示控制

    通过读取拨码开关状态控制LED的开关,同时将接收到的数据显示在发送设备的LED上。

    6.实现其他展示方式

    在终端窗口中展示数据传送的过程和结果。

    7.计算数据传送速度

    在程序中,通过计算数据传送的时间和传输数据的大小,得出数据传输的速度,并在终端窗口中进行展示。

    实现代码:

    发送设备代码

    #include "main.h"
    #include "stm32f4xx_hal.h"
    
    UART_HandleTypeDef huart3;
    
    /* Private function prototypes -----------------------------------------------*/
    void SystemClock_Config(void);
    static void MX_GPIO_Init(void);
    static void MX_USART3_UART_Init(void);
    
    /* User Variable and Define --------------------------------------------------*/
    #define DATA_NUM 1024               //传输数据的长度
    #define TX_BUFF_SIZE DATA_NUM + 2    //缓冲区数据长度,加上 0xFF 和 0xAA 标识位
    uint8_t tx_buff[TX_BUFF_SIZE];      //发送缓冲区
    uint16_t tx_state = 0x000;          //发送数据状态位,初始状态为 0
    uint8_t tx_irq_flag = 0;            //中断标志位
    uint32_t tx_start_time, tx_end_time;//发送开始和结束时间
    uint16_t data_count = 0;            //计数器,用于生成数据
    
    int main(void)
    {
      /* MCU Configuration--------------------------------------------------------*/
    
      /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
      HAL_Init();
    
      /* Configure the system clock */
      SystemClock_Config();
    
      /* Initialize all configured peripherals */
      MX_GPIO_Init();
      MX_USART3_UART_Init();
    
      while (1)
      {
        /*
         * 查询方式,每 10ms 查询一次是否需要发送数据
         */
        if (tx_state == 0)
        {
          tx_buff[0] = 0xFF;     //添加帧头标识
          tx_buff[TX_BUFF_SIZE - 1] = 0xAA;  //添加帧尾标识
          /*
           * 生成数据,写入缓冲区
           */
          for (int i = 1; i < TX_BUFF_SIZE - 1; i++)
          {
            tx_buff[i] = data_count++;
          }
          tx_state = 0x001;   //更新状态位,表示有数据需要发送
        }
        /*
         * 中断方式,接收到数据后立即进行处理
         */
        if (tx_irq_flag == 1)
        {
          tx_buff[0] = 0xFF;     //添加帧头标识
          tx_buff[TX_BUFF_SIZE - 1] = 0xAA;  //添加帧尾标识
          /*
           * 生成数据,写入缓冲区
           */
          for (int i = 1; i < TX_BUFF_SIZE - 1; i++)
          {
            tx_buff[i] = data_count++;
          }
          tx_state = 0x001;   //更新状态位,表示有数据需要发送
          tx_irq_flag = 0;    //清除中断标志位
        }
    
        /*
         * 将发送数据状态位(tx_state)分为 4 个状态表示发送数据的过程
         */
        switch (tx_state)
        {
        case 0x001:   //发送FF
          HAL_UART_Transmit(&huart3, &tx_buff[0], 1, 1);
          tx_state = 0x002;
          break;
        case 0x002:   //发送数据
          HAL_UART_Transmit(&huart3, &tx_buff[1], DATA_NUM, 1);
          tx_state = 0x003;
          break;
        case 0x003:   //发送校验位
          HAL_UART_Transmit(&huart3, &tx_buff[TX_BUFF_SIZE - 2], 1, 1);
          tx_state = 0x000;   //发送完成,等待下次发送
          break;
        }
    
        /*
         * 控制 LED 
         */
        if (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) == GPIO_PIN_SET)
        {
          HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12, GPIO_PIN_SET);
        }
        else
        {
          HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12, GPIO_PIN_RESET);
        }
      }
    }
    
    /**
      * @brief System Clock Configuration
      * @retval None
      */
    void SystemClock_Config(void)
    {
      RCC_OscInitTypeDef RCC_OscInitStruct = {0};
      RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
    
      /** Initializes the RCC Oscillators according to the specified parameters
      * in the RCC_OscInitTypeDef structure.
      */
      RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
      RCC_OscInitStruct.HSEState = RCC_HSE_ON;
      RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
      RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
      RCC_OscInitStruct.PLL.PLLM = 25;
      RCC_OscInitStruct.PLL.PLLN = 336;
      RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
      RCC_OscInitStruct.PLL.PLLQ = 7;
      if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
      {
        Error_Handler();
      }
      /** Initializes the CPU, AHB and APB buses clocks
      */
      RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK
                                    | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
      RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
      RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
      RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
      RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
    
      if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
      {
        Error_Handler();
      }
    }
    
    /**
      * @brief USART3 Initialization Function
      * @param None
      * @retval None
      */
    static void MX_USART3_UART_Init(void)
    {
    
      /* USER CODE BEGIN USART3_Init 0 */
    
      /* USER CODE END USART3_Init 0 */
    
      /* USER CODE BEGIN USART3_Init 1 */
    
      /* USER CODE END USART3_Init 1 */
      huart3.Instance = USART3;
      huart3.Init.BaudRate = 115200;
      huart3.Init.WordLength = UART_WORDLENGTH_8B;
      huart3.Init.StopBits = UART_STOPBITS_1;
      huart3.Init.Parity = UART_PARITY_NONE;
      huart3.Init.Mode = UART_MODE_TX_RX;
      huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;
      huart3.Init.OverSampling = UART_OVERSAMPLING_16;
      if (HAL_UART_Init(&huart3) != HAL_OK)
      {
        Error_Handler();
      }
      /* USER CODE BEGIN USART3_Init 2 */
    
      /* USER CODE END USART3_Init 2 */
    
    }
    
    /**
      * @brief GPIO Initialization Function
      * @param None
      * @retval None
      */
    static void MX_GPIO_Init(void)
    {
      GPIO_InitTypeDef GPIO_InitStruct = {0};
    
      /* GPIO Ports Clock Enable */
      __HAL_RCC_GPIOC_CLK_ENABLE();
      __HAL_RCC_GPIOH_CLK_ENABLE();
      __HAL_RCC_GPIOA_CLK_ENABLE();
      __HAL_RCC_GPIOD_CLK_ENABLE();
    
      /*Configure GPIO pin Output Level */
      HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12, GPIO_PIN_RESET);
    
      /*Configure GPIO pin : PD12 */
      GPIO_InitStruct.Pin = GPIO_PIN_12;
      GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
      GPIO_InitStruct.Pull = GPIO_NOPULL;
      GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
      HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
    
      /*Configure GPIO pin : PA0 */
      GPIO_InitStruct.Pin = GPIO_PIN_0;
      GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
      GPIO_InitStruct.Pull = GPIO_NOPULL;
      HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
    
    }
    
    /**
      * @brief This function is executed in case of error occurrence.
      * @retval None
      */
    void Error_Handler(void)
    {
      /* USER CODE BEGIN Error_Handler_Debug */
      /* User can add his own implementation to report the HAL error return state */
      while (1)
      {
      }
      /* USER CODE END Error_Handler_Debug */
    }
    
    /**
      * @brief This function is executed in case of Hard Fault occurrence.
      * @retval None
      */
    void HardFault_Handler(void)
    {
      /* USER CODE BEGIN HardFault_Handler_Debug */
      /* User can add his own implementation to report the cause of the HardFault error */
      while (1)
      {
      }
      /* USER CODE END HardFault_Handler_Debug */
    }
    
    /**
      * @brief This function is executed in case of Memory Manage error occurrence.
      * @retval None
      */
    void MemManage_Handler(void)
    {
      /* USER CODE BEGIN MemManage_Handler_Debug */
      /* User can add his own implementation to report the HAL error return state */
      while (1)
      {
      }
      /* USER CODE END MemManage_Handler_Debug */
    }
    
    /**
      * @brief This function is executed in case of Prefetch error occurrence.
      * @retval None
      */
    void Prefetch_Fault_Handler(void)
    {
      /* USER CODE BEGIN Prefetch_Fault_Handler_Debug */
      /* User can add his own implementation to report the HAL error return state */
      while (1)
      {
      }
      /* USER CODE END Prefetch_Fault_Handler_Debug */
    }
    
    /**
      * @brief This function is executed in case of Instruction Cache error occurrence.
      * @retval None
      */
    void InstructionCache_Fault_Handler(void)
    {
      /* USER CODE BEGIN InstructionCache_Fault_Handler_Debug */
      /* User can add his own implementation to report the HAL error return state */
      while (1)
      {
      }
      /* USER CODE END InstructionCache_Fault_Handler_Debug */
    }
    
    /**
      * @brief This function is executed in case of Data Cache error occurrence.
      * @retval None
      */
    void DataCache_Fault_Handler(void)
    {
      /* USER CODE BEGIN DataCache_Fault_Handler_Debug */
      /* User can add his own implementation to report the HAL error return state */
      while (1)
      {
      }
      /* USER CODE END DataCache_Fault_Handler_Debug */
    }
    
    /**
      * @brief This function is executed in case of Unused argument
      * @retval None
      */
    void __attribute__((weak))  UserInstructionCallback(void *argument)
    {
      /* USER CODE BEGIN C1_UserInstructionCallback */
      /* Named "Unused" in CubeMX GUI */
      UNUSED(argument);
      /* USER CODE END C1_UserInstructionCallback */
    }
    
    /**
      * @brief  This function is executed in case of user instruction callback
      * @param  uint32_t UserErrorCode
      * @retval None
      */
    void __attribute__((weak))  UserErrorCallback(uint32_t UserErrorCode)
    {
      /* USER CODE BEGIN UserErrorCallback */
      /* Named "Error" in CubeMX GUI */
      UNUSED(UserErrorCode);
      /* USER CODE END UserErrorCallback */
    }
    
    /**
      * @brief This function is executed in case of user error callback
      * @param None
      * @retval None
      */
    
    /*
     * 中断服务函数,接收到数据后设置中断标志位
     */
    void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
    {
      tx_irq_flag = 1;
    }
    
    #ifdef  USE_FULL_ASSERT
    /**
      * @brief  Reports the name of the source file and the source line number
      *         where the assert_param error has occurred.
      * @param  file: pointer to the source file name
      * @param  line: assert_param error line source number
      * @retval None
      */
    void assert_failed(uint8_t *file, uint32_t line)
    {
      /* USER CODE BEGIN 6 */
      /* User can add his own implementation to report the file name and line number,
         ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
      /* USER CODE END 6 */
    }
    
    #endif /* USE_FULL_ASSERT */
    
    

    接收设备代码

    #include "main.h"
    #include "stm32f4xx_hal.h"
    
    UART_HandleTypeDef huart3;
    
    /* Private function prototypes -----------------------------------------------*/
    void SystemClock_Config(void);
    static void MX_GPIO_Init(void);
    static void MX_USART3_UART_Init(void);
    
    /* User Variable and Define --------------------------------------------------*/
    #define DATA_NUM 1024               //传输数据的长度
    #define RX_BUFF_SIZE DATA_NUM + 2    //缓冲区数据长度,加上 0xFF 和 0xAA 标识位
    uint8_t rx_buff[RX_BUFF_SIZE];      //接收缓冲区
    uint16_t rx_state = 0x000;          //接收数据状态位,初始状态为 0
    uint32_t rx_start_time, rx_end_time;//接收开始和结束时间
    uint16_t data_sum = 0;              //数据校验和
    uint16_t data_count = 0;            //计数器,用于生成数据
    
    int main(void)
    {
      /* MCU Configuration--------------------------------------------------------*/
    
      /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
      HAL_Init();
    
      /* Configure the system clock */
      SystemClock_Config();
    
      /* Initialize all configured peripherals */
      MX_GPIO_Init();
      MX_USART3_UART_Init();
    
      while (1)
      {
        /*
         * 接收数据状态位(rx_state)分为 4 个状态表示接收数据的过程
         */
        switch (rx_state)
        {
        case 0x000:   //等待接收数据
          break;
    
        case 0x001:   //接收 FF
          HAL_UART_Receive(&huart3, &rx_buff[0], 1, 1);
          if (rx_buff[0] == 0xFF)  //判断帧头标识是否正确
          {
            rx_start_time = HAL_GetTick();   //记录接收开始时间
            rx_state = 0x002;
          }
          else
          {
            rx_state = 0x000;   //重新开始接收,等待下一帧数据
          }
          break;
    
        case 0x002:   //接收数据
          HAL_UART_Receive(&huart3, &rx_buff[1], DATA_NUM, 1);
          data_sum = 0;         //清空校验和
          /*
           * 计算数据校验和
           */
          for (int i = 1; i < RX_BUFF_SIZE - 2; i++)
          {
            data_sum += rx_buff[i];
          }
          rx_state = 0x003;
          break;
    
        case 0x003:   //接收校验位
          HAL_UART_Receive(&huart3, &rx_buff[RX_BUFF_SIZE - 2], 1, 1);
          rx_end_time = HAL_GetTick();   //记录接收结束时间
          if (rx_buff[RX_BUFF_SIZE - 2] == (data_sum & 0xFF)     //判断接收数据的校验和是否正确
              && rx_buff[RX_BUFF_SIZE - 1] == 0xAA)  //判断帧尾标识是否正确
          {
            rx_state = 0x000;   //接收数据完成,等待下一次接收
            /*
             * LED 跟随拨码开关
             */
            if (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) == GPIO_PIN_SET)
            {
              HAL_GPIO_WritePin(GPIOD, GPIO_PIN_15, GPIO_PIN_SET);
            }
            else
            {
              HAL_GPIO_WritePin(GPIOD, GPIO_PIN_15, GPIO_PIN_RESET);
            }
          }
          else
          {
            rx_state = 0x000;   //重新开始接收,等待下一帧数据
          }
          break;
    
        default:
          rx_state = 0x000;     //重新开始接收,等待下一帧数据
          break;
        }
      }
    }
    
    /**
      * @brief System Clock Configuration
      * @retval None
      */
    void SystemClock_Config(void)
    {
      RCC_OscInitTypeDef RCC_OscInitStruct = {0};
      RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
    
      /** Initializes the RCC Oscillators according to the specified parameters
      * in the RCC_OscInitTypeDef structure.
      */