本帖最后由 渣渣馬 于 2020-6-6 21:44 编辑 #include<reg51.h> #include<intrins.h> sbit D1=P0^0; //LED闪烁 sbit KINT=P3^1; //开关 sbit LS=P3^7; //无源蜂鸣器 char code DATA[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff,0x40}; //CA段码 char code a[][2]={{2,10},{1,10},{11,3}}; //数码显示的几种模式 int t=0,temp=5; //t用于变数码管,temp用于时间变换 void DelayMs(int x) //延时 { int i; while(x--) for(i=0;i<120;i++); } void LED2() //数码显示 { int i,m; m=0x01; for(i=0;i<2;i++) P2=0xff,P1=_crol_(m,i),P2=DATA[a[t]],DelayMs(1); } void BUZZ() //蜂鸣器响0.5s { int i; for(i=0;i<500;i++) LS=!LS,DelayMs(2); } void init() //初始化 { IE=0x8a; //¿aT0 TR0=1; TR1=1; TMOD=0x22; TH0=(256-200)%256; LS=1; D1=1; } void main() { init(); while(1) { if(!KINT) 开关检测 { while(!KINT); //等待开关释放 t=++t%3; //0~2 switch(t) { case 0:temp=5;break; //2Hz case 1:temp=10;break; //1Hz case 2:temp=3;break; //0.3Hz default:break; } } } } void time0() interrupt 1 //定时用 { static int t0=0,t1=0; t0++; if(t0==500) //100ms { t1++; t0=0; if(t1==temp) //¸÷ÖÖê±¼äμÄÇD»» { D1=!D1; BUZZ(); t1=0; } } } void time1() interrupt 3 //LED2数码显示 { LED2(); } /*代码有点多,我是自己敲得,对你希望有用!把代码放在Keil中显示注释*/ |
兄弟,你这H5写的就不到位了! |