独闷闷网

 找回密码
 立即注册
搜索
查看: 3496|回复: 3
收起左侧

[求助] 学习第9节双按键触发哪个地方出错了,为什么过不去呢

[复制链接]
发表于 2014-9-25 23:54:38 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
本人是个初学,学到老师第9节双击按键触发,双击可以实现,可是按键1时间大于间隔时间会重新计数,按键2时间间隔无论多久都可以形成双击键,不是应该间隔时间大于设定应该重新来吗?程序贴上来大家帮忙找找,谢谢!



#include "reg52.h"

#define const_voice_short 40

#define const_key_time1 20
#define const_key_time2 20

#define const_interval_time1 200
#define const_interval_time2 200

void initial_myself();
void initial_peripheral();
void delay_long(unsigned int uiDelaylong);
void T0_time();
void key_service();
void key_scan();

sbit key_sr1=P3^3;
sbit key_sr2=P3^2;
sbit key_gnd_dr=P3^4;

sbit beep_dr=P2^0;

unsigned char ucKeySec=0;

unsigned int uiKeyTimeCnt1=0;
unsigned char ucKeyLock1=0;
unsigned char ucKeyTouchCnt1=0;
unsigned int uiKeyIntervalCnt1=0;

unsigned int uiKeyTimeCnt2=0;
unsigned char ucKeyLock2=0;
unsigned char ucKeyTouchCnt2=0;
unsigned int uiKeyIntervalCnt2=0;

unsigned int uiVoiceCnt=0;

char code dx516[3] _at_ 0x003b;

void main()
{
        initial_myself();
        delay_long(100);
        initial_peripheral();
        while(1)
        {
                key_service();
        }
}

void key_scan()
{
        if(key_sr1==1)
        {
                ucKeyLock1=0;
                uiKeyTimeCnt1=0;
                if(ucKeyTouchCnt1>0)
                {
                        uiKeyIntervalCnt1++;
                        if(uiKeyIntervalCnt1>const_interval_time1)
                        {
                                uiKeyIntervalCnt1=0;
                                ucKeyTouchCnt1=0;
                        }
                }
        }
        else if(ucKeyLock1==0)
        {
                uiKeyTimeCnt1++;
                if(uiKeyTimeCnt1>const_key_time1)
                {
                        uiKeyTimeCnt1=0;
                        ucKeyLock1=1;
                        uiKeyIntervalCnt1=0;

                        ucKeyTouchCnt1++;
                        if(ucKeyTouchCnt1>1)
                        {
                                ucKeyTouchCnt1=0;
                                ucKeySec=1;
                        }
                }
        }
        if(key_sr2==1)
        {
                ucKeyLock2=0;
                uiKeyTimeCnt2=0;
                if(ucKeyTouchCnt2>0)
                {
                        uiKeyIntervalCnt2++;
                        if(ucKeyTouchCnt2>const_interval_time2)
                        {
                                uiKeyIntervalCnt2=0;
                                ucKeyTouchCnt2=0;
                        }
                }
        }
        else if(ucKeyLock2==0)
        {
                uiKeyTimeCnt2++;
                if(uiKeyTimeCnt2>const_key_time2)
                {       
                        uiKeyTimeCnt2=0;
                        ucKeyLock2=1;
                        uiKeyIntervalCnt2=0;

                        ucKeyTouchCnt2++;
                        if(ucKeyTouchCnt2>1)
                        {
                                ucKeyTouchCnt2=0;
                                ucKeySec=2;
                        }
                }
        }
}

void key_service()
{
        switch(ucKeySec)
        {
                case 1:
                    uiVoiceCnt=const_voice_short;
                        ucKeySec=0;
                        break;
                case 2:
                        uiVoiceCnt=const_voice_short;
                        ucKeySec=0;
                        break;
        }
}

void T0_time() interrupt 1
{
        TF0=0;
        TR0=0;

        key_scan();
       
        if(uiVoiceCnt!=0)
        {
                uiVoiceCnt--;
                beep_dr=0;
        }
        else
        {
                ;
                beep_dr=1;
        }
       
        TH0=0xf8;
        TL0=0x2f;
        TR0=1;
}



void delay_long(unsigned int uiDelaylong)
{
        unsigned int i;
        unsigned int j;
        for(i=0;i<uiDelaylong;i++)
        {
                for(j=0;j<500;j++)
                {
                        ;
                }
        }
}


void initial_myself()
{
        key_gnd_dr=0;
        beep_dr=1;

        TMOD=0x01;
        TH0=0xf8;
        TL0=0x2f;
}

void initial_peripheral()
{
        EA=1;
        ET0=1;
        TR0=1;
}

乐于分享,勇于质疑!
 楼主| 发表于 2014-9-26 00:16:07 | 显示全部楼层
重新写了一遍就好了,这是什么问题????没看出来有什么区别呀?
乐于分享,勇于质疑!
发表于 2014-9-26 10:19:24 | 显示全部楼层
trm999 发表于 2014-9-26 00:16
重新写了一遍就好了,这是什么问题????没看出来有什么区别呀?

问题解决了吧?解决了就好。
乐于分享,勇于质疑!
 楼主| 发表于 2014-9-26 11:56:00 | 显示全部楼层
jianhong_wu 发表于 2014-9-26 10:19
问题解决了吧?解决了就好。

谢谢鸿哥关注
乐于分享,勇于质疑!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|独闷闷网 ( 粤ICP备12007667号-2 )

GMT+8, 2024-3-28 20:00 , Processed in 1.049189 second(s), 17 queries .

快速回复 返回顶部 返回列表