?? key_driver.c
字號:
//BY LINZH_BME_SEU
//頭文件
#include "hw_memmap.h"
#include "hw_types.h"
#include "gpio.h"
#include "sysctl.h"
//宏
//函數
void KeyInit(void); //初始化
void SetLine(unsigned int x,unsigned char a); //開第x排
unsigned char GetRow(unsigned int y); //讀第y列
unsigned char KeyPress_base(void); //是否有鍵按下
unsigned char KeyScan_base(void); //哪個鍵正被按著,0~15中最前的一個y
void KeyDelay(unsigned int x);//延時
unsigned char KeyPress(void); //消抖
unsigned char KeyScan(); //消抖
unsigned char KeyRelease(); //釋放
//初始化
void KeyInit(void)
{
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);
GPIOPinTypeGPIOOutput(GPIO_PORTD_BASE,GPIO_PIN_0);
GPIOPinTypeGPIOOutput(GPIO_PORTD_BASE,GPIO_PIN_1);
GPIOPinTypeGPIOOutput(GPIO_PORTD_BASE,GPIO_PIN_2);
GPIOPinTypeGPIOOutput(GPIO_PORTD_BASE,GPIO_PIN_3);
GPIOPinTypeGPIOInput(GPIO_PORTD_BASE,GPIO_PIN_4);
GPIOPinTypeGPIOInput(GPIO_PORTD_BASE,GPIO_PIN_5);
GPIOPinTypeGPIOInput(GPIO_PORTD_BASE,GPIO_PIN_6);
GPIOPinTypeGPIOInput(GPIO_PORTD_BASE,GPIO_PIN_7);
}
//開第x排
void SetLine(unsigned int x,unsigned char a)
{
unsigned char b=a<<x;
if(x==0) GPIOPinWrite(GPIO_PORTD_BASE,GPIO_PIN_0,b);
else if(x==1) GPIOPinWrite(GPIO_PORTD_BASE,GPIO_PIN_1,b);
else if(x==2) GPIOPinWrite(GPIO_PORTD_BASE,GPIO_PIN_2,b);
else if(x==3) GPIOPinWrite(GPIO_PORTD_BASE,GPIO_PIN_3,b);
}
//讀第y列
unsigned char GetRow(unsigned int y)
{
unsigned char b;
if(y==0) b= GPIOPinRead(GPIO_PORTD_BASE,GPIO_PIN_4);
else if(y==1) b= GPIOPinRead(GPIO_PORTD_BASE,GPIO_PIN_5);
else if(y==2) b= GPIOPinRead(GPIO_PORTD_BASE,GPIO_PIN_6);
else if(y==3) b= GPIOPinRead(GPIO_PORTD_BASE,GPIO_PIN_7);
else b=0;
if(b!=0) b=1;
return b;
}
//是否有鍵按下
unsigned char KeyPress_base(void)
{
unsigned int i;
for(i=0;i<4;i++) SetLine(i,1);
for(i=0;i<4;i++)
{
if(GetRow(i)==1) return 1;
}
return 0;
}
//掃描
unsigned char KeyScan_base(void)
{
unsigned int x,y;
for (x=0;x<4;x++)
{
SetLine(x,1);
SetLine((x+1)%4,0);
SetLine((x+2)%4,0);
SetLine((x+3)%4,0);
for (y=0;y<4;y++)
{
if(GetRow(y)==1) return x*4+y;
}
}
return 16; //未按下
}
//延時
void KeyDelay(unsigned int x)
{
unsigned int da,db;
for(da=0;da<x;da++)
for(db=0;db<1000;db++);
}
//消抖的
unsigned char KeyPress(void)
{
if(KeyPress_base()==1)
{
KeyDelay(100);
if(KeyPress_base()==1) return 1;
}
return 0;
}
unsigned char KeyScan(void)
{
unsigned char k=KeyScan_base();
KeyDelay(100);
if(KeyScan_base()==k) return k;
else return 16;
}
unsigned char KeyRelease(void)
{
unsigned char k1;
k1=KeyScan_base();
if(k1!=16)
{
KeyDelay(100);
if(KeyScan_base()!=k1) return 16;
unsigned int x,y;
x=k1/4;
y=k1%4;
SetLine(x,1);
SetLine((x+1)%4,0);
SetLine((x+2)%4,0);
SetLine((x+3)%4,0);
while(GetRow(y)==1);
return k1;
}
return 16;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -