单片机IO口播放乐曲

2008-02-23 05:27:49来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折

标 题: 单片机IO口播放乐曲 //
//--------------------------------------------------------------------------//
// 源程式大公开 //
// (c) Copyright 2001-2002 xuwenjun //
// All Rights Reserved //
// V1.00 //
//--------------------------------------------------------------------------//
//标 题: 单片机IO口播放乐曲 //
//版 本: V1.00 //
//文档名: xwj_lcd16.C //
//创建人: 徐文军 E-mail:xuwenjun@21cn.com //
//日 期: 02-11-5 //
//描 述: 单片机利用T1中断在IO口P1.0输出乐曲 //
//声 明: //
// 以下代码仅免费提供给学习用途,但引用或修改后必须在文档中声明出处. //
// 如用于商业用途请和作者联系. E-mail:xuwenjun@21cn.com //
// 有问题请mailto xuwenjun@21cn.com 欢迎和我交流! //
//--------------------------------------------------------------------------//
//修改人: 徐文军 E-mail:xuwenjun@21cn.com //
//日 期: 02-11-5 //
//说 明: //
// 单片机用T1中断在IO口P1.0输出乐曲(在P1.0口和VCC间接一喇叭即可听到) //
// 1.由于输出的是方波,比较难听:-)您也能够将波形细分用DA输出正弦波 //
// 2.由于T1频繁中断,任何其他中断发生都会是乐曲打顿,只适合试验 //
// 3.优化音符编码:一个字节中包含节拍、音符、升降调,可区分数据和命令//
// 同时人工将音符译为机器码很容易。(格式看下面实例)//
//--------------------------------------------------------------------------//
#include
#include
#define FREQ 11059200
#define freq_chn(f) 255-f/24/147/256,256-f/24/147%6

