proteus與keil Cx51的單片機(jī)仿真(定時(shí)器應(yīng)用2)
電路圖:
本文引用地址:http://butianyuan.cn/article/201611/320720.htm
C程序:
#include
#include
sbit K01=P1^4; //增加鍵
sbit K02=P1^5; //減少鍵
sbit BEEP=P3^7;//蜂鳴器
unsigned char PWM=0x7f;//賦初值
void Beep();
void delayms(unsigned char ms);
void delay(unsigned t);
void main()
{
P0=0xff;
P1=0xff;
P2=0xff;
P3=0xff;
TMOD=0x21;
TH0=0xfc;//1ms延時(shí)常數(shù)12M
TL0=0x18;//頻率調(diào)節(jié)
TH1=PWM;//脈寬調(diào)節(jié)
TL1=0;
EA=1;
ET0=1;
ET1=1;
TR0=1;
while(1)
{
do{if(PWM!=0xff){PWM++;delayms(10);}
else Beep();}while(K01==0);//K01按下,亮度變暗
do{if(PWM!=0x01){PWM--;delayms(10);}
else Beep();}while(K02==0);//K02按下,亮度變亮
} }
void timer0() interrupt 1 //定時(shí)器0中斷服務(wù)程序
{
TR1=0;
TH0=0xfc;
TL0=0x66;
TH1=PWM;
TR1=1;
P0=0x00;
P2=0x00; //啟動(dòng)輸出
}
void timer1() interrupt 3//定時(shí)器1中斷服務(wù)程序
{TR1=0;P0=0xff;P2=0xff;}//結(jié)束輸出
void Beep()//蜂鳴器子程序
{unsigned char i;
for(i=0;i<100;i++){delay(100);BEEP=!BEEP;}//Beep取反
BEEP=1;delayms(100);//關(guān)閉蜂鳴器,并延時(shí)
}
void delay(unsigned char t){while(t--);}//延時(shí)子程序1
void delayms(unsigned char ms)//延時(shí)子程序2
{unsigned char i; while(ms--){for(i=0;i<120;i++);}}
評(píng)論