4X4键盘多功能电子琴

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

#include
#include
#include "12864.c"
#include "math.h"
#include "stdio.h"
#define uchar unsigned char
#define uint unsigned int


extern void music(void);
#define DO 63622 //523
#define RE 63831 //587
#define MI 64017 //659
#define FA 64102 //698
#define SO 64259 //784
#define LA 64398 //880
#define TI 64522 //988
#define DO_H 64578 //1046
#define RE_H 64683 //1174
#define MI_H 64776 //1318
#define FA_H 64819 //1397
#define SO_H 64897 //1568
#define LA_H 64966 //1760
#define TI_H 65028 //1976
#define DO_HH 65057 //2093



const uchar sound[]={ //乐曲数据表
0x40,0x4c,0x4a,0x49,0xc8,0x49,0x47,0x46,
0xe5,0x4e,0x4d,0x4e,0x6c,0x2d,0x4e,
0x4d,0x2c,0x2d,0x2e,0x2d,0x6f,0x2d,
0x2c,0x2d,0x2c,0x29,0x2a,0x2b,
0x2a,0x29,0xc8,0x4c,0x4e,0x49,0x46,0x48,
0xc5,0x26,0x28,0xe5,0x83,0x65,0x26,
0x68,0x29,0x26,0x28,0x45,0x6c,0x2f,0x3d,
0x1c,0x2a,0x2c,0xe9,0x69,0x2a,0x47,0x46,
0x65,0x26,0x48,0x49,0x43,0x48,0x36,0x15,
0x26,0x18,0xe5,0x6a,0x2c,0x47,0x49,0x26,
0x28,0x85,0x33,0x15,0x43,0x25,0x26,0x27,
0x29,0xc6,0x25,0x26,0x68,0x29,0x4c,0x4a,
0x49,0x2a,0x29,0x49,0x26,0x25,0x83,0x88,
0x26,0x28,0x26,0x25,0x23,0x25,0x26,0x28,
0xc5,0x04,0x04,
/*************希望*****************/
0x49,0x4a,0x4a,0x6a,0x29,0x48,0x46,0x68,
0x29,0xc8,0x49,0x4a,0x4a,0x6a,0x2c,0x4a,
0x4a,0x49,0x4a,0xca,0x4c,0x4d,0x4d,0x6d,
0x2c,0x4a,0x4a,0x4c,0x4d,0xcc,0x49,0x4a,
0x4a,0x69,0x2a,0x4a,0x49,0x4a,0x46,
0x18,0xe6,0x29,0x29,0x29,0x28,0x46,
0x29,0x29,0x29,0x28,0x46,0x49,0x4a,
0x48,0x69,0x2a,0x4c,0x2d,0x2d,0x2d,
0x2c,0x4a,0x29,0x29,0x29,0x28,0x46,
0x46,0x45,0x46,0xe6,

/***********喀秋莎*********/
0x66,0x27,0x68,0x26,0x28,0x28,0x27,0x26,0x47,0x43,
0x67,0x28,0x69,0x27,0x29,0x29,0x28,0x27,0x46,0x46,
0x4a,0x4d,0x4c,0x2d,0x2c,0x2b,0x2b,0x2a,0x29,0x4a,
0x46,0x20,0x4b,0x29,0x6a,0x28,0x27,0x23,0x28,0x27,
0x46,0x46,0x4a,0x4d,0x4c,0x2d,0x2c,0x2b,0x2b,0x2a,
0x29,0x4a,0x46, 0x20,0x4b,0x29,0x6a,0x28,0x27,0x23,
0x28,0x27,0x46,0x46,
/*****************同一首歌**************/
0x85,0x48,0x49,0x6a,0x2b,0x4a,0x48,0x89,0x48,
0x46,0xf8, 0x85,0x48,0x49,0x4a,0x2a,0x2b,0x4c,
0x48,0x6b,0x2a,0x4c,0x29,0x2a,0x2a,0x29,0x89,
0x8a,0x4c,0x4e,0x6e,0x2d,0x8d,0x4c,0x2c,0x2d,
0x4e,0x2d,0x2c,0xaa, 0x6b,0x2b,0x4c,0x4d,0x4c,
0x2b,0x2a,0x89,0x47,0x27,0x26,0x45,0x46,0xa8,
0x8f,0x8d,0x6b,0x2c,0x8d,0x4e,0x2e,0x2e,0x4e,
0x2d,0x2c,0xaa, 0x8f,0x8d,0x6b,0x2c,0x8d,0x4d,
0x2d,0x2d,0x4d,0x2b,0x2a,0xf9, 0x8c,0x48,0x49,0x6a,
0x2b,0x4a,0x28,0x28,0x69,0x29,0x49,0x29,0x28,
0x46,0xc6, 0x87,0x67,0x26,0x45,0x4d,0x4c,0x29,0x29,
0x6b,0x2b,0x4b,0x2a,0x29,0xfc,


0x00};



