标签:
在实验板上实现一个简单的计算器,要求在实验板的命令行运行程序,输入两个100以内的数字以及运算符 (+,-,*,/),然后计算结果并在数码管上显示计算结果。
电路设计如下:
实物连接:
代码如下:
#include <wiringPi.h> #include <stdio.h> #include <stdlib.h> #define DIGIT1 9 #define DIGIT2 13 #define DIGIT3 12 #define DIGIT4 8 #define BTN0 10 #define BTN1 11 #define true 1 char digit[10][8] = //the loop-running bar in the left windows { {0,0,0,1,0,0,1,0},//0 {1,0,0,1,1,1,1,1},//1 {0,0,1,1,0,1,0,0},//2 {0,0,0,1,1,1,0,0},//3 {1,0,0,1,1,0,0,1},//4 {0,1,0,1,1,0,0,0},//5 {1,1,0,1,0,0,0,0},//6 {0,0,0,1,1,1,1,1},//7 {0,0,0,1,0,0,0,0},//8 {0,0,0,1,1,0,0,0}//9 }; char sign[8]={1,1,1,1,1,1,0,1};//- char nosign[8]={1,1,1,1,1,1,1,1}; void main() { int pin; //int m = 0, n = 0; //int flag = 1; int chip = 0; int chip0,chip1,chip2,chip3; int run = 0; unsigned int time0 = 0, time1 = 0; int a,b; char c; int result; int num1,num2; int negative = 0; int iteration = 500; int i; if (wiringPiSetup () == -1) //test the install status of wiringPi { exit (1) ; } for (pin = 0 ; pin < 8 ; ++pin) { pinMode (pin, OUTPUT) ; digitalWrite(pin, HIGH); } pinMode(DIGIT1, OUTPUT); pinMode(DIGIT2, OUTPUT); pinMode(DIGIT3, OUTPUT); pinMode(DIGIT4, OUTPUT); // pinMode(BTN0, INPUT); //The start button pinMode(BTN1, INPUT); //The stop/stop button while (true) { scanf("%d%c%d",&a,&c,&b); switch(c){ case ‘+‘:result = a+b; break; case ‘-‘:result = a-b; break; case ‘*‘:result = a*b; break; case ‘/‘:result = a/b; break; } printf("%d\n",result); if(result<0){ negative=1; result = -result; } else negative=0; result = result%100; //only display 2-digit num1 = result/10; num2 = result%10; //printf("%d %d\n",num1,num2); for(i=0;i<iteration;i++){ if(i%3==0){ if(negative){ for (pin = 0; pin < 8; pin++) { digitalWrite(pin, sign[pin]); } } else{ for (pin = 0; pin < 8; pin++) { digitalWrite(pin, nosign[pin]); } } digitalWrite(DIGIT1,1); } else if(i%3==1){ for (pin = 0; pin < 8; pin++) { digitalWrite(pin, digit[num1][pin]); } digitalWrite(DIGIT2,1); } else{ for (pin = 0; pin < 8; pin++) { digitalWrite(pin, digit[num2][pin]); } digitalWrite(DIGIT3,1); } delay(10); digitalWrite(DIGIT1,0); digitalWrite(DIGIT2,0); digitalWrite(DIGIT3,0); } } }
功能说明:支持基本的四则运算,使用前三个数码管显示结果,第一个显示符号,如果结果为正,不显示,如果为负,显示“-”,每个计算结果会在数码管上显示5秒左右,当数码管熄灭后可以进行下一次的运算,结果需要三位数显示的话会对100进行取余后进行显示。
运行,输入算术表达式进行验证
标签:
原文地址:http://www.cnblogs.com/easy-busy/p/4612155.html