用單片機+PWM來解碼WAV真人聲樂
#include
#include "wav.h"
sbit dataOut = P2^2;
#define maxSize 2060
unsigned char pwmData=0;
unsigned char pwmCycle=256; //pwm的頻率
unsigned int datIndex=0;
void TimerInit() {
TMOD=0x22;
TH0=(256-pwmData)%256;
TH1=(256-(pwmCycle-pwmData))%256;
RCAP2H=0xfe; //1
RCAP2L=0xc7; //199
TH2=RCAP2H;
TL2=RCAP2L;
PT2=0;//高中斷優(yōu)先級
TR0=1;TR1=0;TR2=1; //啟動/停止定時器
ET0=1;ET1=1;ET2=1; //打開中斷
EA=1;
}
///////////////////定時器0中斷//////////////////////
Timer0() interrupt 1 {
TR0=0; //turn off Timer0
dataOut=0;
TR1=1; //turn on Timer1
}
///////////////////定時器1中斷///////////////////////
Timer1() interrupt 3 {
TR1=0; //turn off Timer1
dataOut=1;
TH0=(256-pwmData)%256;
TH1=(256-(pwmCycle-pwmData))%256;
TR0=1; //turn on Timer0
}
///////////////////定時器2中斷///////////////////////
Timer2() interrupt 5 {
TF2=0;
pwmData=wavdata1[datIndex];
if(++datIndex==maxSize){
datIndex=0;
}
}
main(){
TimerInit();
while(1);
{}
}
評論