標(biāo)題: stm32vet6單片機(jī)RS485通信程序程序 [打印本頁(yè)]

作者: cq_1120    時(shí)間: 2018-11-28 17:36
標(biāo)題: stm32vet6單片機(jī)RS485通信程序程序
stm32vet6  RS485通信程序  需另外自己購(gòu)買USB轉(zhuǎn)485的模塊

單片機(jī)源程序如下:
  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();            //延時(shí)初始化
  10.         RS485_1_Init(9600);    //第一路485初始化
  11.        
  12.         while(1)
  13.         {                 
  14.                 if(time>=50)                                        //time>=50表示500ms時(shí)間到
  15.                 {
  16.                         time=0;                                         //清除計(jì)數(shù)位
  17.                         RS485_1_printf("請(qǐng)您發(fā)送數(shù)據(jù),以回車結(jié)尾\r\n");   //500ms時(shí)間到,發(fā)送提示信息
  18.                 }
  19.                 if(RS485_1_RX_STA&0x8000)                                  //RS485_1_RX_STA位15置位,表示接收完成
  20.                 {                                          
  21.                         len=RS485_1_RX_STA&0x3fff;                             //得到此次接收到的數(shù)據(jù)長(zhǎng)度
  22.                         RS485_1_RX_BUF[len]='\0'   ;                           //加入結(jié)束符
  23.                         RS485_1_printf("你發(fā)送的數(shù)據(jù):%s\r\n",RS485_1_RX_BUF); //返回接收到的數(shù)據(jù)
  24.                         RS485_1_RX_STA=0;                                      //清除狀態(tài)位
  25.                 }
  26.                 time++;                                             //計(jì)數(shù)+1
  27.                 delay_ms(10);                                       //延時(shí)10ms
  28.         }
  29. }
復(fù)制代碼

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

  7. #include "uart5.h"
  8. #include "delay.h"
  9.        
  10. u8  RS485_1_RX_BUF[RS485_1_RXBUFF_SIZE];  //接收緩存區(qū)
  11. u16 RS485_1_RX_STA=0;                     //接收狀態(tài)標(biāo)記

  12. /*-------------------------------------------------*/
  13. /*函數(shù)名:初始化串口5-RS485_1                      */
  14. /*參  數(shù):bound:波特率                            */
  15. /*返回值:無(wú)                                       */
  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時(shí)鐘
  24.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);  //使能GPIOC時(shí)鐘
  25.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD,ENABLE);  //使能GPIOD時(shí)鐘
  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;             //復(fù)用推挽輸出
  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;         //復(fù)用推挽輸出
  40.     GPIO_Init(GPIOD, &GPIO_InitStructure);               //初始化PD4
  41.        
  42.         USART_InitStructure.USART_BaudRate = bound;                                    //波特率設(shè)置
  43.         USART_InitStructure.USART_WordLength = USART_WordLength_8b;                    //字長(zhǎng)為8位數(shù)據(jù)格式
  44.         USART_InitStructure.USART_StopBits = USART_StopBits_1;                         //一個(gè)停止位
  45.         USART_InitStructure.USART_Parity = USART_Parity_No;                            //無(wú)奇偶校驗(yàn)位
  46.         USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//無(wú)硬件數(shù)據(jù)流控制
  47.         USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;                       //收發(fā)模式
  48.     USART_Init(UART5, &USART_InitStructure);                                      //按配置設(shè)置串口4
  49.        
  50.     USART_Cmd(UART5, ENABLE);                              //使能串口5
  51.         USART_ClearFlag(UART5, USART_FLAG_TC);                        //清除標(biāo)志位
  52.         USART_ITConfig(UART5, USART_IT_RXNE, ENABLE);          //開啟接受中斷

  53.     NVIC_InitStructure.NVIC_IRQChannel = UART5_IRQn;        //串口5中斷
  54.         NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2; //搶占優(yōu)先級(jí)3
  55.         NVIC_InitStructure.NVIC_IRQChannelSubPriority =2;                //子優(yōu)先級(jí)3
  56.         NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;                        //中斷通道使能
  57.         NVIC_Init(&NVIC_InitStructure);                                //根據(jù)配置設(shè)置
  58.        
  59.         RS485_1_RX_TX=0;                                                            //接收模式
  60. }



  61. /*-------------------------------------------------*/
  62. /*函數(shù)名:第一路485的printf                        */
  63. /*參  數(shù):無(wú)                                       */
  64. /*返回值:無(wú)                                       */
  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;  //發(fā)送模式
  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. }
復(fù)制代碼


所有資料51hei提供下載:
7-第1路 RS485_1 通信測(cè)試程序.rar (272.4 KB, 下載次數(shù): 56)



作者: cnc2020    時(shí)間: 2018-11-28 23:00

看一下學(xué)習(xí)一下
作者: zeshoufx    時(shí)間: 2020-6-5 14:32
RS485可以實(shí)現(xiàn)printf函數(shù)嗎?




歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1