midterm4.c 2.11 KB
#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>

const int FndSelectPin[6] = { 4, 17, 18, 27, 22, 23 };
const int FndPin[8] = { 6, 12, 13, 16, 19, 20, 26, 21 };
const int FndFont[10] = { 0x3F, 0x06, 0x5B, 0x4F, 0x66,
			  0x6D, 0x7D, 0x07, 0x7F, 0x67 };
const int Keypad[3] = { 11,9,10 };
int time = 0;
int state = 2;

void FndSelect(int position){
	int i;
	for(i=0;i<6;i++){
		if(i==position){
			digitalWrite(FndSelectPin[i],LOW);
		}
		else{
			digitalWrite(FndSelectPin[i],HIGH);
		}
	}
}
void FndDisplay(int position, int num){
	int i,number;
	int flag=0;
	int shift=0x01;
	number=FndFont[num];
	if(position==2)
		number|=0x80;

	for(i=0;i<8;i++){
		flag=(number&shift);
		digitalWrite(FndPin[i], flag);
		shift<<=1;
	}
	FndSelect(position);
}
void timer() {
	int pos;
	for (pos = 0; pos < 6; pos++) {
		FndDisplay(pos, data[pos]);
		delay(1);
	}
}
void start() {
	state = 0;
}
void stop() {
	state = 1;
}
void reset() {
	time = 0;
	int i;
	for (i = 0; i < 6; i++)
		data[i] = 0;
	state = 2;
	timer();
}
void init() {
	int i;
	if (wiringPiSetupGpio() == -1) {
		printf("wiringPiSetupGpio() error\n");
		exit(-1);
	}
	for (i = 0; i < 6; i++) {
		pinMode(FndSelectPin[i], OUTPUT);
		digitalWrite(FndSelectPin[i], HIGH);
	}
	for (i = 0; i < 8; i++) {
		pinMode(FndPin[i], OUTPUT);
		digitalWrite(FndPin[i], LOW);
	}
	for (i = 0; i < 3; i++)
		pinMode(Keypad[i], INPUT);
}
int main(){
	init();
	if (wiringPiISR(Keypad[0], INT_EDGE_FALLING, &start) < 0) {
		return 1;
	}
	if (wiringPiISR(Keypad[1], INT_EDGE_FALLING, &stop) < 0) {
		return 1;
	}
	if (wiringPiISR(Keypad[2], INT_EDGE_FALLING, &reset) < 0) {
		return 1;
	}
	while (1) {
		if (state == 0) {
			unsigned long prevtime = millis();
			while (1) {
				if (state > 0) break;
				unsigned long curtime = millis();
				if (curtime - prevtime >= 9) {
					prevtime = curtime;
					time++;
				}
				data[0] = time % 10;
				data[1] = (time % 100) / 10;
				data[2] = (time % 1000) / 100;
				data[3] = (time / 1000) % 10;
				data[4] = (time / 10000) % 10;
				data[5] = time / 100000;
				timer();
			}
		}
		else {
			while (1) {
				if (state == 0) break;
				timer();
			}
		}
	}
	return 0;
}