---
title: "C语言调用汇编-电子琴"
author: "poower"
date: "2008-06-30T09:58:00Z"
tags: []
slug: "c"
draft: false
lastmod: "2008-06-30T09:58:00Z"
id: 715348
url: "https://poower.iteye.com/blog/715348"
---

/\*sound.h\*/  
/\*http://blog.csdn.net/scs2000/archive/2006/09/14/1221079.aspx\*/  
#include &lt;stdio.h&gt;  
#include &lt;conio.h&gt;  
#define NOO 1450  
int music1\[8]\[6]={  
    {262,262,294,262,349},  
    {330,262,262,294,262},  
    {392,349,262,262,523},  
    {440,349,262,262,466},  
    {466,440,262,392,349}  
};  
int time1\[8]\[6]={  
    {100,100,200,200,200},  
    {200,200,100,100,200},  
    {200,200,200,200,200},  
    {200,200,200,200,200},  
    {100,200,200,200,200}  
};  
int music2\[3]\[10]={  
    {330,392,330,294,330,392,330,294,330,330},  
    {330,392,330,294,262,294,330,392,294,294},  
    {262,262,220,196,196,220,262,294,332,262}  
};  
int time2\[3]\[11]={  
    {200,200,200,100,100,200,100,100,200,200},  
    {200,200,100,100,200,200,100,100,200,200},  
    {200,100,100,200,100,100,200,100,100,400}  
};  
int music3\[4]\[11]={  
    {441,393,330,393,131,441,393,441,441,1450,1450},  
    {330,393,441,393,330,262,882,393,330,294,294},  
    {294,330,393,393,441,330,294,262,262,1450,1450},  
    {393,330,294,262,882,262,786,786,786,1450,1450}  
};  
int time3\[4]\[11]={  
    {200,100,200,200,200,100,100,200,200,0,0},  
    {200,100,100,200,200,100,100,100,100,200,200},  
    {200,100,200,100,100,200,200,200,200,0,0},  
    {200,100,100,100,100,100,200,200,200,0,0}  
};  
const char * process\_file\[]={  
                   " ",  
                   "┌───┬┬┐┌┬┐┌┬┐┌┬┐┌┬┐┌┬┐┌┬┐┌┬┬───┐",  
                   "│     ├接┤├口┤├技┤├术┤├课┤├程┤├设┤├计┤     │",  
                   "├───┴┴┘└┴┘└┴┘└┴┘└┴┘└┴┘└┴┘└┴┴───┤",  
                   "│                     《音乐发生器》                       │",  
                   "├──────────────────────────────┤",  
                   "│                                                           │",  
                   "├──────────────────────────────┤",  
                   "│                                                           │",  
                   "│                   A.播放第一首音乐                       │",  
                   "│                   B.播放第二首音乐                       │",  
                   "│                   C.播放第三首音乐                       │",  
                   "│                   Q.退出该程序                           │",  
                   "│                                                           │",  
                   "├──────────────────────────────┤",  
                   "│   制作：王康                                             │",  
                   "│   日期：年月日                                    │",  
                   "│                                                            │",  
                   "└──────────────────────────────┘"};  
const char * end\_file\[]={  
                   " ",  
                   "┌─────┐   ┌─┐┌─┐     ┌─┐┌─┐   ┌─────┐",  
                   "│★★★★★├☆☆┤个││人├☆☆☆┤简││介├☆☆┤★★★★★│",  
                   "├─────┘   └─┘└─┘☆└─┘└─┘   └─────┤",  
                   "├───────────────☆───────────────┤",  
                   "│姓名：王康                                                   │",  
                   "├───────────────────────────────┤",  
                   "│性别：男                                                     │",  
                   "├───────────────────────────────┤",  
                   "│班级：                                                        │",  
                   "├───────────────────────────────┤",  
                   "│电话：--XXXXXXXX                                          │",  
                   "├───────────────────────────────┤",  
                   "│QQ号：                                                │",  
                   "├───────────────────────────────┤",  
                   "│邮箱：wkjs@163.com                                            │",  
                   "├───────────────────────────────┤",     
                   "│                        谢谢您的使用                        │",  
                   "└───────────────────────────────┘"};  
