38k遥控器
全部代码下载:
38k红外遥控器(用).rar
(14.75 KB, 下载次数: 12)
单片机计算器程序 数码管显示:
- #include<reg51.h>
- #include<math.h>
- #define uchar unsigned char
- #define uint unsigned int
- #define ulong unsigned long
- uchar code tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x06,0xbf};//共阳极
- ulong num=0,qnum=0;
- uchar key0=0,ikey0=0,kkey=0; //qnum为前一个键值,当为0时表示没有前一个键值,ikey0表示当存在前一个键值下进行加减乘除运算
- //uchar num1,num2,num3,num4,num5,num6; //key0表示当为1时,是数字键,为0时,是清零键,为2时,是运算键,为3时,等号键
- void delay(uint d)
- {
- while(d-->0);
- }
- void error() //当数值溢出的时候
- {
- P2 = 0x00;
- P2 = 0xff;
- P0 = tab[10];
- delay(100);
- P2 = 0x00;
- P0 = 0xff;
- }
- void display(ulong num0) //显示函数
- {
- if(num0>999999&&num0<-99999) //当数值溢出时
- error();
- else if(num0>=0&&num0<=999999) //当数值为正数时
- {
- P2 = 0x00;
- dis1 = 1; //显示最高位,第一位
- P0 = tab[num0/100000];
- delay(100);
- dis1 = 0;
- P0 = 0xff;
-
- P2 = 0x00;
- dis2 = 1; //第二位
- P0 = tab[(num0/10000)%10];
- delay(100);
- dis2 = 0;
- P0 = 0xff;
-
-
- P2 = 0x00;
- dis3 = 1; //第三位
- P0 = tab[(num0/1000)%10];
- delay(100);
- dis3 = 0;
- P0 = 0xff;
-
- P2 = 0x00;
- dis4 = 1; //第四位
- P0 = tab[(num0/100)%10];
- delay(100);
- dis4 = 0;
- P0 = 0xff;
-
- P2 = 0x00;
- dis5 = 1; //第五位
- P0 = tab[(num0/10)%10];
- delay(100);
- dis5 = 0;
- P0 = 0xff;
-
- P2 = 0x00;
- dis6 = 1; //显示最低位,第六位
- P0 = tab[num0%10];
- delay(100);
- dis6 = 0;
- P0 = 0xff;
- }
- else //当数值为负数时
- {
- num0 = abs(num0);
- P2 = 0x00;
- dis1 = 1; //显示最高位,第一位
- P0 = tab[11];
- delay(100);
- dis1 = 0;
- P0 = 0xff;
-
- P2 = 0x00;
- dis2 = 1; //第二位
- P0 = tab[(num0/10000)%10];
- delay(100);
- dis2 = 0;
- P0 = 0xff;
-
-
- P2 = 0x00;
- dis3 = 1; //第三位
- P0 = tab[(num0/1000)%10];
- delay(100);
- dis3 = 0;
- P0 = 0xff;
-
- P2 = 0x00;
- dis4 = 1; //第四位
- P0 = tab[(num0/100)%10];
- delay(100);
- dis4 = 0;
- P0 = 0xff;
-
- P2 = 0x00;
- dis5 = 1; //第五位
- P0 = tab[(num0/10)%10];
- delay(100);
- dis5 = 0;
- P0 = 0xff;
-
- P2 = 0x00;
- dis6 = 1; //显示最低位,第六位
- P0 = tab[num0%10];
- delay(100);
- dis6 = 0;
- P0 = 0xff;
- }
- }
- void key() //键盘扫描函数
- {
- uchar temp;
- P1 = 0xff;
- P1 = 0Xef; //检测第一行
- temp = P1;
- temp = temp&0x0f;
- if(P1!=0X0f)
- {
- delay(5);
- temp = P1;
- temp = temp&0x0f;
- if(P1!=0X0f)
- {
- temp = P1;
- switch(temp)
- {
- case 0xe7: key0 = 0; break; //-----清屏键
- case 0xeb: key0 = 1; num = num*10+0; kkey = 1; break; //-------'0'
- case 0xed: key0 = 3; break; //-----等号键=
- case 0xee: key0 = 2; ikey0 = 10; break; //------加号键+
- }
-
- while(temp!=0x0f)
- {
- temp = P1;
- temp = temp&0x0f;
- }
- //P0 = tab[h];
- }
- }
- P1 = 0xff;
- P1 = 0Xdf; //检测第二行
- temp = P1;
- temp = temp&0x0f;
- if(P1!=0X0f)
- {
- delay(5);
- temp = P1;
- temp = temp&0x0f;
- if(P1!=0X0f)
- {
- temp = P1;
- switch(temp)
- {
- case 0xd7: key0 = 1; num = num*10+1; kkey = 1; break; //------'1'
- case 0xdb: key0 = 1; num = num*10+2; kkey = 1; break; //------'2'
- case 0xdd: key0 = 1; num = num*10+3; kkey = 1; break; //-------'3'
- case 0xde: key0 = 2; ikey0 = 11; break; //-----减号键-
- }
-
- while(temp!=0x0f)
- {
- temp = P1;
- temp = temp&0x0f;
- }
- //P0 = tab[h];
- }
- }
- P1 = 0xff;
- P1 = 0Xbf; //检测第三行
- temp = P1;
- temp = temp&0x0f;
- if(P1!=0X0f)
- {
- delay(5);
- temp = P1;
- temp = temp&0x0f;
- if(P1!=0X0f)
- {
- temp = P1;
- switch(temp)
- {
- case 0xb7: key0 = 1; num = num*10+4; kkey = 1; break; //-------'4'
- case 0xbb: key0 = 1; num = num*10+5; kkey = 1; break; //-------'5'
- case 0xbd: key0 = 1; num = num*10+6; kkey = 1; break; //-------'6'
- case 0xbe: key0 = 2; ikey0 = 12; break; //------乘号键*
- }
-
- while(temp!=0x0f)
- {
- temp = P1;
- temp = temp&0x0f;
- }
- //P0 = tab[h];
- }
- }
- P1 = 0xff;
- P1 = 0X7f; //检测第四行
- temp = P1;
- temp = temp&0x0f;
- if(P1!=0X0f)
- {
- delay(5);
- temp = P1;
- temp = temp&0x0f;
- if(P1!=0X0f)
- {
- temp = P1;
-
- switch(temp)
- {
- case 0x77: key0 = 1; num = num*10+7; kkey = 1; break; //-------'7'
- case 0x7b: key0 = 1; num = num*10+8; kkey = 1; break; //--------'8'
- case 0x7d: key0 = 1; num = num*10+9; kkey = 1; break; //--------'9'
- case 0x7e: key0 = 2; ikey0 = 13; break; //-------除号键
-
- }
-
- while(temp!=0x0f)
- {
- temp = P1;
- temp = temp&0x0f;
- }
- }
- }
-
- }
- void count()
- {
- key();
- if(key0 == 0) //清零
- {
- num = 0;qnum = 0;
- display(num);
- }
- else if(key0 == 1) //数字键,显示数字
- {
- display(num);
-
- }
- else if(key0 == 8)
- {
- display(qnum);
- }
- else if(key0 == 2) //当为运算符号键时
- {
- qnum = num;
- num = 0;
- key0 = 8;
-
- }
- else if(key0 == 3) //等号键
- {
- switch(ikey0)
- {
- case 10:num = num + qnum; key0 = 2; break;
- case 11:num = qnum - num; key0 = 2; break;
- case 12:num = num * qnum; key0 = 2; break;
- case 13:num = qnum / num; key0 = 2; break;
- //default break;
- }
- //}
- }
- }
-
- void main()
- { while(1)
- {
- //display(num);
- count();
- }
- }
复制代码
|