微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > STM32 RS485 收发

STM32 RS485 收发

时间:11-25 来源:互联网 点击:


RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO,ENABLE); //使能GPIO与复用功能时钟


GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9; //USART1 TX使用引脚
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP; //复用推免式输出
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; //50MHZ输出速度

GPIO_Init(GPIOA,&GPIO_InitStructure); //发送端口初始化


GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10; //USART1 RX使用引脚
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING; //浮空输入
//GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; //输入一般不需要配置速度

GPIO_Init(GPIOA,&GPIO_InitStructure); //接收端初始化


USART_Init(USART1,&USART_InitStructure);


USART_Cmd(USART1,ENABLE);


printf("看到此信息就说明串口1初始化完成啦");
}

2.user_rs485.c

//程序功能:RS485驱动 RS485使用的是串口2

#include"stm32f10x.h"
#include"user_rs485.h"
#include"user_led.h"
#include



void User_RS485Config(void)
{

USART_InitTypeDef USART_InitStructure;

GPIO_InitTypeDef GPIO_InitStructure;


RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);//使能USART2时钟

USART_InitStructure.USART_BaudRate =9600;//波特率
USART_InitStructure.USART_WordLength =USART_WordLength_8b;//8位字长
USART_InitStructure.USART_StopBits =USART_StopBits_1;//1停止位
USART_InitStructure.USART_Parity =USART_Parity_No;//无奇偶效验位
USART_InitStructure.USART_Mode =USART_Mode_Rx|USART_Mode_Tx;//异步半双工模式
USART_InitStructure.USART_HardwareFlowControl =USART_HardwareFlowControl_None;//无硬件流控


RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO,ENABLE);//使能GPIO和复用功能时钟


GPIO_InitStructure.GPIO_Pin =GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode =GPIO_Mode_AF_PP; //复用推免式输出
GPIO_InitStructure.GPIO_Speed =GPIO_Speed_50MHz;

GPIO_Init(GPIOA,&GPIO_InitStructure);


GPIO_InitStructure.GPIO_Pin =GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode =GPIO_Mode_IN_FLOATING; //浮空输入

GPIO_Init(GPIOA,&GPIO_InitStructure);


USART_Init(USART2,&USART_InitStructure);


USART_Cmd(USART2,ENABLE);
}

void User_RS485CTRPortConfig(void)
{

GPIO_InitTypeDef GPIO_InitStructure;


RCC_APB2PeriphClockCmd(RS485ModeCTRRCC,ENABLE);


GPIO_InitStructure.GPIO_Pin =RS485ModeCTRPin;
GPIO_InitStructure.GPIO_Mode =GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed =GPIO_Speed_50MHz;


GPIO_Init(RS485ModeCTRPort,&GPIO_InitStructure);
}

u8 User_RS485ModeSet(u8 RS485SetMode)
{
u8 RS485Mode;

switch(RS485SetMode)
{
case 1: //发送模式
RS485Mode=RS485Mode_Tx;
GPIO_SetBits(RS485ModeCTRPort,RS485ModeCTRPin); //控制端口设置为RS485输出模式
printf("RS485被配置为发送模式");
User_LedSpark(Led1,1);
break;
case 2: //接收模式
RS485Mode=RS485Mode_Rx;
GPIO_ResetBits(RS485ModeCTRPort,RS485ModeCTRPin); //控制端口设置为RS485输入模式
printf("RS485被配置为接收模式");
User_LedSpark(Led2,1);
break;
case 3: //空闲模式
RS485Mode=RS485Mode_IDL;
printf("RS485被配置为空闲模式");
User_LedSpark(Led3,1);
break;
default:
RS485Mode=RS485Mode_IDL;
printf("RS485被配置为空闲模式");
break;
}
return RS485Mode;
}

void User_RS485NVICConfig(void)
{

NVIC_InitTypeDef NVIC_InitStructure;


NVIC_SetVectorTable(NVIC_VectTab_FLASH,0x0000);


NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);


NVIC_InitStructure.NVIC_IRQChannel =USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority =1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority =0;
NVIC_InitStructure.NVIC_IRQChannelCmd =ENABLE;


NVIC_Init(&NVIC_InitStructure);
}

user_rs485.h

//串口1配置初始化头文件

#ifndef _USER_RS485_H
#define _USER_RS485_H

#define RS485ModeCTRRCC RCC_APB2Periph_GPIOD
#define RS485ModeCTRPort GPIOD
#define RS485ModeCTRPin GPIO_Pin_3

#define RS485Mode_Tx 1
#define RS485Mode_Rx 2
#define RS485Mode_IDL 0

void User_RS485Config(void); //RS485初始化
void User_RS485CTRPortConfig(void); //RS485模式选择端口初始化
u8 User_RS485ModeSet(u8 RS485SetMode); //RS485模式设置
void User_RS485NVICConfig(void); //RS485中断嵌套中断向量配置

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top