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

📄 裴远华.cpp

📁 音乐发生器及同步显歌词本试验从硬件电路原理图
💻 CPP
字号:
#include <dos.h>    // getvect() and setvect()
#include <conio.h>  // kbhit()
#include <stdlib.h> // exit(0)
#include <stdio.h>  // printf()

#ifdef __cplusplus
  #define __CPPARGS ...
#else
  #define __CPPARGS
#endif
// 声明歌曲“Yesterday once more”的音乐频率
short int nFreq[] = 
{
262,262,294,
330,392,392,330,392,330,
440,392,330,330,392,
440,294,330,392,
440,330,392,
440,660,588,523,494,
392,330,392,330,
262,262,294,
330,392,392,330,392,330,
440,392,330,330,392,
440,494,330,392,440,
494,588,
523,494,440,494,523,494,
523,494,440,440,494,
523,523,440,523,588,
523,588,
660,660,660,660,588,523,
494,523,494,440,330,392,
523,588,
660,660,660,660,660,588,523,
494,523,494,440,330,392,
440,494,
523,494,523,588,523,494,
523,494,523,588,523,588,
660,660,588,523,440,
330,349,330,392,
262,330,330,294,262,294,
330,
0 // 设置0为歌曲结束符
};
// 声明歌曲“Yesterday once more”的音乐时延
short int nTimeDelay[] = 
{
4,4,4,
8,8,4,4,4,4,
8,12,4,4,4,
8,8,4,12,
16,4,4,
8,8,4,8,12,
4,4,8,16,
4,4,4,
4,8,8,4,4,4,
4,8,12,4,4,
8,8,4,8,12,
8,8,
4,4,12,4,4,4,
4,8,12,4,4,
8,8,4,8,12,
8,8,
4,4,4,8,4,4,
4,4,4,8,4,24,
4,4,
4,4,4,4,8,4,4,
4,4,4,12,4,24,
4,4,
4,4,4,12,4,4,
4,4,4,12,4,4,
8,8,4,8,12,
8,8,4,24,
4,4,4,2,6,4,
24
};
// 声明歌曲“Yesterday once more”的歌词
char  a[]="\nWhen I  was  young\nI~d  lis-ten to  the  ra-di-o  wait-ing for  my   favo-rite  songs\nWhen  they  played I~d  sing  a-long\nIt    made  me  smile\nThose  were  such  hap-py  times   and   not  so   long   a   go\nHow  I  won-dered   where  they~d  gone\nBut they~er  back  a-gain   just  like  a    long   lost   friend\nAll  the   song   I   love   so   well\nEv-ery   sha la la la   ev-ery  wo wo wo wo   still  shines\nEv-ery shing a ling a ling that they~re starting to sing so fine\nWhen they get to the part where she~s break-ing her heart\nIt can real-ly made me cry just like be-fore\nIt~-s  yes-ter-day  once more";

const unsigned long CLK = 1193180; // 声明时钟频率
const unsigned char INTR = 0X1C; // 声明中断号

short int  *pFreq; // pointer of frequency
short int  *pTimeDelay; // pointer of time delay
char   *iterator;//pointer  of  song

void interrupt (*oldhandler)(__CPPARGS);
void interrupt handler(__CPPARGS)
{
     // decrease the global counter
     (*pTimeDelay) -- ; // time delay decrease
     
     oldhandler(); // call the old routine
}

int  Playing(void);

void main(void)
{
     printf("\nYesterday once more");
     Playing();
}

int  Playing(void)
{
     // save the old interrupt vector
     oldhandler = getvect(INTR);

     // install the new interrupt handler
     setvect(INTR, handler);

     outportb( 0x303, 0x90); // 设置8255 的工作模式
     outportb( 0x303, 0x0c); // 置pc6=0 禁止8253通道2的gate引脚
     outportb( 0x303, 0x00); // 置pc0=0 禁止7408

     // set 8253's control word, select channel 2,
	 // make it work at mode 3, binary counting
	 // first send to low byte, then send to high byte
     outportb( 0x307, 0xb6);

     pFreq = nFreq; // 取歌曲的第一个音频
     pTimeDelay = nTimeDelay; // 取歌曲的第一个时延
     iterator=a;//取歌词的第一个字符地址

     // 未遇歌曲结束符或未按任意键则继续循环
     while ( *pFreq && !kbhit()) 
	 {
	   // 置8253通道2 的计数初值
       // first send to low byte
	   outportb( 0x306, (unsigned char)(CLK / *pFreq % 256)); 

	   // then send to high byte
	   outportb( 0x306, (unsigned char)(CLK / *pFreq / 256)); 

	   //set 8255's PC6=1 and PC0=1
	   outportb( 0x303, 0x01); 
	   outportb( 0x303, 0x0d);

	   while ( *pTimeDelay){}; //等待延时到

	   // set 8255's PC6=0 and PC0=0
	   outportb( 0x303, 0x0c);
	   outportb( 0x303, 0x00);

      while(*iterator>='a'&&*iterator<='z'||*iterator>='A'&&*iterator <='Z'||*iterator=='~')
      	    {printf("%c",*iterator);
            iterator++;
           }
      while(*iterator ==' ')
           {printf("%c",*iterator);
            iterator ++;
           }
      if(*iterator =='-')
                iterator++;
      if(*iterator =='\n')
           {printf("%c",*iterator);
            iterator++;
           }          //显示歌词

	   // get the next frequency and time delay
	   pFreq++;
	   pTimeDelay++;
} // 歌曲结束或按了任意键

     // set 8255.PC6=0 pc0=0
     outportb( 0x303, 0x0c);
     outportb( 0x303, 0x00);

     // reset the old interrupt handler
     setvect(INTR,oldhandler);

     return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -