初级会员

- 积分
- 60
- 金钱
- 60
- 注册时间
- 2020-3-7
- 在线时间
- 12 小时
|
4金钱
F103按键检测输入,程序烧写后按键没按下前后灯都是亮的,只是按键按下后灯变暗了些,但还是亮的,怎默回事?求大神解答。以下是代码:
bsp_keyscan.h:
#ifndef BSP_KEYSCAN_H
#define BSP_KEYSCAN_H
#include "stm32f10x.h"
#define Key_on 1
#define Key_off 0
void GPIO_Config(void);
void GPIO_Keyscan(void);
uint8_t Key_Scan(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin);
#endif
bsp_keyscan.c:
#include "stm32f10x.h"
#include "bsp_keyscan.h"
void GPIO_Config()
{
GPIO_InitTypeDef GPIO_InitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_0;
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStruct);
}
void GPIO_Keyscan()
{
GPIO_InitTypeDef GPIO_Keyscan_Struct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_Keyscan_Struct.GPIO_Pin=GPIO_Pin_0;
GPIO_Keyscan_Struct.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_Keyscan_Struct);
}
uint8_t Key_Scan(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
{
if(GPIO_ReadInputDataBit(GPIOx, GPIO_Pin)==Key_on )
{
while (GPIO_ReadInputDataBit( GPIOx, GPIO_Pin)==Key_on)
return Key_on;
}
else return Key_off;
}
main.c:
#include "stm32f10x.h"
#include "bsp_keyscan.h"
int main(void){
GPIO_Config();
GPIO_Keyscan();
while(1)
{
if(Key_Scan(GPIOA,GPIO_Pin_0)==Key_on)
{
GPIOB->ODR ^= GPIO_Pin_0;
}
}
}
|
最佳答案
查看完整内容[请看2#楼]
你可以在原来的代码上while (GPIO_ReadInputDataBit( GPIOx, GPIO_Pin)==Key_on)后边加一个“;”,效果也会很明显
|