微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > STM32 矩阵键盘扫描

STM32 矩阵键盘扫描

时间:09-10 来源:互联网 点击:

#define __JUZHENJIANPAN_H

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)

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top