C语言钢琴图案,基于STC单片机的触摸式电钢琴制作

#define SPEED         72//语速(在12MHz时设置为70最佳,值越小语速越快)

#define X2                 2 //音频播放倍速(默认值2,增加/减少时音频速度整倍加/减)

#define A_DA         1600 //音频数据字节的数量,即音频文件的行数。最大值不得大于65535        我的音频是1600

//(如果有较长的音频需要播放可以分段处理,每段字节数不大于65535)//

#define DL_F         1 //音频播放前的延时(单位:mS)。最大值不得大于65535

#define DL_A         1 //音频播放后的延时(单位:mS)。最大值不得大于65535

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

#include //头文件

sbit PWM  = P1 ^ 3;//音频输出接口(只适用PWM接口)

sbit LED  = P3 ^ 6;//状态指示LED灯 正极

sbit LEDG  = P3 ^ 7;//状态指示LED灯 负极

unsigned char code  wav1[];//音频数据表声明

unsigned char code  wav2[];

unsigned char code  wav3[];

unsigned char code  wav4[];

unsigned char code  wav5[];

unsigned char code  wav6[];

unsigned char code  wav7[];

unsigned char code  wav8[];

unsigned char code  wav9[];

unsigned char code wav10[];

unsigned char code wav11[];

unsigned char code wav12[];

unsigned char code wav13[];

unsigned char code wav14[];

unsigned char code wav15[];

unsigned char code wav16[];

unsigned char code wav17[];

unsigned char code wav18[];

unsigned char code wav19[];

unsigned char code wav20[];

unsigned char code wav21[];

unsigned char code wav22[];

unsigned char code wav23[];

unsigned char code wav24[];

unsigned char code wav25[];

unsigned char code wav26[];

unsigned char code wav27[];

unsigned char code wav28[];

unsigned char code wav29[];

unsigned char code wav30[];

unsigned char code wav31[];

unsigned char code wav32[];

unsigned char code wav33[];

unsigned char code wav34[];

unsigned char code wav35[];

unsigned char code wav36[];

sbit  TUI1  =  P2 ^ 1 ;

sbit  TUI2  =  P2 ^ 2 ;

sbit  TUI3  =  P0 ^ 0 ;

sbit  KEY1  =  P2 ^ 3 ;

sbit  KEY2  =  P2 ^ 4 ;

sbit  KEY3  =  P2 ^ 5 ;

sbit  KEY4  =  P2 ^ 6 ;

sbit  KEY5  =  P2 ^ 7 ;

sbit  KEY6  =  P0 ^ 7 ;

sbit  KEY7  =  P0 ^ 6 ;

sbit  KEY8  =  P0 ^ 5 ;

sbit  KEY9  =  P0 ^ 4 ;

sbit  KEY10 =  P0 ^ 3 ;

sbit  KEY11 =  P0 ^ 2 ;

sbit  KEY12 =  P0 ^ 1 ;

unsigned char         xiang           ;

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

void DELAY_MS (unsigned int a){//毫秒级CPU延时函数

unsigned int i;

while( --a != 0){

for(i = 0; i < 600; i++);

}

}

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

void DELAY_NS (unsigned char a){//用于音频延时

unsigned int i;

while( --a != 0){

for(i = 0; i < SPEED; i++);

}

}

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

void PWM_init (void){//PWM初始化函数

CMOD=0x02; //设置PCA定时器

CL=0x00;

CH=0x00;

CCAPM0=0x42; //PWM0设置PCA工作方式为PWM方式(0100 0010)

CCAP0L=0x00; //设置PWM0初始值与CCAP0H相同

CCAP0H=0x00; // PWM0初始时为0

CR=1; //启动PCA定时器

}

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

void PWM0_set (unsigned char a){//PWM0占空比设置函数

CCAP0L= a; //设置值直接写入CCAP0L

CCAP0H= a; //设置值直接写入CCAP0H

}

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

