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

STM32 RS485 收发

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

#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_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);//读取按键编号当有按键被按下的时候

#endif

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

以上,结束。

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

网站地图

Top