|
- /* Includes ------------------------------------------------------------------*/
- #include "main.h"
- #include "stm32f1xx_hal.h"
- #include "dma.h"
- #include "usart.h"
- #include "gpio.h"
- /* USER CODE BEGIN Includes */
- /* USER CODE END Includes */
- /* Private variables ---------------------------------------------------------*/
- /* USER CODE BEGIN PV */
- /* Private variables ---------------------------------------------------------*/
- uint8_t Rx_flag = 0;
- uint8_t TxBuf[TX_DATA_LEN] = {0};
- uint8_t RxBuf[RX_DATA_LEN] = {0};
-
-
- uint8_t recv_end_flag = 0,Rx_len;
-
- /* USER CODE END PV */
- /* Private function prototypes -----------------------------------------------*/
- void SystemClock_Config(void);
- /* USER CODE BEGIN PFP */
- /* Private function prototypes -----------------------------------------------*/
- /* USER CODE END PFP */
- /* USER CODE BEGIN 0 */
- /* USER CODE END 0 */
- int main(void)
- {
- /* USER CODE BEGIN 1 */
-
- /* USER CODE END 1 */
- /* MCU Configuration----------------------------------------------------------*/
- /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
- HAL_Init();
- /* USER CODE BEGIN Init */
- /* USER CODE END Init */
- /* Configure the system clock */
- SystemClock_Config();
- /* USER CODE BEGIN SysInit */
- /* USER CODE END SysInit */
- /* Initialize all configured peripherals */
- MX_GPIO_Init();
- MX_DMA_Init();
- MX_USART1_UART_Init();
- /* USER CODE BEGIN 2 */
- HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_RESET);
- HAL_Delay(1000);
- HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_SET);
-
-
- for(Rx_flag = 0; Rx_flag < TX_DATA_LEN; Rx_flag++)
- {
- TxBuf[Rx_flag] = Rx_flag;
- }
- Rx_flag = 0;
-
- /* 使用DMA传输数据到电脑端 */
- RS485_TX_MODE(); //进入发送模式
- HAL_UART_Transmit_DMA(&huart1,TxBuf, 150);
- HAL_Delay(10);
- HAL_UART_Receive_DMA(&huart1,RxBuf, 5);
-
-
-
- /* USER CODE END 2 */
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
- while (1)
- {
- if(recv_end_flag ==1)
- {
- RS485_TX_MODE(); //进入发送模式
- HAL_UART_Transmit_DMA(&huart1,(uint8_t *)RxBuf,Rx_len);
- Rx_flag = 0;
- HAL_Delay(100);
- for(int i = 0; i < Rx_len ; i++)
- RxBuf[i]=0;
- Rx_len=0;
- recv_end_flag=0;
-
- /* ready for next receive */
- RS485_RX_MODE();
- HAL_UART_Receive_DMA(&huart1,(uint8_t*)RxBuf, 30);
- }
-
-
-
-
- //USART DMA mode
- // if(Rx_flag == 1)
- // {
- // RS485_TX_MODE(); //进入发送模式
- // HAL_UART_Transmit_DMA(&huart1,(uint8_t *)RxBuf,32);
- // Rx_flag = 0;
- // HAL_Delay(10);
- // }
- // if(Rx_flag == 2)
- // {
- // HAL_GPIO_TogglePin(GPIOA,GPIO_PIN_8);
- // Rx_flag = 0;
- // }
-
-
-
- // //TxRx INT mode
- // if(Rx_flag == 1)
- // {
- // RS485_TX_MODE(); //进入发送模式
- // HAL_UART_Transmit_IT(&huart1,(uint8_t *)TxBuf,6);
- // Rx_flag = 0;
- // HAL_Delay(100);
- // }
- // TxRx blocking mode
- // /* 进入发送模式 */
- // RS485_TX_MODE();
- //// HAL_Delay(10);
- // /* 发送数据,轮询直到发送数据完毕 */
- // if(HAL_UART_Transmit(&huart1, temp,DATA_LEN,100)==HAL_OK)
- // {
- // while(__HAL_UART_GET_FLAG(&huart1,UART_FLAG_TC)!=1);
- // }
- //
- //// HAL_Delay(100);
- // /* 进入接收模式 */
- // RS485_RX_MODE();
- //// HAL_Delay(10);
- // /* 轮询直到485接收到数据 */
- // while( __HAL_UART_GET_FLAG(&huart1,UART_FLAG_RXNE)!= HAL_ERROR);
- // HAL_UART_Receive(&huart1,temp,DATA_LEN,100);
- //// HAL_Delay(10);
- /* USER CODE END WHILE */
- /* USER CODE BEGIN 3 */
- }
- /* USER CODE END 3 */
- }
- /** System Clock Configuration
- */
- void SystemClock_Config(void)
- {
- RCC_OscInitTypeDef RCC_OscInitStruct;
- RCC_ClkInitTypeDef RCC_ClkInitStruct;
- /**Initializes the CPU, AHB and APB busses clocks
- */
- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
- RCC_OscInitStruct.HSEState = RCC_HSE_ON;
- RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
- RCC_OscInitStruct.HSIState = RCC_HSI_ON;
- RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
- RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
- RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
- if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
- {
- _Error_Handler(__FILE__, __LINE__);
- }
- /**Initializes the CPU, AHB and APB busses 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_DIV2;
- RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
- RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
- if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
- {
- _Error_Handler(__FILE__, __LINE__);
- }
- /**Configure the Systick interrupt time
- */
- HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
- /**Configure the Systick
- */
- HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
- /* SysTick_IRQn interrupt configuration */
- HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
- }
- /* USER CODE BEGIN 4 */
- /* USER CODE END 4 */
- /**
- * @brief This function is executed in case of error occurrence.
- * @param None
- * @retval None
- */
- void _Error_Handler(char * file, int line)
- {
- /* 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 */
- }
- #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
- /**
- * @}
- */
- /**
- * @}
- */
复制代码
|
评分
-
查看全部评分
|