485通讯-串口6

基于STM32的485串口通讯
摘要由CSDN通过智能技术生成

STM32F407发送-STM32F103接收

F4硬件连接

  • F4引脚PC6/PC7用杜邦线连接单片机485通道

F4代码

usart6.c

#include "sys.h"
#include "usart6.h"
#include "delay.h"
#define USART6_RXBUF_LEN 15

#if EN_USART6_RX   		//如果使能了接收   	  
//接收缓存区 	
u8 USART6_RX_BUF[64];  	//接收缓冲,最大64个字节.
//接收到的数据长度
u8 USART6_RX_CNT=0;   
void USART6_IRQHandler(void)
{
   
	u8 res;	    
	if(USART_GetITStatus(USART6, USART_IT_RXNE) != RESET)//接收到数据
	{
   	 	
	  res =USART_ReceiveData(USART6);//;读取接收到的数据USART2->DR
		if(USART6_RX_CNT<64)
		{
   
			USART6_RX_BUF[USART6_RX_CNT]=res;		//记录接收到的值
			USART6_RX_CNT++;						//接收数据增加1 
		} 
	}  											 
} 
#endif		

//初始化串口3,使用9600波特率,和485设备进行通信 
//bound:波特率
void uart6_init(u32 bound)
{
   
	//初始化结构体
    GPIO_InitTypeDef GPIO_InitStructure;
    USART_InitTypeDef USART_InitStructure;
	 NVIC_InitTypeDef NVIC_InitStructure;
	
	//外部时钟初始化
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC,ENABLE); //使能GPIOC时钟
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART6,ENABLE);//使能USART6时钟

    //GPIO初始化,USART3使用的是PB10,PB11引脚
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; //PC6,PC7引脚
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//复用功能
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;	//速度50MHz
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽复用输出
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉
    GPIO_Init(GPIOC,&GPIO_InitStructure); //初始化PC6,PC7

	//设置引脚复用
    GPIO_PinAFConfig(GPIOC,GPIO_PinSource6,GPIO_AF_USART6); //PC6复用为USART6
    GPIO_PinAFConfig(GPIOC,GPIO_PinSource7,GPIO_AF_USART6); //PC7复用为USART6
	
	//串口初始化
    USART_InitStructure.USART_BaudRate = bound;//一般设置为9600;
    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_I
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值