杜洋工作室

www.doyoung.net

上有好多,要的话我还有流程图

/*********************************************************************************************

程序名:DoToy系列作品?MidTouch21电子琴程序

编写人:杜洋?

编写时间:2009年6月3日

硬件支持:STC11L60XE?外部12MHZ晶振?电源3V

接口说明:详见《DoToy_MidTouch21电路原理图》

修改日志:

NO.1-20090603_17.54?完成电子琴21键的测试(20090603_1备)

NO.2-20090604_01.29?改为第二次新板的硬件电路(20090604_2备)

/*********************************************************************************************

说明:

用STC11Fxx单片机I/O接口的高阻态输入功能,扫描I/O接口电平。

手指将VCC线和I/O接口线半连接,产生高电平信号。

/*********************************************************************************************/

void?INIT(void);

#include?<STC11Fxx.H>?

/*********************************************************************************************/

sbit?SPEAKER=?P1^0;//扬声器,低使能

sbit?LED?=?P3^0;//LED正极,强推

sbit?LED2?=?P3^1;//LED负极

sbit?KEY11?=?P2^0;

sbit?KEY12?=?P2^1;

sbit?KEY13?=?P2^2;

sbit?KEY14?=?P2^3;

sbit?KEY15?=?P2^4;

sbit?KEY16?=?P2^5;

sbit?KEY17?=?P2^6;

sbit?KEY21?=?P2^7;

sbit?KEY22?=?P4^4;

sbit?KEY23?=?P4^5;

sbit?KEY24?=?P4^6;

sbit?KEY25?=?P0^7;

sbit?KEY26?=?P0^6;

sbit?KEY27?=?P0^5;

sbit?KEY31?=?P0^4;

sbit?KEY32?=?P0^3;

sbit?KEY33?=?P0^2;

sbit?KEY34?=?P0^1;

sbit?KEY35?=?P0^0;

sbit?KEY36?=?P3^3;

sbit?KEY37?=?P3^2;

/*********************************************************************************************/

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?=?0xfe;//P1.0为准双向,其他为高阻输入?

P1M0?=?0x00;?

P2M1?=?0xff;//P2全为高阻输入?

P2M0?=?0x00;?

P3M1?=?0xfc;//P3.0为强推,其他为标准双向?

P3M0?=?0x01;?

P4M1?=?0xff;//P4全为高阻输入?

P4M0?=?0x00;?

P4SW?=?0xff;?

LED?=?1;

LED2?=?0;

SPEAKER?=?0;

}

/***************************************************************************************/

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(KEY32?==?1){delay1ms(20);if(KEY32?==?1){MUSIC?=?15;}}

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;}}

//1

if(MUSIC?!=?0xff){?//如果有音阶数值(非0XFF时)

STH0=tab[MUSIC]/256;?//将音阶的频率值装入定时器T0?

STL0=tab[MUSIC]%256;?

TR0=1;?//启动定时器

MUSIC?=?0xff;?//清除寄存器

}else{?//否则?

SPEAKER?=?1;?//关扬声器

LED?=?1;

TR0=0;?//关定时器

}?

}?

}?

/***************************************************************************************/

void?t0(void)?interrupt?1?using?0{//定时器0产生音频?

TH0=STH0;?

TL0=STL0;?

SPEAKER=~SPEAKER;?//取反频率产生音调

LED?=?SPEAKER;?//LED同频闪烁?

}

/***************************************************************************************/

/*************************************************************

*?杜洋工作室?www.DoYoung.net

/*************************************************************/