新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設計應用 > 我的MIDI鍵盤程序

我的MIDI鍵盤程序

作者: 時間:2016-11-29 來源:網(wǎng)絡 收藏
//--------------------------------------------------------------------------------------------------// 函數(shù)名稱: slavesenddata// 入口參數(shù): control,note and vel// 函數(shù)功能: 發(fā)送MIDI信息在IIC總線上//--------------------------------------------------------------------------------------------------//保證Simple.不使用標準I2C協(xié)議。 發(fā)送從機地址,等ACK,再發(fā)送發(fā)送3byte數(shù)據(jù),等ACK.It s very simple.void slavesenddata(unsigned char control,unsigned char note,unsigned char svel){EA=0;IT0=0; //外中斷0為 下降沿觸發(fā) 設定成低電平出發(fā)的話 容易導致 誤觸發(fā)EX0=0; //開外部中斷0iic_start();SystemError=1;IICSendByte(SlaveAddress);//發(fā)送地址if (IICwaitACK()) //等主機的回應{IICSendByte(control);IICSendByte(note);IICSendByte(svel);if (IICwaitACK()) //等主機的回應{SystemError=0;}}iic_stop();IT0=1; //地址不對就不去管了EX0=1; //開中斷繼續(xù)EA=1;}void initial_i2c(){IT0=1; //外中斷0為 下降沿觸發(fā) 設定成低電平出發(fā)的話 容易導致 誤觸發(fā)EX0=1; //開外部中斷0EA=1;}void recvint0() interrupt 0 using 2{unsigned char mAddress=0;EA=0;IT0=0; //EX0=0; //關中斷防止干擾//slave_ACK();mAddress=IICreceiveByte();if (mAddress==MasterAddress){slave_ACK();s_control=IICreceiveByte();s_note=IICreceiveByte();s_svel=IICreceiveByte();nm=1; //提示主程序有新的消息,請注意查收~}else{//延時,不能這樣退出去引發(fā)中斷//ACK占用兩個周期,接受數(shù)據(jù)占用10個周期。延夠時了就跑delayNOP();delayNOP();delayNOP();delayNOP();delayNOP();delayNOP();delayNOP();delayNOP();}IT0=1; //地址不對就不去管了EX0=1; //開中斷繼續(xù)EA=1;} //下面是引腳的連接以及相關必要的宏定義//Crystal at 12MHz#ifdef io_74hc165_SH_LD#else#define io_74hc165_SH_LD P1_0 //默認設置#endif#ifdef io_74hc165_CLK#else#define io_74hc165_CLK P1_1#endif#ifdef io_74hc165_SDA#else#define io_74hc165_SDA P1_2#endif#define io_74hc165_SH_HIGH io_74hc165_SH_LD=1;#define io_74hc165_SH_LOW io_74hc165_SH_LD=1;#ifdef io_74hc164_SCK#else#define io_74hc164_SCK P1_3#endif#ifdef io_74hc164_SDA#else#define io_74hc164_SDA P1_4#endif#define IO_74HC164_SCK_HIGH io_74hc164_SCK = 1 ;#define IO_74HC164_SCK_LOW io_74hc164_SCK = 0 ;#define IO_74HC164_SDA_INPUT io_74hc164_SDA//使用165來接受鍵盤數(shù)據(jù)。兩片作為164輸出。#define uint8 unsigned char#define uchar unsigned char#define uint unsigned intuchar keytype=2;//#define keytype 2#define blacklist_time 240uchar kbtime=0; //記錄時間,占2個字節(jié)uchar keynumber[2][7]; //記錄鍵位,占14個字節(jié)uchar blacklist[7];//uchar code seg[]={"admin"};/*0->NoTouch Response1->Very Soft Touch Response2->Soft Touch Response3->Medium Touch Response4->Hard Touch Response5->Very Hard Touch Response*/uchar code vel[5][251]={{//Very Soft127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,126,126,125,124,124,123,122,122,121,120,120,119,118,118,117,116,116,115,114,114,113,112,112,111,110,110,109,108,108,107,106,106,105,104,104,103,102,102,101,100,100,99,98,98,97,96,96,95,94,94,93,92,92,91,90,90,89,88,88,87,86,86,85,84,84,83,82,82,81,80,80,79,78,78,77,76,76,75,74,74,73,72,72,71,70,70,69,68,68,67,66,66,65,64,64,63,62,62,61,60,60,59,58,58,57,56,56,55,54,54,53,52,52,51,50,50,49,48,48,47,46,46,45,44,44,43,42,42,41,40,40,39,38,38,37,36,36,35,34,34,33,32,32,31,30,30,29,28,28,27,26,26,25,24,24,23,22,22,21,20,20,19,18,18,17,16,16,15,14,14,13,12,12,11,10,10,9,8,8,7,6,6,5,4,4,3,2,2,1,0},{//Soft127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,126,125,125,124,124,123,122,122,121,121,120,120,119,118,118,117,117,116,115,115,114,114,113,112,112,111,111,110,110,109,108,108,107,107,106,105,105,104,104,103,102,102,101,101,100,100,99,98,98,97,97,96,95,95,94,94,93,92,92,91,91,90,90,89,88,88,87,87,86,85,85,84,84,83,82,82,81,81,80,80,79,78,78,77,77,76,75,75,74,74,73,72,72,71,71,70,70,69,68,68,67,67,66,65,65,64,64,63,62,62,61,61,60,60,59,58,58,57,57,56,55,55,54,54,53,52,52,51,51,50,50,49,48,48,47,47,46,45,45,44,44,43,42,42,41,41,40,40,39,38,38,37,37,36,35,35,34,34,33,32,32,31,31,30,30,29,28,28,27,27,26,25,25,24,24,23,22,22,21,21,20,20,19,18,18,17,17,16,15,15,14,14,13,12,12,11,11,10,10,9,8,8,7,7,6,5,5,4,4,3,2,2,1,1,0},{//Medium127,127,127,127,127,127,127,127,127,126,126,125,125,124,124,123,123,122,122,121,121,120,120,119,118,118,117,117,116,116,115,115,114,114,113,113,112,112,111,111,110,110,109,108,108,107,107,106,106,105,105,104,104,103,103,102,102,101,101,100,100,99,98,98,97,97,96,96,95,95,94,94,93,93,92,92,91,91,90,90,89,88,88,87,87,86,86,85,85,84,84,83,83,82,82,81,81,80,80,79,78,78,77,77,76,76,75,75,74,74,73,73,72,72,71,71,70,70,69,68,68,67,67,66,66,65,65,64,64,63,63,62,62,61,61,60,60,59,58,58,57,57,56,56,55,55,54,54,53,53,52,52,51,51,50,50,49,48,48,47,47,46,46,45,45,44,44,43,43,42,42,41,41,40,40,39,38,38,37,37,36,36,35,35,34,34,33,33,32,32,31,31,30,30,29,28,28,27,27,26,26,25,25,24,24,23,23,22,22,21,21,20,20,19,18,18,17,17,16,16,15,15,14,14,13,13,12,12,11,11,10,10,9,8,8,7,7,6,6,5,5,4,4,3,3,2,2,1,1,0},{//Hard100,99,99,98,98,98,97,97,96,96,96,95,95,94,94,94,93,93,92,92,92,91,91,90,90,90,89,89,88,88,88,87,87,86,86,86,85,85,84,84,84,83,83,82,82,82,81,81,80,80,80,79,79,78,78,78,77,77,76,76,76,75,75,74,74,74,73,73,72,72,72,71,71,70,70,70,69,69,68,68,68,67,67,66,66,66,65,65,64,64,64,63,63,62,62,62,61,61,60,60,60,59,59,58,58,58,57,57,56,56,56,55,55,54,54,54,53,53,52,52,52,51,51,50,50,50,49,49,48,48,48,47,47,46,46,46,45,45,44,44,44,43,43,42,42,42,41,41,40,40,40,39,39,38,38,38,37,37,36,36,36,35,35,34,34,34,33,33,32,32,32,31,31,30,30,30,29,29,28,28,28,27,27,26,26,26,25,25,24,24,24,23,23,22,22,22,21,21,20,20,20,19,19,18,18,18,17,17,16,16,16,15,15,14,14,14,13,13,12,12,12,11,11,10,10,10,9,9,8,8,8,7,7,6,6,6,5,5,4,4,4,3,3,2,2,2,1,1,0,0},{//Very Hard92,92,91,91,91,90,90,90,89,89,88,88,88,87,87,87,86,86,85,85,85,84,84,84,83,83,82,82,82,81,81,81,80,80,80,79,79,78,78,78,77,77,77,76,76,75,75,75,74,74,74,73,73,72,72,72,71,71,71,70,70,70,69,69,68,68,68,67,67,67,66,66,65,65,65,64,64,64,63,63,62,62,62,61,61,61,60,60,59,59,59,58,58,58,57,57,57,56,56,55,55,55,54,54,54,53,53,52,52,52,51,51,51,50,50,50,49,49,48,48,48,47,47,47,46,46,45,45,45,44,44,44,43,43,42,42,42,41,41,41,40,40,40,39,39,38,38,38,37,37,37,36,36,35,35,35,34,34,34,33,33,32,32,32,31,31,31,30,30,29,29,29,28,28,28,27,27,27,26,26,25,25,25,24,24,24,23,23,22,22,22,21,21,21,20,20,20,19,19,18,18,18,17,17,17,16,16,15,15,15,14,14,14,13,13,12,12,12,11,11,11,10,10,10,9,9,8,8,8,7,7,7,6,6,5,5,5,4,4,4,3,3,2,2,2,1,1,1,0,0}};uchar count_begin[7][7]; //記錄按鍵時間,占49個字節(jié)//在鋼琴上壓鍵時間大于500mS可以幾乎認為是沒有力度了啊//VIO//VIO程序開始。void v_74hc164WriteData_f( uint8 Dat ) //向74HC164寫一個字節(jié)的內(nèi)容{ //即可并行輸出該字節(jié)uint8 i = 0 ;uint8 SendData = Dat ;for( i = 8 ; i > 0 ; i-- ){IO_74HC164_SCK_LOWSendData <<= 1 ;IO_74HC164_SDA_INPUT = CY ;IO_74HC164_SCK_HIGH}}uchar v_74hc165ReadData_f(){uchar i,c=0x00;for (i=0;i<8;i++){c<=1; //0000 0010if(io_74hc165_SDA){c=c|0x01;}io_74hc165_CLK=0; //下降沿有效io_74hc165_CLK=0;io_74hc165_CLK=1;//進位}return c;}//VIO結束/*74HC1651 BIT1~8 KB DATA 1~874HC1652 BIT1~8 KB DATA 9~1674HC5952 BIT1~8 KB CS 1~8*/void initial_clock(){ TMOD=0x00;TH0=0x10;TL0=0xC1;PCON=0x80;}/*MIDI命令簡表命令代碼(cc)命令說明數(shù)據(jù)kk含義及說明數(shù)據(jù)vv含義及說明8+ 通道號關閉音符對應的MIDI音符0-127關閉音符的速度值9+ 通道號開啟音符對應的MIDI音符0-127壓下琴鍵的速度值(力度)A+ 通道號觸后壓力對應的MIDI音符0-127對應音符的觸后壓力值B+ 通道號控制器控制器號0-7777-7F為通道模式信息控制器值C+ 通道號音色切換音色號 0-127無該字節(jié)數(shù)據(jù)D+ 通道號通道壓力該通道全部鍵盤的觸后壓力無該字節(jié)數(shù)據(jù)E+ 通道號彎音輪彎音輪低位數(shù)據(jù)彎音輪高位數(shù)據(jù)F系統(tǒng)普通信息、實時信息、及高級信息代碼忽略忽略*/

關鍵詞: MIDI鍵盤程

評論


技術專區(qū)

關閉