c语言门铃程序,基于AT89C2051做的音乐门铃 附程序
时间: 2024-03-20 19:38:57 浏览: 72
以下是基于AT89C2051单片机的C语言门铃程序,可以实现音乐门铃的功能:
```c
#include <REGX51.H>
#define T0 0
#define T1 1
unsigned char code music[] = {0x0e,0x0e,0x0f,0x0f,0x10,0x10,0x0f,0x0f,0x0e,0x0e,0x0d,0x0d,0x0c,0x0c,0x0b,0x0b,0x0a,0x0a,0x09,0x09,0x08,0x08,0x07,0x07,0x06,0x06,0x05,0x05,0x04,0x04,0x03,0x03,0x02,0x02,0x01,0x01,0x00};
unsigned char cur_music = 0;
unsigned char cur_time = 0;
void timer0_interrupt() interrupt 1
{
TH0 = 0xff;
TL0 = 0x85;
cur_time++;
}
void timer1_interrupt() interrupt 3
{
unsigned char cur_note = music[cur_music];
if(cur_note == 0x0f)
P1 = 0x00;
else if(cur_note == 0x0e)
P1 = 0xff;
else
P1 = cur_note;
cur_music++;
if(cur_music >= 32)
cur_music = 0;
}
void main()
{
TMOD = 0x11;
TH0 = 0xff;
TL0 = 0x85;
TR0 = 1;
ET0 = 1;
TH1 = 0xfd;
TL1 = 0xfd;
TR1 = 1;
ET1 = 1;
EA = 1;
while(1)
{
if(cur_time >= 50)
{
cur_time = 0;
cur_music = 0;
}
}
}
```
该程序使用了AT89C2051单片机的两个定时器,其中定时器0用于计时,定时器1用于控制门铃发声。门铃的音乐数据保存在一个数组中,每个元素表示一个音符。程序中使用定时器0来控制播放速度,每50个定时器0的中断即为一次门铃的播放。
阅读全文