#include "SysTick.h" //系統延時
#include "stm32f10x.h"
#include "usart.h"
unsigned char temp;
////*********4x4矩陣引出8個引腳,分別接A_Pin0~A_Pin7**********///
void InitKey(void) //
{
GPIO_InitTypeDef GPIOStru;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
GPIOStru.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3;
GPIOStru.GPIO_Mode = GPIO_Mode_Out_PP; //
GPIOStru.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIOStru);
GPIOStru.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
GPIOStru.GPIO_Mode = GPIO_Mode_IPD; // 上拉輸入
GPIOStru.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIOStru);
GPIO_ResetBits(GPIOA,GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7);//拉高
}
int key(void) //
{
int KeyVal=0; //
GPIO_Write(GPIOA,0x0001); //
temp=GPIOA_IDR;
temp=temp & 0x00f0;
if(temp != 0x0000)
{
delay_ms(6);
if(temp != 0x0000)
{
temp=GPIOA_IDR;
temp=temp & 0x00f0;
switch(temp) //
{
case 0x0010: KeyVal=15; break;
case 0x0020: KeyVal=11; break;
case 0x0040: KeyVal=7; break;
case 0x0080: KeyVal=3; break;
}
while(GPIOA->IDR & 0x00f0 != 0x0000);
}
}
GPIO_Write(GPIOA,(GPIOA->ODR & 0xfff0 | 0x2)); //
if(GPIOA->IDR & 0x00f0 != 0x0000)
{
delay_ms(5);
if(GPIOA->IDR & 0x00f0 != 0x0000)
{
switch(GPIOA->IDR & 0x00f0) //
{
case 0x0010: KeyVal=14; break;
case 0x0020: KeyVal=10; break;
case 0x0040: KeyVal=6; break;
case 0x0080: KeyVal=2; break;
}
while(GPIOA->IDR & 0x00f0 != 0x0000);
}
}
GPIO_Write(GPIOA,(GPIOA->ODR & 0xfff0 | 0x4)); //
if(GPIOA->IDR & 0x00f0 != 0x0000)
{
delay_ms(5);
if(GPIOA->IDR & 0x00f0 != 0x0000)
{
switch(GPIOA->IDR & 0x00f0) //
{
case 0x0010: KeyVal=13; break;
case 0x0020: KeyVal=9; break;
case 0x0040: KeyVal=5; break;
case 0x0080: KeyVal=1; break;
}
while(GPIOA->IDR & 0x00f0 != 0x0000);
}
}
GPIO_Write(GPIOA,(GPIOA->ODR & 0xfff0 | 0x8)); //
if(GPIOA->IDR & 0x00f0 != 0x0000)
{
delay_ms(5);
if(GPIOA->IDR & 0x00f0 != 0x0000)
{
switch(GPIOA->IDR & 0x00f0) //
{
case 0x0010: KeyVal=12; break;
case 0x0020: KeyVal=8; break;
case 0x0040: KeyVal=4; break;
case 0x0080: KeyVal=0; break;
}
while(GPIOA->IDR & 0x00f0 != 0x0000);
}
}
return KeyVal;
}
int main()
{
int a;
a=key();
switch(a)
{
case 1: break;
case 1: break;
case 1: break;
case 1: break;
case 1: break;
case 1: break;
case 1: break;
case 1: break;
case 1: break;
}
}
|