标签:
前不久看完了矩阵键盘和数码管的应用,自己动手花了一天时间写了一个简单的计算器
。
下面贴上源代码,大家可以测试一下。
上面4*4的矩阵键盘,作用分别是:
1 2 3 +
4 5 6 —
7 8 9 *
on/c 0 = /
其中矩阵键盘的部分基本是照着书本的代码改的,然后自己做了部分修改。
由于是直接用 int 变量做的计算,而如果用 long 变量的话又要用到大数的运算,所以只能计算小于65536的数。
计算的步骤是这样的:
(主操作数)(操作符)(被操作数)(=)
//main.c #include<reg52.h> #include"involve.h" uint num = 0; //主操作数 uint temp_num = 0; //被操作的数 uint show = 0; //用于显示的数 uint mode = 0; //当前计算器处于的操作状态(操纵符) //‘+‘是1,‘-‘是2,‘*‘是3,‘/‘是4,‘=‘是5,之前无操纵状态则是0.
void main() { while(1) //不停地扫描键盘和展示当前状态 { keyscan(); display(show); } }
1 //keyscan.c 2 #include"involve.h" 3 extern num; 4 extern temp_num; 5 extern show; 6 extern mode; 7 void keyscan() //键盘扫描程序 8 { 9 uchar temp; 10 int key; 11 P3 = 0xfe; 12 temp = P3; 13 temp = temp&0xf0; 14 if(temp!=0xf0) 15 { 16 delay(10); 17 temp = P3; 18 temp = temp&0xf0; 19 if(temp!=0xf0) 20 { 21 temp = P3; 22 switch(temp) 23 { 24 case 0xee: 25 key = 1; 26 break; 27 case 0xde: 28 key = 2; 29 break; 30 case 0xbe: 31 key = 3; 32 break; 33 case 0x7e: 34 key = 10; 35 if(mode==5) {temp_num = 0; } //如果之前是等号,被操作数重新置为0 36 mode = 1; 37 break; 38 } 39 40 while(temp!=0xf0) 41 { 42 temp = P3; 43 temp = temp&0xf0; 44 45 } 46 47 if(key>=0&&key<=9) //无操纵状态则将操作数输入,有操纵状态则将被操纵数输入。 48 if(mode==0) 49 { 50 num = num*10 + key;show = num; 51 } 52 else 53 { 54 temp_num = temp_num*10 + key;show = temp_num; 55 } 56 57 } 58 } 59 60 P3 = 0xfd; 61 temp = P3; 62 temp = temp&0xf0; 63 if(temp!=0xf0) 64 { 65 delay(10); 66 temp = P3; 67 temp = temp&0xf0; 68 if(temp!=0xf0) 69 { 70 temp = P3; 71 switch(temp) 72 { 73 case 0xed: 74 key = 4; 75 break; 76 case 0xdd: 77 key = 5; 78 break; 79 case 0xbd: 80 key = 6; 81 break; 82 case 0x7d: 83 key = 11; 84 if(mode==5) {temp_num = 0; } 85 mode = 2; 86 break; 87 } 88 89 while(temp!=0xf0) 90 { 91 temp = P3; 92 temp = temp&0xf0; 93 94 } 95 if(key>=0&&key<=9) 96 if(mode==0) 97 { 98 num = num*10 + key;show = num; 99 } 100 else 101 { 102 temp_num = temp_num*10 + key;show = temp_num; 103 } 104 } 105 } 106 107 P3 = 0xfb; 108 temp = P3; 109 temp = temp&0xf0; 110 if(temp!=0xf0) 111 { 112 delay(10); 113 temp = P3; 114 temp = temp&0xf0; 115 if(temp!=0xf0) 116 { 117 temp = P3; 118 switch(temp) 119 { 120 case 0xeb: 121 key = 7; 122 break; 123 case 0xdb: 124 key = 8; 125 break; 126 case 0xbb: 127 key = 9; 128 break; 129 case 0x7b: 130 key = 12; 131 if(mode==5) {temp_num = 0; } 132 mode = 3; 133 break; 134 } 135 136 while(temp!=0xf0) 137 { 138 temp = P3; 139 temp = temp&0xf0; 140 141 } 142 if(key>=0&&key<=9) 143 if(mode==0) 144 { 145 num = num*10 + key;show = num; 146 } 147 else 148 { 149 temp_num = temp_num*10 + key; show = temp_num; 150 } 151 } 152 } 153 154 P3 = 0xf7; 155 temp = P3; 156 temp = temp&0xf0; 157 if(temp!=0xf0) 158 { 159 delay(10); 160 temp = P3; 161 temp = temp&0xf0; 162 if(temp!=0xf0) 163 { 164 temp = P3; 165 switch(temp) 166 { 167 case 0xe7: 168 key = 15;initial(); //如果按下on/c键,所有数据都被置为0. 169 break; 170 case 0xd7: 171 key = 0; 172 break; 173 case 0xb7: 174 key = 14;calculate(mode); //按下‘=‘号,开始计算。 175 mode = 5; 176 break; 177 case 0x77: 178 key = 13; 179 if(mode==5) {temp_num = 0; } 180 mode = 4; 181 break; 182 } 183 184 while(temp!=0xf0) 185 { 186 temp = P3; 187 temp = temp&0xf0; 188 189 } 190 if(key>=0&&key<=9) 191 if(mode==0) 192 { 193 num = num*10 + key;show = num; 194 } 195 else 196 { 197 temp_num = temp_num*10 + key;show = temp_num; 198 } 199 } 200 } 201 }
//function.c
//各种函数的定义都在这儿。
#include"involve.h" extern num; extern temp_num; extern show; extern mode; void delay(uint xms) { uint i,j; for(i=0;i<xms;i++) for(j=0;j<110;j++); } void initial() { num = 0; temp_num = 0; mode = 0; show = 0; } void calculate(int x) { switch(x) { case 1: show = num + temp_num;break; case 2: show = num - temp_num;break; case 3: show = num * temp_num; break; case 4: if(temp_num==0) show = 1000000; //之前以为long变量也可以进行四则运算,就想用一个7位数(数码管是6位),来表示被除数是 0 的错误状态。现在开来多次一举了 else show = num / temp_num; } display(show); num = show; }
//involve.h
//各种申明。
1 #include<reg52.h> 2 #define uint unsigned int 3 #define uchar unsigned char 4 5 sbit dula = P2^6; 6 sbit wela = P2^7; 7 8 void delay(uint); 9 void keyscan(); 10 void display(unsigned long); 11 void calculate(int); 12 void initial();
//display.c
//数码管显示程序。
1 #include<reg52.h> 2 #include"involve.h" 3 uchar code table[]={ 4 0x3f,0x06,0x5b,0x4f, 5 0x66,0x6d,0x7d,0x07, 6 0x7f,0x6f,0x77,0x7c, 7 0x39,0x5e,0x79,0x71 8 }; 9 void display(unsigned long int x) 10 { 11 uint temp; 12 int ge,shi,bai,qian,wan,shiwan; 13 if(x==1000000)
//之前以为long变量也可以进行四则运算,就想用一个7位数(数码管是6位),来表示被除数是 0 的错误状态。现在开来多次一举了。 14 { 15 16 } 17 else 18 { 19 ge = x%10; 20 shi = x%100/10; 21 bai = x%1000/100; 22 qian = x%10000/1000; 23 wan = x%100000/10000; 24 shiwan = x%1000000/100000; 25 26 P0 = table[ge]; 27 dula = 1; 28 dula = 0; 29 P0 = 0xff; 30 wela = 1; 31 P0 = 0xdf; 32 wela = 0; 33 //delay(5); 34 35 temp = x/10; 36 P0 = table[shi]; 37 dula = 1; 38 dula = 0; 39 P0 = 0xff; 40 wela = 1; 41 if(temp) P0 = 0xef; 42 wela = 0; 43 //delay(5); 44 45 46 temp = x/100; 47 P0 = table[bai]; 48 dula = 1; 49 dula = 0; 50 P0 = 0xff; 51 wela = 1; 52 if(temp) P0 = 0xf7; 53 wela = 0; 54 //delay(5); 55 56 temp = x/1000; 57 P0 = table[qian]; 58 dula = 1; 59 dula = 0; 60 P0 = 0xff; 61 wela = 1; 62 if(temp) P0 = 0xfb; 63 wela = 0; 64 //delay(5); 65 66 temp = x/10000; 67 P0 = table[wan]; 68 dula = 1; 69 dula = 0; 70 P0 = 0xff; 71 wela = 1; 72 if(temp) P0 = 0xfd; 73 wela = 0; 74 //delay(5); 75 76 77 temp = x/100000; 78 P0 = table[shiwan]; 79 dula = 1; 80 dula = 0; 81 P0 = 0xff; 82 wela = 1; 83 if(temp) P0 = 0xfe; 84 wela = 0; 85 //delay(5); 86 } 87 }
好了程序到这就结束了,下载生成hex文件试试效果吧!
标签:
原文地址:http://www.cnblogs.com/fudianheg/p/4290859.html