本帖最后由 渣渣馬 于 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中顯示注釋*/
|