51單片機(jī)蜂鳴器發(fā)聲

每周學(xué)習(xí)記錄

主函數(shù)

#include <REGX52.H>
#include "key.h"
#include "Delay.h"
#include "Nixie.h"
#include "Buzzer.h"

unsigned char KeyNum;

void main()
{
    Nixie(1,0);
    while (1)
    {
        KeyNum=Key();
        if (KeyNum)
        {
            Buzzer_Time(2000);   
            Nixie(1,KeyNum);
        }
    }
}

延遲模塊

#ifndef _DELAY_H__
#define _DELAY_H__

void Delay(unsigned int xms);

#endif
#include <REGX52.H>

void Delay(unsigned int xms)        //@12.000MHz
{
    unsigned char i, j;
    while(xms--)
    {
        i = 12;
        j = 169;
        do
        {
            while (--j);
        } while (--i);
    }
}

鍵盤輸入模塊

#ifndef _KEY_H__
#define _KEY_H__

unsigned char Key();

#endif
#include <REGX52.H>
#include "Delay.h"

unsigned char Key()
{
    char KeyNumber=0;
    
    if(P3_1==0){Delay(20);while(P3_1==0);Delay(20);KeyNumber=1;}
    if(P3_0==0){Delay(20);while(P3_0==0);Delay(20);KeyNumber=2;}
    if(P3_2==0){Delay(20);while(P3_2==0);Delay(20);KeyNumber=3;}
    if(P3_3==0){Delay(20);while(P3_3==0);Delay(20);KeyNumber=4;}
    
    return KeyNumber;   
}

數(shù)碼管顯示

#ifndef __NIXIE_H__
#define __NIXIE_H__

void Nixie(unsigned char Location,Number);

#endif

#include <REGX52.H>
#include "Delay.h"  //包含Delay頭文件

//數(shù)碼管段碼表
unsigned char NixieTable[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};

//數(shù)碼管顯示子函數(shù)
void Nixie(unsigned char Location,Number)
{
    switch(Location)        //位碼輸出
    {
        case 1:P2_4=1;P2_3=1;P2_2=1;break;
        case 2:P2_4=1;P2_3=1;P2_2=0;break;
        case 3:P2_4=1;P2_3=0;P2_2=1;break;
        case 4:P2_4=1;P2_3=0;P2_2=0;break;
        case 5:P2_4=0;P2_3=1;P2_2=1;break;
        case 6:P2_4=0;P2_3=1;P2_2=0;break;
        case 7:P2_4=0;P2_3=0;P2_2=1;break;
        case 8:P2_4=0;P2_3=0;P2_2=0;break;
    }
    P0=NixieTable[Number];  //段碼輸出
//  Delay(1);               //顯示一段時(shí)間
//  P0=0x00;                //段碼清0,消影
}

蜂鳴器模塊

#ifndef _BUZZER_H__
#define _BUZZER_H__

void Buzzer_Time(unsigned int ms);

#endif
#include <REGX52.H>
#include "Delay.h"
#include <INTRINS.H>

sbit Buzzer=P1^5;

void Buzzer_Delay500us()        //@12.000MHz
{
    unsigned char i;

    _nop_();
    i = 247;
    while (--i);
}


void Buzzer_Time(unsigned int ms)
{
    unsigned int i;
    for (i=0;i<ms*2;i++)
    {
        Buzzer=!Buzzer;
        Buzzer_Delay500us();
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容