#include "N32G435CBL7.h"\n\n// 封装函数,用于读取按键状态\n// 返回值为按键状态,1表示按键按下,0表示按键释放\nint ReadButton(void)\n{\n\t// 读取PA0的引脚状态\n\tif(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0) == Bit_RESET)\n\t{\n\t return 1; // 按键按下\n\t}\n\telse\n\t{\n\t return 0; // 按键释放\n\t}\n}\n\nint main(void)\n{\n\t// 初始化GPIOA的引脚\n\tGPIO_InitTypeDef GPIO_InitStructure;\n\tRCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);\n\tGPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;\n\tGPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;\n\tGPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;\n\tGPIO_Init(GPIOA, &GPIO_InitStructure);\n\t\n\twhile(1)\n\t{\n\t // 读取按键状态\n\t int buttonState = ReadButton();\n\t \n\t if(buttonState == 1)\n\t {\n\t // 按键按下\n\t // 进行相应的操作\n\t }\n\t else\n\t {\n\t // 按键释放\n\t // 进行相应的操作\n\t }\n\t}\n}


原文地址: https://www.cveoy.top/t/topic/p4Iz 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录