|
本帖最后由 1sdaw123 于 2018-5-30 20:28 編輯
#include<reg51.h>
sbit Key=P1^3;
sbit hongwai=P1^4;
sbit LED=P0^0;
sbit SPK=P0^2;
void DelayUs2x(unsigned char t);
void DelayMs(unsigned char t);
void KEY();
main()
{
while(1)
{
KEY();
}
}
/*------------------------------------------------
獨(dú)立按鍵與紅外
------------------------------------------------*/
void KEY()
{ Key=1;
if(!Key)
{
DelayMs(10);
if(!Key)
{
if(hongwai==1)
{
LED=0;
SPK=!SPK;
LED=1;
}else LED=1;
while(Key);
}
}
}
/*------------------------------------------------
延時函數(shù)
------------------------------------------------*/
void DelayUs2x(unsigned char t)
{
while(--t);
}
/*------------------------------------------------
mS延時函數(shù),含有輸入?yún)?shù) unsigned char t,無返回值
unsigned char 是定義無符號字符變量,其值的范圍是
0~255 這里使用晶振12M,精確延時請使用匯編
------------------------------------------------*/
void DelayMs(unsigned char t)
{
while(t--)
{
//大致延時1mS
DelayUs2x(245);
DelayUs2x(245);
}
}
為什么我試驗(yàn)的時候 蜂鳴器不是正常發(fā)聲 聲音很奇怪 LED燈也很微弱
|
|