|
2015屆藍(lán)橋杯答案
單片機(jī)源程序如下:
- #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};//小數(shù)點(diǎn) &0x7f
- extern uchar shijian[];
- /**********************定義的變量*******************************/
- uchar ten=0;
- bit led_shan=0;
- bit fla=0;
- bit S6=0;
- uchar cai=0; // 0 時(shí)為按下 ;1時(shí)為常規(guī)
- uint num=0;
- uchar i;
- uchar jiange=1;
- uchar wendu;
- uchar save[10];
- uchar yi,er,san,si,wu,liu,qi,ba;
- /*************************定義的變量****************************/
- /******************函數(shù)聲明********************/
- 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);
- /******************函數(shù)聲明********************/
- 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) //******** 常 態(tài) ***********
- {
- 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(); //顯示時(shí)鐘;
- 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; //定時(shí)器時(shí)鐘1T模式
- TMOD &= 0xF0; //設(shè)置定時(shí)器模式
- TL0 = 0x00; //設(shè)置定時(shí)初值
- TH0 = 0x28; //設(shè)置定時(shí)初值
- TF0 = 0; //清除TF0標(biāo)志
- TR0 = 1; //定時(shí)器0開(kāi)始計(jì)時(shí)
- ET0=1;EA=1;
- }
- void timer0() interrupt 1 //定時(shí)器0中斷
- {
- num ++;
- if(cai==1) //采集溫度開(kāi)始
- {
- if(num==jiange*200) //控制采集間隔
- {
- num=0;
- save[ten]=wendu; //保存 溫度值
- ten++;
- if(ten==10) //10個(gè)溫度讀完 令cai=2 led閃開(kāi)始 num此時(shí)為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 不閃時(shí) 依次顯示十個(gè)溫度值
- {
- 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() //獨(dú)立按鍵
- {
- 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; //關(guān)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)
- ……………………
- …………限于本文篇幅 余下代碼請(qǐng)從51黑下載附件…………
復(fù)制代碼
所有資料51hei提供下載:
2015.rar
(41.98 KB, 下載次數(shù): 11)
2018-6-4 22:37 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
|
|