void SP1 (void){//音频播放函数

unsigned int a;

LEDG = 0;//LED灯开

DELAY_MS (DL_F);//播放前延时

for(a=0;a

DELAY_NS (X2);//播放数据间隔,即可调整音频速度和音调

if(xiang == 1) {

PWM0_set(wav1[a]);}

if(xiang == 2) {

PWM0_set(wav2[a]);}

if(xiang == 3) {

PWM0_set(wav3[a]);}

if(xiang == 4) {

PWM0_set(wav4[a]);}

if(xiang == 5) {

PWM0_set(wav5[a]);}

if(xiang == 6) {

PWM0_set(wav6[a]);}

if(xiang == 7) {

PWM0_set(wav7[a]);}

if(xiang == 8) {

PWM0_set(wav8[a]);}

if(xiang == 9) {

PWM0_set(wav9[a]);}

if(xiang ==10) {

PWM0_set(wav10[a]);}

if(xiang ==11) {

PWM0_set(wav11[a]);}

if(xiang ==12) {

PWM0_set(wav12[a]);}

if(xiang ==13) {

PWM0_set(wav13[a]);}

if(xiang ==14) {

PWM0_set(wav14[a]);}

if(xiang ==15) {

PWM0_set(wav15[a]);}

if(xiang ==16) {

PWM0_set(wav16[a]);}

if(xiang ==17) {

PWM0_set(wav17[a]);}

if(xiang ==18) {

PWM0_set(wav18[a]);}

if(xiang ==19) {

PWM0_set(wav19[a]);}

if(xiang ==20) {

PWM0_set(wav20[a]);}

if(xiang ==21) {

PWM0_set(wav21[a]);}

if(xiang ==22) {

PWM0_set(wav22[a]);}

if(xiang ==23) {

PWM0_set(wav23[a]);}

if(xiang ==24) {

PWM0_set(wav24[a]);}

if(xiang ==25) {

PWM0_set(wav25[a]);}

if(xiang ==26) {

PWM0_set(wav26[a]);}

if(xiang ==27) {

PWM0_set(wav27[a]);}

if(xiang ==28) {

PWM0_set(wav28[a]);}

if(xiang ==29) {

PWM0_set(wav29[a]);}

if(xiang ==30) {

PWM0_set(wav30[a]);}

if(xiang ==31) {

PWM0_set(wav31[a]);}

if(xiang ==32) {

PWM0_set(wav32[a]);}

if(xiang ==33) {

PWM0_set(wav33[a]);}

if(xiang ==34) {

PWM0_set(wav34[a]);}

if(xiang ==35) {

PWM0_set(wav35[a]);}

if(xiang ==36) {

PWM0_set(wav36[a]);}

}

xiang = 0;

DELAY_MS (DL_A);//播放后延时

LEDG=1;//LED灯关

PWM0_set(0); //设置PWM占空比

PWM = 0;//PWM关闭前清0

}

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

void main (void){//主函数

P0M1 = 0xFE;// (11111110)*P0.0(低音)P2.2(中音)P2.1(高音)强推

P0M0 = 0x01;// (00000001)*P2.3 P2.4 P2.5 P2.6 P2.7 P0.7

P2M1 = 0xF8;// (11111000)*P0.6 P0.5 P0.4 P0.3 P0.2 P0.1 高阻

P2M0 = 0x06;// (00000110)

PWM0_set(128);//设置PWM到中间值,即音频波形的基础电平。

CR=1; //启动PCA定时器

P3M0 = 0xFF;//状态指示LED接口强推设置

PWM = 0;//PWM启动前准备

PWM_init(); //PWM初始化

while(1){

TUI1 = 1;

if(KEY1||KEY2||KEY3||KEY4||KEY5||KEY6||KEY7||KEY8||KEY9||KEY10||KEY11||KEY12 == 1){

DELAY_MS (20);

if(KEY1 == 1){xiang = 1 ;SP1();while(KEY1  == 1);}

if(KEY2 == 1){xiang = 2 ;SP1();while(KEY2  == 1);}

if(KEY3 == 1){xiang = 3 ;SP1();while(KEY3  == 1);}

…………限于本文篇幅 余下代码请从51黑下载附件…………

你可能感兴趣的:(C语言钢琴图案)