C语言嵌入汇编实现音乐发声器

来源:互联网 发布:用友软件上海 编辑:程序博客网 时间:2024/05/07 06:46
*sound.h*/
#include <stdio.h>
#include <conio.h>
#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[]={
                   " ",
                   "┌───┬┬┐┌┬┐┌┬┐┌┬┐┌┬┐┌┬┐┌┬┐┌┬┬───┐",
                   "     ┤├┤├┤├┤├┤├┤├┤├     ",
                    ",
                   "   制作:                                             ",
                   "   日期:年月日                                    ",
                   "                                                            ",
                   "└──────────────────────────────┘"};
const char * end_file[]={
                 
              
                   "├───────────────────────────────┤",    
                   "                        谢谢您的使用                        ",
                   "└───────────────────────────────┘"};
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<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<5;i++)
         {
            for(j=0;j<5;j++)
             {
                 sound_A(music1[i][j],time1[i][j]);
             }
         }
     }
    else if(a=='b')
     {
        for(i=0;i<3;i++)
         {
            for(j=0;j<11;j++)
             {
                 sound_A(music2[i][j],time2[i][j]);
             }
         }
     }
    else if(a=='c')
     {
        for(i=0;i<4;i++)
         {
            for(j=0;j<11;j++)
             {
                 sound_A(music3[i][j],time3[i][j]);
             }
         }
     }
    else
     {
         printf("/t对不起,没有该音乐!/n");
     }
}
void face()
{
    int i;
     system("cls");
    for(i=0;i<19;i++)
     {
         printf("/t%s/n",process_file[i]);
         delay(20000);
     }
     printf("/t请输入您的指令:");
}
void endface()
{
    int i;
     system("cls");
    for(i=0;i<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*/
#include "sound.h"
void operate();
void main()
{
     operate();
}