diff --git a/BSP/Key.c b/BSP/Key.c new file mode 100644 index 0000000..955b898 --- /dev/null +++ b/BSP/Key.c @@ -0,0 +1,29 @@ +#include "stm32f10x.h" +#include "Delay.h" + +void Key_Init(void) { + RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); + + GPIO_InitTypeDef GPIO_InitStructure; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; + GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; + GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_11; + + GPIO_Init(GPIOB, &GPIO_InitStructure); +} + +uint8_t Key_GetNum(void) { + uint8_t KeyNum = 0; + if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1) == 0) { + Delay_ms(10); + while (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1) == 0); + KeyNum = 1; + } + if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_11) == 0) { + Delay_ms(10); + while (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_11) == 0); + KeyNum = 2; + } + + return KeyNum; +} diff --git a/BSP/Key.h b/BSP/Key.h new file mode 100644 index 0000000..564d7c9 --- /dev/null +++ b/BSP/Key.h @@ -0,0 +1,8 @@ +#ifndef __KEY_H +#define __KEY_H + +void Key_Init(void); + +uint8_t Key_GetNum(void); + +#endif \ No newline at end of file diff --git a/BSP/LED.c b/BSP/LED.c new file mode 100644 index 0000000..946783f --- /dev/null +++ b/BSP/LED.c @@ -0,0 +1,44 @@ +#include "stm32f10x.h" + +void LED_Init(void) { + RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); + GPIO_InitTypeDef GPIO_InitStructure; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; + GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; + GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1; + GPIO_Init(GPIOA, &GPIO_InitStructure); + + GPIO_SetBits(GPIOA, GPIO_Pin_0 | GPIO_Pin_1); +} + +void LED0_ON(void) { + GPIO_ResetBits(GPIOA, GPIO_Pin_0); +} + +void LED0_OFF(void) { + GPIO_SetBits(GPIOA, GPIO_Pin_0); +} + +void LED0_TOGGLE(void) { + if (GPIO_ReadOutputDataBit(GPIOA, GPIO_Pin_0) == 0) { + GPIO_SetBits(GPIOA, GPIO_Pin_0); + }else { + GPIO_ResetBits(GPIOA, GPIO_Pin_0); + } +} + +void LED1_ON(void) { + GPIO_ResetBits(GPIOA, GPIO_Pin_1); +} + +void LED1_OFF(void) { + GPIO_SetBits(GPIOA, GPIO_Pin_1); +} + +void LED1_TOGGLE(void) { + if (GPIO_ReadOutputDataBit(GPIOA, GPIO_Pin_1) == 0) { + GPIO_SetBits(GPIOA, GPIO_Pin_1); + }else { + GPIO_ResetBits(GPIOA, GPIO_Pin_1); + } +} \ No newline at end of file diff --git a/BSP/LED.h b/BSP/LED.h new file mode 100644 index 0000000..f0fd0dd --- /dev/null +++ b/BSP/LED.h @@ -0,0 +1,11 @@ +#ifndef __LED_H +#define __LED_H + +void LED_Init(void); +void LED0_ON(void); +void LED0_OFF(void); +void LED0_TOGGLE(void); +void LED1_ON(void); +void LED1_OFF(void); +void LED1_TOGGLE(void); +#endif diff --git a/CMakeLists.txt b/CMakeLists.txt index 5cd50a8..91600c1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -30,6 +30,8 @@ add_executable(${CMAKE_PROJECT_NAME} ${SPL_SRC} User/main.c BSP/Delay.c + BSP/LED.c + BSP/Key.c ) target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE diff --git a/User/main.c b/User/main.c index 7afd2eb..2a74e6b 100644 --- a/User/main.c +++ b/User/main.c @@ -1,34 +1,22 @@ #include "stm32f10x.h" #include "Delay.h" +#include "LED.h" +#include "Key.h" + +uint8_t KeyNum; int main(void){ - RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); - GPIO_InitTypeDef GPIO_InitStruct; - GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP; - GPIO_InitStruct.GPIO_Pin = GPIO_Pin_All; - GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; - GPIO_Init(GPIOA, &GPIO_InitStruct); - - // GPIO_SetBits(GPIOA, GPIO_Pin_0); // 关闭led灯 - // GPIO_ResetBits(GPIOA, GPIO_Pin_0); // 打开led灯 - // GPIO_WriteBit(GPIOA, GPIO_Pin_7, Bit_RESET); // 打开led灯 - // GPIO_WriteBit(GPIOA, GPIO_Pin_0, Bit_SET); // 关闭led灯 + LED_Init(); + Key_Init(); while(1){ - GPIO_Write(GPIOA, ~0x00000001); // 0000 0000 0000 0001 - Delay_ms(500); - GPIO_Write(GPIOA, ~0x00000002); // 0000 0000 0000 0010 - Delay_ms(500); - GPIO_Write(GPIOA, ~0x00000004); // 0000 0000 0000 0100 - Delay_ms(500); - GPIO_Write(GPIOA, ~0x00000008); // 0000 0000 0000 1000 - Delay_ms(500); - GPIO_Write(GPIOA, ~0x00000010); // 0000 0000 0001 0000 - Delay_ms(500); - GPIO_Write(GPIOA, ~0x00000020); // 0000 0000 0010 0000 - Delay_ms(500); - GPIO_Write(GPIOA, ~0x00000040); // 0000 0000 0100 0000 - Delay_ms(500); - GPIO_Write(GPIOA, ~0x00000080); // 0000 0000 1000 0000 - Delay_ms(500); + + KeyNum = Key_GetNum(); + if (KeyNum == 1) { + LED0_TOGGLE(); + } + if (KeyNum == 2) { + LED1_TOGGLE(); + } + } }