DSP2335的按键输入key工程笔记
配置输入
时钟
方向
下拉
本文使用的是矩阵按键 12 13 14 为物理上拉输入,48 49 50为输出;
可以考虑输入AQ滤波;里面没有加;
/*
* key.c
*
* Created on: 2023年11月19日
* Author: peng
*/
#include"key.h"
#include"led1.h"
void key_init(void)
{
EALLOW;
SysCtrlRegs.PCLKCR3.bit.GPIOINENCLK=1;
GpioCtrlRegs.GPAMUX1.bit.GPIO12 = 0;
GpioCtrlRegs.GPADIR.bit.GPIO12 = 0;//输入
GpioCtrlRegs.GPAPUD.bit.GPIO12 = 0;
GpioCtrlRegs.GPAMUX1.bit.GPIO13 = 0;
GpioCtrlRegs.GPADIR.bit.GPIO13 = 0;//输入
GpioCtrlRegs.GPAPUD.bit.GPIO13 = 0;
GpioCtrlRegs.GPAMUX1.bit.GPIO14 = 0;
GpioCtrlRegs.GPADIR.bit.GPIO14 = 0;//输入
GpioCtrlRegs.GPAPUD.bit.GPIO14 = 0;
//输出
GpioCtrlRegs.GPBMUX2.bit.GPIO48 = 0;
GpioCtrlRegs.GPBDIR.bit.GPIO48 = 1;//
GpioCtrlRegs.GPBPUD.bit.GPIO48 = 0;
GpioCtrlRegs.GPBMUX2.bit.GPIO49 = 0;
GpioCtrlRegs.GPBDIR.bit.GPIO49 = 1;//
GpioCtrlRegs.GPBPUD.bit.GPIO49 = 0;
GpioCtrlRegs.GPBMUX2.bit.GPIO50 = 0;
GpioCtrlRegs.GPBDIR.bit.GPIO50 = 1;//
GpioCtrlRegs.GPBPUD.bit.GPIO50 = 0;
EDIS;
GpioDataRegs.GPBSET.bit.GPIO48 = 1;
GpioDataRegs.GPBSET.bit.GPIO49 = 1;
GpioDataRegs.GPBSET.bit.GPIO50 = 1;
}
unsigned char read_row()
{
unsigned char temp=0;
temp=( GpioDataRegs.GPADAT.bit.GPIO12<<0| GpioDataRegs.GPADAT.bit.GPIO13<<1| GpioDataRegs.GPADAT.bit.GPIO14<<2);
return temp;
}
unsigned char read_line()
{
unsigned char temp=0;
temp= (
GpioDataRegs.GPBDAT.bit.GPIO48
|((GpioDataRegs.GPBDAT.bit.GPIO49&0x01)<<1)
|((GpioDataRegs.GPBDAT.bit.GPIO50&0x01)<<2)
);
return temp;
}
void _SCAN_line(char x)
{
GpioDataRegs.GPBDAT.bit.GPIO48=~((long)0x01&x);
GpioDataRegs.GPBDAT.bit.GPIO49=~(((long)0x02&x)>>1);
GpioDataRegs.GPBDAT.bit.GPIO50=~(((long)0x04&x)>>2);
}
char SCAN_row ()
{
return GpioDataRegs.GPADAT.bit.GPIO12<<0| GpioDataRegs.GPADAT.bit.GPIO13<<1| GpioDataRegs.GPADAT.bit.GPIO14<<2;
}
char key_scan()
{
char key = 0;
unsigned char i = 0,tt=0x1;
unsigned int temp=0;
unsigned char p48=0,p49=0,p50=0;
tt=~tt;
static char key_up_flag = 0,key_breakup=0;
for ( i = 0; i < 3; i++)
{
switch (i)//列扫描
{
case 0: _SCAN_line(1);
break;
case 1: _SCAN_line(2);
break;
case 2: _SCAN_line(4);
break;
default:
break;
}
if ( (temp=(SCAN_row() & 0x07) )!= 0x07 )//被按下
{
DELAY_US(10000);
if ((temp=(SCAN_row() & 0x07)) != 0x07)
{
key_up_flag = 1;
switch (temp=( ~temp)&0x07 )
{
case 1: key = (temp - 1) * 3 + i+1; key_breakup = key;return 0;
case 2: key = (temp- 1) * 3 + i+1 ; key_breakup = key;return 0;
case 4: key = (temp - 1) * 3 + i+1; key_breakup = key;return 0;
default:key = 0;
break;
}
// key_breakup = key;
}
}
else
{
if (key_up_flag == 1)
{
key_up_flag = 0;
return key_breakup;
}
}
}
return 0;
}
void key_LED()//按键控制LED
{
//char key = 0;
switch (key_scan())
{
case 1: LED1_TOGGLE; break;
case 2: LED2_TOGGLE; break;
case 3: LED3_TOGGLE; break;
default: break;
}
}