51 单片机 控制节拍产生示例程序
#include <reg51.h>
sbit speaker=P3^7;
unsigned char Do=108;
unsigned char beat_H=(65536-62500)/256,beat_L()=(65536-62500)%256;
unsigned char tone_H=(65536-1908)/256,tone_L()=(65536-1908)%256;
char times,counts=5;
bit beat_flag;
//=====Timer0中断子程序======================
void tone_timer(void)interrupt 1
{TH0=tone_H;
TL0=tone_L;
speaker=~speaker;
}
main()
{char i;
IE=0x82;
TH0=beat_H;
TL0=beat_L;
for(i=0;i<counts;i++)
{times=2;
beat_flag=0;
TR0=1;
if(beat_flag==0)
{speaker=~speaker;delay8us(tone[Do]);}
//===静音(1/4拍)=====================================
speaker=0;
times=2;
beat_flag=0;
TR0=1;
while(beat_flag=0);
}
}
//======================================================
void beat_timer(void)interrupt 1
{TH0=beat_H;
TL0=beat_L;
if(--times==0)
{beat_flag=1;
TR0=0;
}
}
//============================================
void delay8us(unsigned charx)
{unsigned char i,j;
for(i=0;i<x;i++)
for(j=0;j<1;j++);
}