当前位置:首页 > 单片机 > 单片机
[导读]STM32的IO口能够由软件配置成8种模式,如图所示。好出在于在硬件设计的时候,可以方便I/0的选择,从而走线上带来方便。模拟输入输入模式浮空输入输入下拉输入上拉输出模式开漏输出推挽输出复用开漏输出复用推挽输出我

STM32的IO口能够由软件配置成8种模式,如图所示。好出在于在硬件设计的时候,可以方便I/0的选择,从而走线上带来方便。

模拟输入

输入模式浮空输入

输入下拉

输入上拉

输出模式开漏输出

推挽输出

复用开漏输出

复用推挽输出

我们今天使用的按键端口,配置为上拉输出,这样的话,按键未按下时值为1,当按键按下时值为0。LED使用的I/O配置为推挽输出模式,用于指示按键的状态。我们专门把按键模块写成子文件,方便下次功能的移植。程序只是简单的叙述了数字输入的检测功能,实际应用中按键还应加延时消抖处理。程序采用轮询查询的方法,实际上使用中断的方法,更加有效。

KEY.C代码:

#include "key.h"

void KEY_Configuration(void)

{

GPIO_InitTypeDefGPIO_InitStructure;

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC, ENABLE);

GPIO_InitStructure.GPIO_Pin =KEY1 | KEY3;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;

GPIO_Init(GPIOC, &GPIO_InitStructure);

//GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//可以省略,下面都是按照上面相同的配置

//GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;

GPIO_InitStructure.GPIO_Pin =KEY2;

GPIO_Init(GPIOB, &GPIO_InitStructure);

//GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

//GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;

GPIO_InitStructure.GPIO_Pin =KEY4;

GPIO_Init(GPIOA, &GPIO_InitStructure);

}

KEY.H代码

#ifndef __KEY_H

#define __KEY_H

#include "stm32f10x_conf.h"

#define KEY1 GPIO_Pin_4

#define KEY2 GPIO_Pin_10

#define KEY3 GPIO_Pin_13

#define KEY4 GPIO_Pin_0

#define Read_key1() ((GPIOC->IDR & KEY1)?0:1)//按下时才为0

#define Read_key2() ((GPIOB->IDR & KEY2)?0:1)

#define Read_key3() ((GPIOC->IDR & KEY3)?0:1)

#define Read_key4() ((GPIOA->IDR & KEY4)?0:1)

//#define Read_key1() !GPIO_ReadInputDataBit(GPIOC, KEY1)

//#define Read_key2() !GPIO_ReadInputDataBit(GPIOB, KEY2)

//#define Read_key3() !GPIO_ReadInputDataBit(GPIOC, KEY3)

//#define Read_key4() !GPIO_ReadInputDataBit(GPIOA, KEY4)

void KEY_Configuration(void);

#endif


本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除( 邮箱:macysun@21ic.com )。
换一批
延伸阅读
关闭