const uint TONETABLE[15]={DO,RE,MI,FA,SO,LA,TI,DO_H,
RE_H,MI_H,FA_H,SO_H,LA_H,TI_H,DO_HH};

uint toneconst;

void Delayus(uint US)
{
uint i;
US=US*5/4; //5/4是在8MHz晶振下,通过软件仿真反复实验得到的数值
for( i=0;i}

/************************************************
*函数名称: void Delayms(uint16 MS)
*入口参数: uint16 MS,延时的毫秒数
*出口参数: 无
*函数功能: 延时指定毫秒(8M晶振)
*创建时间: 2009-09-01 eleShrimp
*修改说明:
/************************************************/
void Delayms(uint MS)
{
uint i,j;
for( i=0;ifor(j=0;j<1141;j++); //1141是在8MHz晶振下,通过软件仿真反复实验得到的数值
}
/*void delay(uint t)
{
while(t--);
}*/
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
MCUCR = 0x00;
GICR = 0x00;
TIMSK = 0x04; //timer interrupt sources
SREG=0x80;// //全局中断开
SEI(); //re-enable interrupts
//all peripherals are now initialized
}

#pragma interrupt_handler timer1_ovf_isr:9
void timer1_ovf_isr(void)
{
TCNT1=toneconst;

PORTD^=BIT(4);
}

/************************************************
*函数名称: void music(void)
*入口参数: 无
*出口参数: 无
*函数功能: 音乐播放函数
*创建时间: 2009-09-01 eleShrimp
*修改说明:
/************************************************/
void music(void)
{
uchar i,temp=0,temp1=0;
uint sound_add=0;
TCCR1A = 0x00;
TCCR1B =(1<temp1=sound[sound_add];

while(temp1!=0)
{
temp=temp1;
temp&=0x0f;
if(temp!=0)
{
TIMSK|=(1<temp--;
toneconst=TONETABLE[temp];
TCNT1=toneconst;
}
temp=temp1;
temp>>=4;
temp&=0x0f;
Delayms(temp*129);//根据SOUNDTABLE中数据的高四位*5后,为音调持续时间
TIMSK&=~(1<sound_add++;
temp1=sound[sound_add];
if(sound_add==1)
{
send_command(1);
delay_nms(10);
send_command(0x80);
for(i=0;i<16;i++)
{
send_data(shu[i]);
//delay(10);
}
}
if(sound_add==40 )
{
send_command(0x80);
for(i=0;i<16;i++)
{
send_data(shu[i]);
//delay(10);
}

send_command(0x90);
for(i=0;i<16;i++)
{
send_data(shu1[i]);
//delay(10);
}
send_command(0x88);
for(i=0;i<16;i++)
{
send_data(shu2[i]);
// delay(10);
}
}
if(sound_add==55 )
{
send_command(0x80);
for(i=0;i<16;i++)
{
send_data(shu[i]);
//delay(10);
}
send_command(0x90);
for(i=0;i<16;i+

+)
{
send_data(shu3[i]);
//delay(10);
}
send_command(0x88);
for(i=0;i<16;i++)
{
send_data(shu4[i]);
// delay(10);
}
}
if(sound_add==70 )
{
send_command(0x80);
for(i=0;i<16;i++)
{
send_data(shu[i]);
//delay(10);
}
send_command(0x90);
for(i=0;i<16;i++)
{
send_data(shu5[i]);
//delay(10);
}
send_command(0x88);
for(i=0;i<16;i++)
{
send_data(shu6[i]);
// delay(10);
}
}
if(sound_add==85 )
{
send_command(0x80);
for(i=0;i<16;i++)
{
send_data(shu[i]);
//delay(10);
}

send_command(0x90);
for(i=0;i<16;i++)
{
send_data(shu7[i]);
//delay(10);
}
send_command(0x88);
for(i=0;i<16;i++)
{
send_data(shu8[i]);
// delay(10);
}
}
if(sound_add==99 )
{
send_command(0x80);
for(i=0;i<16;i++)
{
send_data(shu[i]);
//delay(10);
}
send_command(0x90);
for(i=0;i<16;i++)
{
send_data(shu9[i]);
//delay(10);
}
send_command(0x88);
for(i=0;i<16;i++)
{
send_data(shu10[i]);
// delay(10);
}
}

if(sound_add==110 )
{
send_command(0x80);
for(i=0;i<16;i++)
{
send_data(shu11[i]);
//delay(10);
}
send_command(0x90);
for(i=0;i<16;i++)
{
send_data(shu12[i]);
//delay(10);
}
send_command(0x88);
for(i=0;i<16;i++)
{
send_data(shu13[i]);
// delay(10);
}
}

if(sound_add==120 )
{
send_command(0x80);
for(i=0;i<16;i++)
{
send_data(shu11[i]);
//delay(10);
}
send_command(0x90);
for(i=0;i<16;i++)
{
send_data(shu14[i]);
//delay(10);
}
send_command(0x88);
for(i=0;i<16;i++)
{
send_data(shu15[i]);
// delay(10);
}
}
if(sound_add==130 )
{
send_command(0x80);
for(i=0;i<16;i++)
{
send_data(shu11[i]);
//delay(10);
}
send_command(0x90);
for(i=0;i<16;i++)
{
send_data(shu16[i]);
//delay(10);
}
send_command(0x88);
for(i=0;i<16;i++)
{
send_data(shu17[i]);
// delay(10);
}
}
if(sound_add==140 )
{
send_command(0x80);
for(i=0;i<16;i++)
{
send_data(shu11[i]);
//delay(10);
}
send_command(0x90);
for(i=0

;i<16;i++)
{
send_data(shu18[i]);
//delay(10);
}
send_command(0x88);
for(i=0;i<16;i++)
{
send_data(shu19[i]);
// delay(10);
}
}
if(sound_add==151 )
{
send_command(0x80);
for(i=0;i<16;i++)
{
send_data(shu11[i]);
//delay(10);
}
send_command(0x90);
for(i=0;i<16;i++)
{
send_data(shu20[i]);
//delay(10);
}
send_command(0x88);
for(i=0;i<16;i++)
{
send_data(shu21[i]);
// delay(10);
}
}
if(sound_add==161 )
{
send_command(0x80);
for(i=0;i<16;i++)
{
send_data(shu11[i]);
//delay(10);
}
send_command(0x90);
for(i=0;i<16;i++)
{
send_data(shu22[i]);
//delay(10);
}
send_command(0x88);
for(i=0;i<16;i++)
{
send_data(shu23[i]);
// delay(10);
}
}
if(sound_add==172 )
{
send_command(0x80);
for(i=0;i<16;i++)
{
send_data(shu11[i]);
//delay(10);
}
send_command(0x90);
for(i=0;i<16;i++)
{
send_data(shu24[i]);
//delay(10);
}
send_command(0x88);
for(i=0;i<16;i++)
{
send_data(shu25[i]);
// delay(10);
}
}
if(sound_add==181 )
{
send_command(0x80);
for(i=0;i<16;i++)
{
send_data(shu26[i]);
//delay(10);
}
send_command(0x90);
for(i=0;i<16;i++)
{
send_data(shu27[i]);
//delay(10);
}
send_command(0x88);
for(i=0;i<16;i++)
{
send_data(shu28[i]);
// delay(10);
}
}
if(sound_add==191 )
{
send_command(0x80);
for(i=0;i<16;i++)
{
send_data(shu26[i]);
//delay(10);
}
send_command(0x90);
for(i=0;i<16;i++)
{
send_data(shu29[i]);
//delay(10);
}
send_command(0x88);
for(i=0;i<16;i++)
{
send_data(shu30[i]);
// delay(10);
}
}
if(sound_add==201 )
{
send_command(0x80);
for(i=0;i<16;i++)
{
send_data(shu26[i]);
//delay(10);
}
send_command(0x90);
for(i=0;i<16;i++)
{
send_data(shu31[i]);
//delay(10);
}
send_command(0x88);
for(i=0;i<16;i++)
{
send_data(shu32[i]);
// delay(10);
}
}
if(sound_add==212 )
{
send_command(0x80);
for(i=0;i<16;i++)
{
send_data(shu26[i]);
//delay(10);
}
send_command(0x90);

for(i=0;i<16;i++)
{
send_data(shu33[i]);
//delay(10);
}
send_command(0x88);
for(i=0;i<16;i++)
{
send_data(shu34[i]);
// delay(10);
}
}
if(sound_add==223 )
{
send_command(0x80);
for(i=0;i<16;i++)
{
send_data(shu26[i]);
//delay(10);
}
send_command(0x90);
for(i=0;i<16;i++)
{
send_data(shu31[i]);
//delay(10);
}
send_command(0x88);
for(i=0;i<16;i++)
{
send_data(shu32[i]);
// delay(10);
}
}
if(sound_add==234 )
{
send_command(0x80);
for(i=0;i<16;i++)
{
send_data(shu26[i]);
//delay(10);
}
send_command(0x90);
for(i=0;i<16;i++)
{
send_data(shu33[i]);
//delay(10);
}
send_command(0x88);
for(i=0;i<16;i++)
{
send_data(shu34[i]);
// delay(10);
}
}

if(sound_add==245 )
{
send_command(0x80);
for(i=0;i<16;i++)
{
send_data(shu35[i]);
//delay(10);
}
send_command(0x90);
for(i=0;i<16;i++)
{
send_data(shu36[i]);
//delay(10);
}
send_command(0x88);
for(i=0;i<16;i++)
{
send_data(shu37[i]);
// delay(10);
}
}
if(sound_add==256 )
{
send_command(0x80);
for(i=0;i<16;i++)
{
send_data(shu35[i]);
//delay(10);
}
send_command(0x90);
for(i=0;i<16;i++)
{
send_data(shu38[i]);
//delay(10);
}
send_command(0x88);
for(i=0;i<16;i++)
{
send_data(shu39[i]);
// delay(10);
}
}
if(sound_add==272 )
{
send_command(0x80);
for(i=0;i<16;i++)
{
send_data(shu35[i]);
//delay(10);
}
send_command(0x90);
for(i=0;i<16;i++)
{
send_data(shu40[i]);
//delay(10);
}
send_command(0x88);
for(i=0;i<16;i++)
{
send_data(shu41[i]);
// delay(10);
}
}
if(sound_add==285 )
{
send_command(0x80);
for(i=0;i<16;i++)
{
send_data(shu35[i]);
//delay(10);
}
send_command(0x90);
for(i=0;i<16;i++)
{
send_data(shu42[i]);
//delay(10);
}
send_command(0x88);
for(i=0;i<16;i++)
{
send_data(shu43[i]);
// delay(10);
}
}
if(sound_add==299 )
{
send_command(0x80);
for(i=0;i<16;i++)
{
send_data(shu35[i]);
//delay(10);
}
send_command(0x90);
for(i=0;i<16;i++)

{
send_data(shu44[i]);
//delay(10);
}
send_command(0x88);
for(i=0;i<16;i++)
{
send_data(shu45[i]);
// delay(10);
}
}
if(sound_add==311 )
{
send_command(0x80);
for(i=0;i<16;i++)
{
send_data(shu35[i]);
//delay(10);
}
send_command(0x90);
for(i=0;i<16;i++)
{
send_data(shu46[i]);
//delay(10);
}
send_command(0x88);
for(i=0;i<16;i++)
{
send_data(shu47[i]);
// delay(10);
}
}
if(sound_add==323 )
{
send_command(0x80);
for(i=0;i<16;i++)
{
send_data(shu35[i]);
//delay(10);
}
send_command(0x90);
for(i=0;i<16;i++)
{
send_data(shu48[i]);
//delay(10);
}
send_command(0x88);
for(i=0;i<16;i++)
{
send_data(shu49[i]);
// delay(10);
}
}
if(sound_add==338 )
{
send_command(0x80);
for(i=0;i<16;i++)
{
send_data(shu35[i]);
//delay(10);
}
send_command(0x90);
for(i=0;i<16;i++)
{
send_data(shu50[i]);
//delay(10);
}
send_command(0x88);
for(i=0;i<16;i++)
{
send_data(shu51[i]);
// delay(10);
}
}
if(sound_add==352)
{
send_command(1);
delay_nms(10);
}

}
PORTD &=~BIT(4) ;
}



uchar key_press(void)
{
uchar i;
DDRA=0XFF;
PORTA=0XF0;

DDRA=0X0F;

i=PINA;
if(i==0XF0)
{
DDRA=0XFF;
return 0;
}
else
{
DDRA=0XFF;
return 1;
}
}

uchar key_scan(void)
{
uchar key,i=0X7F,j;
delay(10);
if(key_press())
{
do
{
i=(i<<1|i>>7);
PORTA=i;
DDRA=0X0F;

key=PINA;
j=key&0XF0;

}while(j==0XF0);

while(key_press());
switch(key)
{
case 0xEE:
key=0x0;
break;
case 0xDE:
key=0x1;
break;
case 0xBE:
key=0x2;
break;
case 0x7E:
key=0x3;
break;
case 0xED:
key=0x4;
break;
case 0xDD:
key=0x5;
break;
case 0xBD:
key=0x6;
break;
case 0x7D:
key=0x7;
break;
case 0xEB:
key=0x8;
break;
case 0xDB:
key=0x9;
break;
case 0xBB:
key=0xA;
break;
case 0x7B:
key=0xB;
break;
case 0xE7:
key=0xC;
break;
case 0xD7:
key=0xD;
break;
case 0xB7:
key=0xE;
break;
case 0x77:
key=0xF;
break;
default:
key=16;
}
}
else
{
key=16;
}
return key;
}

void ctc(uchar key)
{

switch(key)
{
case 0x0:
//key=;
TCCR1A=0X50;

TCCR1B=0X09;
OCR1A=15266;
delay_nms(500);
TCCR1A=0X00;
TCCR1B=0X00;
break;
case 0x1:
TCCR1A=0X50;
TCCR1B=0X09;
OCR1A=13604;
delay_nms(500);
TCCR1A=0X00;
TCCR1B=0X00;
//key=;
break;
case 0x2:
TCCR1A=0X50;
TCCR1B=0X09;
OCR1A=12120;
delay_nms(500);
TCCR1A=0X00;
TCCR1B=0X00;
//key=;
break;
case 0x3:
TCCR1A=0X50;
TCCR1B=0X09;
OCR1A=11460;
delay_nms(500);
TCCR1A=0X00;
TCCR1B=0X00;
//key=0x3;
break;
case 0x4:
TCCR1A=0X50;
TCCR1B=0X09;
OCR1A=10203;
delay_nms(500);
TCCR1A=0X00;
TCCR1B=0X00;
//key=0x4;
break;
case 0x5:
TCCR1A=0X50;
TCCR1B=0X09;
OCR1A=9090;
delay_nms(500);
TCCR1A=0X00;
TCCR1B=0X00;
//key=0x5;
break;
case 0x6:
TCCR1A=0X50;
TCCR1B=0X09;
OCR1A=8096;
delay_nms(500);
TCCR1A=0X00;
TCCR1B=0X00;
//key=0x6;
break;
case 0x7:
TCCR1A=0X50;
TCCR1B=0X09;
OCR1A=3819;
delay_nms(500);
TCCR1A=0X00;
TCCR1B=0X00;
//key=0x7;
break;
case 0x8:
TCCR1A=0X50;
TCCR1B=0X09;
OCR1A=3403;
delay_nms(500);
TCCR1A=0X00;
TCCR1B=0X00;
//key=0x8;
break;
case 0x9:
TCCR1A=0X50;
TCCR1B=0X09;
OCR1A=3032;
delay_nms(500);
TCCR1A=0X00;
TCCR1B=0X00;
//key=0x9;
break;
case 0xA:
TCCR1A=0X50;
TCCR1B=0X09;
OCR1A=2862;
delay_nms(500);
TCCR1A=0X00;
TCCR1B=0X00;
//key=0xA;
break;
case 0xB:
TCCR1A=0X50;
TCCR1B=0X09;
OCR1A=2550;
delay_nms(500);
TCCR1A=0X00;
TCCR1B=0X00;
//key=0xB;
break;
case 0xC:
TCCR1A=0X50;
TCCR1B=0X09;
OCR1A=2272;
delay_nms(500);
TCCR1A=0X00;
TCCR1B=0X00;
//key=0xC;
break;
case 0xD:
TCCR1A=0X50;
TCCR1B=0X09;
OCR1A=2023;
delay_nms(500);
TCCR1A=0X00;
TCCR1B=0X00;
//key=0xD;
break;
case 0xE:
//key=0xE;
break;
case 0xF:
//key=0xF;
break;
default:
key=16;
}



}
void main(void)
{
uchar l;
DDRB=0XFF;
PORTB=0XFF;
DDRD=0X7f;


DDRC=0XFF;
PORTC=0X00;
init_devices();
initial();
SREG=0x80;
PORTD|=BIT(7);
while(1)
{
if((PIND&0X80)==0)
{
music();
}
if(key_press())
{
l=key_scan();
ctc(l);

}
}


}

相关文档
最新文档