51單片機應用實例
當按下開關SP1,AT89S51單片機產生“叮咚”聲從P1.0端口輸出到LM386,經過放大之后送入喇叭。
電路原理圖
系統板上硬件連線
(1. 把“單片機系統”區域中的P1.0端口用導線連接到“音頻放大模塊”區域中的SPK IN端口上;
(2. 在“音頻放大模塊”區域中的SPK OUT端口上接上一個8歐或者是16歐的喇叭;
(3. 把“單片機系統”區域中的P3.7/RD端口用導線連接到“獨立式鍵盤”區域中的SP1端口上;
程序設計方法
(1. 我們用單片機實定時/計數器T0來產生700HZ和500HZ的頻率,根據定時/計數器T0,我們取定時250us,因此,700HZ的頻率要經過3次250us的定時,而500HZ的頻率要經過4次250us的定時。
(2. 在設計過程,只有當按下SP1之后,才啟動T0開始工作,當T0工作完畢,回到最初狀態。
(3. “叮”和“咚”聲音各占用0.5秒,因此定時/計數器T0要完成0.5秒的定時,對于以250us為基準定時2000次才可以。
C語言源程序
#include <AT89X51.H>
unsigned char t5hz;
unsigned char t7hz;
unsigned int tcnt;
bit stop;
bit flag;
void main(void)
{
unsigned char i,j;
TMOD=0x02;
TH0=0x06;
TL0=0x06;
ET0=1;
EA=1;
while(1)
{
if(P3_7==0)
{
for(i=10;i>0;i--)
for(j=248;j>0;j--);
if(P3_7==0)
{
t5hz=0;
t7hz=0;
tcnt=0;
flag=0;
stop=0;
TR0=1;
while(stop==0);
}
}
}
}
void t0(void) interrupt 1 using 0
{
tcnt++;
if(tcnt==2000)
{
tcnt=0;
if(flag==0)
{
flag=~flag;
}
else
{
stop=1;
TR0=0;
}
}
if(flag==0)
{
t7hz++;
if(t7hz==3)
{
t7hz=0;
P1_0=~P1_0;
}
}
else
{
t5hz++;
if(t5hz==4)
{
t5hz=0;
P1_0=~P1_0;
}
}
}
歡迎光臨 (http://www.zg4o1577.cn/bbs/) | Powered by Discuz! X3.1 |