kalimba
简介
本项目通过51单片机配合鼠标按键和蜂鸣器,制作了一个简单的卡林巴电子琴,一共13个琴键,可以演奏很多音乐。
简介:本项目通过51单片机配合鼠标按键和蜂鸣器,制作了一个简单的卡林巴电子琴,一共13个琴键,可以演奏很多音乐。开源协议
:MIT
描述
项目渲染图:
软件源代码
#include "stc15.h"
#define MAIN_Fosc 24000000 //时钟频率24M
typedef unsigned char u8;
typedef unsigned int u16;
typedef unsigned long u32;
sbit P_PWM = P1^4; //PWM引脚
u16 PWM_high;
void Pin_Init()
{
P1 = P3 = P5 = 0xff;
P_PWM = 0;
P1M1 &= ~(1 << 4);
P1M0 |= (1 << 4);
}
u16 cur_tune = 0; //用来记录当前频率
void PWM_SetTune(u16 tune)
{
cur_tune = tune;
if(tune == 0)
{
TR0 = 0;
PT0 = 0;
TMOD = 00;
TMOD = 0;
INT_CLKO = 0;
P1M0 = 0;
P_PWM = 0;
}
else
{
EA = 0;
PT0 = 1;
P1M0 |= (1 << 4);
TR0 = 0;
PT0 = 1;
TMOD &= ~0x03;
TMOD &= ~0x04;
INT_CLKO |= 0x01;
PWM_high =65535UL - 1000000UL/tune;
TH0 = (u8)(PWM_high >> 8);
TL0 = (u8)PWM_high;
TR0 = 1;
}
}
char last_value[13]= {1,1,1,1,1,1,1,1,1,1,1,1,1}; //记录13个按键上次的按键值
char now_value[13] = {1,1,1,1,1,1,1,1,1,1,1,1,1}; //记录13个按键当前的按键值
//每个按键对应的频率,采用的是五声音接
u16 tune[13] = {262, 294, 330,392,440,523, 587, 660,784,880, 1046,1174,1318};
void main()
{
int i = 0;
Pin_Init();
PWM_SetTune(0);
while (1)
{
now_value[0] = P10;
now_value[1] = P37;
now_value[2] = P11;
now_value[3] = P36;
now_value[4] = P12;
now_value[5] = P33;
now_value[6] = P13;
now_value[7] = P32;
now_value[8] = P15;
now_value[9] = P31;
now_value[10] = P54;
now_value[11] = P30;
now_value[12] = P55;
for(i=0; i<13; i++)
{
if(last_value[i] != now_value[i]) //
{
if(now_value[i] == 0)
{
PWM_SetTune(tune[i]);
}
else if (cur_tune == tune[i])
{
PWM_SetTune(0);
}
last_value[i] = now_value[i];
}
}
if( (last_value[0] == 1) &&
(last_value[1] == 1) &&
(last_value[2] == 1) &&
(last_value[3] == 1) &&
(last_value[4] == 1) &&
(last_value[5] == 1) &&
(last_value[6] == 1) &&
(last_value[7] == 1) &&
(last_value[8] == 1) &&
(last_value[9] == 1) &&
(last_value[10] == 1) &&
(last_value[11] == 1) &&
(last_value[12] == 1))
{
PWM_SetTune(0);
}
}
}
评论