题目:在无源蜂鸣器上循环演奏低音“DO、RE、MI、FA、SO、LA、SI”这7个音调。
音调表:
?编程思路:
#include <reg52.h>
#include "TimeInit.h"
#define Fosc (11059200L)
unsigned char i=0£?
unsigned char cnt=0;
//char Tone[]={'D','R','M','F','S','L','x'};
code unsigned char FreqH[]={0xF8,0xF9,0xFA,0xFA,0xFB,0xFB,0xFC};
code unsigned char FreqL[]={0x8B,0x5B,0x14,0x66,0x03,0x8F,0x0B};
sbit beep=P3^7;
void main(void)
{
T0Init(10);
T1Init(50);
while(1);
}
void T0Tnt(void) interrupt 1
{
VTH0=FreqH[i];
VTL0=FreqL[i];
TH0=VTH0;
TL0=VTL0;
beep=~beep;
}
void T1Tnt(void) interrupt 3
{
TH1=VTH1;
TL1=VTL1;
cnt++;
if(cnt>=10)
{
cnt=0;
i++;
if(i>=7)
{
i=0;
}
}
}
?
#include <TimeInit.h>
#define Fosc (11059200L)
unsigned char VTH0,VTL0,VTH1,VTL1;
void T0Init(unsigned int vT0)
{
TMOD=(TMOD&0xf0)|0x01;
VTH0=(65536-vT0*11/12)/256;
VTL0=(65536-vT0*11/12)%256;
TH0=VTH0;
TL0=VTL0;
EA=1;
ET0=1;
TR0=1;
}
void T1Init(unsigned char vT1)
{
TMOD=(TMOD&0x0f)|0x10;
VTH1=(65536-vT1*Fosc/12000)/256;
VTL1=(65536-vT1*Fosc/12000)%256;
TH1=VTH1;
TL1=VTL1;
EA=1;
ET1=1;
TR1=1;
}
?
?
#ifndef _TIMEINIT_H_
#define _TIMEINIT_H_
#include <reg52.h>
#define Fosc (11059200L)
extern unsigned char VTH0,VTL0,VTH1,VTL1;
extern void T0Init(unsigned int vT0);
extern void T1Init(unsigned char vT1);
#endif
?
?
?
|