2015届蓝桥杯答案
单片机源程序如下:
- #include<stc15f2k60s2.h>
- #include "ds18b20.h"
- #include <ds1302.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
- extern uchar shijian[];
- /**********************定义的变量*******************************/
- uchar ten=0;
- bit led_shan=0;
- bit fla=0;
- bit S6=0;
- uchar cai=0; // 0 时为按下 ;1时为常规
- uint num=0;
- uchar i;
- uchar jiange=1;
- uchar wendu;
- uchar save[10];
- uchar yi,er,san,si,wu,liu,qi,ba;
- /*************************定义的变量****************************/
- /******************函数声明********************/
- void init();
- void keyscan();
- 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();
- ds1302_init();
- Timer0Init();
- yi=11;er=11;san=11;si=11;wu=11;liu=10;qi=0;ba=1;
- while(1)
- {
- if(cai==0) //******** 常 态 ***********
- {
- yi=11;er=11;san=11;si=11;wu=11;liu=10;
- qi=jiange/10;ba=jiange%10;
- }
- else if(cai==1) //S5 被按下 ,采集温度
- {
- ds1302_get(); //显示时钟;
- yi=shijian[2]/10;er=shijian[2]%10;san=10;
- si=shijian[1]/10;wu=shijian[1]%10;liu=10;
- qi=shijian[0]/10;ba=shijian[0]%10;
-
- if(shijian[0]%2==0) //提示符闪烁
- {
- san=10;liu=10;
- }
- else
- {
- san=11;liu=11;
- }
- }
- wendu=ds18b20_get();
- keyscan();
- 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(cai==1) //采集温度开始
- {
- if(num==jiange*200) //控制采集间隔
- {
- num=0;
- save[ten]=wendu; //保存 温度值
- ten++;
- if(ten==10) //10个温度读完 令cai=2 led闪开始 num此时为0
- { //显示 温度值 save[0]
- cai=2;led_shan=1;ten=0;
- yi=10;er=0;san=0;si=11;wu=11;liu=10;qi=save[0]/10;ba=save[0]%10;
- }
- }
- }
- if((cai==2)&&(num==200)) //led灯闪烁;
- {
- num=0; //赋0
- if(led_shan==1)
- {
- if(fla==0)
- {fla=1;P2=0X80;P0=0XFE;}
- else if(fla==1)
- {fla=0;P2=0X80;P0=0XFF;}
- }
- else if(led_shan==0) //led 不闪时 依次显示十个温度值
- {
- yi=10;er=ten/10;san=ten%10;si=11;wu=11;liu=10;qi=save[ten]/10;ba=save[ten]%10;
- ten++;
- if(ten==10) //显示完成后归0 以便S7按下
- {
- ten=0;
- cai=3;
- }
- }
- }
- }
- void keyscan() //独立按键
- {
- if(P30==0) //S7
- {
- delayms(5);
- if(P30==0)
- {
- if(cai==3) //返回初值
- cai=0;
- }
- while(!P30);
- }
- else if(P31==0) //S6
- {
- delayms(5);
- if(P31==0)
- {
- if(cai==2)
- {
- led_shan=0; //关led
- P2=0X80;
- P0=0XFF;
- }
- }
- while(!P31);
- }
- else if(P32==0)//S5
- {
- delayms(5);
- if(P32==0)
- {
- if(cai==0)
- {
- cai=1;num=0;
- }
- }
- while(!P32);
- }
- else if(P33==0) //S4
- {
- delayms(5);
- if(P33==0)
- {
- if(cai==0)
- {
- if(jiange==1)
- jiange=5;
- else if(jiange==5)
- jiange=30;
- else if(jiange==30)
- jiange=60;
- else if(jiange==60)
- jiange=1;
- }
- }
- while(!P33);
- }
- }
- 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;
- P0=0X20;
- P2=0XFF;
- P0=tab[liu];
- delayms(1);
- }
- void display4(uchar qi,uchar ba)
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
2015.rar
(41.98 KB, 下载次数: 11)
|