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