因仑“3+1”工程特种兵精英论坛

标题: 实验22-按键发音 [打印本页]

作者: 张衍波    时间: 2015-5-5 16:23
标题: 实验22-按键发音
#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int

sbit BEEP = P1^4;
sbit K1   = P1^0;
sbit K2   = P1^1;
sbit K3   = P1^2;
sbit K4   = P1^3;

void DelayMS(uint x)
{
        uchar t;
        while(x--)
        {
                 for(t=0;t<120;t++);
        }
}

void Play(uchar t)
{
        uchar i;
        for(i=0;i<100;i++)
        {
                 BEEP = ~BEEP;
                DelayMS(t);
        }
        BEEP = 0;
}

void main()
{
        P1 = 0xff;
        while(1)
        {
                 if(K1==0) Play(1);
                if(K2==0) Play(2);
                if(K3==0) Play(3);
                if(K4==0) Play(4);
        }
}
[attach]2676[/attach]







欢迎光临 因仑“3+1”工程特种兵精英论坛 (http://bbs.enlern.com/) Powered by Discuz! X3.4