新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設計應用 > STM32 矩陣鍵盤掃描

STM32 矩陣鍵盤掃描

作者: 時間:2012-09-10 來源:網(wǎng)絡 收藏

#define __JUZHENJIANPAN_H

本文引用地址:http://butianyuan.cn/article/148437.htm

extern u8 Keyscan(void);

extern void GPIO_Config(void);

#endif

#include stm32f10x.h

#includejuzhenjianpan.h

#includegpiobitmap.h

#define PC0 PCo_0

#define PC1 PCo_1

#define PC2 PCo_2

#define PC3 PCo_3

#define PC4 PCo_4

void GPIO_Config(void)

{

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC ,ENABLE);

}

u8 Keyscan(void)

{

u8 key;

u16 temp;

key=0;

GPIOC->CRL=0X00000000;//將PC0設置成out——pp輸出

GPIOC->CRL|=0X33333333;

GPIOC->ODR|=0x00ff;

GPIOC->CRL=0x00000000; /////去除將PCx設置成out——pp輸出是的某些設置位

GPIOC->CRL|=0X44444444; /////配置為浮空輸入了

GPIOC->CRL=0XFFFFFFF0;//將PC0設置成out——pp輸出

GPIOC->CRL|=0X00000003;

PC0=0;

temp=GPIOC->IDR;

temp=0x00ff;

if(temp!=0x00fe)

{

switch(temp)

{

case 0x00ee: key=4;break;

case 0x00de: key=3;break;

case 0x00be: key=2;break;

case 0x007e: key=1;break;

}

temp=GPIOC->IDR;

temp=0x00f0;

while(temp!=0xf0)

{

temp=GPIOC->IDR;

temp=0x00f0;

}

}

GPIOC->CRL=0X00000000;//將PC0設置成out——pp輸出

GPIOC->CRL|=0X33333333;

GPIOC->ODR|=0x00ff;

GPIOC->CRL=0x00000000;

GPIOC->CRL|=0X44444444;

GPIOC->CRL=0XFFFFFF0F; //PC0設置成out——pp輸出

GPIOC->CRL|=0X00000030;

PC1=0;

temp=GPIOC->IDR;

temp=0x00ff;

if(temp!=0x00fd)

{

switch(temp)

{

case 0x00ed: key=8;break;

case 0x00dd: key=7;break;

case 0x00bd: key=6;break;

case 0x007d: key=5;break;

}

temp=GPIOC->IDR;

temp=0x00f0;

while(temp!=0xf0)

{

temp=GPIOC->IDR;

temp=0x00f0;

}

}

GPIOC->CRL=0X00000000;//將PC0設置成out——pp輸出

GPIOC->CRL|=0X33333333;

GPIOC->ODR|=0x00ff;

GPIOC->CRL=0x00000000;

GPIOC->CRL|=0X44444444;

GPIOC->CRL=0XFFFFF0FF;//PC0設置成out——pp輸出

GPIOC->CRL|=0X00000300;

PC2=0;

temp=GPIOC->IDR;

temp=0x00ff;

if(temp!=0x00fb)

{

switch(temp)

{

case 0x00eb: key=12;break;

case 0x00db: key=11;break;

case 0x00bb: key=10;break;

case 0x007b: key=9;break;

}

temp=GPIOC->IDR;

temp=0x00f0;

while(temp!=0xf0)

{

temp=GPIOC->IDR;

temp=0x00f0;

}

}

GPIOC->CRL=0X00000000;

GPIOC->CRL|=0X33333333; //將PC0設置成out——pp輸出

GPIOC->ODR|=0x00ff;

GPIOC->CRL=0x00000000;

GPIOC->CRL|=0X44444444;

GPIOC->CRL|=0X00003000;

PC3=0;

temp=GPIOC->IDR;

temp=0x00ff;

if(temp!=0x00f7)

{

switch(temp)

{

case 0x00e7: key=16;break;

case 0x00d7: key=15;break;

case 0x00b7: key=14;break;

case 0x0077: key=13;break;

}

temp=GPIOC->IDR;

temp=0x00f0;

while(temp!=0xf0)

{

temp=GPIOC->IDR;

temp=0x00f0;

}

}

return key;

}

#ifndef __GPIOBITMAP_H

