51單片機(jī)-蜂鳴器
單片機(jī)如何輸出頻率:
得到600HZ的頻率:
#include
sbit p = P3^4;
void main(){
p = 1;
EA = 1;
ET0 = 1;
TMOD = 0x01;
TH0 = (65536-833)/256;
TL0 = (65536-833)%256;
TR0 = 1;
while(1);
}
void time0() interrupt 1{
p = ~p;
TH0 = (65536-833)/256;
TL0 = (65536-833)%256;
}
內(nèi)循環(huán)一次8.3us
void delay8us(unsigned char x){
unsigned char i,j;
for(i = 0; i < x; i++)
for(j = 0; j < 1; j++)
;
}
音符頻率及定時(shí)器初值對(duì)照表:
4*4電子琴程序的實(shí)現(xiàn):
#include
#define uchar unsigned char
#define uint unsigned int
uint DATE,i;
sbit buzz = P3^4;
sbit L0 = P0^0;
sbit L1 = P0^1;
sbit L2 = P0^2;
sbit L3 = P0^3;
sbit H0 = P0^4;
sbit H1 = P0^5;
sbit H2 = P0^6;
sbit H3 = P0^7;
uchar TH0_DATA = 0;
uchar TL0_DATA = 0;
uchar code qin[] = {262,277,293,329,349,392,440,494};
評(píng)論