摘要
本文章主要介绍了采用51单片机设计并实现一款电子卡林巴琴的思路及过程,设计好的卡林巴琴渲染图如下图所示:
硬件开原地址:
https://oshwhub.com/Dr.Zhang/kalinba
设计过程
本项目采用的核心元器件是蜂鸣器和鼠标按键,如下图所示:
主控芯片采用的是STC15W204,该单片机共有14个IO口,其中一个用作PWM输出,驱动蜂鸣器发出声音,另外13个IO用作琴键,一共使用了13个琴键。
其核心思想是,按下对应的琴键后,蜂鸣器播放对应频率的声音。
原理图如下:
立创开源地址:https://oshwhub.com/Dr.Zhang/kalinba
软件源码
软件代码还不是分厂的晚上,仅供参考:
#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);
}
}
}
实物效果图
演示视频
【卡林巴】【电子琴】51单片机 蜂鸣器 鼠标按键
如果你喜欢本文章,欢迎评论
转发
点赞
收藏
我是鹏老师!
转载:https://blog.csdn.net/weixin_48033504/article/details/116305333
查看评论