⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 audio.c

📁 单片机C8051F020的音频输出程序
💻 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 + -