>
首 页文章中心单片机论坛电脑援救中心杀毒软件下载电子元件速查集成电路速查客户留言下载中心商品展示频道繁體中文
设为首页
加入收藏
联系我们
您当前的位置:中国单片机网 -> 单片机讲座 -> 文章内容 退出登录 用户管理
栏目导航
· 经典软件 · 单片机讲座
· 电源技术 · 称重技术
· 硬件与电路 · 资料下载
· 淘宝商品 · 轻松一刻
· 金华电大资讯 · 胡老师招生
· l
热门文章
· [图文] 本站承接单片..
· 金华广播电视大学(金..
· 关于金华广播电视大..
· 关于金华广播电视大..
· [组图] 三极管s8050管..
· [组图] 9014三极管管..
· [组图] 三极管9012引..
· [组图] 中央广播电视..
· [组图] 三极管9013管..
· [组图] 万用表的使用..
相关文章
· [图文] 单片机串口通..
· 单片机百科知识
· 单片机对PT2262的编..
· 基于单片机的实验室..
· 中国单片机网 开通 ..
· [组图] 单片机在步进..
· 常用PIC系列8位单片..
· [组图] 利用单片机I/..
· 汇编指令大全(含指..
· 单片机基础知识汇总..
单片机IO口播放乐曲
作者:本站  来源:www.chinadpj.com  发布时间:2008-6-20 1:16:05  发布人:admin

减小字体 增大字体

单片机IO口播放乐曲

标 题: 单片机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


freq_chn(175), //3 mi
freq_chn(185), //4 fa
freq_chn(208), //5 suo
freq_chn(234), //6 la
freq_chn(262), //7 xi

0xff,0xea, //0 休止符
freq_chn(278), //1 do
freq_chn(312), //2 re
freq_chn(350), //3 mi
freq_chn(371), //4 fa
freq_chn(416), //5 suo
freq_chn(467), //6 la
freq_chn(525), //7 xi

0xff,0xea, //0 休止符
freq_chn(556), //1 do
freq_chn(624), //2 re
freq_chn(700), //3 mi
freq_chn(742), //4 fa
freq_chn(833), //5 suo
freq_chn(935), //6 la
freq_chn(1049), //7 xi

0xff,0xea, //0 休止符
freq_chn(1112), //1 do
freq_chn(1248), //2 re
freq_chn(1400), //3 mi
freq_chn(1484), //4 fa
freq_chn(1618), //5 suo
freq_chn(1764), //6 la
freq_chn(1968), //7 xi
};
unsigned char temp_TH1;
unsigned char temp_TL1;

// 音乐_编码2 -- IO定义 //
sbit PIN_MSC=P1^0; // 音乐输出端口 //

// 音乐_编码2 -- 函数原型//
void music_delay(unsigned char x); //音乐节拍延时
void music_play(unsigned char *msc); //播放音乐子程式

// 音乐_编码2 -- 函数 //
//---------------------------------------------------------------------------//
void music_int_t1 (void) interrupt 3 using 1 //定时中断1//
{
PIN_MSC=~PIN_MSC;
TH1=temp_TH1;
TL1=temp_TL1;
}

//---------------------------------------------------------------------------//
void music_delay(unsigned char n) //延时 125*n 毫秒
{
unsigned char i=125,j;
do {
do {
for (j=0; j<230; j ) _nop_(); //j=(11159KHz/12-10)/4
}while(--i);
}while(--n);
}

//---------------------------------------------------------------------------//
void music_play(unsigned char *msc) //音乐
{
unsigned char music_freq=32; //音高
unsigned char music_long; //节拍
unsigned char music_data=0; //音符数据
bit music_up=0; //升半音
bit music_break=0; //断奏
temp_TH1=0xff;
temp_TL1=0xea; //关输出(输出超声波)
TMOD=0x11; //T0:16位定时器、T1:16位定时器
// ET0=0; //关T0定时器中断
ET1=1; //开T1定时器中断
//---------------------------------------------------------
while (*msc != 0x00)
{
music_data=*msc & 0x07;
music_long=*msc>>4;
//---------------------------------------------------------
if (music_long != 0) //是音符
{
if (*msc >=128) //需升降调
{
if (((*msc) & 0x08) == 0)
{if (music_freq >= 16) music_freq -= 16;} //音高下降
else
{if (music_freq < 48) music_freq = 16;} //音高上升
}
//---------------------------------------------------------
if (music_up==1) //升半音
{
temp_TH1=music_frequp_tab[(music_freq (music_data <<1))];
temp_TL1=music_frequp_tab[(music_freq (music_data <<1) 1)];
}
else //不升半音
{
temp_TH1=music_freq_tab[(music_freq (music_data <<1))];
temp_TL1=music_freq_tab[(music_freq (music_data <<1) 1)];
}
//---------------------------------------------------------
if (music_break) //断奏
{
music_delay(music_l_tab[music_long&0x07]-1);
temp_TH1=0xff;
temp_TL1=0xea;
music_delay(1);
}
else //连奏
music_delay(music_l_tab[music_long&0x07]);
}
//---------------------------------------------------------
else //是功能码
{
switch(music_data)
{
case 0x04:
music_up=0; //不升半音
break;
case 0x05:
music_up=1; //升半音
break;
case 0x06:
music_break=0; //断奏
break;
case 0x07:
music_break=1; //连奏
break;
default:
break;
}
}
//---------------------------------------------------------
msc ;
}
ET1=0; //播放结束,关T1中断
PIN_MSC=1; //关输出
// temp_TH1=0xff;
// temp_TL1=0xea; //关输出(输出超声波)
// ET0=1; //开T0定时器中断
// TMOD=0x51; //T0:16位定时器、T1:16位计数器
}

//---------------------------------------------------------------------------//
void main(void) //示例
{
// 配置定时器--------------------------------------
TMOD = 0x11; // TMOD: 定时器1:模式1:16位;定时器2:模式1:16位 //
// TCON = 0x50; // TCON: 定时器 1运行; 定时器 2运行 //
// 配置并开启中断:
IP = 0x08; //中断优先寄存器IP:|D7_x |x|x |PS|PT1|PX1|PT0|D0_PX0|//
IE = 0x88; //中断允许寄存器IE:|D7_EA|x|ET2|ES|ET1|EX1|ET0|D0_EX0|//
for (;;) {
//在此调用键盘和显示函数//
music_play(music_tab1);
music_play(music_tab2);
music_play(music_tab3);
music_play(music_tab4);
}
}
//---------------------------------------------------------------------------//

[本站 www.chinadpj.com(中国单片机网),摘自 http://www.netinter.cn/info/html/chengxusheji/C-C--/20080224/10464.html 作者:-  ]

[] [返回上一页] [打 印] [收 藏]
下一篇文章:浙江贸易学校简介
∷相关文章评论∷    (评论内容只代表网友观点,与本站立场无关!) [更多评论…]
关于本站 - 网站帮助 - 广告合作 - 下载声明 - 友情连接 - 网站地图 - 管理登录
Copyright © 2002-2009 www.chinadpj.com. All Rights Reserved. 浙ICP备06040125号
Powered by:NewAsp SiteManageSystem Version 2.1 SP1 1030