STM32 按键输入
#include "stm32f10x.h"
#include "key.h"
//按键初始化函数
void KEY_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
#ifndef __KEY_H #define __KEY_H #define KEY0 GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_12)//读取按键0 #define KEY1 GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_13)//读取按键1 #define KEY2 GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_14)//读取按键2 #define KEY3 GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_15)//读取按键3 #define KEY4 GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_4)//读取按键0 #define KEY5 GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_5)//读取按键1 #define KEY6 GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_6)//读取按键2 #define KEY7 GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_7)//读取按键3 #define KEY8 GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_8)//读取按键3 void KEY_Init(void);//IO初始化 #endif
#include "stm32f10x.h" #include <stdio.h> #include "sys.h" #include "delay.h" #include "oled.h" #include "24l01.h" #include "key.h" /* OLED SCL-D0--PC15 SDA-D1--PC14 RST---PC13 DC---PB4*/ /* 2401--SPI1 MISO-PA6 SCK-PA5 CE-PB3 IRQ-PA3 MOSI-PA7 CSN-PA2 */ // c8t6
u8 tmp_buf[1] ; int main(void) { Stm32_Clock_Init(9);//系统时钟设置为外部晶振,9倍频 delay_init(72);//系统SysTick初始化 JTAG_Set(JTAG_SWD_DISABLE); //=====关闭JTAG接口 JTAG_Set(SWD_ENABLE); //=====打开SWD接口 可以利用主板的SWD接口调试 OLED_Init(); KEY_Init(); //NRF24L01_Init(); //=====NRF24L01无线模块初始化 // NRF24L01_FindMyself(); //=====NRF24L01无线模块检查 检测不到就停在这边自检闪灯 //tmp_buf[0]= 1 ; while (1) { // TX_Mode(); // OLED_ShowNumber(0,30,tmp_buf[0],5,12); // NRF24L01_TxPacket(tmp_buf); if(KEY0 == 0) { delay_ms(10); OLED_ShowString(0,20,"Key0"); } if(KEY1 == 0) { delay_ms(10); OLED_ShowString(0,20,"Key1"); } if(KEY2 == 0) { delay_ms(10); OLED_ShowString(0,20,"Key2"); } if(KEY3 == 0) { delay_ms(10); OLED_ShowString(0,20,"Key3"); } if(KEY4 == 0) { delay_ms(10); OLED_ShowString(0,20,"Key4"); } if(KEY5 == 0) { delay_ms(10); OLED_ShowString(0,20,"Key5"); } if(KEY6 == 0) { delay_ms(10); OLED_ShowString(0,20,"Key6"); } if(KEY7 == 0) { delay_ms(10); OLED_ShowString(0,20,"Key7"); } if(KEY8 == 0) { delay_ms(10); OLED_ShowString(0,20,"Key8"); } OLED_Refresh_Gram(); } }