#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
#define dao 523 //将“dao”宏定义为中音“1”的频率 523Hz
#define re 587 //将“re”宏定义为中音“2”的频率 587Hz
sbit buzzer=P2^4;
sbit s1=P1^0; //最好不要用到P3口
sbit s2=P1^1;
uchar C;
uchar code yinfu[]={dao,re};
void delay(uint t) //延时t MS
{
uint i,j;
for(i=t;i>0;i--)
for(j=110;j>0;j--);
}
void key_scan() //键盘扫描
{
if(s1==0)
{
C=yinfu[0];
delay(10);
if(s1==0);
while(!s1)
buzzer=1;
}
if(s2==0)
{
C=yinfu[1];
delay(10);
if(s2==0);
while(!s2)
buzzer=1;
}
}
void main()
{
buzzer=1;
TMOD=0x01;
key_scan(); //设置定时器0为工作模式1
TH0=(65536-500000/C)/256; //装T0的初值
TL0=(65536-500000/C)%256;
EA=1; //开总中断
ET0=1; //开定时器0中断
TR0=1; //启动定时器0
while(1);
}
void T0_time() interrupt 1
{
TH0=(65536-500000/C)/256; //装T0的初值
TL0=(65536-500000/C)%256;
buzzer=~buzzer;
}
此帖出自
小平头技术问答
一周热门 更多>