#define __GPIOBITMAP_H

#include stm32f10x.h

//IO口操作宏定義

#define BITBAND(addr, bitnum) ((addr 0xF0000000)+0x2000000+((addr 0xFFFFF)5)+(bitnum2))

#define MEM_ADDR(addr) *((volatile unsigned long *)(addr))

#define BIT_ADDR(addr, bitnum) MEM_ADDR(BITBAND(addr, bitnum))

//IO口地址映射

#define GPIOA_ODR_Addr (GPIOA_BASE+12) //0x4001080C

#define GPIOB_ODR_Addr (GPIOB_BASE+12) //0x40010C0C

#define GPIOC_ODR_Addr (GPIOC_BASE+12) //0x4001100C

#define GPIOD_ODR_Addr (GPIOD_BASE+12) //0x4001140C

#define GPIOE_ODR_Addr (GPIOE_BASE+12) //0x4001180C

#define GPIOF_ODR_Addr (GPIOF_BASE+12) //0x40011A0C

#define GPIOG_ODR_Addr (GPIOG_BASE+12) //0x40011E0C

#define GPIOA_IDR_Addr (GPIOA_BASE+8) //0x40010808

#define GPIOB_IDR_Addr (GPIOB_BASE+8) //0x40010C08

#define GPIOC_IDR_Addr (GPIOC_BASE+8) //0x40011008

#define GPIOD_IDR_Addr (GPIOD_BASE+8) //0x40011408

#define GPIOE_IDR_Addr (GPIOE_BASE+8) //0x40011808

#define GPIOF_IDR_Addr (GPIOF_BASE+8) //0x40011A08

#define GPIOG_IDR_Addr (GPIOG_BASE+8) //0x40011E08

//IO口操作,只對單一的IO口!

//確保n的值小于16!

#define PAout(n) BIT_ADDR(GPIOA_ODR_Addr,n) //輸出

#define PAin(n) BIT_ADDR(GPIOA_IDR_Addr,n) //輸入

#define PBout(n) BIT_ADDR(GPIOB_ODR_Addr,n) //輸出

#define PBin(n) BIT_ADDR(GPIOB_IDR_Addr,n) //輸入

#define PCout(n) BIT_ADDR(GPIOC_ODR_Addr,n) //輸出

#define PCin(n) BIT_ADDR(GPIOC_IDR_Addr,n) //輸入

#define PDout(n) BIT_ADDR(GPIOD_ODR_Addr,n) //輸出

#define PDin(n) BIT_ADDR(GPIOD_IDR_Addr,n) //輸入

#define PEout(n) BIT_ADDR(GPIOE_ODR_Addr,n) //輸出

#define PEin(n) BIT_ADDR(GPIOE_IDR_Addr,n) //輸入

#define PFout(n) BIT_ADDR(GPIOF_ODR_Addr,n) //輸出

#define PFin(n) BIT_ADDR(GPIOF_IDR_Addr,n) //輸入

#define PGout(n) BIT_ADDR(GPIOG_ODR_Addr,n) //輸出

#define PGin(n) BIT_ADDR(GPIOG_IDR_Addr,n) //輸入

/*------------------------------

*******************************

各GPIO輸出口地址定義

********************************

--------------------------------*/

#define PAo_0 PAout(0)

#define PAo_1 PAout(1)

#define PAo_2 PAout(2)

#define PAo_3 PAout(3)

#define PAo_4 PAout(4)

#define PAo_5 PAout(5)

#define PAo_6 PAout(6)

#define PAo_7 PAout(7)

#define PAo_8 PAout(8)

#define PAo_9 PAout(9)

#define PAo_10 PAout(10)

#define PAo_11 PAout(11)

#define PAo_12 PAout(12)

#define PAo_13 PAout(13)

#define PAo_14 PAout(14)

#define PAo_15 PAout(15)

//----------------------------------//

#define PBo_0 PBout(0)

#define PBo_1 PBout(1)

#define PBo_2 PBout(2)

#define PBo_3 PBout(3)

#define PBo_4 PBout(4)

#define PBo_5 PBout(5)

#define PBo_6 PBout(6)

#define PBo_7 PBout(7)

#define PBo_8 PBout(8)

#define PBo_9 PBout(9)

