?? audio.c
字號:
#include "C8051F020.h"
unsigned char m;
unsigned char rti;
unsigned char len;
unsigned char code toneh[14] = {0xfc,0xfc,0xfd,0xfd,0xfd,0xfd,0xfe,
0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,0xff}; /*音頻表:高位. */
unsigned char code tonel[14] = {0x44,0xac,0xba,0x34,0x82,0xc8,0x05,
0x22,0x56,0x84,0x9a,0xc1,0xe4,0x03}; /*音頻表:低位 */
//unsigned char code song[]="123112313455345556543256543215111511";
//unsigned char code length[]="444444444444444422224422224444444444"; /* 音譜 */
//梁祝
unsigned char code song[]="5321112uytttttu6755676567611656523432111572y1ttty1tttteetty112y1t551665352222223uytty12e1yyt61tttt335u2y1tt5eetetyu2yyyty6112532321yte311yqytety1tttttt5553tttttttt";
unsigned char code length[]="444422444424224444124422224122222222242444444424224242424124122244122122242424124441244442122242424124422442211222224242241244422422224222222222424222422442224222";
//兩只蝴蝶
//unsigned char code song[]="3233232116122321yytt323323211y122321y122";
//unsigned char code length[]="2242222422242222224222422224222422222242"; /* 音譜 */
void SysInit()
{
int i; // delay counter
WDTCN = 0x07; // Watchdog Timer Control Register
WDTCN = 0xde; // disable watchdog timer
WDTCN = 0xad;
OSCXCN = 0x67; // start external oscillator with 25MHz crystal
for (i=0; i < 256; i++) ; // XTlVlD blanking interval (>1ms)
while (!(OSCXCN & 0x80)) ; // Wait for crystal osc. to settle
OSCICN = 0x88; // select external oscillator as SYSClK
// source and enable missing clock detector
// OSCICN = 0x97; // select internal oscillator as SYSClK
XBR2 = 0x40; // Enable crossbar and weak pull-ups
P1MDOUT = 0xFF;
}
void timer0(void) interrupt 1 /*定時器0中斷服務程序 */
{
P11 = !P11; /* P1口取反 */
TH0 = toneh[rti]; /* 裝入音頻初值 */
TL0 = tonel[rti];
}
void timer1(void) interrupt 3 /*定時器1中斷服務程序 */
{
TH1 = 0x34; /*重裝定時初值 */
TL1 = 0x8c;
m++;
}
unsigned char playc(char ch) /*音符---音頻轉換程序 */
{
unsigned char ti;
switch (ch)
{
case'q': ti=0; break;
case'w': ti=1; break;
case'e': ti=2; break;
case'r': ti=3; break;
case't': ti=4; break;
case'y': ti=5; break;
case'u': ti=6; break;
case'1': ti=7; break;
case'2': ti=8; break;
case'3': ti=9; break;
case'4': ti=10; break;
case'5': ti=11; break;
case'6': ti=12; break;
case'7': ti=13; break;
case' ': ti=50; break;
default: ti=50; break;
}
if (ti==50)
return(100);
return(ti);
}
void main(void)
{
unsigned char i;
SysInit();
m = 0;
TMOD = 0x11; /* 定時方式1 */
P11 = 0;
TH0 = toneh[0]; /*定時器字置音頻初值 */
TL0=tonel[0];
TH1=0x34; /*定時器置定時25ms初值 */
TL1=0x8c;
IP=0x08; /*定時器1中斷優先 */
EA=1; /*開CPU中斷 */
ET0=1; /*開T/C0中斷 */
ET1=1; /*開T/C1中斷 */
TR0=1; /*啟動T/C0 */
TR1=1; /*啟動T/C1 */
//for(i=0; i<36; i++)
i=0;
while(song[i]!='\0')
{
rti=playc(song[i]);
len=5*(length[i]-0x30);
while(m<len) ;
m=0;
i++;
}
TR0=0; /*關相關中斷 */
TR1=0;
P11=0; /*關PC喇叭 */
while(1) ;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -