本帖最后由 tlled 于 2020-4-22 21:59 编辑
这节来学习下GPIO输入,按键输入LED输出。
一、硬件电路
K1-K4按键使用的是PB1,PB2,PB10,PB11 四个IO口。
二、程序部分
2.1、main.c
- #include "config.h"
- int main(void)
- {
- delay_init();
- LED_Init();
- KEY_Init();
- while(1) //ÎÞÏÞÑ»·
- {
- key_test();
- }
2.2、key.c- #include "config.h"
- void KEY_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
-
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA|RCC_AHBPeriph_GPIOB|RCC_AHBPeriph_GPIOC,ENABLE);//ʹÄÜGPIOA,GPIOB,GPIOCʱÖÓ
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_10|GPIO_Pin_11;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- }
- void key_test(void)
- {
-
- if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1)==0) //K1 UP
- {
- LED4_ON();
- }
- else
- {
- LED4_OFF();
- }
- if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_2)!=0) //K2 UP
- {
- LED3_ON();
- }
- else
- {
- LED3_OFF();
- }
- if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_10)!=0) //K3 UP
- {
- LED2_ON();
- }
- else
- {
- LED2_OFF();
- }
- if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_11)!=0) //K4 UP
- {
- LED1_ON();
- }
- else
- {
- LED1_OFF();
- }
-
- }
2.3、程序源码
mm32_prj-20200422.rar
(1.65 MB, 下载次数: 0)
三、执行结果
按下K1-K4,对应的LED指示灯亮,松开灭。
|