2014届蓝桥杯答案
单片机源程序如下:
- #include<stc15f2k60s2.h>
- #include "ds18b20.h"
- #define uchar unsigned char
- #define uint unsigned int
-
- uchar code tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf,0xff};//小数点 &0x7f
- /**********************定义的变量*******************************/
- bit flag=0;
- uchar q,i;
- uchar num=0;
- uchar an;
- bit o=0;
- bit she=0;
- bit qing=0;
- uchar wendu;
- uchar shang=30;
- uchar xia=25;
- uchar yi,er,san,si,wu,liu,qi,ba;
- /*************************定义的变量****************************/
- /******************函数声明********************/
- void init();
- void keyscan16();
- void Timer0Init();
- void delayms(int ms);
- void display1(uchar yi,uchar er);
- void display2(uchar san,uchar si);
- void display3(uchar wu,uchar liu);
- void display4(uchar qi,uchar ba);
- /******************函数声明********************/
- void main()
- {
- init();
- Timer0Init();
- yi=10;er=1;san=10;si=11;wu=11;liu=11;qi=wendu/10;ba=wendu%10;
- while(1)
- {
- if(she==0)
- {
- wendu=Tempget(); //调用**************************
- if((wendu>=xia)&&(wendu<=shang))q=1;
- else if(wendu<xia) q=0;
- else if(wendu>shang) q=2;
- if(q==0)
- {
- P2=0XA0;
- P0=0X00;
- }
- else if(q==1)
- {
- P2=0XA0;
- P0=0X00;
- }
- else if(q==2)
- {
- P2=0XA0;
- P0=0X10;
- }
- yi=10;er=q;san=10;si=11;wu=11;liu=11;qi=wendu/10;ba=wendu%10;// -1-***28
- }
- else if(she==1)
- {
- if(qing==1)
- {
- qing=0;
- yi=10;er=11;san=11;si=11;wu=11;liu=10;qi=11;ba=11;
-
- }
- if(er==11&&o==1)
- {
- er=an;o=0;
- }
- else if(san==11&&o==1)
- {
- san=an;o=0;
- }
- else if(qi==11&&o==1)
- {
- qi=an;o=0;
- }
- else if(ba==11&&o==1)
- {
- ba=an;o=0;
- shang=er*10+san;
- xia=qi*10+ba;
- if(shang<xia)
- {
- P2=0X80; //LED2亮
- P0=0XFD;
- yi=10;er=11;san=11;si=11;wu=11;liu=10;qi=11;ba=11;
- }
- }
-
- }
- keyscan16();
- display1(yi,er);
- display2(san,si);
- display3(wu,liu);
- display4(qi,ba);
- }
- }
- void init()
- {
- P2=0XA0;
- P0=0X00;
-
- P2=0X80;
- P0=0XFF;
-
- P2=0XC0;
- P0=0XFF;
- P2=0XFF;
- P0=0XFF;
- }
- void delayms(int ms)
- {
- int i,j;
- for(i=ms;i>0;i--)
- for(j=845;j>0;j--);
- }
- void Timer0Init(void) //5毫秒@11.0592MHz
- {
- AUXR |= 0x80; //定时器时钟1T模式
- TMOD &= 0xF0; //设置定时器模式
- TL0 = 0x00; //设置定时初值
- TH0 = 0x28; //设置定时初值
- TF0 = 0; //清除TF0标志
- TR0 = 1; //定时器0开始计时
- ET0=1;
- EA=1;
- }
- void timer0()interrupt 1 //定时器0中断
- {
- num++;
- if(num==160&&q==0)
- {
- num=0;
- if(flag==0){flag=1;P2=0X80;P0=0XFE;}
- else if(flag==1){flag=0;P2=0X80;P0=0XFF;}
- }
- if(num==80&&q==1)
- {
- num=0;
- if(flag==0){flag=1;P2=0X80;P0=0XFE;}
- else if(flag==1){flag=0;P2=0X80;P0=0XFF;}
- }
- if(num==40&&q==2)
- {
- num=0;
- if(flag==0){flag=1;P2=0X80;P0=0XFE;}
- else if(flag==1){flag=0;P2=0X80;P0=0XFF;}
- }
- }
- void keyscan16()//矩阵键盘
- {
- uchar temp;
- P44=0;P42=1;P3=0X7F; //第一列
- temp=P3;
- temp&=0x0f;
- if(temp!=0x0F)
- {
- delayms(5);
- temp=P3;
- temp&=0x0F;
- if(temp!=0x0F)
- {
- temp=P3;
- switch(temp)
- {
- case 0x7E:an=0;o=1 ;break; //S7
- case 0x7D:an=3;o=1 ;break; //S6
- case 0x7B:an=6;o=1 ;break; //S5
- case 0x77:an=9;o=1 ;break; //S4
- }
- while(temp!=0x0f)
- {
- temp=P3;
- temp&=0x0f;
- }
- }
- }
-
- P44=1;P42=0;P3=0XBF; //第二列
- temp=P3;
- temp&=0x0f;
- if(temp!=0x0F)
- {
- delayms(5);
- temp=P3;
- temp&=0x0F;
- if(temp!=0x0F)
- {
- temp=P3;
- switch(temp)
- {
- case 0xBE:an=1;o=1;break; //S7
- case 0xBD:an=4;o=1;break; //S6
- case 0xBB:an=7;o=1;break; //S5
- case 0xB7:
- if(she==0)
- {
- she=1;
- ET0=0;EA=0;
- P2=0X80;P0=0XFF;P2=0XA0;P0=0X00;
- yi=10;er=11;san=11;si=11;wu=11;liu=10;qi=11;ba=11;
-
- }
- else if(she==1)
- {
- P2=0X80;P0=0XFF;
- she=0;ET0=1;EA=1;
- shang=er*10+san;
- xia=qi*10+ba;
- }
- break; //S4
- }
- while(temp!=0x0f)
- {
- temp=P3;
- temp&=0x0f;
- }
- }
- }
- P44=1;P42=1;P3=0XDF; //第三列
- temp=P3;
- temp&=0x0f;
- if(temp!=0x0F)
- {
- delayms(5);
- temp=P3;
- temp&=0x0F;
- if(temp!=0x0F)
- {
- temp=P3;
- switch(temp)
- {
- case 0xDE:an=2;o=1;break; //S7
- case 0xDD:an=5;o=1;break; //S6
- case 0xDB:an=8;o=1;break; //S5
- case 0xD7:qing=1;break; //S4
- }
- while(temp!=0x0f)
- {
- temp=P3;
- temp&=0x0f;
- }
- }
- }
- }
- void display1(uchar yi,uchar er)
- {
- P2=0XC0;
- P0=0X01;
- P2=0XFF;
- P0=tab[yi];
- delayms(1);
-
- P2=0XC0;
- P0=0X02;
- P2=0XFF;
- P0=tab[er];
- delayms(1);
- }
- void display2(uchar san,uchar si)
- {
- P2=0XC0;
- P0=0X04;
- P2=0XFF;
- P0=tab[san];
- delayms(1);
-
- P2=0XC0;
- P0=0X08;
- P2=0XFF;
- P0=tab[si];
- delayms(1);
- }
- void display3(uchar wu,uchar liu)
- {
- P2=0XC0;
- P0=0X10;
- P2=0XFF;
- P0=tab[wu];
- delayms(1);
-
- P2=0XC0;
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
2014-温度采集.rar
(36.4 KB, 下载次数: 17)
|