51hei.png (58.12 KB, 下載次數: 54)
下載附件
2021-5-27 16:00 上傳
51hei.png (39.66 KB, 下載次數: 51)
下載附件
2021-5-27 16:01 上傳
51hei.png (90.08 KB, 下載次數: 64)
下載附件
2021-5-27 16:01 上傳
單片機源程序如下:
- #include<STC15F2K60S2.H>
- #include<intrins.h>
- #include "ds18b20.h"
- #include "ds1302.h"
- //#include "abc.h"
- #include "2.h"
- uchar code duanxuan[]={0XC0,0XF9,0XA4,0XB0,0X99,0X92,0X82,0XF8,0X80,0X90,0XBF,0XFF,0XC6};//0,1,2,3,4,5,6,7,8,9,-,空,c
- uchar code duanxuan1[]={0x40, 0x79, 0x24, 0x30, 0x19,0x12, 0x02, 0x78, 0x00, 0x10, 0xBF};//0~9帶點1
- uchar code weixuan[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
- uchar baojing=0,moshi=0,hour,min,sec,hour_i,hour_n=0,min_i,min_n=0,sec_i,sec_n=0,flase=0xff;
- uchar s1=0,s2=0,s3=0,s4=0,com=1;
- uint count0,count1,count3=0,wendu;
- bit flag,wendub=0;
- uchar time[7]={20,1,1,1,23,59,50};
- uchar du; //數碼顯示掃描變量
- extern uchar shijian13[2];
- extern uchar ds[];
- void keyscan4();
- void all_chushihua();
- void xianshi0();
- void xianshi1();
- void xianshi2();
- void xianshi3();
- void xianshi4();
- void xianshi5();
- void xianshi6();
- void xian();
- void display();
- void writetime1();
- void readtime1();
- void Delay5ms();
- void main()
- {
- all_chushihua();
- writetime1();
- TMOD &= 0xF0; //設置定時器模式 //2ms T0定時器
- TMOD |= 0x01; //設置定時器模式
- TL0 = 0xCD; //設置定時初值
- TH0 = 0xF8; //設置定時初值
- TF0 = 0; //清除TF0標志
- EA=1;
- ET0=1;
- TR0 = 1; //定時器0開始計時
- while(1)
- {
- wendu=temget();
- readtime1();
- hour=time[4];min=time[5];sec=time[6];
- keyscan4();
- if((hour==hour_n)&&(min==min_n)&&(sec==sec_n))
- {
- s1=0;
- s2=0;
- s3=0;
- s4=0;
- baojing=1;
- count0=0;
- count1=0;
- // count2=0;
- }
-
- }
- }
- //***************資料包*********************//
- //void writetime1()
- //{
- // uchar i,a,d=0x80; //
- // Ds1302_Single_Byte_Write(0x8e,0); //
- // for (i=0;i<7;i++)
- // {
- // a=time[6-i]/10; //高四位
- // a<<=4;
- // a|=time[6-i]%10; //低四位
- // Ds1302_Single_Byte_Write(d,a); //
- // d+=2; //
- // }
- // Ds1302_Single_Byte_Write(0x8e,0x80); //
- //}
- //void readtime1() //時間讀取
- //{
- // uchar i,a,d=0x81;
- // Ds1302_Single_Byte_Write(0x8e,0);
- // for (i=0;i<7;i++)
- // {
- // EA=0;/////!!!!!!!!!!!讀數據加中斷禁止,否則會出現偶然錯誤
- // a=Ds1302_Single_Byte_Read(d);
- // EA=1;
- // SDA_R=0; ///////!!!!每次讀取一個字節要把IO口拉低或者延時
- //
- // time[6-i]=a/16*10+a%16;
- // d+=2;
- // }
- // Ds1302_Single_Byte_Write(0x8e,0x80);
- //}
- //****************王玉帆資料驅動*****************//
- void writetime1()
- {
- uchar i,a,d=0x80; //
- Write_Ds1302_Byte(0x8e,0); //
- for (i=0;i<7;i++)
- {
- a=time[6-i]/10; //高四位
- a<<=4;
- a|=time[6-i]%10; //低四位
- Write_Ds1302_Byte(d,a); //
- d+=2; //
- }
- Write_Ds1302_Byte(0x8e,0x80); //
- }
- void readtime1() //時間讀取
- {
- uchar i,a,d=0x81;
- Write_Ds1302_Byte(0x8e,0);
- for (i=0;i<7;i++)
- {
- EA=0;/////!!!!!!!!!!!讀數據加中斷禁止,否則會出現偶然錯誤
- a=Read_Ds1302_Byte(d);
- EA=1;
- SDA=0; ///////!!!!每次讀取一個字節要把IO口拉低或者延時
-
- time[6-i]=a/16*10+a%16;
- d+=2;
- }
- Write_Ds1302_Byte(0x8e,0x80);
- }
- void all_chushihua()
- {
- P0=0XFF; //關閉LED燈
- P2=(P2&0X1F)|0X80; //Y4C=1;
- P2=(P2&0X1F);
- P0=0X00; //關閉蜂鳴器,繼電器
- P2=(P2&0X1F)|0XA0; //Y5C=1;
- P2=(P2&0X1F);
- P0=0XFF; //選擇所有數碼管
- P2=(P2&0X1F)|0XC0; //Y6C=1;
- P2=(P2&0X1F);
- P0=0XFF; //關閉所有數碼管
- P2=(P2&0X1F)|0XE0; //Y7C=1;
- P2=(P2&0X1F);
- }
- void keyscan4()
- {
- if(P30==0)
- {
- Delay5ms();
- if(P30==0)
- {
- s1=1;
- if(moshi==0)
- {
- hour_i=hour;
- min_i=min;
- sec_i=sec;
- }
- if(moshi<4)
- moshi++;
- if(moshi>=4)
- {
- time[6]=sec_i;
- time[5]=min_i;
- time[4]=hour_i;
- writetime1();
- moshi=0;
- }
- }
- while(!P30);
- }
- if(P31==0)
- {
- Delay5ms();
- if(P31==0)
- {
- s2=1;
-
- if(moshi>=4) moshi++;
- if(moshi==0) moshi=4;
- if(moshi>=7) moshi=0;
- }
- while(!P31);
- }
- if(P32==0)
- {
- Delay5ms();
- if(P32==0)
- {
- s3=1;
- switch(moshi)
- {
- case 1: hour_i++;if(hour_i>23) hour_i=0;break;
- case 2: min_i++;if(min_i>59) min_i=0;break;
- case 3: sec_i++;if(sec_i>=59) sec_i=0;break;
- case 4: hour_n++;if(hour_n>=23) hour_n=0;break;
- case 5: min_n++;if(min_n>=59) min_n=0;break;
- case 6: sec_n++;if(sec_n>=59) sec_n=0;break;
- }
- }
- while(!P32);
- }
- if(P33==1)
- wendub=0;
- if(P33==0)
- {
- Delay5ms();
- if(P33==0)
- {
- s4=1;
- if(moshi==0)
- {
- wendu=temget();
- wendub=1;
- }
- else
- {
-
- switch(moshi)
- {
- case 1: if(hour_i!=0) hour_i--;else hour_i=23;break;
- case 2: if(min_i!=0) min_i--; else min_i=59;break;
- case 3: if(sec_i!=0) sec_i--; else sec_i=59;break;
- case 4: if(hour_n!=0) hour_n--;else hour_n=23;break;
- case 5: if(min_n!=0) min_n--; else min_n=59;break;
- case 6: if(sec_n!=0) sec_n--; else sec_n=59;break;
- }
- }
- }
- }
- while(!P33);
- }
- void xianshi0()
- {
- if(!wendub)
- {
- switch(du)
- {
- case 0:P0=duanxuan[hour/10];break;
- case 1:P0=duanxuan[hour%10];break;
- case 2:P0=duanxuan[10];break;
- case 3:P0=duanxuan[min/10];break;
- case 4:P0=duanxuan[min%10];break;
- case 5:P0=duanxuan[10];break;
- case 6:P0=duanxuan[sec/10];break;
- case 7:P0=duanxuan[sec%10];break;
- }
- }
- else
- {
- switch(du)
- {
- case 0:P0=duanxuan[11];break;
- case 1:P0=duanxuan[11];break;
- case 2:P0=duanxuan[11];break;
- case 3:P0=duanxuan[11];break;
- case 4:P0=duanxuan[wendu/1000%10];break;
- case 5:P0=duanxuan1[wendu/100%10];break;
- case 6:P0=duanxuan[wendu/10%10];break;
- case 7:P0=duanxuan[wendu%10];break;
- }
- }
- }
- void xianshi1()
- {
- switch(du)
- {
- case 0:P0=duanxuan[hour_i/10]|flase;break;
- case 1:P0=duanxuan[hour_i%10]|flase;break;
- case 2:P0=duanxuan[10];break;
- case 3:P0=duanxuan[min_i/10];break;
- case 4:P0=duanxuan[min_i%10];break;
- case 5:P0=duanxuan[10];break;
- case 6:P0=duanxuan[sec_i/10];break;
- case 7:P0=duanxuan[sec_i%10];break;
- }
- }
- void xianshi2()
- {
- switch(du)
- {
- case 0:P0=duanxuan[hour_i/10];break;
- case 1:P0=duanxuan[hour_i%10];break;
- case 2:P0=duanxuan[10];break;
- case 3:P0=duanxuan[min_i/10]|flase;break;
- case 4:P0=duanxuan[min_i%10]|flase;break;
- case 5:P0=duanxuan[10];break;
- case 6:P0=duanxuan[sec_i/10];break;
- case 7:P0=duanxuan[sec_i%10];break;
- }
- }
- void xianshi3()
- {
- switch(du)
- {
- case 0:P0=duanxuan[hour_i/10];break;
- case 1:P0=duanxuan[hour_i%10];break;
- case 2:P0=duanxuan[10];break;
- case 3:P0=duanxuan[min_i/10];break;
- case 4:P0=duanxuan[min_i%10];break;
- case 5:P0=duanxuan[10];break;
- case 6:P0=duanxuan[sec_i/10]|flase;break;
- case 7:P0=duanxuan[sec_i%10]|flase;break;
- }
- }
- void xianshi4()
- {
- switch(du)
- {
- case 0:P0=duanxuan[hour_n/10]|flase;break;
- case 1:P0=duanxuan[hour_n%10]|flase;break;
- case 2:P0=duanxuan[10];break;
- case 3:P0=duanxuan[min_n/10];break;
- case 4:P0=duanxuan[min_n%10];break;
- case 5:P0=duanxuan[10];break;
- case 6:P0=duanxuan[sec_n/10];break;
- case 7:P0=duanxuan[sec_n%10];break;
- }
- }
- void xianshi5()
- {
- switch(du)
- {
- case 0:P0=duanxuan[hour_n/10];break;
- case 1:P0=duanxuan[hour_n%10];break;
- case 2:P0=duanxuan[10];break;
- case 3:P0=duanxuan[min_n/10]|flase;break;
- case 4:P0=duanxuan[min_n%10]|flase;break;
- case 5:P0=duanxuan[10];break;
- case 6:P0=duanxuan[sec_n/10];break;
- case 7:P0=duanxuan[sec_n%10];break;
- }
- }
- void xianshi6()
- {
- switch(du)
- {
- case 0:P0=duanxuan[hour_n/10];break;
- case 1:P0=duanxuan[hour_n%10];break;
- case 2:P0=duanxuan[10];break;
- case 3:P0=duanxuan[min_n/10];break;
- case 4:P0=duanxuan[min_n%10];break;
- case 5:P0=duanxuan[10];break;
- case 6:P0=duanxuan[sec_n/10]|flase;break;
- case 7:P0=duanxuan[sec_n%10]|flase;break;
- }
- }
- void xian()
- {
- switch(moshi)
- {
- case 0:xianshi0();break;
- case 1:xianshi1();break;
- case 2:xianshi2();break;
- case 3:xianshi3();break;
- case 4:xianshi4();break;
- case 5:xianshi5();break;
- case 6:xianshi6();break;
- }
- }
- void display()
- {
-
- P0=weixuan[du];
- P2=(P2&0X1F)|0xc0;
- P2=(P2&0X1F);
- xian();
- P2=(P2&0X1F)|0XE0;
- P2=(P2&0X1F);
- if(++du>7) du=0;
- }
- void led(uchar liang)
- {
- P0=liang;
- P2=(P2&0x1f)|0x80;
- P2=(P2&0x1f);
- }
- void T1dingshiqi() interrupt 1
- {
-
- display(); //用于控制整體時鐘變化
- count1++; //用于控制LED燈閃爍
- count0++;
- count3++;
- if(baojing==1) //讓燈閃爍時間間隔位0.2秒
- {
- if(count1>=100)
- {
- count1=0;
- if(flag==0)
- {
- flag=1;
- led(0xfe);
- }
- else
- {
- flag=0;
- led(0xff);
- }
- }
- if((s1==1)||(s2==1)||(s3==1)||(s4==1))
- {
- s1=0;
- s2=0;
- s3=0;
- s4=0;
- led(0xff);
- baojing=0;
- }
- //count0++;
- if(count0>=2500)
- {
- count0=0;
- led(0xff);
- baojing=0;
- // count2++; //限定燈就閃爍5秒
- // if(count2>=5)
- // {
- // led(0xff);
- // baojing=0;
- // } //在設定時間的時候讓設置的數字以間隔為一秒閃爍
- }
- }
- else
- {
- led(0xff);
- }
- if(count3>=500)
- {
- count3=0;
- flase=~flase;
- }
- TL0 = 0xCD; //設置定時初值
- TH0 = 0xF8; //設置定時初值
- }
- void Delay5ms() //@11.0592MHz
- {
- unsigned char i, j;
- i = 54;
- j = 199;
- do
- {
- while (--j);
- } while (--i);
- }
復制代碼
51hei.png (10.2 KB, 下載次數: 37)
下載附件
2021-5-27 16:01 上傳
所有資料51hei提供下載:
藍橋杯大賽第8屆.rar
(209.71 KB, 下載次數: 25)
2021-5-27 13:50 上傳
點擊文件名下載附件
程序及題 下載積分: 黑幣 -5
|