#include "monitor.h" #define PA_DDR (*(volatile unsigned char *)0xFFFFD1) #define PA_DR (*(volatile unsigned char *)0xFFFFD3) #define PB_DDR (*(volatile unsigned char *)0xFFFFD4) #define PB_DR (*(volatile unsigned char *)0xFFFFD6) #define PRINTF(a) { printf(a); } #define S_WAIT 0 // 待機中 #define S_MASURE 1 // 計測中 #define S_PAUSE 2 // 一時停止 #define TRUE 1 #define FALSE 0 int Time; int Measure; void init_time() { Time = 0; Measure = 0; } void disp_time() { printf("%d\n", Time); } void measure_time() { long int w; if (Measure) { for(w=0; w<999999;w++); Time++; printf("%d\n", Time); } } void start_time() { Measure = TRUE; } void stop_time() { Measure = FALSE; } int main() { unsigned int state; // 状態変数を宣言 state = S_WAIT; // 初期状態を定義 PA_DDR = 0; PB_DDR = 0xFF; while (1) // 状態を無限に回るので。 { switch (state) // switch文により、全ての状態を列記する。 { case S_WAIT : if (!(PA_DR & 0x1)) //(スタートボタンが押された) { state = S_MASURE; PRINTF("start"); init_time(); } break; case S_MASURE : if (!(PA_DR & 0x2)) //(ストップボタンが押された) { state = S_PAUSE; stop_time(); // 計測を一時停止 PRINTF("stop"); disp_time(); } else { start_time(); // 時間を計測 } break; case S_PAUSE : if (!(PA_DR & 0x1)) // (スタートボタンが押された) { state = S_MASURE; } else { if (!(PA_DR & 0x4)) // (リセットボタンが押された) { state = S_WAIT; stop_time(); init_time(); } } break; default : // 必ずしも必要ない。ただ、万が一、誤動作 state = S_WAIT; // が起こった場合にシステムの信頼性が増す。 break; } measure_time(); } return 0; }