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

📄 课程设计样品cexp5.cpp

📁 综合微机通用总线、定时计数器、并行接口、中断控制器等芯片等控制原理
💻 CPP
字号:

//*------------------------------------------------------*/
//*    Cexp5.cpp(音乐发生器接口实验)                       */
//*    音乐发生器程序Source File                           */
//*    Copyright (c) 2001 by HUST                        */
//*------------------------------------------------------*/


#include <dos.h>       //getvect(),setvect();
#include <conio.h>     //kbhit();
#include <stdlib.h>    //exit(0);
#include <stdio.h>     //printf();

#ifdef __cplusplus
  #define __CPPARGS 
#else
  #define __CPPARGS
#endif

short int nFreq[] = {
    //声明歌曲“两个老虎”的音乐频率 
    262,294,330,262,
    262,294,330,262,
    330,349,392,
    330,349,392,
    392,440,392,349,330,262,
    392,440,392,349,330,262,
    294,196,262,
    294,196,262,
    0
};
short int nTimeDelay[] = {
    //声明歌曲“两个老虎”的音乐延时 
    8,8,8,8,
    8,8,8,8,
    8,8,16,
    8,8,16,
    4,4,4,4,8,8,
    4,4,4,4,8,8,
    8,8,16,
    8,8,16   
};

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

short int  *pFreq;      // 声明音频指针
short int  *pTimeDelay; // 声明延时指针

void interrupt (*oldhandler)(__CPPARGS);
void interrupt handler(__CPPARGS)
{
     (*pTimeDelay) -- ; // 延时点数减一
     oldhandler();      // 调用原例程
}


int  Playing(void);


void main(void)
{
   printf("now play music!\n"); 
   printf("strike ESC to exit\n"); 
     Playing();
}

int  Playing(void)
{
oldhandler = getvect(INTR); // 保存原中断向量
setvect(INTR, handler);     // 装载新中断向量
     outportb( 0x303, 0x90); // 初始化8255
     outportb( 0x303, 0x0c); // 置pc6=0 禁止8253通道2的gate引脚
     outportb( 0x303, 0x00); // 置pc0=0 禁止7408
     outportb( 0x307, 0xb6); // 初始化8253

     pFreq      = nFreq;      // 取歌曲的第一个音频
     pTimeDelay = nTimeDelay; // 取歌曲的第一个时延

     while ( *pFreq && !kbhit()) 
	 {
	   outportb( 0x306, (unsigned char)(CLK / *pFreq % 256)); 
	   // 先送低字节
	   outportb( 0x306, (unsigned char)(CLK / *pFreq / 256)); 
// 再送高字节
       // 以上置8253通道2 的计数初值

	   outportb( 0x303, 0x01); 
	   outportb( 0x303, 0x0d); // 开扬声器

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

	   outportb( 0x303, 0x0c);
	   outportb( 0x303, 0x00); // 关闭扬声器

	   pFreq++;      // 获取下一个音频
	   pTimeDelay++; // 获取下一个延时
     } // 歌曲结束或按了任意键

    outportb( 0x303, 0x0c);
    outportb( 0x303, 0x00);   // 再次关闭扬声器
 
setvect(INTR,oldhandler); // 恢复原中断向量
 
     return 0;
}

⌨️ 快捷键说明

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