分享

单片机按键按下的先后顺序判断

 飞往天际 2019-01-28
http://www./mcu/article_2018050839098.html

K4、K3:“进出”模拟检测键。
每先按K3、再按K4,即代表“进”一人次;
每先按K4、再按K3,则代表“出”一人次。
每“进”一人次,就在原“进”显示基础上加1(“进”显示初始状态为“b000”);
每“出”一人次,就在原“出”显示基础上加1(“出”显示初始状态为“C000”)。 
悬赏分:100 - 解决时间:2010-6-25 19:14 
最好把C语言程序给出来~

按照题目,编写了程序。用 PROTEUS 软件仿真截图如下。

原来编写的程序,是使用汇编语言,现按照要求改成了C语言。
//================================================================
#include
#define uint unsigned int
#define uchar unsigned char
sbit K3 = P3^2;
sbit K4 = P3^3;
uchar code table[] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,
//                       0     1     2     3     4     5     6     7     8     9 
                       0x88,0x83,0xc6,0xa1,0x86,0x8e,0xc2,0x89,0xff};
//                       A     b     C     d     E     F     G     H    [ ]
uchar DispD[] = {0,0,0,11,0,0,0,12};
uchar DispW[] = {0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};
//------------------------------------
void delay(uint z)
{
     uint x, y;
     for(x = z; x > 0; x--) for(y = 110; y > 0; y--);
}
//------------------------------------
void display(void)
{
     uint i;
     for (i = 0; i < 8; i++) {
       P2 = 0;
       P0 = table[DispD[i]];
       P2 = DispW[i];
       delay(3);
     } 
}
//------------------------------------
void main()
{
     uint in_num = 0, outnum = 0;
     while(1) {
       display();
//--------------------------
       if (K3 == 0) {
         display();
         if (K3 == 0) {
           while(K3 == 0) display();
           while(K4 == 1) display();
           in_num++;
           DispD[0] = in_num % 10;
           DispD[1] = in_num / 10 % 10;
           DispD[2] = in_num / 100;
           while(K4 == 0) display();
         }
       }
//--------------------------
       if (K4 == 0) {
         display();
         if (K4 == 0) {
           while(K4 == 0) display();
           while(K3 == 1) display();
           outnum++;
           DispD[4] = outnum % 10;
           DispD[5] = outnum / 10 % 10;
           DispD[6] = outnum / 100;
           while(K3 == 0) display();
         }
       }
//--------------------------
     }

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约