本帖最后由 xyz549040622 于 2014-1-7 17:12 编辑
实际使用过程中发现这么一个问题,有两段代码
- switch(Read_KeyBoard())
- {
- case 0x40:
- Key_ESC_Value = 1;
- SendStr("ESC! \r\n");
- break;
- case 0x80:
- Key_OK_Value = 1;
- SendStr("OK! \r\n");
- break;
- default : break;
- }
- if((Read_KeyBoard()) == 0x40)
- {
- Key_ESC_Value = 1;
- SendStr("ESC! \r\n");
- }
- if((Read_KeyBoard()) == 0x80)
- {
- Key_OK_Value = 1;
- SendStr("OK! \r\n");
从理论上说,两段代码达到的效果是一样的,可是实际中,如果用下面的代码,第二个if就进不去,只会执行第一个if,这两段代码有什么区别吗?
- UINT8U Read_KeyBoard()
- {
- static UINT8U Key_State = 0,Key_Value,Key_Hold_Time;//
- UINT8U Key_Return_Value = No_Press;
- switch(Key_State)
- {
- case 0: //×î³õµÄ״̬£¬°´¼üɨÃè
- // SendStr("Press is no!");
- Key_Value = Key_PortIn;//
- if(Key_Value != No_Press)//
- {
- Key_State++;//
- }
- break;
- case 1:
- // SendStr("Press is 1 \n");
- if(Key_Value == Key_PortIn)//
- {
- Key_State++;//
- Key_Hold_Time = 0;
- }
- else
- Key_State--;//
- break;
- case 2:
- if(Key_PortIn == No_Press)//
- {
- // SendStr("Press is off \n");
- Key_Return_Value = Key_Value;//µ
- Key_State = 0;//
- SendByte(Key_Return_Value);
- // TR0 = 0;
- }
- break;
- default : break;
- }
- return Key_Return_Value;
- }
|