找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 4826|回复: 4
打印 上一主题 下一主题
收起左侧

基于STM32f103C8T6的485通讯参考源码有问题 求帮助

[复制链接]
跳转到指定楼层
楼主
ID:398895 发表于 2018-9-17 10:17 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
1、该例程为RS485例程。

2、使用说明
   (1)工程文件路径:例程目录\RS485\MDK-ARM\Project.uvproj。
   (2)请使用MDK 4.0以上版本打开,MDK版本过低会导致无法识别工程。
   (3)下载调试工具为ULINK。
   (4)请将RS485A、RS485B连接RS485转RS232,并打开超级终端或串口助手,配置波特率115200,8位,一个停止位,无校验位。
   (5)HEX文件下载到板子后,使用超级终端或串口调试助手可以看到调试信息,表明例程运行正确。

3、注意事项
   请务必在下载、调试、运行过程中,保持板子上电、ULINK连接并插在电脑上。

单片机源程序如下:
  1. /****************************************Copyright (c)****************************************************
  2. **--------------File Info---------------------------------------------------------------------------------
  3. ** File name:               main.c
  4. ** Descriptions:            The RS485 application function
  5. **
  6. **--------------------------------------------------------------------------------------------------------
  7. ** Created by:              AVRman
  8. ** Created date:            2010-10-30
  9. ** Version:                 v1.0
  10. ** Descriptions:            The original version
  11. **
  12. **--------------------------------------------------------------------------------------------------------
  13. ** Modified by:            
  14. ** Modified date:           
  15. ** Version:                 
  16. ** Descriptions:            
  17. **
  18. *********************************************************************************************************/

  19. /* Includes ------------------------------------------------------------------*/
  20. #include "stm32f10x.h"
  21. #include <stdio.h>

  22. #ifdef __GNUC__
  23.   /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
  24.      set to 'Yes') calls __io_putchar() */
  25.   #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
  26. #else
  27.   #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
  28. #endif /* __GNUC__ */

  29. #define DIR485_Receive()    GPIO_ResetBits(GPIOB,GPIO_Pin_2)
  30. #define DIR485_Send()       GPIO_SetBits(GPIOB,GPIO_Pin_2)

  31. /* Private function prototypes -----------------------------------------------*/
  32. void USART_Configuration(void);


  33. /*******************************************************************************
  34. * Function Name  : Delay
  35. * Description    : Delay Time
  36. * Input          : - nCount: Delay Time
  37. * Output         : None
  38. * Return         : None
  39. * Attention                 : None
  40. *******************************************************************************/
  41. void  Delay (uint32_t nCount)
  42. {
  43.   for(; nCount != 0; nCount--);
  44. }

  45. /*******************************************************************************
  46. * Function Name  : main
  47. * Description    : Main program
  48. * Input          : None
  49. * Output         : None
  50. * Return         : None
  51. * Attention                 : None
  52. *******************************************************************************/
  53. int main(void)
  54. {
  55.     uint8_t ch;
  56.         USART_Configuration();
  57.     printf("*****************************************************************\r\n");
  58.     printf("*                                                               *\r\n");
  59.     printf("*  Thank you for using HY-RedBull V3.0 Development Board ! ^_^  *\r\n");
  60.     printf("*                                                               *\r\n");
  61.     printf("*****************************************************************\r\n");
  62.            printf("\r\nPlease input any word :\r\n");
  63.     /* Infinite loop */
  64.     while (1){
  65.                 /* Loop until RXNE = 1 */
  66.         while (USART_GetFlagStatus(USART3, USART_FLAG_RXNE) == RESET);
  67.                 ch = USART_ReceiveData(USART3);
  68.                 Delay(10000);
  69.                 printf("%c",ch);  
  70.     }
  71. }

  72. /*******************************************************************************
  73. * Function Name  : USART_Configuration
  74. * Description    : Configure USART3
  75. * Input          : None
  76. * Output         : None
  77. * Return         : None
  78. * Attention                 : None
  79. *******************************************************************************/
  80. void USART_Configuration(void)
  81. {
  82.   GPIO_InitTypeDef GPIO_InitStructure;
  83.   USART_InitTypeDef USART_InitStructure;

  84.   RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB , ENABLE);
  85.   RCC_APB1PeriphClockCmd( RCC_APB1Periph_USART3 , ENABLE);
  86.         
  87.   /*
  88.   *  USART3_TX -> PB10 , USART3_RX -> PB11 , 485_DIR -> PB2
  89.   */                                
  90.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;                 
  91.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  92.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  93.   GPIO_Init(GPIOB, &GPIO_InitStructure);        
  94.             
  95.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;                 
  96.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  97.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  98.   GPIO_Init(GPIOB, &GPIO_InitStructure);                  

  99.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;               
  100.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;  
  101.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  102.   GPIO_Init(GPIOB, &GPIO_InitStructure);

  103.   USART_InitStructure.USART_BaudRate = 115200;
  104.   USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  105.   USART_InitStructure.USART_StopBits = USART_StopBits_1;
  106.   USART_InitStructure.USART_Parity = USART_Parity_No;
  107.   USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  108.   USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

  109.   USART_Init(USART3, &USART_InitStructure);
  110.   USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
  111.   USART_ClearFlag(USART3,USART_FLAG_TC);
  112.   USART_Cmd(USART3, ENABLE);
  113. }

  114. /**
  115.   * @brief  Retargets the C library printf function to the USART.
  116.   * @param  None
  117.   * @retval None
  118.   */
  119. PUTCHAR_PROTOTYPE
  120. {
  121.   DIR485_Send();

  122.   /* Place your implementation of fputc here */
  123.   /* e.g. write a character to the USART */
  124.   USART_SendData(USART3, (uint8_t) ch);

  125.   /* Loop until the end of transmission */
  126.   while (USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET)
  127.   {}

  128.   DIR485_Receive();

  129.   return ch;
  130. }

  131. #ifdef  USE_FULL_ASSERT

  132. /**
  133.   * @brief  Reports the name of the source file and the source line number
  134. ……………………

  135. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码

所有资料51hei提供下载:
RS485.rar (336.45 KB, 下载次数: 141)




分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏3 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:578440 发表于 2019-7-5 13:54 | 只看该作者
程序不能用
回复

使用道具 举报

板凳
ID:427398 发表于 2019-10-19 17:53 | 只看该作者
有原理图 吗?
回复

使用道具 举报

地板
ID:668292 发表于 2019-12-19 13:56 | 只看该作者
不知道行不行  先看看
回复

使用道具 举报

5#
ID:592010 发表于 2021-6-6 17:10 | 只看该作者
不能用,已验证!
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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