找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3042|回复: 0
收起左侧

stm32f103单线半双工uart通信程序

[复制链接]
ID:476222 发表于 2020-1-18 21:48 | 显示全部楼层 |阅读模式
在使用数字舵机时,所用到的通信方式为uart通信,但舵机只有三根接线,出去vcc和gnd,只有一条通信线,也就是说要实现双向通信,只能使用单线半双工模式,本人在利用stm32标准库配置uart串口时,发现标准库配置较为繁琐,且容错率较低,稍有不慎,uart就无法实现单项通信,遂决定使用STcubeMX软件,使用官方hal库对单片机进行配置,自己摸索实属不易,现仅能实现uart1接收再通过uart1发送,以STM32F103C8为例,仅通过A9即可实现上位机与单片机之间的通讯。现将相关文件上传。

单片机源程序如下:
  1. /* USER CODE END Header */

  2. /* Includes ------------------------------------------------------------------*/
  3. #include "main.h"
  4. #include "usart.h"
  5. #include "gpio.h"

  6. /* Private includes ----------------------------------------------------------*/
  7. /* USER CODE BEGIN Includes */

  8. /* USER CODE END Includes */

  9. /* Private typedef -----------------------------------------------------------*/
  10. /* USER CODE BEGIN PTD */

  11. /* USER CODE END PTD */

  12. /* Private define ------------------------------------------------------------*/
  13. /* USER CODE BEGIN PD */

  14. /* USER CODE END PD */

  15. /* Private macro -------------------------------------------------------------*/
  16. /* USER CODE BEGIN PM */

  17. /* USER CODE END PM */

  18. /* Private variables ---------------------------------------------------------*/

  19. /* USER CODE BEGIN PV */

  20. /* USER CODE END PV */

  21. /* Private function prototypes -----------------------------------------------*/
  22. void SystemClock_Config(void);
  23. /* USER CODE BEGIN PFP */

  24. /* USER CODE END PFP */

  25. /* Private user code ---------------------------------------------------------*/
  26. /* USER CODE BEGIN 0 */

  27. /* USER CODE END 0 */

  28. /**
  29.   * @brief  The application entry point.
  30.   * @retval int
  31.   */
  32. int main(void)
  33. {
  34.   /* USER CODE BEGIN 1 */
  35.         uint32_t preticks;
  36.         uint16_t i,len;
  37.   /* USER CODE END 1 */

  38.   /* MCU Configuration--------------------------------------------------------*/

  39.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  40.   HAL_Init();

  41.   /* USER CODE BEGIN Init */

  42.   /* USER CODE END Init */

  43.   /* Configure the system clock */
  44.   SystemClock_Config();

  45.   /* USER CODE BEGIN SysInit */

  46.   /* USER CODE END SysInit */

  47.   /* Initialize all configured peripherals */
  48.   MX_GPIO_Init();
  49.   MX_USART1_UART_Init();
  50.   /* USER CODE BEGIN 2 */

  51.         printf("hello this is one wire uart demo: \r\n");
  52.   /* USER CODE END 2 */

  53.   /* Infinite loop */
  54.   /* USER CODE BEGIN WHILE */
  55.   while (1)
  56.   {
  57.     /* USER CODE END WHILE */

  58.     /* USER CODE BEGIN 3 */
  59.                 if( (BufWrite  != BufRead) && (RevTicks > 5) )
  60.                 {
  61.                         len = (BufWrite+255-BufRead)%255;
  62.                         HAL_HalfDuplex_EnableTransmitter(&huart1);
  63.                        
  64.                         for( i = 0; i< len ; i++)
  65.                         {
  66.                                 HAL_UART_Transmit(&huart1,RxBuf+BufRead,1,100 );
  67.                                 BufRead++;
  68.                         }
  69.                        
  70.                         HAL_HalfDuplex_EnableReceiver(&huart1);
  71.                        
  72.                         printf("\r\n");
  73.                 }
  74.   }
  75.   /* USER CODE END 3 */
  76. }

  77. /**
  78.   * @brief System Clock Configuration
  79.   * @retval None
  80.   */
  81. void SystemClock_Config(void)
  82. {
  83.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  84.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  85.   /**Initializes the CPU, AHB and APB busses clocks
  86.   */
  87.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  88.   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  89.   RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  90.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  91.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  92.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  93.   RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  94.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  95.   {
  96.     Error_Handler();
  97.   }
  98.   /**Initializes the CPU, AHB and APB busses clocks
  99.   */
  100.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  101.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  102.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  103.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  104.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  105.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  106.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  107.   {
  108.     Error_Handler();
  109.   }
  110. }

  111. /* USER CODE BEGIN 4 */
  112. int fputc( int dat, FILE *file )
  113. {
  114.         uint8_t ch = dat;
  115.        
  116.         HAL_HalfDuplex_EnableTransmitter(&huart1);
  117.         HAL_UART_Transmit(&huart1,&ch,1,100 );
  118.         HAL_HalfDuplex_EnableReceiver(&huart1);
  119.        
  120. }

  121. /* USER CODE END 4 */

  122. /**
  123.   * @brief  This function is executed in case of error occurrence.
  124.   * @retval None
  125.   */
  126. void Error_Handler(void)
  127. {
  128.   /* USER CODE BEGIN Error_Handler_Debug */
  129.   /* User can add his own implementation to report the HAL error return state */

  130.   /* USER CODE END Error_Handler_Debug */
  131. }

  132. #ifdef  USE_FULL_ASSERT
  133. /**
  134.   * @brief  Reports the name of the source file and the source line number
  135.   *         where the assert_param error has occurred.
  136.   * @param  file: pointer to the source file name
  137.   * @param  line: assert_param error line source number
  138.   * @retval None
  139.   */
  140. void assert_failed(uint8_t *file, uint32_t line)
  141. {
  142.   /* USER CODE BEGIN 6 */
  143.   /* User can add his own implementation to report the file name and line number,
  144.      tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  145.   /* USER CODE END 6 */
  146. }
  147. #endif /* USE_FULL_ASSERT */

  148. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
复制代码

所有资料51hei提供下载:
stm32_1wireusart.7z (32.9 KB, 下载次数: 56)

发送和接收效果

发送和接收效果

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表