我做了一个5*5的矩阵键盘,用的单片机是STM32F103VET6。前面一直使用都没有问题。这次更换口线后(以前换个几次,没问题),按几下就会死机。不知道那里硬件冲突了。请大家帮忙看看。PB8-12输出高电平。PD8-12作为键盘信信息输入口线。(PD2用作了SDIO-CMD,PD1控制485输入输出,PD5,PD6作串口2)。下面是按键函数:
int key(void) //êμÏÖ¾ØÕó¼üÅì¡£·μ»ØÖμÎa£¬¸÷°′¼üμļüÖ죬′˼üÖμóéóû§×Ô¼o¶¨òå¡£
{
//int KeyVal=20; //keyValÎa×îoó·μ»ØμļüÖμ¡£
GPIO_Write(GPIOB,(GPIOB->ODR & 0xe0ff | 0x1f00)); //ÏèèÃ
B8μ½
B12è«2¿êä3ö¸ß¡£
if((GPIOD->IDR & 0x1f00)==0x0000) //èç1û£¬
D8μ½
D12è«Îa0£¬Ôò£¬Ã»óD¼ü°′Ï¡£′Ëê±£¬·μ»ØÖμÎa-1.
{
return -1;
}
else
{
delay_ms(120); //Ñóê±5ms襶¶¶ˉ¡£
if((GPIOD->IDR & 0x1f00)==0x0000) //èç1û£¬Ñóê±5msoó£¬
D8μ½
D12óÖè«Îa0£¬Ôò£¬¸Õ2Åòy½ÅμÄμçλ±ä»ˉêǶ¶¶ˉ2úéúμÄ.
return -1;
}
GPIO_Write(GPIOB,(GPIOB->ODR & 0xe0ff | 0x800)); //èÃ
D8μ½
D11êä3ö¶t½øÖÆμÄ1000.
switch(GPIOD->IDR & 0x1f00) //¶Ô
D12μ½
D15μÄÖμ½øDDÅD¶Ï£¬òÔêä3ö2»í¬μļüÖμ¡£
{
case 0x0100: KeyVal=18; break;
case 0x0200: KeyVal=0; break;
case 0x0400: KeyVal=19; break;
case 0x0800: KeyVal=12; break;
case 0x1000: KeyVal=10; break;
}
GPIO_Write(GPIOB,(GPIOB->ODR & 0xe0ff | 0x1000)); //èÃPD8μ½PD11êä3ö¶t½øÖÆμÄ0001.
switch(GPIOD->IDR & 0x1f00) //¶ÔPD12μ½PD15μÄÖμ½øDDÅD¶Ï£¬òÔêä3ö2»í¬μļüÖμ¡£
{
case 0x0100: KeyVal=4; break;
case 0x0200: KeyVal=1; break;
case 0x0400: KeyVal=20; break;
case 0x0800: KeyVal=16; break;
case 0x1000: KeyVal=15; break;
}
GPIO_Write(GPIOB,(GPIOB->ODR & 0xe0ff | 0x100)); //èÃPD8μ½PD11êä3ö¶t½øÖÆμÄ0001.
switch(GPIOD->IDR & 0x1f00) //¶ÔPD12μ½PD15μÄÖμ½øDDÅD¶Ï£¬òÔêä3ö2»í¬μļüÖμ¡£
{
case 0x0100: KeyVal=7; break;
case 0x0200: KeyVal=22; break;
case 0x0400: KeyVal=23; break;
case 0x0800: KeyVal=24; break;
case 0x1000: KeyVal=25; break;
}
GPIO_Write(GPIOB,(GPIOB->ODR & 0xe0ff | 0x200)); //èÃPD8μ½PD11êä3ö¶t½øÖÆμÄ0010.
switch(GPIOD->IDR & 0x1f00) //¶ÔPD12μ½PD15μÄÖμ½øDDÅD¶Ï£¬òÔêä3ö2»í¬μļüÖμ¡£
{
case 0x0100: KeyVal=8; break;
case 0x0200: KeyVal=5; break;
case 0x0400: KeyVal=2; break;
case 0x0800: KeyVal=11; break;
case 0x1000: KeyVal=13; break;
}
GPIO_Write(GPIOB,(GPIOB->ODR & 0xe0ff | 0x400)); //èÃPD8μ½PD11êä3ö¶t½øÖÆμÄ0100.
switch(GPIOD->IDR & 0x1f00) //¶ÔPD12μ½PD15μÄÖμ½øDDÅD¶Ï£¬òÔêä3ö2»í¬μļüÖμ¡£
{
case 0x0100: KeyVal=9; break;
case 0x0200: KeyVal=6; break;
case 0x0400: KeyVal=3; break;
case 0x0800: KeyVal=21; break;
case 0x1000: KeyVal=14; break;
}
Press = 1;
return KeyVal;
}
主函数里面查询按键函数部分:
if(key() != -1)key();
while (Press == 1)
{
GPIO_Write(GPIOB,(GPIOB->ODR & 0xe0ff | 0x1f00));
nSw2=GPIOD->IDR & 0x1f00;
if(nSw2 != 0x0000)Press = 1;
else Press = 0;
}
nKeycode=KeyVal;
if(nKeycode != -1)
{
printf("nKeycode = %d ",nKeycode);
}
KeyVal = -1;
请大家帮忙分析下现在出现死机的原因。是不是硬件有冲突了。
一周热门 更多>