找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開(kāi)始

搜索
查看: 4608|回復(fù): 6
打印 上一主題 下一主題
收起左側(cè)

網(wǎng)上買51開(kāi)發(fā)板,自學(xué)單片機(jī),寫(xiě)了一個(gè)蜂鳴器發(fā)聲的程序,下載進(jìn)去蜂鳴器不響。程....

  [復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
#include<reg52.h>
sbit BUZZ =P1^6;
unsigned int code NoteFrequ[]={523,587,659,698,784,880,988,1047,1175,1319,1397,1568,1760,1976};
unsigned int code NoteReload[]={
   65536-(11059200/12)/(523*2),
  65536-(11059200/12)/(587*2),
  65536-(11059200/12)/(659*2),
  65536-(11059200/12)/(698*2),
  65536-(11059200/12)/(784*2),
  65536-(11059200/12)/(880*2),
  65536-(11059200/12)/(988*2),
  65536-(11059200/12)/(1047*2),
  65536-(11059200/12)/(1175*2),
  65536-(11059200/12)/(1319*2),
  65536-(11059200/12)/(1397*2),
  65536-(11059200/12)/(1568*2),
  65536-(11059200/12)/(1760*2),
  65536-(11059200/12)/(1976*2),
};
bit enable =1;
  bit tmrflag=0;
  unsigned char T0RH=0xFF;
unsigned char T0RL=0x00;
void PlayTwoTiger();
void main()
{
unsigned int i;

EA=1;
TMOD=0x01;
TH0=T0RH;
TL0=T0RL;
ET0=1;
TR0=1;

while(1)
{
  PlayTwoTiger();
  for(i=0;i<40000;i++);
}
}
void PlayTwoTiger()
{
unsigned char beat;
unsigned char note;
unsigned int time=0;
unsigned int beatTime=0;
unsigned int soundTime=0;
unsigned char code  TwoTigerNote[]={
     1, 2, 3, 1,   1, 2, 3, 1,   3,4,5,  3,4,5,  
     5,6,5,4,3,1,  5,6,5,4,3,1,  1,5,1,  1,5,1,
};
unsigned char code  TwoTigerBeat[]={4,  4 , 4  ,4,      4 , 4 , 4 , 4,     4,4,8,      4,4,8,
                                    3,1,3,1 ,4, 4,       3,1,3,1,4,4,     4,4,8,      4,4,8,
};
  for(beat=0;beat<sizeof(TwoTigerNote); )
{
while(!tmrflag);

{
beep=0;
delay(1);
beep=1;
delay(1);
}
tmrflag=0;
if(time==0)
{
   note = TwoTigerNote[beat] - 1;
  T0RH = NoteReload[note]>> 8;
  T0RL = NoteReload[note];
  beatTime = (TwoTigerBeat[beat] * NoteFrequ[note])>>2;
   
  soundTime = beatTime - (beatTime>>2);
  enable = 1;
  time++;
}
else
{
  if(time >=beatTime)
  {
   time =0;
   beat++;
  }
  else
  {
   time++;
   if(time==soundTime)
   {
    enable=0;
   }
}
}
}
}
void InterruptTimer0() interrupt 1
{
TH0=T0RH;
TL0=T0RL;
tmrflag=1;
if(enable)
  BUZZ=~BUZZ;
else
  BUZZ=1;
}

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:467499 發(fā)表于 2019-1-13 18:04 | 只看該作者
有懂的大神幫忙看下啊,謝謝。
回復(fù)

使用道具 舉報(bào)

板凳
ID:379933 發(fā)表于 2019-1-14 00:10 | 只看該作者
定時(shí)器那里貌似有問(wèn)題,我建議新手干脆用STC下載器里的軟件延時(shí)計(jì)算器算出來(lái),我是這個(gè)學(xué)期才開(kāi)的單片機(jī)課如果說(shuō)錯(cuò)了請(qǐng)多包涵。

評(píng)分

參與人數(shù) 7黑幣 +245 收起 理由
刀刀見(jiàn)紅 + 35
shuisheng60 + 35 很給力!
yoxi + 35 很給力!
找人PK + 35 很給力!
areyouok + 35 贊一個(gè)!
jinsheng7533967 + 35
#include51.h + 35 說(shuō)出自己的想法這很好

查看全部評(píng)分

回復(fù)

使用道具 舉報(bào)

地板
ID:454000 發(fā)表于 2019-1-14 01:56 | 只看該作者
給一個(gè)能發(fā)聲音的程式你燒進(jìn)去看看,看是外圍電路沒(méi)接好還是程式本聲的問(wèn)題,曼慢來(lái),別急!

#include<reg52.h>
sbit SPK=P1^6;    //定義喇叭端口
unsigned char frq;

void DelayUs2x(unsigned char t);//函數(shù)聲明
void DelayMs(unsigned char t);

void Init_Timer0(void)
{
TMOD |= 0x01;          //使用模式1,16位定時(shí)器,                     
EA=1;            //總中斷打開(kāi)
ET0=1;           //定時(shí)器中斷打開(kāi)
TR0=1;           //定時(shí)器開(kāi)關(guān)打開(kāi)
}

main()
{
unsigned char i;
Init_Timer0(); //初始化定時(shí)器
while(1)
      {
   frq=0;
   for(i=0;i<60;i++)//播放1s左右一種頻率
      {
      DelayMs(10);
          }
   frq=100;
   for(i=0;i<60;i++)//播放1s左右另外一種頻率
      {  
      DelayMs(10);
          }
         
         }
}

void DelayUs2x(unsigned char t)
{   
while(--t);
}

void DelayMs(unsigned char t)
{
     
while(t--)
{
     //大致延時(shí)1mS
     DelayUs2x(245);
         DelayUs2x(245);
}
}

               //  定時(shí)器中斷子程序
void Timer0_isr(void) interrupt 1
{
TH0=0xfe;                  //重新賦值
TL0=frq;         //低8位值在主程序中不斷累加
SPK=!SPK;        //端口電平取反
}

評(píng)分

參與人數(shù) 5黑幣 +82 收起 理由
還有誰(shuí)? + 15
zhengchong60 + 15
Sawardeakar + 10
yoxi + 12 贊一個(gè)!
admin + 30 回帖助人的獎(jiǎng)勵(lì)!

查看全部評(píng)分

回復(fù)

使用道具 舉報(bào)

5#
ID:21069 發(fā)表于 2019-2-13 20:49 | 只看該作者
呵呵公園 發(fā)表于 2019-1-14 00:10
定時(shí)器那里貌似有問(wèn)題,我建議新手干脆用STC下載器里的軟件延時(shí)計(jì)算器算出來(lái),我是這個(gè)學(xué)期才開(kāi)的單片機(jī)課 ...

說(shuō)出自己的想法這很好

評(píng)分

參與人數(shù) 1黑幣 +8 收起 理由
嘉立創(chuàng)AAA + 8 很給力!

查看全部評(píng)分

回復(fù)

使用道具 舉報(bào)

6#
ID:213173 發(fā)表于 2019-2-14 13:52 | 只看該作者
1.你首先確認(rèn)開(kāi)發(fā)板上是無(wú)源蜂鳴器,否則響了也不是你想要的音樂(lè)聲。
2.你的程序不能通過(guò)編譯,蜂鳴器端口定義名稱與程序中指令不符,delay延時(shí)程序漏寫(xiě)。修改后經(jīng)仿真可以發(fā)出音樂(lè)聲。


#include<reg52.h>
sbit BUZZ =P1^6;
unsigned int code NoteFrequ[]={523,587,659,698,784,880,988,1047,1175,1319,1397,1568,1760,1976};
unsigned int code NoteReload[]={
   65536-(11059200/12)/(523*2),
  65536-(11059200/12)/(587*2),
  65536-(11059200/12)/(659*2),
  65536-(11059200/12)/(698*2),
  65536-(11059200/12)/(784*2),
  65536-(11059200/12)/(880*2),
  65536-(11059200/12)/(988*2),
  65536-(11059200/12)/(1047*2),
  65536-(11059200/12)/(1175*2),
  65536-(11059200/12)/(1319*2),
  65536-(11059200/12)/(1397*2),
  65536-(11059200/12)/(1568*2),
  65536-(11059200/12)/(1760*2),
  65536-(11059200/12)/(1976*2),
};
bit enable =1;
bit tmrflag=0;
unsigned char T0RH=0xFF;
unsigned char T0RL=0x00;

void PlayTwoTiger();

void delay(unsigned int k)
{
        unsigned int i,j;
        for(i=k;i>0;i--)
                for(j=110;j>0;j--);
}

void main()
{
        unsigned int i;
        EA=1;
        TMOD=0x01;
        TH0=T0RH;
        TL0=T0RL;
        ET0=1;
        TR0=1;
        while(1)
        {
                PlayTwoTiger();
                for(i=0;i<40000;i++);
        }
}
void PlayTwoTiger()
{
        unsigned char beat;
        unsigned char note;
        unsigned int time=0;
        unsigned int beatTime=0;
        unsigned int soundTime=0;
        unsigned char code  TwoTigerNote[]={
        1, 2, 3, 1,   1, 2, 3, 1,   3,4,5,  3,4,5,  
        5,6,5,4,3,1,  5,6,5,4,3,1,  1,5,1,  1,5,1,};
        unsigned char code  TwoTigerBeat[]={4,  4 , 4  ,4,      4 , 4 , 4 , 4,     4,4,8,      4,4,8,
        3,1,3,1 ,4, 4,       3,1,3,1,4,4,     4,4,8,      4,4,8,};
        for(beat=0;beat<sizeof(TwoTigerNote); )
        {
                while(!tmrflag);       
                {
                        BUZZ=0;
                        delay(1);
                        BUZZ=1;
                        delay(1);
                }
                tmrflag=0;
                if(time==0)
                {
                        note = TwoTigerNote[beat] - 1;
                        T0RH = NoteReload[note]>> 8;
                        T0RL = NoteReload[note];
                        beatTime = (TwoTigerBeat[beat] * NoteFrequ[note])>>2;
                        soundTime = beatTime - (beatTime>>2);
                        enable = 1;
                        time++;
                }
                else
                {
                        if(time >=beatTime)
                        {
                                time =0;
                                beat++;
                        }
                        else
                        {
                                time++;
                                if(time==soundTime)
                                {
                                        enable=0;
                                }
                        }
                }
        }
}
void InterruptTimer0() interrupt 1
{
        TH0=T0RH;
        TL0=T0RL;
        tmrflag=1;
        if(enable)
          BUZZ=~BUZZ;
        else
          BUZZ=1;
}



回復(fù)

使用道具 舉報(bào)

7#
ID:476644 發(fā)表于 2019-2-15 13:27 | 只看該作者
會(huì)不會(huì)是硬件的問(wèn)題?
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表