#ifndef __KEY4X4_H
#define __KEY4X4_H
#include "sys.h"
////#include "stm32f10x_lib.h"
#define S0 GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_4)//
#define S1 GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_5)//
#define S2 GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_6)//
#define S3 GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_7)//
#define S0_D0 5
#define S1_D1 6
#define S2_D2 7
#define S3_D3 8
void KEY4x4_Init(void);
u8 Key_scan(u8);
#endif
#include "key4x4.h"
#include "sys.h"
#include "usart.h"
#include "delay.h"
void KEY4x4_Init(void) //IO³õʼ»¯
{
//
//ÍÆÍìÊä³ö
GPIO_InitTypeDef GPIO_InitStructure;
//
//
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//ʹÄÜPORTEʱÖÓ
// GPIO_InitStructure.GPIO_Pin = Keyboard_Line ;
// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //ÉèÖóÉÍÆÍìÊä³ö
// GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
// GPIO_Init(Keyboard_Control_Port, &GPIO_InitStructure);//³õʼ»¯GPIOE7,8,9,10
// GPIO_SetBits(Keyboard_Control_Port,Keyboard_Line);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//ʹÄÜPORTA,PORTCʱÖÓ
GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);//¹Ø±Õjtag£¬Ê¹ÄÜSWD£¬¿ÉÒÔÓÃSWDģʽµ÷ÊÔ
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;//PA15
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //ÉèÖóÉÉÏÀÊäÈë
GPIO_Init(GPIOA, &GPIO_InitStructure);//³õʼ»¯GPIOA15
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;//PA15
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //ÉèÖóÉÉÏÀÊäÈë
GPIO_Init(GPIOA, &GPIO_InitStructure);//³õʼ»¯GPIOA15
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;//PA15
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //ÉèÖóÉÉÏÀÊäÈë
GPIO_Init(GPIOA, &GPIO_InitStructure);//³õʼ»¯GPIOA15
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;//PA15
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //ÉèÖóÉÉÏÀÊäÈë
GPIO_Init(GPIOA, &GPIO_InitStructure);//³õʼ»¯GPIOA15
// //ÏÂÀÊäÈë
// GPIO_InitStructure.GPIO_Pin = Keyboard_Row;
// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //PA0ÉèÖóÉÊäÈ룬ĬÈÏÏÂÀ
// GPIO_Init(Keyboard_Control_Port, &GPIO_InitStructure);//³õʼ»¯GPIOE
//
//// GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3;
//// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //PA0ÉèÖóÉÊäÈ룬ĬÈÏÏÂÀ
//// GPIO_Init(GPIOC, &GPIO_InitStructure);//³õʼ»¯GPIOE
}
u8 Key_scan(u8 mode)
{
static u8 key_up=1;//°´¼ü°´ËÉ¿ª±êÖ¾
if(mode)key_up=1; //Ö§³ÖÁ¬°´
if(key_up&&(S0==0||S1==0||S2==0||S3==0))
{
delay_ms(15);
key_up=0;
if(S0==0) return S0_D0;
else if(S1==0) return S1_D1;
else if(S2==0) return S2_D2;
else if(S3==0) return S3_D3;
}else if(S0==1&&S1==1&&S2==1&&S3==1)key_up=1;
return 0;
}
这是.c和.h文件
while(1)
{
DD=Key_scan(0);
if(DD)
{
switch(DD)
{
case S0_D0:
PDout(2)=!PDout(2);break;
case S1_D1:
PDout(2)=!PDout(2);break;
case S2_D2:
PDout(2)=!PDout(2);break;
case S3_D3:
PDout(2)=!PDout(2);break;
}
}else delay_ms(10);
}
这是主函数,我接的是开发板的外部按键,不知道哪里出了问题,总是不管用。
|