原理讲解
对应代码(51单片机)
unsigned char code Table_KeyValue[64]={
0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09
,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10,0x11,0x12,0x13
,0x14,0x15,0x16,0x17,0x18,0x19,0x1a,0x1b,0x1c,0x1d
,0x1e,0x1f,0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27
,0x28,0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f,0x30,0x31
,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x3a,0x3b
,0x3c,0x3d,0x3e,0x3f,0x40};
unsigned char code Table_RowScan[8]={
0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
unsigned char code Table_ColumnScan[8]={
0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
unsigned char key_value=0;
unsigned char keyscan(void)
{
unsigned char row;
unsigned char col;
unsigned char key_value;
key_value=0;
for(row=0;row<8;row++)
{
P2=Table_RowScan[row];
if((P1&0xff)<0xff)
{
delayms(10);
if((P1&0xff)<0xff)
{
for(col=0;col<8;col++)
{
if((P1&Table_ColumnScan[col])==0x00)
{
key_value=Table_KeyValue[row*8+col];
while((P1&Table_ColumnScan[col])==0x00);
break;
}
}
}
}
}
return key_value;
}
对应代码(STM32单片机)
unsigned char Table_KeyValue[64]={
0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09
,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10,0x11,0x12,0x13
,0x14,0x15,0x16,0x17,0x18,0x19,0x1a,0x1b,0x1c,0x1d
,0x1e,0x1f,0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27
,0x28,0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f,0x30,0x31
,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x3a,0x3b
,0x3c,0x3d,0x3e,0x3f,0x40};
unsigned char Table_RowScan[8]={
0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
unsigned char Table_ColumnScan[8]={
0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
unsigned char key_value=0;
void IO_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOC, ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
unsigned char keyscan(void)
{
unsigned char row;
unsigned char col;
unsigned char key_value;
unsigned char Col_temp;
key_value=0;
for(row=0;row<8;row++)
{
GPIO_Write(GPIOC, Table_RowScan[row]);
Col_temp=(GPIO_ReadInputData(GPIOA));
if(Col_temp<0xff)
{
delayms(10);
if(Col_temp<0xff)
{
for(col=0;col<8;col++)
{
if((Col_temp&Table_ColumnScan[col])==0x00)
{
key_value=Table_KeyValue[row*8+col];
Col_temp=(GPIO_ReadInputData(GPIOA));
while((Col_temp&Table_ColumnScan[col])==0x00)
{
Col_temp=(GPIO_ReadInputData(GPIOA));
delayms(10);
}
break;
}
}
}
}
}
return key_value;
}