// /************************************************************** melodyc2 Copyrights(C)2004 air variable http://www.geocities.co.jp/SiliconValley-Sunnyvale/4889/ version 2.30 May,22,2004 Maxium note of 377 **************************************************************/ #include // tone Frequency set (as apend of soft delay 4) #define CL 19, #define C$L 33, #define DL 45, #define D$L 57, #define EL 68, #define FL 79, #define F$L 89, #define GL 99, #define G$L 108, #define AL 116, #define A$L 124, #define BL 131, #define C 139, #define C$ 145, #define D 152, #define D$ 158, #define E 163, #define F 169, #define F$ 174, #define G 178, #define G$ 183, #define A 187, #define A$ 191, #define B 195, #define CH 198, #define C$H 202, #define DH 205, #define D$H 208, #define EH 211, #define FH 213, #define F$H 216, #define GH 218, #define G$H 220, #define AH 222, #define A$H 224, #define BH 226, #define R 0, #define CMD 255, #define END 0 #define TEMPO , //Length (Midi timebase like 48 tick) #define L1 192, //whole #define L2 96, //half #define L3 64, //triplets half #define L4 48, //quater #define L4D 72, //dotted quater #define L6 32, //triplets quater #define L8 24, //eighth #define L8D 36, //dotted eighth #define L12 16, //triplets eighth #define L16 12, //sixteenth #define L16D 18, //dotted sixteenth #define L24 8, //triplets sixteenth #define L32 6, //32th #define L32D 9, //dotted 32th #define L48 4, //triplets 32th #define L64 3, //64th #define L96 2, //triplets 64th #define L192 1, //192th(The minimum unit) //output port is #define sound_output 0x04 //gpio3 #define led_output 0x02 //gpio2 #define keyinput 0x01 //gpio0 //Tempo trim (0 to 4) #define tempo_multi 8; //SONG DATA max 377 note const char songdata[]={ CMD 150 TEMPO //tempocont (except 255) A$L L16 R L16 A$L L16 R L16 G$L L16 GL L16 FL L12 R L32 A$L L16 R L16 A$L L16 R L16 G$L L16 GL L16 FL L12 R L32 GL L16 R L16 GL L16 R L16 D$L L16 DL L16 CL L12 R L32 D$L L16 R L16 DL L16 R L16 CL L8 R L8 //--- C L8 D L8 D$ L16 D L16 C L8 D$ L8 F L8 G L8 F L8 D$ L8 G L8 A$ L8 G$ L16 A$ L16 G L8 F L8 G L8 R L8 // C L8 D L8 D$ L16 D L16 C L8 D$ L8 F L8 G L8 F L8 CH L12 R L32 CH L12 R L32 A$ L8 G L16 A$ L16 G L12 R L32 G L12 R L32 CH L8 R L8 // CH L12 R L32 C L12 R L32 CH L12 R L32 C L12 R L32 CH L12 R L32 C L12 R L32 CH L12 R L32 C L12 R L32 R L8 CH L8 A$ L12 R L32 A$ L12 R L32 G L8 A$ L8 CH L8 R L8 // CH L12 R L32 C L12 R L32 CH L12 R L32 C L12 R L32 CH L12 R L32 C L12 R L32 CH L12 R L32 C L12 R L32 R L8 CH L8 A$ L12 R L32 A$ L12 R L32 G L8 F L8 G L8 R L8 //--- A$ L8 R L8 A$ L8 R L8 A$ L8 R L8 A$ L8 R L8 A$ L16 R L16 A$ L16 R L16 G$ L16 G L16 F L12 R L32 A$ L16 R L16 A$ L16 R L16 G$ L16 G L16 F L12 R L32 D$ L8 F L8 G L16 R L16 G$ L16 G L16 F L8 D$ L8 A$ L8 R L8 // A$ L16 R L16 A$ L16 R L16 G$ L16 G L16 F L12 R L32 A$ L16 R L16 A$ L16 R L16 G$ L16 G L16 F L12 R L32 G L16 R L16 G L16 R L16 D$ L16 D L16 C L12 R L32 D$ L16 R L16 D L16 R L16 C L8 R L8 //Rest R L4 //stop CMD END // Music Last of data }; //see Configr -> Configuration Bits... __CONFIG(0x3184); char tone; char tempocont = 100;//Default TEMPO #pragma interrupt_level 1 //Timer Int process/ void interrupt toneset( void ) { TMR0 = tone; if (tone != 0) GPIO ^= sound_output;//alternate port T0IF =0; } //tempo control wtime(char t0) { int i,t1; for ( i=0 ; i != t0 ;i++) { t1 = tempocont * tempo_multi; while ( --t1 ); } } sleep_control(){ char tmp=2; //power up flash while(tmp--){ GPIO &= ~led_output;//LED flash wtime(20); GPIO |= led_output;//LED flash off wtime(20); } GIE = 0;//Int off T0IE = 0;//timer interrupt is off while ( (GPIO & keyinput)==0 );//wait if key is pushing TRISIO = 0b11111111;//GPIO0 is all input asm("nop"); WPU = 0b00110111;//ALL Pin Pull Up(then Low power) IOCB = 0b00000001;//GPIO0 SW sending GPIF = 0;//GPIF flag clear GPIE = 1;//sleep waikup enable GIE = 0;//Interrupt service is disable //vector non jump and execute next command SLEEP();//sleep! NOP();//Wake up // Wakeup when GPIO0 is LOW GPIE = 0;//GPIO int desable GPIF = 0;//GPIO int flag reset T0IE = 1;//timer on TRISIO = ~(sound_output|led_output); WPU = 0b00000001;//GP0 Pull Up(sw only) GIE = 1; //Interrupt is All enable GPIO &= ~led_output;//LED flash while ( (GPIO & keyinput)==0 );//wait if key is pushing } main(){ int n=0; char tmp,tmp2; //Initialization //Beep out is GPIO2, Power Hold is GPIO1 //Power Hold 1:Power is hold 0:Power is off OSCCAL = _READ_OSCCAL_DATA();//OSCCAL AUTO SET CMCON = 7; ANSEL = 0b00000000; TRISIO = 0b11111101;//LED use GPIO = 0b00000000; TMR0 =0x00 ; GIE = 1; PEIE = 1; tone =0x00; OPTION = 0b00000010; // prescale 8 T0IE = 1; sleep_control();//sleep while(1){ //command detect tmp=songdata[n]; tmp2=songdata[n+1]; if (tmp == 255) { //Command? if (tmp2 == 0){ //Tempo or End of data? n=0; sleep_control();//sleep } else{ tempocont = tmp2; //New tempo change n +=2; } } else{ if ((GPIO&keyinput)==0) sleep_control();// resume tone=tmp; wtime(tmp2); GPIO ^= led_output;//LED flash n +=2; } } }