#include "button.h" // 初始化按键的 IO 针脚 void button_init(void) { // 定义一个 GPIO 初始化啊的结构体 GPIO_InitTypeDef GPIO_InitStruct; // 初始化时钟 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOA, ENABLE); // 将 PC8 和 PC9 设置成上拉输入 1000 // up 上拉 GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPU; // 针脚 GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_8; // 输出速度 // GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOC, &GPIO_InitStruct); // 将 PA0 设置成下拉输入 GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPD; // 针脚 GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0; // 输出速度 GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStruct); } // 获取某一个按键 nu 的状态 // 返回 1 按下, 返回 0 抬起 int button_status(int nu) { int status = 0; switch (nu) { case 0: status = !GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_9); break; case 1: status = !GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_8); break; case 2: status = GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0); break; default: break; } return status; }