<meter id="pryje"><nav id="pryje"><delect id="pryje"></delect></nav></meter>
          <label id="pryje"></label>

          新聞中心

          EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > 單片機(jī)鍵盤輸入及串口發(fā)送

          單片機(jī)鍵盤輸入及串口發(fā)送

          作者: 時(shí)間:2016-11-26 來(lái)源:網(wǎng)絡(luò) 收藏
          #include"reg52.h"

          typedef unsigned char uchar;
          typedef unsigned int uint;

          //數(shù)碼管端口控制
          sbit SG = P0^0;//控制個(gè)位
          sbit SS = P0^1;//控制十位
          unsigned char code rst[6]={0xe4,0xc0,0xe0,0xc0,0xe0,0x22};//定義一個(gè)code類型 實(shí)現(xiàn)軟件復(fù)位
          uchar tab0[] = {0x81,0xbd,0x92,0x98,0xac,0xc8,0xc0,0x9d,0x80,0x88}; //0~9共陽(yáng)數(shù)碼管
          uchar tab1[2] ={0xfe,0xfe} ;
          uchar SendBuf[10];//發(fā)送的數(shù)
          uchar *p;
          uchar t1,t2;
          uchar channel;
          uchar SendLen=0,BufLen=7;
          Timer_0();//數(shù)碼動(dòng)態(tài)掃描的定時(shí)函數(shù)
          Delay(uint t);//按鍵去抖的延時(shí)函數(shù)
          uint Key_1();//鍵盤掃描函數(shù)
          uint Key_2();//鍵盤返回值函數(shù)
          Delay();//延時(shí)函數(shù)
          Reset();//復(fù)位函數(shù)
          Affirm();//確認(rèn)發(fā)送函數(shù)
          void init_ser1();//串口初始化函數(shù)
          void SendTo();
          void Coding(char,char,char,char,char);
          main()
          {
          ucharn;
          init_ser1();//串口初始化函數(shù)
          P2=0x00;
          P3=0x0f;
          p=tab1;
          Timer_0(); //定時(shí)器初始化函數(shù)
          SendTo();
          t1 = 0;
          t2 = 0;
          while(1)
          {

          n=Key_2();
          if(n!=0xff)
          {

          本文引用地址:http://www.ex-cimer.com/article/201611/321821.htm

          //Led=0;
          if(*p==0xfe)
          {
          if(p==tab1)
          {
          t1 = n;
          }
          else if(p==tab1+1)
          {
          t2 = n;
          }
          //Led=1;
          Delay(9000); //特別關(guān)鍵的延時(shí)
          //Led=0;

          *p=tab0[n];
          p++;
          }
          }

          }
          }
          uint Key_1()
          {
          uint i,j;
          if(P2!=0x00)
          {
          Delay(300);//去除按鍵抖動(dòng)
          if(P2!=0x00)
          {
          i=P2;
          return(i);
          }
          }
          if(P3!=0xf0)
          {
          Delay(300);
          if(P3!=0xf0)

          j=P3;
          return(j);
          }

          }
          uint Key_2()
          {
          switch(key_1()) //根據(jù)掃描結(jié)果選取值
          {
          case 0x01:return 1;break;
          case0x02:return 2;break;
          case0x04:return 3;break;
          case0x08:return 4;break;
          case0x10:return 5;break;
          case0x20:return 6;break;
          case0x40:return 7;break;
          case0x80:return 8;break;
          case0x1f:return 9;break;
          case0x2f:Reset(); break;
          case0x4f:return 0;break;
          case0x8f:Affirm();break;
          default:return 0xff;break;
          }
          }
          Delay(uint t)//延時(shí)函數(shù)
          {

          while(--t);

          }
          Reset()//清除復(fù)位函數(shù)
          {
          Delay(20000);
          Coding(0,0,0,0,0);
          (*((void (*)())rst))();//復(fù)位
          }
          Affirm()//確認(rèn)發(fā)送函數(shù)
          {


          Delay(20000);
          channel = t1*10 + t2;
          Coding(channel,0,0,0,0);
          SendTo();

          //flag=1;
          }
          Timer_0()//定時(shí)器0初始化
          {
          TMOD=TMOD&0xf0|0x01;
          TR0=1;
          ET0=1;
          //EA=1;
          }
          interrupt_0() interrupt 1//數(shù)碼管掃描
          {
          TH0=0xf8;
          TL0=0x30;

          P1=tab1[0];
          SG=1;
          SS=0;
          Delay(150);

          P1=tab1[1];
          SG=0;
          SS=1;

          }

          void Coding(char CHANNEL,char Command3,char Command4,char Command5,char Command6)
          {
          SendBuf[0]=0xFF;
          SendBuf[1]=CHANNEL;
          SendBuf[2]=Command3;
          SendBuf[3]=Command4;
          SendBuf[4]=Command5;
          SendBuf[5]=Command6;
          SendBuf[6]=Command6+Command5+Command4+Command3+CHANNEL;
          }

          void init_ser1()
          {

          // REN=1;//允許串行口接收數(shù)據(jù)
          //
          // SM0=0;
          // SM1=1;//串行口工作方式為1
          //
          // TMOD=TMOD&0xf0|0x20;//通過(guò)定時(shí)器1設(shè)置串口波特率
          // PCON|=0x0;
          // TH1=0xFD;
          // TL1=0xFD; //波特率9600
          // PS=1;
          //
          // TR1=1;//啟動(dòng)定時(shí)器1
          // ES=1;//開串行口中斷
          // PS=1;
          PCON=0X00;
          SCON=0X50;
          TMOD|=0X20;
          TH1=0xFd;
          TL1=0xFd;
          TI=1;
          EA=1;
          TR1=1;
          ES=1;
          }
          void Serial(void)interrupt 4
          {
          if(RI)
          {
          RI=0;
          }
          if(TI)
          {
          TI=0;
          //Delay(20);
          if(SendLen >= BufLen) BufLen = 0;
          if(BufLen)
          {
          SBUF = SendBuf[SendLen++];
          }
          }
          }
          void SendTo(void)
          {
          BufLen = 7;
          SendLen = 0;
          SBUF = "" ;
          do{}
          while(BufLen);
          }



          評(píng)論


          技術(shù)專區(qū)

          關(guān)閉
          看屁屁www成人影院,亚洲人妻成人图片,亚洲精品成人午夜在线,日韩在线 欧美成人 (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })();