#include void delay(unsigned int t); unsigned char buffer; sbit PB1 = P3^2; //設定PB1代表P3.2 sbit PB2 = P3^3; //設定PB1代表P3.3 void main(void) { buffer = 0; //設定buffer初值為0 while(1) { while(PB1 == 0) //檢查PB1是否為0 { delay(3); //延遲, 判斷是否為彈跳訊號 if (PB1 == 0) //如果PB1仍然為0, 則 { buffer++; //buffer內的值加1 P2 = ~buffer; //將buffer內的值反向送到P2顯示 } while(PB1 == 0); //如果PB1仍然為0, 則停在此行,直到放開 } while(PB2 == 0) //檢查PB2是否為0 { delay(3); if (PB2 == 0) { buffer--; //buffer內的值減1 P2 = ~buffer; //將buffer內的值反向送到P2顯示 } while(PB2 == 0); } } } void delay(unsigned int t) { unsigned int i; while(t--) { i=10000; while(i--); } }