基于51计算器可发送给上位机可红外
单片机源程序如下:
- #include"lcd12864.h"
- #include"reg52.h"
- #include"i2c.h"
- #include"math.h"
- sbit IRIN=P3^2;
- int app=0;
- unsigned char IrValue[6];
- unsigned char Time;
- unsigned char code smgduan[17]={
- 0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
- 0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0X76};
- void chuan()
- {
- TMOD=0X20;
- TH1=0XFD;
- TL1=0XFD;
- TR1=1;
- REN=1;
- SM0=1;
- SM1=1;
- }
- void delay(unsigned int i)
- {
- while(i--);
- }
- void IrInit()
- {
- IT0=1;//下降沿触发
- EX0=1;//打开中断0允许
- EA=1; //打开总中断
- IRIN=1;//初始化端口
- }
- char hongwai()
- {
- if((smgduan[IrValue[2]/16]==0x06)&&(smgduan[IrValue[2]%16]==0x7d))
- return '0';
- if((smgduan[IrValue[2]/16]==0x3f)&&(smgduan[IrValue[2]%16]==0x39))
- return '1';
- if((smgduan[IrValue[2]/16]==0x06)&&(smgduan[IrValue[2]%16]==0x7f))
- return '2';
- if((smgduan[IrValue[2]/16]==0x6d)&&(smgduan[IrValue[2]%16]==0x79))
- return '3';
- if((smgduan[IrValue[2]/16]==0x3f)&&(smgduan[IrValue[2]%16]==0x7f))
- return '4';
- if((smgduan[IrValue[2]/16]==0x06)&&(smgduan[IrValue[2]%16]==0x39))
- return '5';
- if((smgduan[IrValue[2]/16]==0x6d)&&(smgduan[IrValue[2]%16]==0x77))
- return '6';
- if((smgduan[IrValue[2]/16]==0x66)&&(smgduan[IrValue[2]%16]==0x5b))
- return '7';
- if((smgduan[IrValue[2]/16]==0x6d)&&(smgduan[IrValue[2]%16]==0x5b))
- return '8';
- if((smgduan[IrValue[2]/16]==0x66)&&(smgduan[IrValue[2]%16]==0x77))
- return '9';
- if((smgduan[IrValue[2]/16]==0x3f)&&(smgduan[IrValue[2]%16]==0x6f))
- return '+';
- if((smgduan[IrValue[2]/16]==0x06)&&(smgduan[IrValue[2]%16]==0x6d))
- return '-';
- if((smgduan[IrValue[2]/16]==0x66)&&(smgduan[IrValue[2]%16]==0x4f))
- return '/';
- if((smgduan[IrValue[2]/16]==0x66)&&(smgduan[IrValue[2]%16]==0x3f))
- return 'x';
- if((smgduan[IrValue[2]/16]==0x3f)&&(smgduan[IrValue[2]%16]==0x07))
- return '=';
- if((smgduan[IrValue[2]/16]==0x66)&&(smgduan[IrValue[2]%16]==0x07))
- return '.';
- if((smgduan[IrValue[2]/16]==0x66)&&(smgduan[IrValue[2]%16]==0x7d))
- return 'p';
- if((smgduan[IrValue[2]/16]==0x66)&&(smgduan[IrValue[2]%16]==0x6d))
- return '|';
- else return 0xff;
- }
- main()
- {
- char i,sign,s,j,k,h=0;
- unsigned char num;
- char temp[16]; //最大输入16个
- bit firstflag;
- double a=0,b=0;
- chuan();
- IrInit();
- LCD_Init();
- // LCD_Write_com(0x01); //清屏
-
- while(1)
- { if(app==1)
- {num=hongwai();
- app=0;
- }
- else
- num = KeyPro();
- if(num!=0xff)
- {
- if(j == 0)
- LCD_Write_com(0x01);
- j=1;
- if(num=='|')
- {
- a=At24c02Read(1);
- firstflag = 1;
- }
- if(('+' == num) || ('-' == num) || ('x' == num) || ('/' == num) || ('=' == num)|| ('p' == num))
- {
- if(num=='p')
- {
- LCD_Write_Char(i,0,num);
- i++;
- h=1;
- }
- else
- {k=0;
- if(firstflag==0) //flag=1为第一个数,0为第二个数
- {
- sscanf(temp,"%f " ,&a);
- firstflag = 1;
- if(h==1)
- a=a*a;
- }
- else
- {sscanf(temp," %f " ,&b);
- if(h==1)
- b=b*b;}
- for(s = 0;s<16;s++)
- temp[s] = 0;
- LCD_Write_Char(i,0,num);
- i++ ;
- //////////////////
- if(num != '=')
- sign = num;
- else
- { j=0;
- i=0;
- firstflag = 0;
- switch(sign)
- {
- case '+' :a = a+b;break;
- case '-' :a = a-b;break;
- case 'x' :a = a*b;break;
- case '/' :a = a/b;break;
- default:break;
- }
- //LCD_Write_Char(1,1,answer);
- At24c02Write(1,a); //在地址1内写入数据num
- sprintf(temp,"%.2f",a); //输出浮点型,无用的0不输出
- Write_String(0,3,temp);//显示到液晶屏
- for(s=0;temp[s]!='\0';s++)
- {SBUF=temp[s];
- while(!TI);
- TI=0;
- }
- SBUF=' ';
- while(!TI);
- TI=0;
- sign = 0;
- a = 0;
- b = 0;
- for(s=0;s<16;s++)
- temp[s]=0;
- }
- }
- }
- else if((i<24)&&(num!='s')&&(num!='p')&&(num!='k'))
- {
- if((1==i)&&(temp[0]=='0'))
- {
- if(num == '.')
- {
- temp[k]=num;
- LCD_Write_Char(1,0,num);//输出数据
- i++;
- k++;
- }
- else
- {
- temp[0] = num;
- LCD_Write_Char(0,0,num);
- }
- }
- else
- {
- temp[k] = num;
- LCD_Write_Char(i,0,num);
- i++;
- k++;
- }
- }
- }
- }
- }
- void ReadIr() interrupt 0
- {
- unsigned char j,k;
- unsigned err;
- Time=0;
- delay(700); //7ms
- if(IRIN==0) //确认是否真的接收到正确的信号
- {
-
- err=1000; //1000*10us=10ms,超过说明接收到错误的信号
- /*当两个条件都为真是循环,如果有一个条件为假的时候跳出循环,免得程序出错的时
- 侯,程序死在这里*/
- while((IRIN==0)&&(err>0)) //等待前面9ms的低电平过去
- {
- delay(1);
- err--;
- }
- if(IRIN==1) //如果正确等到9ms低电平
- {
- err=500;
- while((IRIN==1)&&(err>0)) //等待4.5ms的起始高电平过去
- {
- delay(1);
- err--;
- }
- for(k=0;k<4;k++) //共有4组数据
- {
- for(j=0;j<8;j++) //接收一组数据
- {
- err=60;
- while((IRIN==0)&&(err>0))//等待信号前面的560us低电平过去
- {
- delay(1);
- err--;
- }
- err=500;
- while((IRIN==1)&&(err>0)) //计算高电平的时间长度。
- {
- delay(10); //0.1ms
- Time++;
- err--;
- if(Time>30)
- {
- return;
- }
- }
- IrValue[k]>>=1; //k表示第几组数据
- if(Time>=8) //如果高电平出现大于565us,那么是1
- {
- IrValue[k]|=0x80;
- }
- Time=0; //用完时间要重新赋值
- }
- }
- }
- if(IrValue[2]!=~IrValue[3])
- {
- return;
- }
- app=1;
- }
- }
复制代码
所有程序51hei提供下载:
计算机.7z
(29.38 KB, 下载次数: 16)
|