51单片机配合振动传感器实现振动计数

实验器材

51单片机开发板(需要配置显示数码管,蜂鸣器)

数字振动传感器

三根杜邦线

实验器材的组装

实验器材的组装中,在开发板的单片机Vcc端与Vss端分别与传感器的Vcc与Ground端口相连

将传感器的数字信号开关端口DO与单片机的外部中断0端口INT0(P3^3)相连

结构图

C程序

#include <reg52.h> 
#include <intrins.h>
#define uint unsigned int  
#define uchar unsigned char
sbit  voice=P2^3; //设置几个特殊端口控制几个功能
sbit  wes=P2^7;
sbit  dus=P2^6;
uint a,c,d,shu[2]={0,0};
uchar pro;
uint code num[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; //用来显示数字的数码段
uint code HELLO[5]={0x76,0x79,~0xc7,~0xc7,0x3f}; 
uint code LNU[3]={~0xc7,~0xc8,~0xc1};
void delay(uint i);
void soundandlight();
void light();
void dark();
void main()
{
    TMOD=0X01;
    TH0=(65535-50000)/256;
    TL0=(65535-50000)%256;
    EA=1; //开总中断 
    ET0=1; //开定时器T0中断
    TR0=1;
    d=50;
    a=0;
    while(d)
    {
        pro=0xf7;
            for(c=0;c<3;c++)
        {
            wes=1;
            P0=pro;
            wes=0;
            P0=0x00;
        
            dus=1;
            P0=LNU[c];
            dus=0;
            P0=0xff;
            delay(1);
            pro=_crol_(pro,1);
        }
    }
    dark();
    soundandlight();
    d=50;
    while(d)
    {
        pro=0xfd;
            for(c=0;c<5;c++)
        {
            wes=1;
            P0=pro;
            wes=0;
            P0=0x00;
        
            dus=1;
            P0=HELLO[c];
            dus=0;
            P0=0xff;
            delay(1);
            pro=_crol_(pro,1);
        }
    }
    dark();
    soundandlight();
    ET0=0;
    EX0=1; //开外部中断0中断 
    IT0=0; //选择低电平触发的触发方式 
    while(1)
    {
        shu[0]=a/10;
        shu[1]=a%10;
        pro=0xef;
        for(c=0;c<2;c++)
        {
            wes=1;
            P0=pro;
            wes=0;
            P0=0x00;
            
            dus=1;
            P0=num[shu[c]];
            dus=0;
            P0=0xff;
            
            pro=_crol_(pro,1);
            
            delay(1);
        }
        
    }
}

void  delay(uint i)        //延时函数             
{    uchar t;
     while (i--)
      {    for(t=0; t<120; t++);    
      }
}

void soundandlight() //控制蜂鸣器鸣响与led灯闪亮 
{
    P1=0x00;
    voice=0;
    delay(500);
    voice=1;
    P1=0xff;
}

void jiance() interrupt 0 //外部中断0进行检测的响应 
{
            wes=1;
            P0=0x00;
            wes=0;
            P0=0xff;
            
            dus=1;
            P0=0x00;
            dus=0;
    a++;
    light();
    delay(500);
}

void light()  //控制led灯闪亮 
{
    P1=0x00;
    delay(500);
    P1=0xff;
}

void dark()  //用于消隐,在数码管显示完毕后消除显示 
{            //以达到清晰的效果 
            wes=1;
            P0=0x00;
            wes=0;
            
            dus=1;
            P0=0x00;
            dus=0;
            P0=0xff;
}
void time0() interrupt 1 //定时器T0中断程序 用来控制字段显示时间
{
    d--;
}

实际效果

开发板上电以后,显示初始化界面。

显示结束后进入工作状态;拨动振动传感器看到开发板上的计数增加

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值