新手上路
- 积分
- 49
- 金钱
- 49
- 注册时间
- 2019-9-10
- 在线时间
- 12 小时
|
2金钱
初学小白,在做按键试验时下载后按键无作用,在线求解!!!
用的是STM32f103系列
主函数:
#include "stm32f10x.h"
#include "sys.h"
#include "led.h"
#include "delay.h"
#include "been.h"
#include "key.h"
#define led0 PBout(5)
#define led1 PEout(5)
#define been PBout(8)
int main()
{
void key_init();
void been_init();
void led_init();
void delay_init();
int key = 0;
while(1)
{
key=key_scan();
if(key==0|key==1|key==2|key==3)
led0=0;led1=0;
switch (key)
{
case 0:
led0=!led0; led1=!led1; break;
case 1:
led0=!led0;break;
case 2:
led1=!led1;break;
case 3:
been=!been;break;
default:
delay_ms(20); break;
}
}
}
按键驱动函数:
#include "key.h"
#include "stm32f10x.h"
#include "delay.h"
#include "sys.h"
#define key0 PAin(0)
#define key1 PEin(2)
#define key2 PEin(3)
#define key3 PEin(4)
void key_init()
{
GPIO_InitTypeDef GPIO_Init_key;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE,ENABLE);
GPIO_Init_key.GPIO_Mode=GPIO_Mode_IPD;
GPIO_Init_key.GPIO_Pin=GPIO_Pin_0;
GPIO_Init_key.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_Init_key);
GPIO_Init_key.GPIO_Mode=GPIO_Mode_IPU;
GPIO_Init_key.GPIO_Pin=GPIO_Pin_2;
GPIO_Init_key.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOE,&GPIO_Init_key);
GPIO_Init_key.GPIO_Pin=GPIO_Pin_3;
GPIO_Init(GPIOE,&GPIO_Init_key);
GPIO_Init_key.GPIO_Pin=GPIO_Pin_4;
GPIO_Init(GPIOE,&GPIO_Init_key);
}
int key_scan(void)
{
static int key_p=1;
if(key0==1&&key_p==1)
{
delay_ms(20);
key_p=1;
if(key0==1)
{
return 0;
key_p=0;
}
}
else if(key1==0&&key_p==1)
{
delay_ms(20);
key_p=1;
if(key1==0)
{
return 1;
key_p=0;
}
}
else if(key2==0&&key_p==1)
{
delay_ms(20);
key_p=1;
if(key2==0)
{
return 2;
key_p=0;
}
}
else if(key3==0&&key_p==1)
{
delay_ms(20);
key_p=1;
if(key1==0)
{
return 3;
key_p=0;
}
}
return 4;
}
按键驱动头文件:
#ifndef __KEY_H__ //__KEY_H
#define __KEY_H__
#include "sys.h"
#define key0 PAin(0)
#define key1 PEin(2)
#define key2 PEin(3)
#define key3 PEin(4)
void key_init(void);
int key_scan(void);
#endif
前面led驱动和蜂鸣器驱动应该没有问题,求解!!!
其中使用了正点原子sts.h函数的PAout()与PAin()函数!
|
最佳答案
查看完整内容[请看2#楼]
通过硬件仿真...直接在按键判断里边打个断点 全速运行 之后按下你判断的那个按键 然后看能不能运行到断点处
|