×
单片机 > 单片机程序设计 > 详情

51单片机计算器源程序 可发送给上位机 可红外

发布时间:2023-03-23 发布时间:
|

单片机源程序如下:

#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]!='


『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
ARM微处理器的液晶触摸屏接口设计