|
最近看了杜洋的21键触摸电子琴,觉得挺有意思的。,。 于是拿着开发板模拟了一个,,,用的是STC12C5A60S2, 但是怎么调都不能达到杜洋的效果。
就是手触摸IO口连接的按键连上VCC就能发声。下面是程序
void INIT(void);
#include <reg52.h>
sfr P1M1=0x91;
sfr P1M0=0x92;
sfr P0M1=0x93;
sfr P0M0=0x94;
sfr P2M1=0x95;
sfr P2M0=0x96;
sfr P3M1=0xB1;
sfr P3M0=0xB2;
/*********************************************************************************************/
sbit SPEAKER = P2^3;//扬声器
sbit KEY11 = P1^5;
sbit KEY12 = P1^4;
sbit KEY13 = P1^3;
sbit KEY14 = P1^2;
sbit KEY15 = P1^1;
sbit KEY16 = P1^0;
sbit KEY17 = P0^7;
sbit KEY21 = P0^2;
sbit KEY22 = P0^1;
sbit KEY23 = P0^0;
sbit KEY24 = P0^3;
sbit KEY25 = P0^4;
sbit KEY26 = P0^5;
sbit KEY27 = P0^6;
sbit KEY31 = P2^4;
sbit KEY33 = P2^2;
sbit KEY34 = P2^1;
sbit KEY35 = P2^0;
sbit KEY36 = P1^7;
sbit KEY37 = P1^6;
/*********************************************************************************************/
unsigned char MUSIC;
unsigned char STH0,STL0;
unsigned int code tab[]={ //音阶表
63628,63835,64021,64103,64260,64400,64524, //低音1-7
64580,64684,64777,64820,64898,64968,65030, //中音1-7
65058,65110,65157,65178,65217,65252,65283 //高音1-7
};
/*********************************************************************************************/
void delay1ms (unsigned int a){ // 1ms延时程序(12MHz 10倍于51单片机速度时)
unsigned int i;
while( --a != 0){
for(i = 0; i < 600; i++);
}
}/***************************************************************************************/
void INIT(void){//初始化程序
TMOD=0x11;
ET0=1;
ET1=1;
EA=1;
P0M1 = 0xff;//I/O接口工作方式
P0M0 = 0x00;//P0全为高阻输入
P1M1 = 0xff;//高阻输入
P1M0 = 0x00;
P2M1 = 0xf7;//P2.3准双向,其他全为高阻输入
P2M0 = 0x00;
SPEAKER = 1;
}
/***************************************************************************************/
void main(void){
INIT(); //初始化
while(1){
if(KEY37 == 1){delay1ms(20);if(KEY37 == 1){MUSIC = 20;}} //高音B(37)
if(KEY36 == 1){delay1ms(20);if(KEY36 == 1){MUSIC = 19;}}
if(KEY35 == 1){delay1ms(20);if(KEY35 == 1){MUSIC = 18;}}
if(KEY34 == 1){delay1ms(20);if(KEY34 == 1){MUSIC = 17;}}
if(KEY33 == 1){delay1ms(20);if(KEY33 == 1){MUSIC = 16;}}
if(KEY31 == 1){delay1ms(20);if(KEY31 == 1){MUSIC = 14;}}
//3
if(KEY27 == 1){delay1ms(20);if(KEY27 == 1){MUSIC = 13;}}
if(KEY26 == 1){delay1ms(20);if(KEY26 == 1){MUSIC = 12;}}
if(KEY25 == 1){delay1ms(20);if(KEY25 == 1){MUSIC = 11;}}
if(KEY24 == 1){delay1ms(20);if(KEY24 == 1){MUSIC = 10;}}
if(KEY23 == 1){delay1ms(20);if(KEY23 == 1){MUSIC = 9;}}
if(KEY22 == 1){delay1ms(20);if(KEY22 == 1){MUSIC = 8;}}
if(KEY21 == 1){delay1ms(20);if(KEY21 == 1){MUSIC = 7;}}
//2
if(KEY17 == 1){delay1ms(20);if(KEY17 == 1){MUSIC = 6;}}
if(KEY16 == 1){delay1ms(20);if(KEY16 == 1){MUSIC = 5;}}
if(KEY15 == 1){delay1ms(20);if(KEY15 == 1){MUSIC = 4;}}
if(KEY14 == 1){delay1ms(20);if(KEY14 == 1){MUSIC = 3;}}
if(KEY13 == 1){delay1ms(20);if(KEY13 == 1){MUSIC = 2;}}
if(KEY12 == 1){delay1ms(20);if(KEY12 == 1){MUSIC = 1;}}
if(KEY11 == 1){delay1ms(20);if(KEY11 == 1){MUSIC = 0;}}
if(MUSIC != 0xff){ //如果有音阶数值(非0XFF时)
STH0=tab[MUSIC]/256; //将音阶的频率值装入定时器T0
STL0=tab[MUSIC]%256;
TR0=1; //启动定时器
MUSIC = 0xff; //清除寄存器
}else{ //否则
SPEAKER = 1; //关扬声器
TR0=0; //关定时器
}
}
}
/***************************************************************************************/
void t0(void) interrupt 1 using 0{//定时器0产生音频
TH0=STH0;
TL0=STL0;
SPEAKER=~SPEAKER; //取反频率产生音调
}
电路原理是一样的 ,,只不过我把IO口改了。
这个是杜洋的原理图![](http://cache.amobbs.com/bbs_upload782111/files_46/ourdev_682452LZKAFH.jpg)
(原文件名:SCH_S.jpg)
有人做过吗? 指点指点吧 |
阿莫论坛20周年了!感谢大家的支持与爱护!!
曾经有一段真挚的爱情摆在我的面前,我没有珍惜,现在想起来,还好我没有珍惜……
|