void light(int a)  
{  
    asm MOV DX,283H  
    asm MOV AL,80H  
    asm OUT DX,AL  
    asm MOV DX,280H  
    asm MOV AL,0H;  
    asm OUT DX,AL  
    switch(a)  
    {  
    case 262:  
    case 131:  
        asm MOV AL,01H  
        asm OUT DX,AL  
        break;  
    case 294:  
        asm MOV AL,03H  
        asm OUT DX,AL  
        break;  
    case 330:  
        asm MOV AL,07H  
        asm OUT DX,AL  
        break;  
    case 350:  
        asm MOV AL,0FH  
        asm OUT DX,AL  
        break;  
    case 393:  
    case 786:  
        asm MOV AL,1FH  
        asm OUT DX,AL  
        break;  
    case 441:  
    case 882:  
        asm MOV AL,3FH  
        asm OUT DX,AL  
        break;  
    case 495:  
        asm MOV AL,7FH  
        asm OUT DX,AL  
        break;  
    };  
}  
void sound\_A(int a,int b)  
{                
        int i=0;  
        asm MOV DI,a    /\*选择号计数器，先写低字节，后写高字节，选择工作方式，二进制\*/  
        asm MOV AL,10110110B  
        asm MOV DX,12H  
        asm MOV AX,34DEH  
        asm DIV DI  
        asm OUT 42H,AL /\*先送低字节到号计数器\*/  
        asm MOV AL,AH   /\*取高字节送AL\*/  
        asm OUT 42H,AL /\*后送高字节到号计数器\*/  
        asm IN AL,61H   /\*读入的PB口原输出值\*/  
        asm MOV AH,AL    
        asm OR AL,3     /\*8255的PB0口PB1口输出有效\*/  
        asm OUT 61H,AL  
        /\*调用灯亮函数\*/  
        light(a);  
        /\*时间延迟\*/  
        for(i=0;i&lt;b;i++)  
        {  
            delay(2000);  
        }  
        /\*关闭扬声器\*/  
        asm MOV AL,AH  
        asm OUT 61H,AL  
}  
void sound(char a)  
{  
    int i,j;  
    if(a=='a')  
    {  
        for(i=0;i&lt;5;i++)  
        {  
            for(j=0;j&lt;5;j++)  
            {  
                sound\_A(music1\[i]\[j],time1\[i]\[j]);  
            }  
        }  
    }  
    else if(a=='b')  
    {  
        for(i=0;i&lt;3;i++)  
        {  
            for(j=0;j&lt;11;j++)  
            {  
                sound\_A(music2\[i]\[j],time2\[i]\[j]);  
            }  
        }  
    }  
    else if(a=='c')  
    {  
        for(i=0;i&lt;4;i++)  
        {  
            for(j=0;j&lt;11;j++)  
            {  
                sound\_A(music3\[i]\[j],time3\[i]\[j]);  
            }  
        }  
    }  
    else  
    {  
        printf("\\t对不起，没有该音乐！\\n");  
    }  
}  
void face()  
{  
    int i;  
    system("cls");  
    for(i=0;i&lt;19;i++)  
    {  
        printf("\\t%s\\n",process\_file\[i]);  
        delay(20000);  
    }  
    printf("\\t请输入您的指令：");  
}  
void endface()  
{  
    int i;  
    system("cls");  
    for(i=0;i&lt;19;i++)  
    {  
        printf("\\t%s\\n",end\_file\[i]);  
        delay(20000);  
    }  
    printf("\\t");  
}  
void operate()  
{  
    char a;  
    face();  
    while(1)  
    {  
        a=getche();  
        switch(a)  
        {  
        case 'a':  
        case 'A':  
            sound('a');  
            printf("\\n\\t音乐播放结束，谢谢你的收听，点任意键继续！");  
            getch();  
            face();  
            break;  
        case 'b':  
        case 'B':  
            sound('b');  
            printf("\\n\\t音乐播放结束，谢谢你的收听，点任意键继续！");  
            getch();  
            face();  
            break;  
        case 'c':  
        case 'C':  
            sound('c');  
            printf("\\n\\t音乐播放结束，谢谢你的收听，点任意键继续！");  
            getch();  
            face();  
            break;  
        case 'q':  
        case 'Q':  
            endface();  
            exit(1);  
            break;  
        default:  
            printf("\\t您的输入有误,请核实，谢谢！");  
            getch();  
            face();  
            break;  
        }  
    }  
}  
 /\*sound.c\*/

void main()  
{  
    operate();  
}