標(biāo)題:
如何使用STC15f系列單片機(jī)的兩個(gè)定時(shí)器來播放音樂,求可用的源代碼
[打印本頁]
作者:
2646402530
時(shí)間:
2017-7-31 18:45
標(biāo)題:
如何使用STC15f系列單片機(jī)的兩個(gè)定時(shí)器來播放音樂,求可用的源代碼
如何使用STC15f系列單片機(jī)的兩個(gè)定時(shí)器來播放音樂,求可用的源代碼
一個(gè)定時(shí)器用于控制音調(diào)
一個(gè)用于控制節(jié)拍
中間不使用delay延時(shí)
網(wǎng)上找了好久沒找到
作者:
cjjcjj1
時(shí)間:
2017-7-31 22:01
提示:
作者被禁止或刪除 內(nèi)容自動(dòng)屏蔽
作者:
HC6800-ES-V2.0
時(shí)間:
2017-8-1 08:08
我這個(gè)程序是STC89C52RC的,你可以試試,應(yīng)該可以直接用的。#include <reg51.h>
sbit voice=P1^5;//蜂鳴器
unsigned char code sound[]={0xff,
0x26,0x20,0x2b,0x20,0x26,0x80,0xff,0x20,0x2b,0x20,0x26,0x20,0x26,0x10,//兩只蝴蝶
0x2b,0x10,0x30,0x80,0xff,0x40,0x39,0x20,0x30,0x20,0x2b,0x40,0x2b,0x20,
0x26,0x20,0x2b,0x20,0x30,0x20,0x39,0x20,0x39,0x10,0x30,0x10,0x40,0x80,
0x40,0x80,0x26,0x20,0x2b,0x20,0x26,0x80,0xff,0x20,0x2b,0x20,0x26,0x20,
0x26,0x10,0x2b,0x10,0x30,0x80,0xff,0x40,0x39,0x20,0x30,0x20,0x2b,0x40,
0x2b,0x20,0x26,0x20,0x2b,0x20,0x30,0x20,0x39,0x20,0x30,0x20,0x2b,0xc0,
0x26,0x20,0x2b,0x20,0x26,0x80,0xff,0x20,0x2b,0x20,0x26,0x20,0x26,0x10,
0x2b,0x10,0x30,0xc0,0x39,0x20,0x30,0x20,0x2b,0x40,0x2b,0x20,0x26,0x20,
0x2b,0x20,0x30,0x20,0x39,0x20,0x39,0x10,0x30,0x10,0x40,0xc0,0x26,0x20,
0x20,0x20,0x20,0x80,0xff,0x20,0x20,0x20,0x1c,0x20,0x20,0x20,0x26,0xc0,
0x2b,0x20,0x2b,0x20,0x2b,0x60,0x2b,0x20,0x26,0x20,0x2b,0x20,0x30,0x20,
0x39,0x20,0x39,0x20,0x30,0x20,0x30,0x20,0x30,0xc0,0xff,0x40,0xff,0x40,
0xff,0x20,0x20,0x20,0x20,0x20,0x1c,0x20,0x18,0x20,0x19,0x20,0x19,0x20,
0x1c,0x20,0x26,0x20,0x2b,0x20,0x2b,0x20,0x26,0x20,0x26,0x80,0xff,0x20,
0x26,0x20,0x26,0x20,0x20,0x20,0x1c,0x40,0x1c,0x40,0xff,0x20,0x39,0x20,
0x26,0x20,0x2b,0x20,0x2b,0xc0,0x26,0x20,0x20,0x20,0x20,0x20,0x26,0x20,
0x20,0x40,0xff,0x20,0x18,0x40,0x19,0x20,0x1c,0x20,0x1c,0x10,0x19,0x10,
0x26,0x40,0xff,0x20,0x1c,0x20,0x1c,0x20,0x19,0x20,0x1c,0x20,0x20,0x20,
0x26,0x20,0x2b,0x20,0xff,0x20,0x26,0x20,0x2b,0x20,0x26,0x20,0x20,0x80,
0xff,0x20,0x20,0x20,0x20,0x20,0x1c,0x20,0x18,0x20,0x19,0x20,0x19,0x20,
0x1c,0x20,0x26,0x20,0x2b,0x20,0x2b,0x20,0x26,0x20,0x26,0x80,0xff,0x20,
0x26,0x20,0x26,0x20,0x20,0x20,0x1c,0x40,0x1c,0x40,0xff,0x20,0x39,0x20,
0x26,0x20,0x2b,0x20,0x2b,0xc0,0x26,0x20,0x20,0x20,0x20,0x20,0x26,0x20,
0x20,0x40,0xff,0x20,0x18,0x40,0x19,0x20,0x1c,0x20,0x1c,0x10,0x19,0x10,
0x26,0x40,0xff,0x20,0x1c,0x20,0x1c,0x20,0x19,0x20,0x1c,0x20,0x20,0x20,
0x26,0x20,0x2b,0x20,0xff,0x20,0x40,0x40,0x39,0x20,0x30,0xc0,0xff,0xc0,
0x00
};//音樂數(shù)據(jù),0xff為起始數(shù)據(jù),0x00為終止數(shù)據(jù),以后數(shù)據(jù)兩兩一組,前者為音階,后者為節(jié)拍
unsigned char zdjs=0,jp;
void del(yj);
void main(void)
{
unsigned int dpjs=0;
unsigned char yj;
TMOD=0x01;
IE=0x82;
TH0=0xd8;
TL0=0xf0;
TR0=1;
while(1)
{
zdjs=0;
dpjs++;
yj=sound[dpjs];
dpjs++;
jp=sound[dpjs];
while(zdjs!=jp)
{
if(yj!=0xff)
{
if(yj!=0)
{
voice=!voice;
del(yj);
}
else
{
dpjs=0;
break;
}
}
else
{
voice=1;
del(jp);
}
}
}
}
time0() interrupt 1 using 1//定時(shí)器中斷,記錄時(shí)間
{
TH0=0xd8;
TL0=0xf0;
zdjs++;
}
void del(yj)
{
unsigned char yj2=2;
while(yj!=0)
{
while(yj2!=0)
{
yj2--;
}
yj2=2;
yj--;
}
}
只用了一個(gè)定時(shí)器。
作者:
zl2168
時(shí)間:
2017-8-1 10:58
本帖最后由 zl2168 于 2017-8-1 10:59 編輯
實(shí)例75 定時(shí)器控制播放生日快樂歌
先
Proteus
仿真一下,確認(rèn)有效。
實(shí)例75 定時(shí)器控制播放生日快樂歌.rar
(26.96 KB, 下載次數(shù): 15)
2017-8-1 10:59 上傳
點(diǎn)擊文件名下載附件
實(shí)例76 定時(shí)器控制播放世上只有媽媽好歌曲.rar
(27.15 KB, 下載次數(shù): 5)
2017-8-1 10:59 上傳
點(diǎn)擊文件名下載附件
生日快樂歌.jpg
(101.98 KB, 下載次數(shù): 31)
下載附件
2017-8-1 10:59 上傳
以上摘自張志良編著《
80C51
單片機(jī)仿真設(shè)計(jì)實(shí)例教程——基于
Keil C
和
Proteus
》清華大學(xué)出版社
ISBN 978-7-302-41682-1
,
內(nèi)有常用的單片機(jī)應(yīng)用
100
案例,用于仿真實(shí)驗(yàn)操作,
電路與程序真實(shí)可靠可信可行
。
書中電路和程序設(shè)計(jì)有詳細(xì)說明,程序語句條條有注解。
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1