標(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ī)源程序如下:
#include "main.h"
#include "delay.h"
#include "uart5.h"
int main(void)
{
u16 len;
u8 time;
delay_init(); //延時(shí)初始化
RS485_1_Init(9600); //第一路485初始化
while(1)
{
if(time>=50) //time>=50表示500ms時(shí)間到
{
time=0; //清除計(jì)數(shù)位
RS485_1_printf("請(qǐng)您發(fā)送數(shù)據(jù),以回車結(jié)尾\r\n"); //500ms時(shí)間到,發(fā)送提示信息
}
if(RS485_1_RX_STA&0x8000) //RS485_1_RX_STA位15置位,表示接收完成
{
len=RS485_1_RX_STA&0x3fff; //得到此次接收到的數(shù)據(jù)長(zhǎng)度
RS485_1_RX_BUF[len]='\0' ; //加入結(jié)束符
RS485_1_printf("你發(fā)送的數(shù)據(jù):%s\r\n",RS485_1_RX_BUF); //返回接收到的數(shù)據(jù)
RS485_1_RX_STA=0; //清除狀態(tài)位
}
time++; //計(jì)數(shù)+1
delay_ms(10); //延時(shí)10ms
}
}
復(fù)制代碼
/*-------------------------------------------------*/
/* RS485_1_1源文件,有2個(gè)函數(shù) */
/*-------------------------------------------------*/
/* void RS485_1_1_Init(u32 bound) 485初始化串口5*/
/* void RS485_1_1_printf(char* fmt,...)485printf函數(shù) */
/*-------------------------------------------------*/
#include "uart5.h"
#include "delay.h"
u8 RS485_1_RX_BUF[RS485_1_RXBUFF_SIZE]; //接收緩存區(qū)
u16 RS485_1_RX_STA=0; //接收狀態(tài)標(biāo)記
/*-------------------------------------------------*/
/*函數(shù)名:初始化串口5-RS485_1 */
/*參 數(shù):bound:波特率 */
/*返回值:無(wú) */
/*-------------------------------------------------*/
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時(shí)鐘
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE); //使能GPIOC時(shí)鐘
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD,ENABLE); //使能GPIOD時(shí)鐘
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; //PC12
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //復(fù)用推挽輸出
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; //復(fù)用推挽輸出
GPIO_Init(GPIOD, &GPIO_InitStructure); //初始化PD4
USART_InitStructure.USART_BaudRate = bound; //波特率設(shè)置
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //字長(zhǎng)為8位數(shù)據(jù)格式
USART_InitStructure.USART_StopBits = USART_StopBits_1; //一個(gè)停止位
USART_InitStructure.USART_Parity = USART_Parity_No; //無(wú)奇偶校驗(yàn)位
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//無(wú)硬件數(shù)據(jù)流控制
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收發(fā)模式
USART_Init(UART5, &USART_InitStructure); //按配置設(shè)置串口4
USART_Cmd(UART5, ENABLE); //使能串口5
USART_ClearFlag(UART5, USART_FLAG_TC); //清除標(biāo)志位
USART_ITConfig(UART5, USART_IT_RXNE, ENABLE); //開啟接受中斷
NVIC_InitStructure.NVIC_IRQChannel = UART5_IRQn; //串口5中斷
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2; //搶占優(yōu)先級(jí)3
NVIC_InitStructure.NVIC_IRQChannelSubPriority =2; //子優(yōu)先級(jí)3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //中斷通道使能
NVIC_Init(&NVIC_InitStructure); //根據(jù)配置設(shè)置
RS485_1_RX_TX=0; //接收模式
}
/*-------------------------------------------------*/
/*函數(shù)名:第一路485的printf */
/*參 數(shù):無(wú) */
/*返回值:無(wú) */
/*-------------------------------------------------*/
__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; //發(fā)送模式
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);
}
復(fù)制代碼
所有資料51hei提供下載:
7-第1路 RS485_1 通信測(cè)試程序.rar
(272.4 KB, 下載次數(shù): 56)
2018-11-28 17:37 上傳
點(diǎn)擊文件名下載附件
已成功通過測(cè)試
下載積分: 黑幣 -5
作者:
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