新闻中心

STM32 RS485 收发

作者: 时间:2016-11-25 来源:网络 收藏



void User_RS485Config(void)
{

USART_InitTypeDefUSART_InitStructure;

GPIO_InitTypeDefGPIO_InitStructure;

本文引用地址://m.amcfsurvey.com/article/201611/321304.htm


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_InitTypeDefGPIO_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_InitTypeDefNVIC_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 RS485ModeCTRRCCRCC_APB2Periph_GPIOD
#define RS485ModeCTRPortGPIOD
#define RS485ModeCTRPinGPIO_Pin_3


#define RS485Mode_Tx1
#define RS485Mode_Rx2
#define RS485Mode_IDL0


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


#endif

4.user_key.c

//程序功能:板载4Key驱动


#include"stm32f10x.h"
#include"user_key.h"
#include"user_led.h"
#include"user_beep.h"



void User_KeyConfig(void)
{
GPIO_InitTypeDefGPIO_InitStructure;


RCC_APB2PeriphClockCmd(Key1RCC,ENABLE);

GPIO_InitStructure.GPIO_Pin=Key1Pin;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;

GPIO_Init(Key1Port,&GPIO_InitStructure);


RCC_APB2PeriphClockCmd(Key2RCC,ENABLE);

GPIO_InitStructure.GPIO_Pin=Key2Pin;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;

GPIO_Init(Key2Port,&GPIO_InitStructure);


RCC_APB2PeriphClockCmd(Key3RCC,ENABLE);

GPIO_InitStructure.GPIO_Pin=Key3Pin;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;

GPIO_Init(Key3Port,&GPIO_InitStructure);


RCC_APB2PeriphClockCmd(Key4RCC,ENABLE);

GPIO_InitStructure.GPIO_Pin=Key4Pin;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;

GPIO_Init(Key4Port,&GPIO_InitStructure);
}

u8 User_KeyRead(void)
{

if(!GPIO_ReadInputDataBit(Key1Port,Key1Pin))
{
User_BeepStatus(BeepStatus_TurnOn);
User_LedSpark(Led1,1);
return KeySend;
}

if(!GPIO_ReadInputDataBit(Key2Port,Key2Pin))
{
User_BeepStatus(BeepStatus_TurnOn);
User_LedSpark(Led2,1);
return KeyReceive;
}

if(!GPIO_ReadInputDataBit(Key3Port,Key3Pin))
{
User_BeepStatus(BeepStatus_TurnOn);
User_LedSpark(Led3,1);
return KeyIDL;
}

if(!GPIO_ReadInputDataBit(Key4Port,Key4Pin))
{
User_BeepStatus(BeepStatus_TurnOn);
User_LedSpark(Led4,1);
return KeyNone;
}

else
{
return KeyNo;
}
}

user_key.h

//按键处理头文件
#ifndef USER_KEY_H
#define USER_KEY_H



#define Key1RCCRCC_APB2Periph_GPIOA
#define Key1PortGPIOA
#define Key1PinGPIO_Pin_0

#define Key2RCCRCC_APB2Periph_GPIOC
#define Key2PortGPIOC
#define Key2PinGPIO_Pin_13

#define Key3RCCRCC_APB2Periph_GPIOA
#define Key3PortGPIOA
#define Key3PinGPIO_Pin_1

#define Key4RCCRCC_APB2Periph_GPIOC
#define Key4PortGPIOC
#define Key4PinGPIO_Pin_3

#define KeySend1
#define KeyReceive2
#define KeyIDL3
#define KeyNone4
#define KeyNo0


void User_KeyConfig(void);//使用GPIO初始化
u8 User_KeyRead(void);//读取按键编号当有按键被按下的时候


#endif

5.beep led与前面文章《STM32 基于库函数控制按键&nb…》同。

以上,结束。


上一页 1 2 下一页

关键词:STM32RS48

评论


技术专区

关闭