#define PBo_10 PBout(10)

#define PBo_11 PBout(11)

#define PBo_12 PBout(12)

#define PBo_13 PBout(13)

#define PBo_14 PBout(14)

#define PBo_15 PBout(15)

//--------------------------------//

#define PCo_0 PCout(0)

#define PCo_1 PCout(1)

#define PCo_2 PCout(2)

#define PCo_3 PCout(3)

#define PCo_4 PCout(4)

#define PCo_5 PCout(5)

#define PCo_6 PCout(6)

#define PCo_7 PCout(7)

#define PCo_8 PCout(8)

#define PCo_9 PCout(9)

#define PCo_10 PCout(10)

#define PCo_11 PCout(11)

#define PCo_12 PCout(12)

#define PCo_13 PCout(13)

#define PCo_14 PCout(14)

#define PCo_15 PCout(15)

//---------------------------//

#define PDo_0 PDout(0)

#define PDo_1 PDout(1)

#define PDo_2 PDout(2)

#define PDo_3 PDout(3)

#define PDo_4 PDout(4)

#define PDo_5 PDout(5)

#define PDo_6 PDout(6)

#define PDo_7 PDout(7)

#define PDo_8 PDout(8)

#define PDo_9 PDout(9)

#define PDo_10 PDout(10)

#define PDo_11 PDout(11)

#define PDo_12 PDout(12)

#define PDo_13 PDout(13)

#define PDo_14 PDout(14)

#define PDo_15 PDout(15)

//------------------------------//

#define PEo_0 PEout(0)

#define PEo_1 PEout(1)

#define PEo_2 PEout(2)

#define PEo_3 PEout(3)

#define PEo_4 PEout(4)

#define PEo_5 PEout(5)

#define PEo_6 PEout(6)

#define PEo_7 PEout(7)

#define PEo_8 PEout(8)

#define PEo_9 PEout(9)

#define PEo_10 PEout(10)

#define PEo_11 PEout(11)

#define PEo_12 PEout(12)

#define PEo_13 PEout(13)

#define PEo_14 PEout(14)

#define PEo_15 PEout(15)

//---------------------------//

#define PFo_0 PFout(0)

#define PFo_1 PFout(1)

#define PFo_2 PFout(2)

#define PFo_3 PFout(3)

#define PFo_4 PFout(4)

#define PFo_5 PFout(5)

#define PFo_6 PFout(6)

#define PFo_7 PFout(7)

#define PFo_8 PFout(8)

#define PFo_9 PFout(9)

#define PFo_10 PFout(10)

#define PFo_11 PFout(11)

#define PFo_12 PFout(12)

#define PFo_13 PFout(13)

#define PFo_14 PFout(14)

#define PFo_15 PFout(15)

//---------------------------//

#define PGo_0 PGout(0)

#define PGo_1 PGout(1)

#define PGo_2 PGout(2)

#define PGo_3 PGout(3)

#define PGo_4 PGout(4)

#define PGo_5 PGout(5)

#define PGo_6 PGout(6)

#define PGo_7 PGout(7)

#define PGo_8 PGout(8)

#define PGo_9 PGout(9)

#define PGo_10 PGout(10)

#define PGo_11 PGout(11)

#define PGo_12 PGout(12)

#define PGo_13 PGout(13)

#define PGo_14 PGout(14)

#define PGo_15 PGout(15)

/*------------------------------

********************************

各GPIO輸入口地址定義

********************************

--------------------------------*/

#define PAi_0 PAin(0)

#define PAi_1 PAin(1)

#define PAi_2 PAin(2)

#define PAi_3 PAin(3)

#define PAi_4 PAin(4)

#define PAi_5 PAin(5)

#define PAi_6 PAin(6)

#define PAi_7 PAin(7)

#define PAi_8 PAin(8)

#define PAi_9 PAin(9)

#define PAi_10 PAin(10)

#define PAi_11 PAin(11)

#define PAi_12 PAin(12)

#define PAi_13 PAin(13)

#define PAi_14 PAin(14)

#define PAi_15 PAin(15)

//---------------------------------//

#define PBi_0 PBin(0)

#define PBi_1 PBin(1)

#define PBi_2 PBin(2)

#define PBi_3 PBin(3)

#define PBi_4 PBin(4)

#define PBi_5 PBin(5)

#define PBi_6 PBin(6)

#define PBi_7 PBin(7)

#define PBi_8 PBin(8)

#define PBi_9 PBin(9)

#define PBi_10 PBin(10)

#define PBi_11 PBin(11)

#define PBi_12 PBin(12)

#define PBi_13 PBin(13)

#define PBi_14 PBin(14)

#define PBi_15 PBin(15)

//---------------------------------//

#define PCi_0 PCin(0)

#define PCi_1 PCin(1)

#define PCi_2 PCin(2)

#define PCi_3 PCin(3)

#define PCi_4 PCin(4)

#define PCi_5 PCin(5)

#define PCi_6 PCin(6)

#define PCi_7 PCin(7)

#define PCi_8 PCin(8)

#define PCi_9 PCin(9)

#define PCi_10 PCin(10)

#define PCi_11 PCin(11)

#define PCi_12 PCin(12)

#define PCi_13 PCin(13)

#define PCi_14 PCin(14)

#define PCi_15 PCin(15)

//-------------------------------------//

#define PDi_0 PDin(0)

#define PDi_1 PDin(1)

#define PDi_2 PDin(2)

#define PDi_3 PDin(3)

#define PDi_4 PDin(4)

#define PDi_5 PDin(5)

#define PDi_6 PDin(6)

#define PDi_7 PDin(7)

#define PDi_8 PDin(8)

#define PDi_9 PDin(9)

#define PDi_10 PDin(10)

#define PDi_11 PDin(11)

#define PDi_12 PDin(12)

#define PDi_13 PDin(13)

#define PDi_14 PDin(14)

#define PDi_15 PDin(15)

//-------------------------------------//

#define PEi_0 PEin(0)

#define PEi_1 PEin(1)

#define PEi_2 PEin(2)

#define PEi_3 PEin(3)

#define PEi_4 PEin(4)

#define PEi_5 PEin(5)

#define PEi_6 PEin(6)

#define PEi_7 PEin(7)

#define PEi_8 PEin(8)

#define PEi_9 PEin(9)

#define PEi_10 PEin(10)

#define PEi_11 PEin(11)

#define PEi_12 PEin(12)

#define PEi_13 PEin(13)

#define PEi_14 PEin(14)

#define PEi_15 PEin(15)

//---------------------------------//

#define PFi_0 PFin(0)

#define PFi_1 PFin(1)

#define PFi_2 PFin(2)

#define PFi_3 PFin(3)

#define PFi_4 PFin(4)

#define PFi_5 PFin(5)

#define PFi_6 PFin(6)

#define PFi_7 PFin(7)

#define PFi_8 PFin(8)

#define PFi_9 PFin(9)

#define PFi_10 PFin(10)

#define PFi_11 PFin(11)

#define PFi_12 PFin(12)

#define PFi_13 PFin(13)

#define PFi_14 PFin(14)

#define PFi_15 PFin(15)

//---------------------------------//

#define PGi_0 PGin(0)

#define PGi_1 PGin(1)

#define PGi_2 PGin(2)

#define PGi_3 PGin(3)

#define PGi_4 PGin(4)

#define PGi_5 PGin(5)

#define PGi_6 PGin(6)

#define PGi_7 PGin(7)

#define PGi_8 PGin(8)

#define PGi_9 PGin(9)

#define PGi_10 PGin(10)

#define PGi_11 PGin(11)

#define PGi_12 PGin(12)

#define PGi_13 PGin(13)

#define PGi_14 PGin(14)

#define PGi_15 PGin(15)

//---------------------------------//

///////////////////////////////////////////////////////////////////

//Ex_NVIC_Config專用定義

#define GPIO_A 0

#define GPIO_B 1

#define GPIO_C 2

#define GPIO_D 3

#define GPIO_E 4

#define GPIO_F 5

#define GPIO_G 6

#define FTIR 1 //下降沿觸發(fā)

#define RTIR 2 //上升沿觸發(fā)

#endif

DIY機械鍵盤相關社區(qū):機械鍵盤DIY




關鍵詞: 掃描 鍵盤 矩陣 STM32

評論


相關推薦

技術專區(qū)

關閉