新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > 單片機(jī)設(shè)計(jì)的四個(gè)按鍵發(fā)音

單片機(jī)設(shè)計(jì)的四個(gè)按鍵發(fā)音

作者: 時(shí)間:2016-11-21 來源:網(wǎng)絡(luò) 收藏
電路圖:

程序?qū)崿F(xiàn)功能:
實(shí)現(xiàn)四個(gè)按鍵發(fā)出不同的音調(diào),代碼如下:
#include
#define uchar unsigned char
#define uint unsigned int
sbit BEEP = P3^7; //將P3.7接口賦予BEEP(接喇叭)
sbit K1 = P1^4; //將P1.4接口賦予K1
sbit K2 = P1^5; //將P1.5接口賦予K2
sbit K3 = P1^6; //將P1.6接口賦予k3
sbit K4 = P1^7; //將p1.7接口賦予K4
//延時(shí)
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; //半個(gè)周期取反一次
DelayMS(t); //延時(shí)
}
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);
}
}



關(guān)鍵詞: 按鍵發(fā)音單片

評(píng)論


技術(shù)專區(qū)

關(guān)閉