// 音乐_编码2 -- 存储器定义 //
unsigned char code music_tab1[]={ //音符码格式:| D7 |D6 D5 D4| D3 |D2 D1 D0|
//梁祝 |变调| 节拍|升降| 音符 |
0xcb, //示例:0xcb=11001011B,即升调,4/16拍,3Mi低音
0x35,0x16,0xb9,0x12,0x96,0x99,0xa5,
0xbd,0x99,0x96,0x15,0x13,0x15,0x62,
0x32,0x13,0xa7,0x26,0x25,0x16,0xa9,0x22,
0xa3,0xa9,0x96,0x15,0x16,0x99,0xe5,
0xbb,0x15,0xa7,0xaa,0x96,0x99,0xd5,
0x13,0x15,0x23,0x15,0x16,0x17,0x9a,0xd6,0x15,0x16,
0xb9,0x12,0x25,0x23,0x23,0x22,0x13,0x12,0x21,0x96,0x15,
0x43,0xc9,0x96,0x99,0x96,0x15,0x13,0x15,0x16,0x99,
0xd5, //功能码格式:| D7 D6 D5 D4 | D3 D2 D1 D0|
// |高4位=0时是功能码|根据低4位散转 |
0x00, //最后字节为功能码必须==0x00退出
};
unsigned char code music_tab2[]={ //康定情歌
0x23,0x25,0x26,0x16,0x15, 0x36,0x13,0x42,
0x23,0x25,0x26,0x16,0x15, 0x26,0x53,
0x23,0x25,0x26,0x16,0x15, 0x26,0x23,0x42,
0x25,0x23,0x12,0x13,0x12,0x11, 0x22,0xd6,
0x26,0xda, 0x25,0x53, 0x12,0x11,0xd6,
0xad,0x23,0x12,0x13,0x12,0x11, 0x22,0xd6,
0x00,
};
unsigned char code music_tab3[]={ //美酒加咖啡
0x55,0x23,0x25,0x26,0xa9,0x22, 0x73,
0x45,0x25,0x26,0x23,0x22,0x21,0x23, 0x72,
0x52,0x23,0x45,0x43, 0x22,0x21,0xa6,0x25,0x66,
0xc9,0xa6,0x25,0x23,0x25,0x26,0xa9, 0xf5,
0x45,0x23,0x25,0x46,0xc9, 0x22,0x13,0x12,0x21,0x22,0x63,
0x22,0x23,0x25,0x26,0x25,0x23,0x22,0x21, 0x72,
0x52,0x23,0x45,0x43, 0x22,0x21,0xa6,0x25,0x66,
0xb9,0x12,0x23,0x25,0x22,0xa5,0xab,0x22, 0x71,
0x00,
};
unsigned char code music_tab4[]={ //北国之春
0x15,0x16,0x75, 0x15,0x16,0x55,0x13,0x15,0x16,0x15,0x16,0x99,0x12,0x13,
0x65,0x45,0x13,0x12,0x11,0x12, 0x73,
0x62,0x42,0x12,0x11,0x96,0x15, 0x76,
0x55,0x23,0x45,0xa9,0x22, 0x53,0x12,0x13,0x65,
0x52,0x22,0x42,0x21,0xa6, 0xe9,0x91,0x12,0x13,0x12,0x13,0x15,0x99,0x12,
//以上为过门
0x20,0x23,0x23,0x23,0x63, 0x22,0x23,0x23,0x22,0x41,0xa6,0x25,
0x53,0x22,0x21,0xa9,0x21,0xa6, 0x75,
0x26,0xa9,0x21,0xa6,0xc9,0x12,0x11,0x96,0x15,
0x23,0x25,0x25,0x26,0x45,0x21,0x22,
0x13,0x35,0x25,0xa9,0xa6,0x25,0x23,0x22, 0x71,
0x52,0x25,0x45,0x13,0x15,0x13,0x12, 0x21,0x41,0x22,0x43,0x45,
0x23,0x25,0x26,0xa9,0x42,0x12,0x33, 0x72,
0x51,0x22,0x43,0x22,0x21, 0xc6,0xc9,0x29,0xc6,0x45,0x23,0x23,
0x53,0x25,0x26,0x25,0x13,0x12,0x21, 0x62,0x42,0x23,0x25,
0x55,0x26,0x25,0x23,0x45, 0x26,0xa9,0x21,0x22,0x63,
0x52,0x22,0x22,0x13,0x12,0x21,0xa6,0xf9,
0x00,
};

unsigned char code music_l_tab[8]={0,1,2,3,4,6,8,16};//节拍延时单位
unsigned char code music_freq_tab[64]={ //音符定时器值表
0xff,0xea, //0 休止符
freq_chn(131), //freq_chn(x/256 //1 do 高字节 //freq_chn(x%6 //1 do 低字节
freq_chn(147), //2 re
freq_chn(165), //3 mi
freq_chn(175), //4 fa
freq_chn(196), //5 suo
freq_chn(221), //6 la
freq_chn(248), //7 xi

0xff,0xea, //0 休止符
freq_chn(262), //1 do
freq_chn(294), //2 re
freq_chn(330), //3 mi
freq_chn(350), //4 fa
freq_chn(393), //5 suo
freq_chn(441), //6 la
freq_chn(495), //7 xi

0xff,0xea, //0 休止符
freq_chn(525), //1 do
freq_chn(589), //2 re
freq_chn(661), //3 mi
freq_chn(700), //4 fa
freq_chn(786), //5 suo
freq_chn(882), //6 la
freq_chn(990), //7 xi

0xff,0xea, //0 休止符
freq_chn(1049), //1 do
freq_chn(1178), //2 re
freq_chn(1322), //3 mi
freq_chn(1400), //4 fa
freq_chn(1572), //5 suo
freq_chn(1665), //6 la
freq_chn(1869), //7 xi

};

unsigned char code music_frequp_tab[64]={ //升半音
0xff,0xea, //0 休止符
freq_chn(139), //1 do 高字节
freq_chn(156), //2 re

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇: 学习指针不可少的好文章 !

下一篇: 按键程式设计,能够处理加速键、一键多功能等情况