<pre style="max-width: 100%;"><code class="cpp hljs" codemark="1"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span></span>
<span class="hljs-keyword">typedef</span> <span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">int</span> u16;
<span class="hljs-keyword">typedef</span> <span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">char</span> u8;
sbit Beep=P1^<span class="hljs-number">5</span>;
<span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> LED P2</span>
<span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">char</span> n=<span class="hljs-number">0</span>; <span class="hljs-comment">//n为节拍常数变量 </span>
<span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">char</span> code music_tab[] ={
<span class="hljs-number">0x18</span>, <span class="hljs-number">0x30</span>, <span class="hljs-number">0x1C</span> , <span class="hljs-number">0x10</span>, <span class="hljs-comment">//格式为: 频率常数, 节拍常数, 频率常数, 节拍常数, </span>
<span class="hljs-number">0x20</span>, <span class="hljs-number">0x40</span>, <span class="hljs-number">0x1C</span> , <span class="hljs-number">0x10</span>,
<span class="hljs-number">0x18</span>, <span class="hljs-number">0x10</span>, <span class="hljs-number">0x20</span> , <span class="hljs-number">0x10</span>,
<span class="hljs-number">0x1C</span>, <span class="hljs-number">0x10</span>, <span class="hljs-number">0x18</span> , <span class="hljs-number">0x40</span>,
<span class="hljs-number">0x1C</span>, <span class="hljs-number">0x20</span>, <span class="hljs-number">0x20</span> , <span class="hljs-number">0x20</span>,
<span class="hljs-number">0x1C</span>, <span class="hljs-number">0x20</span>, <span class="hljs-number">0x18</span> , <span class="hljs-number">0x20</span>,
<span class="hljs-number">0x20</span>, <span class="hljs-number">0x80</span>, <span class="hljs-number">0xFF</span> , <span class="hljs-number">0x20</span>,
<span class="hljs-number">0x30</span>, <span class="hljs-number">0x1C</span>, <span class="hljs-number">0x10</span> , <span class="hljs-number">0x18</span>,
<span class="hljs-number">0x20</span>, <span class="hljs-number">0x15</span>, <span class="hljs-number">0x20</span> , <span class="hljs-number">0x1C</span>,
<span class="hljs-number">0x20</span>, <span class="hljs-number">0x20</span>, <span class="hljs-number">0x20</span> , <span class="hljs-number">0x26</span>,
<span class="hljs-number">0x40</span>, <span class="hljs-number">0x20</span>, <span class="hljs-number">0x20</span> , <span class="hljs-number">0x2B</span>,
<span class="hljs-number">0x20</span>, <span class="hljs-number">0x26</span>, <span class="hljs-number">0x20</span> , <span class="hljs-number">0x20</span>,
<span class="hljs-number">0x20</span>, <span class="hljs-number">0x30</span>, <span class="hljs-number">0x80</span> , <span class="hljs-number">0xFF</span>,
<span class="hljs-number">0x20</span>, <span class="hljs-number">0x20</span>, <span class="hljs-number">0x1C</span> , <span class="hljs-number">0x10</span>,
<span class="hljs-number">0x18</span>, <span class="hljs-number">0x10</span>, <span class="hljs-number">0x20</span> , <span class="hljs-number">0x20</span>,
<span class="hljs-number">0x26</span>, <span class="hljs-number">0x20</span>, <span class="hljs-number">0x2B</span> , <span class="hljs-number">0x20</span>,
<span class="hljs-number">0x30</span>, <span class="hljs-number">0x20</span>, <span class="hljs-number">0x2B</span> , <span class="hljs-number">0x40</span>,
<span class="hljs-number">0x20</span>, <span class="hljs-number">0x20</span>, <span class="hljs-number">0x1C</span> , <span class="hljs-number">0x10</span>,
<span class="hljs-number">0x18</span>, <span class="hljs-number">0x10</span>, <span class="hljs-number">0x20</span> , <span class="hljs-number">0x20</span>,
<span class="hljs-number">0x26</span>, <span class="hljs-number">0x20</span>, <span class="hljs-number">0x2B</span> , <span class="hljs-number">0x20</span>,
<span class="hljs-number">0x30</span>, <span class="hljs-number">0x20</span>, <span class="hljs-number">0x2B</span> , <span class="hljs-number">0x40</span>,
<span class="hljs-number">0x20</span>, <span class="hljs-number">0x30</span>, <span class="hljs-number">0x1C</span> , <span class="hljs-number">0x10</span>,
<span class="hljs-number">0x18</span>, <span class="hljs-number">0x20</span>, <span class="hljs-number">0x15</span> , <span class="hljs-number">0x20</span>,
<span class="hljs-number">0x1C</span>, <span class="hljs-number">0x20</span>, <span class="hljs-number">0x20</span> , <span class="hljs-number">0x20</span>,
<span class="hljs-number">0x26</span>, <span class="hljs-number">0x40</span>, <span class="hljs-number">0x20</span> , <span class="hljs-number">0x20</span>,
<span class="hljs-number">0x2B</span>, <span class="hljs-number">0x20</span>, <span class="hljs-number">0x26</span> , <span class="hljs-number">0x20</span>,
<span class="hljs-number">0x20</span>, <span class="hljs-number">0x20</span>, <span class="hljs-number">0x30</span> , <span class="hljs-number">0x80</span>,
<span class="hljs-number">0x20</span>, <span class="hljs-number">0x30</span>, <span class="hljs-number">0x1C</span> , <span class="hljs-number">0x10</span>,
<span class="hljs-number">0x20</span>, <span class="hljs-number">0x10</span>, <span class="hljs-number">0x1C</span> , <span class="hljs-number">0x10</span>,
<span class="hljs-number">0x20</span>, <span class="hljs-number">0x20</span>, <span class="hljs-number">0x26</span> , <span class="hljs-number">0x20</span>,
<span class="hljs-number">0x2B</span>, <span class="hljs-number">0x20</span>, <span class="hljs-number">0x30</span> , <span class="hljs-number">0x20</span>,
<span class="hljs-number">0x2B</span>, <span class="hljs-number">0x40</span>, <span class="hljs-number">0x20</span> , <span class="hljs-number">0x15</span>,
<span class="hljs-number">0x1F</span>, <span class="hljs-number">0x05</span>, <span class="hljs-number">0x20</span> , <span class="hljs-number">0x10</span>,
<span class="hljs-number">0x1C</span>, <span class="hljs-number">0x10</span>, <span class="hljs-number">0x20</span> , <span class="hljs-number">0x20</span>,
<span class="hljs-number">0x26</span>, <span class="hljs-number">0x20</span>, <span class="hljs-number">0x2B</span> , <span class="hljs-number">0x20</span>,
<span class="hljs-number">0x30</span>, <span class="hljs-number">0x20</span>, <span class="hljs-number">0x2B</span> , <span class="hljs-number">0x40</span>,
<span class="hljs-number">0x20</span>, <span class="hljs-number">0x30</span>, <span class="hljs-number">0x1C</span> , <span class="hljs-number">0x10</span>,
<span class="hljs-number">0x18</span>, <span class="hljs-number">0x20</span>, <span class="hljs-number">0x15</span> , <span class="hljs-number">0x20</span>,
<span class="hljs-number">0x1C</span>, <span class="hljs-number">0x20</span>, <span class="hljs-number">0x20</span> , <span class="hljs-number">0x20</span>,
<span class="hljs-number">0x26</span>, <span class="hljs-number">0x40</span>, <span class="hljs-number">0x20</span> , <span class="hljs-number">0x20</span>,
<span class="hljs-number">0x2B</span>, <span class="hljs-number">0x20</span>, <span class="hljs-number">0x26</span> , <span class="hljs-number">0x20</span>,
<span class="hljs-number">0x20</span>, <span class="hljs-number">0x20</span>, <span class="hljs-number">0x30</span> , <span class="hljs-number">0x30</span>,
<span class="hljs-number">0x20</span>, <span class="hljs-number">0x30</span>, <span class="hljs-number">0x1C</span> , <span class="hljs-number">0x10</span>,
<span class="hljs-number">0x18</span>, <span class="hljs-number">0x40</span>, <span class="hljs-number">0x1C</span> , <span class="hljs-number">0x20</span>,
<span class="hljs-number">0x20</span>, <span class="hljs-number">0x20</span>, <span class="hljs-number">0x26</span> , <span class="hljs-number">0x40</span>,
<span class="hljs-number">0x13</span>, <span class="hljs-number">0x60</span>, <span class="hljs-number">0x18</span> , <span class="hljs-number">0x20</span>,
<span class="hljs-number">0x15</span>, <span class="hljs-number">0x40</span>, <span class="hljs-number">0x13</span> , <span class="hljs-number">0x40</span>,
<span class="hljs-number">0x18</span>, <span class="hljs-number">0x80</span>, <span class="hljs-number">0x00</span>
};
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">int0</span><span class="hljs-params">()</span> interrupt 1 <span class="hljs-comment">//采用中断0 控制节拍 </span>
</span>{ TH0=<span class="hljs-number">0xd8</span>;
TL0=<span class="hljs-number">0xef</span>;
n--;
}
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">delay</span> <span class="hljs-params">(<span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">char</span> m)</span> <span class="hljs-comment">//控制频率延时 </span>
</span>{
<span class="hljs-keyword">unsigned</span> i=<span class="hljs-number">3</span>*m;
<span class="hljs-keyword">while</span>(--i);
}
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">delayms</span><span class="hljs-params">(<span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">char</span> a)</span> <span class="hljs-comment">//豪秒延时子程序 </span>
</span>{
<span class="hljs-keyword">while</span>(--a); <span class="hljs-comment">//采用while(--a) 不要采用while(a--); 各位可编译一下看看汇编结果就知道了! </span>
}
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">Led</span><span class="hljs-params">()</span>
</span>{
u8 k;
led=<span class="hljs-number">0xfe</span>;
delay(<span class="hljs-number">5000</span>);
<span class="hljs-keyword">while</span>(<span class="hljs-number">1</span>)
{
<span class="hljs-keyword">for</span>(k=<span class="hljs-number">0</span>;k<<span class="hljs-number">8</span>;k++)
{
led=_cror_(led,<span class="hljs-number">1</span>);
delay(<span class="hljs-number">5000</span>);
}
<span class="hljs-keyword">for</span>(k=<span class="hljs-number">0</span>;k<<span class="hljs-number">8</span>;k++)
{
led=_crol_(led,<span class="hljs-number">1</span>);
delay(<span class="hljs-number">5000</span>);
}
}
}
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">Music</span><span class="hljs-params">()</span>
</span>{
<span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">char</span> p,m; <span class="hljs-comment">//m为频率常数变量 </span>
<span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">char</span> i=<span class="hljs-number">0</span>;
play:
<span class="hljs-keyword">while</span>(<span class="hljs-number">2</span>)
{
a: p=music_tab;
IF(p==<span class="hljs-number">0x00</span>) { i=<span class="hljs-number">0</span>, delayms(<span class="hljs-number">1000</span>); <span class="hljs-keyword">goto</span> play;} <span class="hljs-comment">//如果碰到结束符,延时1秒,回到开始再来一遍 </span>
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(p==<span class="hljs-number">0xff</span>) { i=i+<span class="hljs-number">1</span>;delayms(<span class="hljs-number">100</span>),TR0=<span class="hljs-number">0</span>; <span class="hljs-keyword">goto</span> a;} <span class="hljs-comment">//若碰到休止符,延时100ms,继续取下一音符 </span>
<span class="hljs-keyword">else</span> {m=music_tab[i++], n=music_tab[i++];} <span class="hljs-comment">//取频率常数 和 节拍常数 </span>
TR0=<span class="hljs-number">1</span>; <span class="hljs-comment">//开定时器1 </span>
<span class="hljs-keyword">while</span>(n!=<span class="hljs-number">0</span>) Beep=~Beep,delay(m); <span class="hljs-comment">//等待节拍完成, 通过P1口输出音频(可多声道哦!) </span>
TR0=<span class="hljs-number">0</span>; <span class="hljs-comment">//关定时器1 </span>
}
}
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">()</span>
</span>{
TMOD&=<span class="hljs-number">0x0f</span>;
TMOD|=<span class="hljs-number">0x01</span>;
TH0=<span class="hljs-number">0xd8</span>;
TL0=<span class="hljs-number">0xef</span>;
IE=<span class="hljs-number">0x82</span>;
Led();
Music();
}</code></pre>
<p><br></p>
一周热门 更多>