生日快樂
[(⊙o⊙)…額帮寻!哈哈援制!]昨天晚上好不容易搞好軟件問題【(~ ̄(OO) ̄)ブ】,那不得趁熱打鐵趕緊整一下使自己玩玩货徙,就照書打了代碼花了原理圖左权,成功聽到仿真聲音,不覺有些小激動(dòng)
書中代碼區(qū)
#include <reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit BEEP=P1^0; //定義喇叭輸出端口
uchar tick,t1,th; //定義節(jié)拍和T0初始變量
uchar TABLE[]={ //音符節(jié)拍碼表
0x82,0x01,0x81,0x94,0x84,0xB4,0xA4,0X04,
0X82,0X01,0X81,0X94,0X84,0XC4,0XB4,0X04,
0X82,0X01,0X81,0XF4,0XD4,0XB4,0XA4,0X94,
0XE2,0X01,0XE1,0XD4,0XB4,0XC4,0XB4,0X04,
0X82,0X01,0X81,0X94,0X84,0XB4,0XA4,0X04,
0X82,0X01,0X81,0X94,0X84,0XC4,0XB4,0X04,
0X82,0X01,0X81,0XF4,0XD4,0XB4,0XA4,0X94,
0XE2,0X01,0XE1,0XD4,0XB4,0XC4,0XB4,0X04,
0X00,
};
uchar TABLE1[]={ //音符對應(yīng)的定時(shí)器初始表
0xfd,0x04,0xfd,0x90,0xfc,0x09,0xfc,0x44,
0xfc,0xac,0xfb,0x09,0xfb,0x34,0xfb,0x82,
0xfb,0xc8,0xfe,0x06,0xfe,0x22,0xfe,0x56,
0xfe,0x85,0xfe,0x9a,0xfe,0xc1,
};
/********************************T0中斷服務(wù)函數(shù) *******************************/
timer0() interrupt 1 using 1{
TL0=t1;TH0=th; //重裝定時(shí)初值
BEEP=~BEEP; //喇叭輸出定時(shí)取反
}
/***************************基本單位延時(shí)函數(shù)*************************/
void delay1(){
uint i;
for(i=0;i<20000;i++);
}
/*****************************節(jié)拍延時(shí)函數(shù)**************************/
void delay(tt){
uchar i;
for (i=0;i<=tt;i++) delay1();
}
/***********************************主函數(shù)******************************/
void main(){
uchar t,t1,k=0;
while(1){
TMOD=0X01;IE=0X82; //定義T0工作方式痴颊,開中斷
while (TABLE[k]!=0){ //判斷節(jié)拍碼是否為結(jié)束碼
tick=(TABLE[k])&0x0f; //不是則取節(jié)拍碼
t=(_crol_(TABLE[k],4))&0x0f;//取音符碼
if(t!=0){ //判斷取得的音符碼是否為0
t1=--t*2+1; //不是根據(jù)取得的音符碼計(jì)算T0初值
t=t*2;
t1=TL0=TABLE1[t1];
th=TH0=TABLE1[t];
TR0=1; //啟動(dòng)T0
}
else TR0=0; //取得音符碼為0則停止T0
delay(tick); //根據(jù)節(jié)拍碼延時(shí)
k++;
}
TR0=0; //取得結(jié)束碼則停止T0
}
}