经典按键扫描程序

来源:互联网 发布:猜数游戏编程 编辑:程序博客网 时间:2024/05/29 03:11

setbit(PORTC,PORTC0);
                      NOP();
                      NOP();                                                  
                      if (checkbit(PINC,PINC4)) setbit(new_key1,0);
                      if (checkbit(PINC,PINC5)) setbit(new_key1,1);
                      if (checkbit(PINC,PINC6)) setbit(new_key1,2);
                      clrbit(PORTC,PORTC0);
                      setbit(PORTC,PORTC1);
                      NOP();
                      NOP();
                      if (checkbit(PINC,PINC4)) setbit(new_key2,0);
                      if (checkbit(PINC,PINC5)) setbit(new_key2,1);
                      if (checkbit(PINC,PINC6)) setbit(new_key2,2);   
                      clrbit(PORTC,PORTC1);
                      setbit(PORTC,PORTC2);
                      NOP();
                      NOP();
                      if (checkbit(PINC,PINC4)) setbit(new_key3,0);
                      if (checkbit(PINC,PINC5)) setbit(new_key3,1);
                      if (checkbit(PINC,PINC6)) setbit(new_key3,2);
                      clrbit(PORTC,PORTC2);
                      setbit(PORTC,PORTC3);
                      NOP();
                      NOP();
                      if (checkbit(PINC,PINC4)) setbit(new_key4,0);
                      if (checkbit(PINC,PINC5)) setbit(new_key4,1);
                      if (checkbit(PINC,PINC6)) setbit(new_key4,2);
                      clrbit(PORTC,PORTC3);
//------------------------------------接收到正确命令处理
                      if (checkbit(works,uorder_ok)) unit_order();
                      if (checkbit(works,norder_ok)) net_order();            
                  
                      time1++;
                      }while (time1<0xfe);
                      time1=0x00;
//-------------------------------------按键处理程序
                      tmp_key1=old_key1;
                      tmp_key1=(tmp_key1^new_key1);
                      tmp_key1=(tmp_key1&new_key1);
                      old_key1=new_key1;
                      if (checkbit(tmp_key1,0))
                           {
                         key_data=0x01;
                         scan_in();
                         }
                      if (checkbit(tmp_key1,1))
                           {
                         key_data=0x02;
                         scan_in();
                         }
                      if (checkbit(tmp_key1,2))
                           {
                         key_data=0x03;
                         scan_in();
                         }     
                       new_key1=0x00;
                      tmp_key2=old_key2;
                      tmp_key2=(tmp_key2^new_key2);
                      tmp_key2=(tmp_key2&new_key2);
                      old_key2=new_key2;
                      if (checkbit(tmp_key2,0))
                           {
                         key_data=0x04;
                         scan_in();
                         }
                      if (checkbit(tmp_key2,1))
                           {
                         key_data=0x05;
                         scan_in();
                         }
                      if (checkbit(tmp_key2,2))
                           {
                         key_data=0x06;
                         scan_in();
                         }     
                       new_key2=0x00;
                     
                      tmp_key3=old_key3;
                      tmp_key3=(tmp_key3^new_key3);
                      tmp_key3=(tmp_key3&new_key3);
                      old_key3=new_key3;
                      if (checkbit(tmp_key3,0))
                           {
                         key_data=0x07;
                         scan_in();
                         }
                      if (checkbit(tmp_key3,1))
                           {
                         key_data=0x08;
                         scan_in();
                         }
                      if (checkbit(tmp_key3,2))
                           {
                         key_data=0x09;
                         scan_in();
                         }     
                       new_key3=0x00;
                     
                      tmp_key4=old_key4;
                      tmp_key4=(tmp_key4^new_key4);
                      tmp_key4=(tmp_key4&new_key4);
                      old_key4=new_key4;
                      if (checkbit(tmp_key4,0))
                           {
                         key_data=0x0C;
                         scan_in();
                         }
                      if (checkbit(tmp_key4,1))
                           {
                         key_data=0x00;
                         scan_in();
                         }
                      if (checkbit(tmp_key4,2))
                           {
                         key_data=0x0A;
                         scan_in();
                         }     
                       new_key4=0x00;—原文来自《新e代电子网》网址是:http://www.elecm.com/a/read-htm-tid-103023-fpage-6.html