STM32 矩阵键盘扫描程序
扫描二维码
随时随地手机看文章
#define __JUZHENJIANPAN_H
extern u8 KeysCAN(void);
extern void GPIO_Config(void);
#endif
#include "stm32f10x.h"
#include"juzhenjianpan.h"
#include"gpiobitmap.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)+(bitnum<<2))
#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) //输出
#definePCIn(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