#include "stm32f10x.h"
#include "sys.h"
#include "delay.h"
void keyscan(void);
void keydown(void);
unsigned char key;
unsigned char dis_buf;
unsigned char temp;
unsigned char LED7Code[] = {0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71};
GPIO_InitTypeDef GPIO_InitStructure;
int main(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
delay_init();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC, ENABLE); //??PB,PE????
GPIO_PinRemapConfig(GPIO_Remap_SWJ_Disable, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //????
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO????50MHz
GPIO_Init(GPIOC, &GPIO_InitStructure);
//GPIO_Write(GPIOC,0X0000);
while(1)
{
keydown(); //调用按键判断检测程序
GPIO_Write(GPIOC,LED7Code[dis_buf%16]&0x7f); //LED7 0x7f为小数点 共阴和共阳此处也是不一样; %16表示输出16进制
}
}
void keyscan(void)
{ temp = 0;
//P1=0xF0;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_Write(GPIOB,0XF0FF);
delay_ms(10);
//temp=P1; //读P1口
temp=(GPIO_ReadInputData(GPIOB))>>8&0xF0; //高8位的屏蔽低四位
temp=~((temp>>4)|0x00F0); //将高四位移到地位处处理
if(temp==1)
key=1; //第一个按键值
else if(temp==2)
key=2; //第2个按键值
else if(temp==4)
key=3; //第3个按键值
else if(temp==8)
key=4; //第4个按键值
else
key=16;
//P1=0x0F;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //输出模式下 I/O输出速度 50M HZ
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_Write(GPIOB,0X0FFF);
delay_ms(10); //延时
//temp=P1;
temp=(GPIO_ReadInputData(GPIOB)>>8)&0x0F;
temp=~(temp|0xF0);
if(temp==1)
key=key+0;
else if(temp==2)
key=key+4;
else if(temp==4)
key=key+8;
else if(temp==8)
key=key+12;
else
key=16;
dis_buf = key;
// dis_buf = dis_buf & 0x0f;
}
void keydown(void)
{ int retval;
//P1=0xF0; //将高4位全部置1 低四位全部置0
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11 ; // 选择所有脚
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //配置成推挽式输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //输出模式下 I/O输出速度 50M HZ
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15 ; // 选择所有脚
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //配置成推挽式输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //输出模式下 I/O输出速度 50M HZ
GPIO_Init(GPIOB, &GPIO_InitStructure); //初PA口始化
retval = GPIO_ReadInputData(GPIOB); //读PB口状态
if(retval>>8!=0xF0) //判断按键是否按下 如果按钮按下 会拉低P1其中的一个端口
{
keyscan(); //调用按键扫描程序
}
}
|