|
/*
實驗說明:
實驗接線:
1,靜態數碼管模塊-->單片機管腳
參考靜態數碼管顯示實驗接線(開發攻略內在對應的實驗章節內實驗現象有接線說明)
2,矩陣按鍵模塊-->單片機管腳
參考矩陣按鍵實驗接線(開發攻略內在對應的實驗章節內實驗現象有接線說明)
3,蜂鳴器模塊-->單片機管腳
BEEP-->P20
實驗現象:
按下不同鍵,蜂鳴器發出不同聲音,同時靜態數碼管上顯示鍵值,這里僅僅是一個簡單的調聲程序,大家如要
設計與實際電子琴功能,還需優化。
*/
#include<reg52.h>
typedef unsigned char u8;
typedef unsigned int u16;
sbit beep=P2^0;
#define GPIO_KEY P1
u8 KeyValue; //用來存放讀取到的鍵值
u8 value;
u8 smg[16]={~0x3F, ~0x06, ~0x5B, ~0x4F, ~0x66, ~0x6D, ~0x7D, ~0x07,
~0x7F, ~0x6F, ~0x77, ~0x7C, ~0x39, ~0x5E, ~0x79, ~0x71}; //0~A
void delay(u16 i)
{
while(i--);
}
void KEY_Scan(void)
{
char a=0;
GPIO_KEY=0x0f;
if(GPIO_KEY!=0x0f)//讀取按鍵是否按下
{
delay(1000);//延時10ms進行消抖
if(GPIO_KEY!=0x0f)//再次檢測鍵盤是否按下
{
//測試列
GPIO_KEY=0X0F;
switch(GPIO_KEY)
{
case(0X07): KeyValue=1;break;
case(0X0b): KeyValue=2;break;
case(0X0d): KeyValue=3;break;
case(0X0e): KeyValue=4;break;
}
//測試行
GPIO_KEY=0XF0;
switch(GPIO_KEY)
{
case(0X70): KeyValue=KeyValue;break;
case(0Xb0): KeyValue=KeyValue+4;break;
case(0Xd0): KeyValue=KeyValue+8;break;
case(0Xe0): KeyValue=KeyValue+12;break;
}
while((a<50)&&(GPIO_KEY!=0xf0)) //檢測按鍵松手檢測
{
delay(1000);
a++;
}
}
}
}
void sound()
{
u16 i=200;
if(KeyValue==1)
{
KeyValue=0;
P0=smg[0];
while(i--)
{
beep=~beep; //低音1
delay(380);
}
}
if(KeyValue==2)
{
KeyValue=0;
P0=smg[1];
while(i--)
{
beep=~beep; //低音2
delay(341);
}
}
if(KeyValue==3)
{
KeyValue=0;
P0=smg[2];
while(i--)
{
beep=~beep; //低音3
delay(303);
}
}
if(KeyValue==4)
{
KeyValue=0;
P0=smg[3];
while(i--)
{
beep=~beep; //低音4
delay(286);
}
}
if(KeyValue==5)
{
KeyValue=0;
P0=smg[4];
while(i--)
{
beep=~beep; //低音5
delay(255);
}
}
if(KeyValue==6)
{
KeyValue=0;
P0=smg[5];
while(i--)
{
beep=~beep; //低音6
delay(227);
}
}
if(KeyValue==7)
{
KeyValue=0;
P0=smg[6];
while(i--)
{
beep=~beep; //低音7
delay(203);
}
}
if(KeyValue==8)
{
KeyValue=0;
P0=smg[7];
while(i--)
{
beep=~beep; //中音1
delay(191);
}
}
if(KeyValue==9)
{
KeyValue=0;
P0=smg[8];
while(i--)
{
beep=~beep; //中音2
delay(170);
}
}
if(KeyValue==10)
{
KeyValue=0;
P0=smg[9];
while(i--)
{
beep=~beep; //中音3
delay(152);
}
}
if(KeyValue==11)
{
KeyValue=0;
P0=smg[10];
while(i--)
{
beep=~beep; //中音4
delay(143);
}
}
if(KeyValue==12)
{
KeyValue=0;
P0=smg[11];
while(i--)
{
beep=~beep; //中音5
delay(128);
}
}
if(KeyValue==13)
{
KeyValue=0;
P0=smg[12];
while(i--)
{
beep=~beep; //中音6
delay(114);
}
}
if(KeyValue==14)
{
KeyValue=0;
P0=smg[13];
while(i--)
{
beep=~beep; //中音7
delay(101);
}
}
if(KeyValue==15)
{
KeyValue=0;
P0=smg[14];
while(i--)
{
beep=~beep; //高音1
delay(96);
}
}
if(KeyValue==16)
{
KeyValue=0;
P0=smg[15];
while(i--)
{
beep=~beep; //高音2
delay(85);
}
}
}
void time0init()
{
TMOD=0X01;
TH0=0XFc;
TL0=0X18;
TR0=1;
ET0=1;
EA=1;
}
void main()
{
time0init();
while(1);
}
void tim0() interrupt 1
{
TH0=0XFc;
TL0=0X18;
KEY_Scan();
sound();
}
|
-
-
基于單片機的智能電子琴設計.zip
2020-1-21 14:37 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
17.48 KB, 下載次數: 11, 下載積分: 黑幣 -5
|