标题: 请教5个单片机IO口和数组设置问题 [打印本页]

作者: yong761228    时间: 2018-12-5 21:33
标题: 请教5个单片机IO口和数组设置问题
各位师傅,您们好
我有P1口做按钮,用到P1.0,P1.1,P1.2,P1.3,P1.4 做按钮,支持复合按钮,就是多个按钮可以同时按下。对应数组
uint data yanshi[32]={0,3,5,10,20,30,60,120,180,240,300,360,480,600,900,1200,1500,1800,2700,3600,4200,
             4800,5400,6000,6600,7200,7800,8400,9000,9600,10200,10800};  //延时秒数


按钮和数组对应程序如下:
     uchar temp;
        temp=P1;
        temp=~temp;
        temp=temp&0x1F;
        YS=yanshi[temp];

现在的问题,如果YS指定的某个,对应参数正确,例如YS=yanshi[5];延时就是20秒,但是用按钮的程序YS=yanshi[temp]不稳定,有时候20秒,有时候40秒才停,就是多跑了几次,检测问题应该出现在上面的读键盘程序。希望师傅们帮我看看怎样编写,我学单片机时间比较短,水平有限,非常感谢。


作者: yong761228    时间: 2018-12-5 22:56
我已经找到解决方法了,大家可以参考https://blog.csdn.net/sdnu2011d1/article/details/78268082, 高手就是高手,简单程序就解决了。
unsigned char Trg;
unsigned char Cont;
void KeyRead( void )
{
    unsigned char ReadData = P1^0xff;   // 1
    Trg = ReadData & (ReadData ^ Cont);      // 2
    Cont = ReadData;                                // 3
}
作者: hello_liu    时间: 2018-12-6 09:04
unsigned char Trg;
unsigned char Cont;
void KeyRead( void )
{
    unsigned char ReadData = P1^0xff;   // 1
    Trg = ReadData & (ReadData ^ Cont);      // 2
    Cont = ReadData;                                // 3
}




欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1