|
- key.c
- #include "key.h"
- static u8 key_up=1;//2»Ö§3Öᬰ′
- int key=0;
- void delayms(unsigned int n)
- {
- unsigned int i,j;
- for(i=0;i<n;i++)
- {
- for(j=0;j<123;j++);
- }
- }
- void KEY_Init()
- {
- GPIO_InitTypeDef GPIO_InitStructer;
-
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
-
- GPIO_InitStructer.GPIO_Pin=GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
- GPIO_InitStructer.GPIO_Mode=GPIO_Mode_IPU;
- GPIO_Init(GPIOB, &GPIO_InitStructer);
-
- GPIO_InitStructer.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3;
- GPIO_InitStructer.GPIO_Mode=GPIO_Mode_Out_PP;
- GPIO_InitStructer.GPIO_Speed=GPIO_Speed_50MHz;
- GPIO_Init(GPIOB, &GPIO_InitStructer);
- }
- u8 keyscan(void)
- {
- u16 temp,num;
- GPIO_Write(GPIOB,0xf0);
- while(1)
- {
- if(P5==1&& P6==1 && P7==1&& P8==1)
- {
- key=240;
- key_up=1;
- }
- else if(key_up==1&&(P5==0||P6==0||P7==0||P8==0))
- {
- // GPIO_ResetBits(GPIOB, GPIO_Pin_0);
- // GPIO_SetBits(GPIOB, GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3);
- GPIO_Write(GPIOB,0xfe);
- temp=GPIO_ReadInputData(GPIOB);
- temp&=0xf0;
- if(temp!=0xf0)
- {
- delayms(2);
- temp=GPIO_ReadInputData(GPIOB);
- temp&=0xf0;
- switch(temp)
- {
- case 0xe0:num=0x00;break;
- case 0xd0:num=0x01;break;
- case 0xb0:num=0x02;break;
- case 0x70:num=0x03;break;
- default:break;
- }
- }
-
- // GPIO_ResetBits(GPIOB, GPIO_Pin_1);
- // GPIO_SetBits(GPIOB, GPIO_Pin_0|GPIO_Pin_2|GPIO_Pin_3);
- GPIO_Write(GPIOB,0xfd);
- temp=GPIO_ReadInputData(GPIOB);
- temp&=0xf0;
- if(temp!=0xf0)
- {
- delayms(2);
- temp=GPIO_ReadInputData(GPIOB);
- temp&=0xf0;
- switch(temp)
- {
- case 0xe0:num=0x04;break;
- case 0xd0:num=0x05;break;
- case 0xb0:num=0x06;break;
- case 0x70:num=0x07;break;
- default:break;
- }
- }
-
- // GPIO_ResetBits(GPIOB, GPIO_Pin_2);
- // GPIO_SetBits(GPIOB, GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_3);
- GPIO_Write(GPIOB,0xfb);
- temp=GPIO_ReadInputData(GPIOB);
- temp&=0xf0;
- if(temp!=0xf0)
- {
- delayms(2);
- temp=GPIO_ReadInputData(GPIOB);
- temp&=0xf0;
- switch(temp)
- {
- case 0xe0:num=0x08;break;
- case 0xd0:num=0x09;break;
- case 0xb0:num=0x0a;break;
- case 0x70:num=0x0b;break;
- default:break;
- }
- }
-
- // GPIO_ResetBits(GPIOB, GPIO_Pin_3);
- // GPIO_SetBits(GPIOB, GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2);
- GPIO_Write(GPIOB,0xf7);
- temp=GPIO_ReadInputData(GPIOB);
- temp&=0xf0;
- if(temp!=0xf0)
- {
- delayms(2);
- temp=GPIO_ReadInputData(GPIOB);
- temp&=0xf0;
- switch(temp)
- {
- case 0xe0:num=0x0c;break;
- case 0xd0:num=0x0d;break;
- case 0xb0:num=0x0e;break;
- case 0x70:num=0x0f;break;
- default:break;
- }
- }
- return (num);
- }
- }
- }
复制代码
|
|