51hei.png (99.28 KB, 下載次數: 44)
下載附件
2021-5-27 18:34 上傳
51hei.png (70.1 KB, 下載次數: 66)
下載附件
2021-5-27 18:35 上傳
51hei.png (83.07 KB, 下載次數: 55)
下載附件
2021-5-27 18:35 上傳
單片機源程序如下:
- #include <STC15F2K60S2.H>
- #include <intrins.h>
- #include "iic.h"
- #define uchar unsigned char
- #define uint unsigned int
- void Delay1ms();
- uchar code duanxuan[]={0XC0,0XF9,0XA4,0XB0,0X99,0X92,0X82,0XF8,0X80,0X90,0XBF,0XFF,0XC6,0x8e,0xc1};//0,1,2,3,4,5,6,7,8,9,-,空,c,f,u
- uchar code duanxuan1[]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10,0x8e,0xc1}; //0,1,2,3,4,5,6,7,8,9,f,u
- bit dianya_biao=1,pinlv_biao=0,DI,shuma_biao=0,led_biao=0,dianya_moshi=0;
- uint count_ya=0,count_pin=0,count_yashua=0,count_pinshua=0,dat_f,dat_u,dat_u1,flag,DAC_jiemian=0,LED_jiemian=0,zhujiemian=0,shuma_jiemian=0,hao=2;
- void led_xianshi();
- void dac();
- void keyscan4();
- void all_chushihua();
- void Delay5ms();
- void dingshiqi_chushihua();
- void hc138(uchar n);
- void display(uchar pos,uchar dat);
- void shuma_xianshi();
- void xian();
- void main()
- {
- all_chushihua();
- dingshiqi_chushihua();
-
- while(1)
- {
- led_xianshi();
- keyscan4();
- xian();
- }
- }
- 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 hc138(uchar n)
- {
- switch(n)
- {
- case 4:P2=(P2&0X1F)|0X80;break;
- case 5:P2=(P2&0X1F)|0XA0;break;
- case 6:P2=(P2&0X1F)|0XC0;break;
- case 7:P2=(P2&0X1F)|0XE0;break;
- case 8:P2=P2&0X1F;break;
- }
- }
- void display(uchar pos,uchar dat)
- {
- P0=0X01<<pos;
- hc138(6); //位選通道打開
- hc138(8);
- P0=dat;
- hc138(7); //段選通道打開
- hc138(8);
- }
- void dingshiqi_chushihua()
- {
- TMOD=0X16; //t1定時器16位定時,t0定時器8位自動重裝計數
- TH0=0xff;
- TL0=0XFF;
- TH1=(65536-2000)/256;
- TL1=(65536-2000)%256;
- EA=1;
- ET1=1;
- ET0=1;
- TR1=1;
- TR0=1;
- }
- void t0dingshiqi() interrupt 1
- {
- count_pin++;
- }
- void t1dingshiqi() interrupt 3
- {
- TH1=(65536-2000)/256;
- TL1=(65536-2000)%256;
- count_pinshua++;
- count_yashua++;
- if(count_yashua==250)
- {
- dianya_biao=1;
- count_yashua=0;
- }
- if(count_pinshua==500)
- {
- count_pinshua=0;
- dat_f=count_pin;
- count_pin=0;
-
- }
- }
- void shuma_xianshi()
- {
- if(shuma_biao==0)
- {
-
- display(0,duanxuan[13]);
- Delay1ms();
- if(dat_f>9999)
- {
- display(3,duanxuan[dat_f/10000]);
- Delay1ms();
- }
-
- if(dat_f>999)
- {
- display(4,duanxuan[(dat_f/1000)%10]);
- Delay1ms();
- }
-
- if(dat_f>99)
- {
- display(5,duanxuan[(dat_f/100)%10]);
- Delay1ms();
- }
-
- if(dat_f>9)
- {
- display(6,duanxuan[(dat_f)/10%10]);
- Delay1ms();
- }
-
- display(7,duanxuan[dat_f%10]);
- Delay1ms();
- hc138(8);
-
- }
- else if(shuma_biao==1)
- {
- P0=0XFF;
- hc138(6);
- hc138(8);
- P0=0XFF;
- hc138(7);
- hc138(8);
- }
-
-
- }
- void led_xianshi()
- {
- P0=0XFF;
- hc138(4);
- hc138(8);
- flag=dat_u1/1.961;
- if(led_biao==0)
- {
-
- if(zhujiemian%2==1)
- {
- P0=(P0&0XFC)|0X01;
- hc138(4);
- hc138(8);
- }
- if(zhujiemian%2==0)
- {
-
- P0=(P0&0xfc)|0X02;
- hc138(4);
- hc138(8);
- }
- if((flag<150)||((flag>=250)&&(flag<350)))
- {
- P0=(P0&0XFB)|0X04;
- hc138(4);
- hc138(8);
- }
- if(((flag>=150)&&(flag<250))||(flag>=350))
- {
- P0=(P0&0XFB)|0X00;
- hc138(4);
- hc138(8);
- }
- if((dat_f>1000)||((dat_f>=5000)&&(dat_f<10000)))
- {
- P0=(P0&0XF7)|0x08;
- hc138(4);
- hc138(8);
- }
- if(((dat_f>=1000)&&(dat_f<5000))||(dat_f>=10000))
- {
- P0=(P0&0XF7)|0X00;
- hc138(4);
- hc138(8);
- }
- if(dianya_moshi==0)
- {
- P0=(P0&0XEF)|0X10;
- hc138(4);
- hc138(8);
- }
- if(dianya_moshi==1)
- {
- P0=(P0&0XEF)|0X00;
- hc138(4);
- hc138(8);
- }
-
-
- }
- else
- {
- P0=0xff;
- hc138(4);
- hc138(8);
- }
-
- }
- void dac()
- {
- if(dianya_biao)
- {
- dianya_biao=0;
- if(dianya_moshi==0)
- {
- dac_out(102.4); //5v對應255,2v對應102.4
- //dat_u=IIC_RecByte();//雖然按理這樣寫但是顯示2.55v,故等于個2保險
- dat_u=200;
- }
- else
- {
- dat_u1=getad(0x03);
- }
- }
- if(shuma_biao==0)
- {
- if(dianya_moshi==0)
- {
- display(0,duanxuan[14]);
- Delay1ms();
- display(5,duanxuan1[dat_u/100]);
- Delay1ms();
- display(6,duanxuan[dat_u/10%10]);
- Delay1ms();
- display(7,duanxuan[dat_u/1%10]);
- Delay1ms();
- }
- else if(dianya_moshi==1)
- {
-
- display(0,duanxuan1[11]);
- Delay1ms();
- display(5,duanxuan1[dat_u1/100]);
- Delay1ms();
- display(6,duanxuan[dat_u1/10%10]);
- Delay1ms();
- display(7,duanxuan[dat_u1/1%10]);
- Delay1ms();
- }
- }
- else if(shuma_biao==1)
- {
- P0=0XFF;
- hc138(6);
- hc138(8);
- P0=0XFF;
- hc138(7);
- hc138(8);
- }
-
- }
- void xian()
- {
- switch(hao)
- {
- case 0:
- if(zhujiemian%2==1)
- {
-
- shuma_xianshi();
- }
- if((zhujiemian%2==0)&&(zhujiemian!=0))
- {
- dianya_moshi=0;
- dac();
- } break;
- case 1:
- if(DAC_jiemian%2==1)
- {
- dianya_moshi=0;
- dac();
- }
- if((DAC_jiemian%2==0)&&(DAC_jiemian!=0))
- {
- dianya_moshi=1;
- dac();
- } break;
- case 2:
- dac(); break;
- }
- }
- void keyscan4()
- {
- if(P33==0)
- {
- Delay5ms();
- if(P33==0)
- {
- zhujiemian++;
- hao=0;
- }
- while(!P33);
-
-
- }
- else if(P32==0)
- {
- Delay5ms();
- if(P32==0)
- {
- DAC_jiemian++;
- hao=1;
- }
- while(!P32);
-
- }
- else if(P31==0)
- {
- Delay5ms();
- if(P31==0)
- {
- LED_jiemian++;
- if(LED_jiemian%2==1)
- {
- led_biao=1;
- }
- if((LED_jiemian%2==0)&&(LED_jiemian!=0))
- {
- led_biao=0;
- }
- }
- while(!P31);
- }
- else if(P30==0)
- {
- Delay5ms();
- if(P30==0)
- {
- shuma_jiemian++;
- if(shuma_jiemian%2==1)
- {
- shuma_biao=1;
- }
- if((shuma_jiemian%2==0)&&(shuma_jiemian!=0))
- {
- shuma_biao=0;
- }
- }
- while(!P30);
-
-
- }
- }
- void Delay5ms() //@12.000MHz
- {
- unsigned char i, j;
- i = 59;
- j = 90;
- do
- {
- while (--j);
- } while (--i);
- }
- void Delay1ms() //@12.000MHz
- {
- unsigned char i, j;
- i = 12;
- j = 169;
- do
- {
- while (--j);
- } while (--i);
- }
復制代碼
所有資料51hei提供下載:
藍橋杯大賽第10屆.rar
(320.06 KB, 下載次數: 16)
2021-5-27 17:26 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|