51单片机 蜂鸣器

main.c中

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

unsigned char KeyNum;
 
void main()
 {
    Nixie(KeyNum,0);
    while(1)
    {
       KeyNum=Key();
       if(KeyNum)//按键按下
       {
           Buzzer_Time(100);
           Nixie(KeyNum,KeyNum);
       }
    }
}

delay.c中

void Delay(unsigned int xms)
{
    unsigned char i, j;
    while(xms--)
    {
        i = 2;
        j = 239;
        do
        {
            while (--j);
        } while (--i);
    }
}

delay.h中

#ifndef __DELAY_H__
#define __DELAY_H__

void Delay(unsigned int xms);

#endif

KEY.c中

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

// 获取独立按键键码,
unsigned char Key()
{
    unsigned 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;
}

KEY.h中

#ifndef __KEY_H__
#define    __KEY_H__

unsigned char Key();

#endif

Nixie.c中

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

//数码管段码表
unsigned char NixieTable[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};

//数码管显示子函数
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);                //显示一段时间
//    P0=0x00;                //段码清0,消影
}

Nixie.h中

#ifndef __NIXIE_H__
#define __NIXIE_H__

void Nixie(unsigned char Location,Number);

#endif

Buzzer.c中

#include <REGX52.H>
#include"INTRINS.h"
/**
   *    注释:蜂鸣器私有延时函数,延时500微妙
    输入参数:
    输出参数:
*/
sbit Buzzer=P1^5;

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

    _nop_();//要调用函数INTRINS
    i = 247;
    while (--i);
}
/**
   *    注释:蜂鸣器发生
    输入参数:毫秒 发生时长
    输出参数: 无
*/
void Buzzer_Time(unsigned int ms)
{
    unsigned int i;
    for(i=1;i<ms*2;i++)    
       {
         Buzzer=!Buzzer;
         Buzzer_lay500us();   
        } 
    
}
Buzzer.h中

#ifndef __BUZZER_H__
#define __BUZZER_H__

void Buzzer_Time(unsigned int ms);

#endif

  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值