stm32vet6 RS485通信程序 需另外自己购买USB转485的模块
单片机源程序如下:
- #include "main.h"
- #include "delay.h"
- #include "uart5.h"
- int main(void)
- {
- u16 len;
- u8 time;
-
- delay_init(); //延时初始化
- RS485_1_Init(9600); //第一路485初始化
-
- while(1)
- {
- if(time>=50) //time>=50表示500ms时间到
- {
- time=0; //清除计数位
- RS485_1_printf("请您发送数据,以回车结尾\r\n"); //500ms时间到,发送提示信息
- }
- if(RS485_1_RX_STA&0x8000) //RS485_1_RX_STA位15置位,表示接收完成
- {
- len=RS485_1_RX_STA&0x3fff; //得到此次接收到的数据长度
- RS485_1_RX_BUF[len]='\0' ; //加入结束符
- RS485_1_printf("你发送的数据:%s\r\n",RS485_1_RX_BUF); //返回接收到的数据
- RS485_1_RX_STA=0; //清除状态位
- }
- time++; //计数+1
- delay_ms(10); //延时10ms
- }
- }
复制代码
- /*-------------------------------------------------*/
- /* RS485_1_1源文件,有2个函数 */
- /*-------------------------------------------------*/
- /* void RS485_1_1_Init(u32 bound) 485初始化串口5*/
- /* void RS485_1_1_printf(char* fmt,...)485printf函数 */
- /*-------------------------------------------------*/
- #include "uart5.h"
- #include "delay.h"
-
- u8 RS485_1_RX_BUF[RS485_1_RXBUFF_SIZE]; //接收缓存区
- u16 RS485_1_RX_STA=0; //接收状态标记
- /*-------------------------------------------------*/
- /*函数名:初始化串口5-RS485_1 */
- /*参 数:bound:波特率 */
- /*返回值:无 */
- /*-------------------------------------------------*/
- void RS485_1_Init(u32 bound)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- USART_InitTypeDef USART_InitStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
-
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5,ENABLE); //使能串口5时钟
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE); //使能GPIOC时钟
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD,ENABLE); //使能GPIOD时钟
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; //PC12
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
- GPIO_Init(GPIOC, &GPIO_InitStructure); //初始化PC12
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //PD2
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入
- GPIO_Init(GPIOD, &GPIO_InitStructure); //初始化PD2
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; //PD4
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //复用推挽输出
- GPIO_Init(GPIOD, &GPIO_InitStructure); //初始化PD4
-
- USART_InitStructure.USART_BaudRate = bound; //波特率设置
- USART_InitStructure.USART_WordLength = USART_WordLength_8b; //字长为8位数据格式
- USART_InitStructure.USART_StopBits = USART_StopBits_1; //一个停止位
- USART_InitStructure.USART_Parity = USART_Parity_No; //无奇偶校验位
- USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制
- USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收发模式
- USART_Init(UART5, &USART_InitStructure); //按配置设置串口4
-
- USART_Cmd(UART5, ENABLE); //使能串口5
- USART_ClearFlag(UART5, USART_FLAG_TC); //清除标志位
- USART_ITConfig(UART5, USART_IT_RXNE, ENABLE); //开启接受中断
- NVIC_InitStructure.NVIC_IRQChannel = UART5_IRQn; //串口5中断
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2; //抢占优先级3
- NVIC_InitStructure.NVIC_IRQChannelSubPriority =2; //子优先级3
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //中断通道使能
- NVIC_Init(&NVIC_InitStructure); //根据配置设置
-
- RS485_1_RX_TX=0; //接收模式
- }
- /*-------------------------------------------------*/
- /*函数名:第一路485的printf */
- /*参 数:无 */
- /*返回值:无 */
- /*-------------------------------------------------*/
- __align(8) u8 RS485_1_TX_BUF[RS485_1_TXBUFF_SIZE];
- void RS485_1_printf(char* fmt,...)
- {
- u16 i,length;
-
- va_list ap;
- va_start(ap,fmt);
- vsprintf((char*)RS485_1_TX_BUF,fmt,ap);
- va_end(ap);
-
- RS485_1_RX_TX=1; //发送模式
- delay_ms(1);
-
- length=strlen((const char*)RS485_1_TX_BUF);
- while((UART5->SR&0X40)==0);
- for(i = 0;i < length;i ++)
- {
- UART5->DR = RS485_1_TX_BUF[i];
- while((UART5->SR&0X40)==0);
- }
-
- RS485_1_RX_TX=0; //接收模式
- delay_ms(1);
- }
复制代码
所有资料51hei提供下载:
7-第1路 RS485_1 通信测试程序.rar
(272.4 KB, 下载次数: 56)
|