void User_RS485Config(void)
{
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;本文引用地址:http://m.butianyuan.cn/article/201611/321304.htm
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);//使能USART2時(shí)鐘
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;//無奇偶效驗(yàn)位
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和復(fù)用功能時(shí)鐘
GPIO_InitStructure.GPIO_Pin =GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode =GPIO_Mode_AF_PP; //復(fù)用推免式輸出
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: //發(fā)送模式
RS485Mode=RS485Mode_Tx;
GPIO_SetBits(RS485ModeCTRPort,RS485ModeCTRPin); //控制端口設(shè)置為RS485輸出模式
printf("RS485被配置為發(fā)送模式");
User_LedSpark(Led1,1);
break;
case 2: //接收模式
RS485Mode=RS485Mode_Rx;
GPIO_ResetBits(RS485ModeCTRPort,RS485ModeCTRPin); //控制端口設(shè)置為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模式設(shè)置
void User_RS485NVICConfig(void); //RS485中斷嵌套中斷向量配置
#endif
4.user_key.c
//程序功能:板載4Key驅(qū)動(dòng)
#include"stm32f10x.h"
#include"user_key.h"
#include"user_led.h"
#include"user_beep.h"
void User_KeyConfig(void)
{
GPIO_InitTypeDef GPIO_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 Key1RCC RCC_APB2Periph_GPIOA
#define Key1Port GPIOA
#define Key1Pin GPIO_Pin_0
#define Key2RCC RCC_APB2Periph_GPIOC
#define Key2Port GPIOC
#define Key2Pin GPIO_Pin_13
#define Key3RCC RCC_APB2Periph_GPIOA
#define Key3Port GPIOA
#define Key3Pin GPIO_Pin_1
#define Key4RCC RCC_APB2Periph_GPIOC
#define Key4Port GPIOC
#define Key4Pin GPIO_Pin_3
#define KeySend 1
#define KeyReceive 2
#define KeyIDL 3
#define KeyNone 4
#define KeyNo 0
void User_KeyConfig(void);//使用GPIO初始化
u8 User_KeyRead(void);//讀取按鍵編號(hào)當(dāng)有按鍵被按下的時(shí)候
#endif
5.beep led與前面文章《STM32 基于庫函數(shù)控制按鍵&nb…》同。
以上,結(jié)束。
評(píng)論