標(biāo)題: 下面單片機(jī)程序中while((s5==0)&&(count<500000)) 怎么理解 [打印本頁(yè)]

作者: 陌路狂花    時(shí)間: 2018-3-12 19:42
標(biāo)題: 下面單片機(jī)程序中while((s5==0)&&(count<500000)) 怎么理解
#include "button.h"
#include "reg51.h"
sbit s4=P3^3;
sbit s5=P3^2;
sbit s6=P3^1;
sbit s7=P3^0;

void delay_button(unsigned char m)
{
         unsigned char n;
         for(m;m>0;m--)
         {
                 for(n=250;n>0;n--)
                {}
         }
}
unsigned char keyboard()
{
     unsigned char button_flag=0;
         int count=0;
         if(s4==0)
         {
                   delay_button(10);
                  if(s4==0)
                  {
                          while((s4==0)&&(count<500000))
                          {
                                button_flag=4;
                                count++;
                          }
                  }
         }
               
         if(s5==0)
         {
                   delay_button(10);
                  if(s5==0)
                  {
                           while((s5==0)&&(count<500000))
                           {
                           button_flag=5;
                           count++;
                           }
                  }
         }
         
         if(s6==0)
         {
                    delay_button(10);
                   if(s6==0)
                   {
                           while((s6==0)&&(count<500000))
                           {
                                 button_flag=6;
                                 count++;
                           }
                         }
          }
               
         if(s7==0)
         {
                    delay_button(10);
                   if(s7==0)
                   {
                           while((s7==0)&&(count<500000))
                           {
                                 button_flag=7;
                                 count++;
                           }
                   }
         }

         if(count>500000)
         count=0;
         return button_flag;
}



作者: huangkeke    時(shí)間: 2018-3-12 20:16
當(dāng)s5的值為0并且count的值小于500000這兩個(gè)條件都為真,進(jìn)入while()循環(huán);否則不進(jìn)入while()循環(huán)。
作者: huangkeke    時(shí)間: 2018-3-12 20:18
s5的值為0并且count<500000兩個(gè)條件都為真的時(shí)候進(jìn)入While()循環(huán)
作者: 藍(lán)廊坊市    時(shí)間: 2018-3-12 21:24
&&是按位與   (s5==0)&&(count<500000)同時(shí)為真時(shí)執(zhí)行
作者: 陌路狂花    時(shí)間: 2018-3-13 08:13
為什么count<500000,而不是其他
作者: HC6800-ES-V2.0    時(shí)間: 2018-3-13 10:26
這條語(yǔ)句就是松鍵消抖的功能。
鍵被按下就等于零(S1==0),后面就是控制按鍵的時(shí)間的(count<50000)。
這兩個(gè)條件同時(shí)滿足,說明鍵是按著的,而且時(shí)間不是很長(zhǎng)。
如果松開鍵,或者按鍵時(shí)間太長(zhǎng),都會(huì)跳出循環(huán),避免程序死在這里。

個(gè)人認(rèn)為,這樣的程序不好。
對(duì)于像你的程序,這樣的簡(jiǎn)單按鍵功能,還可以使用,看不問題,但如果按鍵功能復(fù)雜一點(diǎn),那么,就會(huì)在按下鍵不松的一段時(shí)間內(nèi),程序會(huì)讓那個(gè)功能重復(fù)很多次(事實(shí)上,你的程序已經(jīng)重復(fù)了很多次,最大可以重復(fù)49999次,只是功能簡(jiǎn)單,眼睛看不出來而已)。
作者: shuisheng    時(shí)間: 2019-1-30 21:16
while((s5==0)&&(count<500000))
分解來分析:(s5==0)&&(count<500000)
有三個(gè)結(jié)果1.(1&&1)// (s5==0是真)&&(count<500000也是真)
               2.(0&&1)// (s5==0是假)&&(count<500000是真)
               3.(1&&0)// (s5==0是真)&&(count<500000是假)
最終while(中只有1或者0),     當(dāng)while(1時(shí))執(zhí)行button_flag=5;count++; 否則就跳




歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1