標(biāo)題: 矩陣鍵盤的問題 [打印本頁]

作者: lchyx.    時(shí)間: 2017-4-10 20:58
標(biāo)題: 矩陣鍵盤的問題
#include<reg52.h>
#include "juzhen.h"

sbit s1=P3^0;
sbit s2=P3^1;
sbit s3=P3^2;
sbit s4=P3^3;

sbit key1=P3^4;
sbit key2=P3^5;
sbit key3=P3^6;
sbit key4=P3^7;

uchar dat=0,ucr;
bit key_b;
sbit duan=P2^5;
sbit wei=P2^6;

uchar code dx[11]={
0xc0,0xf9,0xa4,0xb0,0x99,
0x92,0x82,0xf8,0x80,0x90,
0xbf          
};

uchar code wx[]={
0x80,0x40,0x20,0x10,
0x08,0x04,0x02,0x01
//0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80
//0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f
};

uchar shuju[8]={0,0,10,0,0,10,0,0};

void delayms(uint x)
{
uint y;
for(;x>0;x--)
   for(y=20;y>0;y--);
}

/*顯示函數(shù)*/
void xianshi()
{
  uint i;

  for(i=0;i<8;i++)        //8位的循環(huán)
  {
    duan=1;
        P0=dx[shuju[i]]; //將i的值送給dx數(shù)組內(nèi)的shuju數(shù)組
        duan=0;
        P0=0xff;

    wei=1;
    P0=wx[i];                //將i的值送給位
        wei=0;
        delayms(5);
  }
}
/********************************
    按鍵掃描
*********************************/
void saomiao()
{
  uchar col,col_f,row,ci,shu;
  dat=0;//key_b=0;
  row=0xfe;                                                //將值0xfe送給row
    for(ci=0;ci<4;ci++)                        //4位的循環(huán)
    {
           P3=row;                                        //將row的值送給P3
           col=P3;                                        //col等于P3的值
           col_f=col;                                //col_f等于col的值
           for(shu=0;shu<4;shu++)        //再一個(gè)四位的循環(huán)
           {
              if((col_f&0x10)==0)        //檢測是否等于0
                  {
                         delayms(5);                //延時(shí)
                         if(P3!=col)                //再次檢測P3是否不等于col
                 return;                //P3不等于col,那么就返回

                         else                                //否則

                                 delayms(10);        //延時(shí)
                                 while(P3==col)        //檢測P3是否等于col
                                 {xianshi();}        //如果等于,那么就調(diào)用顯示函數(shù)
//                                  shuju[0]=dat;
                                  key_b=1;                //key_b等于1
                                  return;                //返回                 
                  }
                   else                                        //上面檢測是否等于0,如果不等于,那么就執(zhí)行下面的程序
                  {
                   dat++;                                //先是dat++;
                   col_f=col_f>>1;                //col_f等于col_f右移一位
                  }
           }
           row=row<<1;                                //row等于row左移一位
           row=row+1;                                //row等于row+1
    }
}

void tiao()
{
//  if(t_f_s==0)
  saomiao();                                   //首先掃描鍵盤
  while((dat<10)|(dat==16))           //如果dat小于10或者dat等于16
  {
           if(key_b==1)                           //判斷是否有鍵按下
        {
        shuju[0]=dat;                           //數(shù)組shuju的第三位等于dat
        }
    xianshi();                                   //顯示
  }
}

#include<reg52.h>
#include "main.h"
extern uchar dat;

void main()
{
  bit sta=0;
  while(1)
  {
   saomiao();
   xianshi();


   if(dat==12)                                //檢測dat是否等于12
         {tiao();}        //如果是,那么就執(zhí)行函數(shù)

  }
}


為什么程序卡在 if(dat==12)這里?里面的程序執(zhí)行不了


作者: zl2168    時(shí)間: 2017-4-14 21:22
為什么沒人解答?提問莫名其妙!




歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1