思路如下,請參考。
- void main(void) {
- int timerCount[5]={0,0,0,0,0}; //5個計時器
- char timerPtr; //計時器指針
- char timerShowPtr; //計時器顯示指針
- char timerStat; //計時狀態(tài) 1 計時 0 停止計時
- timerPtr=0; //初始狀態(tài)為全零
- timerShowPtr=0;
- timerStat=0;
- while (1) {
- if (timerStat==1) {
- timerCount[timerPtr]++;
- }
- else {
- 顯示當前計時器 timerCount[timerShowPtr];
- }
- }
- }
- void exint0() interrupt 0 { //按鍵1中斷
- timerStat=1-timerStat; //計時和停止計時切換
- }
- void exint1() interrupt 2 { //按鍵2中斷
- if (timerStat==0) { //停止計時中
- timerShowPtr++;
- if (timerShowPtr>=5) timerShowPtr=0;
- }
- else { //計時中
- timerPtr++;
- if (timerPtr>=5) timerPtr=0;
- timeCount[timerPtr]=0;
- }
- }
復制代碼 |