找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2467|回复: 2
收起左侧

stm32vet6单片机RS485通信程序程序

[复制链接]
ID:435120 发表于 2018-11-28 17:36 | 显示全部楼层 |阅读模式
stm32vet6  RS485通信程序  需另外自己购买USB转485的模块

单片机源程序如下:
  1. #include "main.h"
  2. #include "delay.h"
  3. #include "uart5.h"

  4. int main(void)
  5. {   
  6.         u16 len;
  7.     u8  time;
  8.        
  9.         delay_init();            //延时初始化
  10.         RS485_1_Init(9600);    //第一路485初始化
  11.        
  12.         while(1)
  13.         {                 
  14.                 if(time>=50)                                        //time>=50表示500ms时间到
  15.                 {
  16.                         time=0;                                         //清除计数位
  17.                         RS485_1_printf("请您发送数据,以回车结尾\r\n");   //500ms时间到,发送提示信息
  18.                 }
  19.                 if(RS485_1_RX_STA&0x8000)                                  //RS485_1_RX_STA位15置位,表示接收完成
  20.                 {                                          
  21.                         len=RS485_1_RX_STA&0x3fff;                             //得到此次接收到的数据长度
  22.                         RS485_1_RX_BUF[len]='\0'   ;                           //加入结束符
  23.                         RS485_1_printf("你发送的数据:%s\r\n",RS485_1_RX_BUF); //返回接收到的数据
  24.                         RS485_1_RX_STA=0;                                      //清除状态位
  25.                 }
  26.                 time++;                                             //计数+1
  27.                 delay_ms(10);                                       //延时10ms
  28.         }
  29. }
复制代码

  1. /*-------------------------------------------------*/
  2. /*            RS485_1_1源文件,有2个函数              */
  3. /*-------------------------------------------------*/
  4. /* void RS485_1_1_Init(u32 bound)      485初始化串口5*/
  5. /* void RS485_1_1_printf(char* fmt,...)485printf函数 */
  6. /*-------------------------------------------------*/

  7. #include "uart5.h"
  8. #include "delay.h"
  9.        
  10. u8  RS485_1_RX_BUF[RS485_1_RXBUFF_SIZE];  //接收缓存区
  11. u16 RS485_1_RX_STA=0;                     //接收状态标记

  12. /*-------------------------------------------------*/
  13. /*函数名:初始化串口5-RS485_1                      */
  14. /*参  数:bound:波特率                            */
  15. /*返回值:无                                       */
  16. /*-------------------------------------------------*/
  17. void RS485_1_Init(u32 bound)
  18. {                  
  19.     GPIO_InitTypeDef GPIO_InitStructure;
  20.         USART_InitTypeDef USART_InitStructure;
  21.         NVIC_InitTypeDef NVIC_InitStructure;
  22.        
  23.         RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5,ENABLE);  //使能串口5时钟
  24.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);  //使能GPIOC时钟
  25.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD,ENABLE);  //使能GPIOD时钟
  26.        
  27.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;            //PC12
  28.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  29.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;             //复用推挽输出
  30.     GPIO_Init(GPIOC, &GPIO_InitStructure);               //初始化PC12
  31.    
  32.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;            //PD2
  33.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  34.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入
  35.     GPIO_Init(GPIOD, &GPIO_InitStructure);               //初始化PD2
  36.        
  37.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;            //PD4
  38.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  39.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;         //复用推挽输出
  40.     GPIO_Init(GPIOD, &GPIO_InitStructure);               //初始化PD4
  41.        
  42.         USART_InitStructure.USART_BaudRate = bound;                                    //波特率设置
  43.         USART_InitStructure.USART_WordLength = USART_WordLength_8b;                    //字长为8位数据格式
  44.         USART_InitStructure.USART_StopBits = USART_StopBits_1;                         //一个停止位
  45.         USART_InitStructure.USART_Parity = USART_Parity_No;                            //无奇偶校验位
  46.         USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制
  47.         USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;                       //收发模式
  48.     USART_Init(UART5, &USART_InitStructure);                                      //按配置设置串口4
  49.        
  50.     USART_Cmd(UART5, ENABLE);                              //使能串口5
  51.         USART_ClearFlag(UART5, USART_FLAG_TC);                        //清除标志位
  52.         USART_ITConfig(UART5, USART_IT_RXNE, ENABLE);          //开启接受中断

  53.     NVIC_InitStructure.NVIC_IRQChannel = UART5_IRQn;        //串口5中断
  54.         NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2; //抢占优先级3
  55.         NVIC_InitStructure.NVIC_IRQChannelSubPriority =2;                //子优先级3
  56.         NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;                        //中断通道使能
  57.         NVIC_Init(&NVIC_InitStructure);                                //根据配置设置
  58.        
  59.         RS485_1_RX_TX=0;                                                            //接收模式
  60. }



  61. /*-------------------------------------------------*/
  62. /*函数名:第一路485的printf                        */
  63. /*参  数:无                                       */
  64. /*返回值:无                                       */
  65. /*-------------------------------------------------*/

  66. __align(8) u8  RS485_1_TX_BUF[RS485_1_TXBUFF_SIZE];  

  67. void RS485_1_printf(char* fmt,...)
  68. {  
  69.         u16 i,length;
  70.        
  71.         va_list ap;
  72.         va_start(ap,fmt);
  73.         vsprintf((char*)RS485_1_TX_BUF,fmt,ap);
  74.         va_end(ap);
  75.        
  76.         RS485_1_RX_TX=1;  //发送模式
  77.         delay_ms(1);
  78.        
  79.         length=strlen((const char*)RS485_1_TX_BUF);       
  80.         while((UART5->SR&0X40)==0);
  81.         for(i = 0;i < length;i ++)
  82.         {                       
  83.                 UART5->DR = RS485_1_TX_BUF[i];
  84.                 while((UART5->SR&0X40)==0);       
  85.         }       
  86.        
  87.         RS485_1_RX_TX=0;  //接收模式
  88.         delay_ms(1);
  89. }
复制代码


所有资料51hei提供下载:
7-第1路 RS485_1 通信测试程序.rar (272.4 KB, 下载次数: 56)

评分

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

查看全部评分

回复

使用道具 举报

ID:68875 发表于 2018-11-28 23:00 | 显示全部楼层

看一下学习一下
回复

使用道具 举报

ID:452289 发表于 2020-6-5 14:32 | 显示全部楼层
RS485可以实现printf函数吗?
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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