⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 music_car._c

📁 单片机开发资料光盘-双龙-686M.zip
💻 _C
字号:
/*******************************************/
/*            广州天河双龙电子公司         */
/*            http://www.sl.com.cn         */
/*              电子音乐演示程序           */
/*******************************************/
// 在 icc avr 所在目录的 <include>文件夹中有 "sl3010.c" 文件
//使用说明:音乐输出端口:PORTC.0,音乐数据存放
//于AT90S8515 的EEPROM存贮器中。
// 可以用并口下载软件写入新的音乐文件

//PA口为音乐灯,当不同的音乐节奏,点亮不同的灯。
//声控起动音乐机器人程序,未起动时,PA口的8 LED 呈广告灯效果(霓虹灯)
//声控起动后,机器人开始演奏音乐,同时PA口LED灯根据音乐节拍闪烁
//按遥控器上不同的键后可改变机器人演奏的乐曲。
//连续 按住 < C > 键时间超过 10s,音乐声停止,等待声控重新起动演奏

#include <io8515v.h>
#include <macros.h>
#include <eeprom.h>
#include <sl3010.c>

/* 		  音乐数据  	  */
/* 四分之一拍为0x10, 二分之一拍为0x20, 一拍为0x40 */
/* 低1-1 2-2 3-3 4-4 5-5 6-6 7-7 */
/* 中1-8 2-9 3-A 4-B 5-C 6-D 7-E */
/* 高1-F */								  	
#pragma data:eeprom
unsigned char sound[]={ //乐曲数据表
//情非得以 
0x20,0x1c,0x1d,0x4c,0x4c,0x4c,0x4c,0x20,0x2d,0x19,0x1a,0x19,0x18,0x48,0x48,0x48,
0x48,0x47,0x47,0x46,0x46,0x45,0x45,0x45,0x45,0x45,0x45,0x45,0x45,0x45,

0x23,0x24,0x25,0x44,0x45,0x24,0x23,0x22,0x42,0x40,0x40,0x23,0x22,0x21,0x41,0x28,
0x48,0x23,0x25,0x45,0x40,0x40,0x21,0x21,0x42,0x21,0x21,0x41,0x20,0x28,0x47,0x26,
0x17,0x16,0x15,0x45,0x40,0x31,0x11,0x21,0x21,0x21,0x22,0x42,0x40,0x20,0x23,0x23,
0x24,0x25,0x24,0x23,0x24,0x25,0x26,0x23,0x23,0x42,0x42,0x40,0x23,0x22,0x21,0x41,
0x28,0x28,0x28,0x23,0x25,0x45,0x45,0x40,0x20,0x25,0x46,0x28,0x28,0x48,0x20,0x29,
0x47,0x26,0x17,0x16,0x15,0x45,0x25,0x25,0x26,0x28,0x18,0x16,0x26,0x28,0x26,0x28,
0x19,0x19,0x49,0x49,0x20,0x25,0x2a,0x18,0x19,0x29,0x28,0x16,0x17,0x16,0x15,0x25,
0x25,0x28,0x29,0x2a,0x29,0x48,0x28,0x48,0x2c,0x4c,0x4c,0x40,0x2a,0x2c,0x1c,0x1a,
0x2a,0x2a,0x29,0x4a,0x2b,0x2a,0x4a,0x4a,0x40,0x2a,0x29,0x29,0x28,0x28,0x26,0x48,
0x29,0x26,0x45,0x45,0x40,0x25,0x25,0x26,0x21,0x18,0x16,0x26,0x28,0x26,0x28,0x29,
0x49,0x49,0x20,0x25,0x28,0x29,0x2a,0x29,0x49,0x28,0x48,0x2c,0x4c,0x4c,0x40,0x2a,
0x2c,0x15,0x1a,0x2a,0x2a,0x29,0x4a,0x2b,0x2a,0x4a,0x4a,0x40,0x2a,0x29,0x29,0x28,
0x28,0x26,0x28,0x28,0x29,0x26,0x45,0x45,0x40,0x25,0x25,0x26,0x28,0x28,0x26,0x2a,
0x19,0x18,0x28,0x18,0x19,0x48,0x48,0x40,0x40,0x40,0x40,0x28,0x48,0x29,0x28,0x28,
0x2a,0x1a,0x1a,0x1a,0x18,0x40,0x20,0x26,0x28,0x28,0x28,0x26,0x28,0x48,0x26,0x25,
0x45,0x45,0x40,0x40,0x40,0x28,0x48,0x28,0x28,0x1c,0x1d,0x2d,0x2c,0x28,0x28,0x28,
0x20,0x26,0x19,0x1a,0x29,0x19,0x18,0x28,0x28,0x26,0x21,0x29,0x49,0x49,0x40,0x20,
0x28,0x29,0x2a,0x4a,0x4a,0x20,0x25,0x28,0x29,0x48,0x48,0x48,0x25,0x25,0x26,0x28,
0x28,0x26,0x2a,0x19,0x18,0x28,0x18,0x19,0x49,0x49,0x40,0x40,

		0x00,0x00
						};
#pragma data:data						
/*	  		    音阶表				*/	
const unsigned int 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};
/*			  音阶重装常数			*/						
unsigned int toneconst;								  							   						 				 	 	 
unsigned int sound_add=0;
unsigned int delay_time1,delay_time2,led_delayms=600;
unsigned char temp,temp1;
unsigned char pa,pb,pc,pd,remote_in,timers,cny_in,cny_times,rst_time,cds,pd_select,remote_select;
unsigned char gzsl,t0_times;
unsigned char sound_1,sound_2;

const unsigned char led_data[]=
    {//设置LED广告灯数据表
     0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,
     0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,
     0x00,0x18,0x3c,0x7e,0xff,0x7e,0x3c,0x18,
     0x00,0x18,0x3c,0x7e,0xff,0x7e,0x3c,0x18,
     0xf8,0xf1,0xe3,0xc7,0x8f,0x1f,0x8f,0xc7,0xe3,0xf1,
     0xf8,0xf1,0xe3,0xc7,0x8f,0x1f,0x8f,0xc7,0xe3,0xf1,
     0xfe,0xfc,0xf8,0xf0,0xe0,0xc0,0x80,0x00,0x80,0xc0,0xe0,0xf0,0xf8,0xfc,
     0xfe,0xfc,0xf8,0xf0,0xe0,0xc0,0x80,0x00,0x80,0xc0,0xe0,0xf0,0xf8,0xfc,
     0xff,0xe7,0xdb,0xbd,0x7e,0xbd,0xdb,0xe7,
     0xff,0xe7,0xdb,0xbd,0x7e,0xbd,0xdb,0xe7,
     0xff,0x00,0xff,0x00,0xff,0x00,0xff,0x00,
	 
     0x0a,0x0a
    };

const unsigned char music_11[]=
{//七子之歌
    0x45,0x23,0x25,0x23,0x65,0x26,0x25,0x23,0x26,0x45,0x45,0x41,
    0x21,0x22,0x43,0x25,0x23,0x42,0x20,0x23,0x45,0x45,0x25,0x25,
    0x26,0x25,0x23,0x45,0x25,0x26,0x25,0x28,0x26,0x45,0x45,0x41,
    0x25,0x23,0x22,0x21,0x42,0x23,0x65,0x25,0x22,0x43,0x41,0x41,
    0x41,0x41,0x45,0x23,0x25,0x23,0x65,0x26,0x25,0x23,0x26,0x45,
    0x45,0x41,0x21,0x22,0x43,0x25,0x23,0x42,0x20,0x23,0x45,0x45,
    0x25,0x25,0x26,0x25,0x23,0x45,0x25,0x26,0x25,0x28,0x26,0x45,
    0x45,0x41,0x25,0x23,0x22,0x21,0x42,0x23,0x65,0x25,0x22,0x43,
    0x41,0x41,0x41,0x20,0x25,0x4a,0x29,0x28,0x26,0x25,0x45,0x26,
    0x46,0x25,0x36,0x18,0x2a,0x28,0x49,0x49,0x49,0x20,0x25,0x4a,
    0x39,0x18,0x26,0x25,0x45,0x26,0x46,0x25,0x46,0x2a,0x29,0x49,
    0x49,0x49,0x49,0x4a,0x39,0x18,0x26,0x25,0x45,0x48,0x26,0x25,
    0x23,0x22,0x41,0x40,0x22,0x23,0x45,0x45,0x45,0x45,0x40,0x25,
	0x26,0x48,0x48,0x48,
	0x00,0x00
};
const unsigned char music_22[]=
{//走进新时代
0x68,0x26,0x25,0x23,0x22,0x13,0x12,0x41,0x41,0x41,0x23,0x25,
0x28,0x48,0x28,0x29,0x2a,0x29,0x26,0x45,0x45,0x45,0x45,0x68,
0x29,0x29,0x28,0x26,0x25,0x46,0x46,0x46,0x25,0x26,
0x29,0x43,0x25,0x22,0x23,0x22,0x21,0x42,0x42,0x42,0x42,0x63,
0x25,0x26,0x25,0x26,0x25,0x23,0x22,0x13,0x12,0x41,0x23,0x25,
0x28,0x28,0x28,0x28,0x29,0x2a,0x2a,0x29,0x46,0x46,0x46,0x46,
0x46,0x29,0x2a,0x28,0x28,0x26,0x25,0x26,0x26,0x25,0x43,0x43,
0x22,0x42,0x26,0x25,0x23,0x22,0x13,0x12,0x41,0x41,0x41,0x41,
0x00,0x00
};
const unsigned char music_33[]=
{
//星语心愿
0x36,0x18,0x27,0x26,0x25,0x26,0x43,0x43,0x43,0x26,0x25,0x36,0x18,0x27,0x18,0x17,
0x26,0x27,0x48,0x48,0x48,0x28,0x29,0x2a,0x2a,0x2a,0x2a,0x49,0x49,0x28,0x27,0x26,
0x27,0x45,0x45,0x46,0x48,0x47,0x26,0x25,0x46,0x46,0x46,0x40,0x40,0x40,0x40,0x40,
0x36,0x18,0x27,0x26,0x25,0x26,0x26,0x26,0x46,0x46,0x26,0x25,0x36,0x18,0x27,0x18,
0x17,0x26,0x27,0x48,0x48,0x48,0x28,0x29,0x2a,0x2a,0x2a,0x2a,0x49,0x49,0x28,0x27,
0x26,0x27,0x45,0x45,0x46,0x26,0x28,0x47,0x45,0x40,0x26,0x28,0x47,0x45,0x46,0x46,
0x46,0x46,0x46,0x46,0x46,0x23,0x25,0x46,0x4a,0x39,0x18,0x27,0x28,0x48,0x48,0x48,
0x28,0x27,0x36,0x16,0x45,0x47,0x46,0x46,0x46,0x23,0x25,0x46,0x4a,0x39,0x18,0x27,
0x28,0x48,0x48,0x28,0x27,0x36,0x16,0x45,0x27,0x26,0x46,0x46,0x46,0x46,0x46,0x46,
0x00,0x00
};
const unsigned char music_44[]=
{
//上海滩
0x2a,0x2c,0x4d,0x4d,0x4d,0x2a,0x2c,0x49,0x49,0x49,0x2a,0x2c,
0x2d,0x4f,0x2d,0x4c,0x1d,0x18,0x2a,0x49,0x49,0x49,0x29,0x2a,
0x4c,0x4c,0x4c,0x29,0x2a,0x29,0x26,0x4d,0x4d,0x4d,0x26,0x28,
0x39,0x1a,0x29,0x27,0x26,0x28,0x45,0x45,0x45,0x2a,0x2c,0x4d,
0x4d,0x4d,0x2a,0x2c,0x49,0x49,0x49,0x2a,0x2c,0x2d,0x4f,0x2d,
0x4c,0x1d,0x18,0x2a,0x49,0x49,0x49,0x29,0x2a,0x4c,0x4c,0x4c,
0x29,0x2a,0x29,0x26,0x4d,0x4d,0x4d,0x26,0x28,0x39,0x1a,0x29,
0x27,0x26,0x25,0x48,0x48,0x20,0x2f,0x2f,0x2d,0x4f,0x4f,0x20,
0x2d,0x2f,0x2d,0x4c,0x4c,0x4c,0x2c,0x2a,0x6d,0x2c,0x48,0x19,
0x18,0x28,0x4a,0x4a,0x20,0x2a,0x2a,0x29,0x4a,0x4a,0x20,0x2f,
0x2f,0x2e,0x4d,0x4d,0x4d,0x2a,0x2a,0x69,0x2a,0x2f,0x2e,0x2d,
0x2a,0x4c,0x4c,0x4c,
0x00,0x00
};

//TIMER0 initialisation - prescale:1024
// desired value: 25mSec
// actual value: 24.960mSec (0.2%)
void timer0_init(void)
{
 TCCR0 = 0x00; //stop timer
 TCNT0 = 0x3D; //set count value
 TCCR0 = 0x05; //start timer
}

#pragma interrupt_handler timer0_ovf_isr:iv_TIMER0_OVF
void timer0_ovf_isr(void)
{
 TCNT0 = 0x3D; //reload counter value
 t0_times++;
 if(gzsl == 0x88)
   {
    if(t0_times == 10)
      {
	   timers++;
	   t0_times = 0;
	   pa = led_data[timers];
	   if(pa==0x0a){timers=0;}
	   PORTA = pa;	   
      }
   }
 if(gzsl == 0x55)
   {
	if(t0_times == 4)     //4*25ms=100ms  扫描一次
      {
	   t0_times = 0;
	   remote_in = PINB & 0x0F;
	   switch (remote_in)
      	 {//检测遥控器是否有键按下
          case 0x04:  // < A >
			  rst_time=0;
			  sound_add=0;
			  sound_1 = 0x11;
      	    break;
          case 0x02:  // < B >
		      rst_time=0;
			  sound_add=0;
			  sound_1 = 0x22;
      	    break;
          case 0x08:  // < C >
		      rst_time++;
			  sound_add=0;
			  sound_1 = 0x33;
      	    break;
          case 0x01:  // < D >
              rst_time=0;
			  sound_add=0;
			  sound_1 = 0x44;
      	    break;
      	 }
      if(sound_1 == 0x11)
	    {
		 temp1 = music_11[sound_add];
		}
      if(sound_1 == 0x22)
	    {
		 temp1 = music_22[sound_add];
		}
      if(sound_1 == 0x33)
	    {
		 temp1 = music_33[sound_add];
		}
      if(sound_1 == 0x44)
	    {
		 temp1 = music_44[sound_add];
		}
      if(sound_1 == 0x88)
	    {
		 temp1=EEPROMread((int)&sound[sound_add]);
		}
      }
   }
}

void stop_timer0()
{
	CLI();
	TCCR0 = 0x00;//停止timer0
	MCUCR = 0x00;
    GIMSK = 0x00;
    TIMSK = 0x00;
    SEI();
}

#pragma interrupt_handler timer1_ovf_isr:iv_TIMER1_OVF
void timer1_ovf_isr(void)
	 {
	    {
		 TCNT1=toneconst;
	     PORTC^=1;
		}
	 }	 	 
	 
void music_car()
{//音乐机器人程序
 
 port_init();//调用端口初始化函数
 
 work_status = 0x30;//置对应的工作状态标志

music_rst: 
 PORTA = 0x0F;

  gzsl=0x88;timers=0;t0_times=0;
  CLI(); //disable all interrupts
  timer0_init();

  MCUCR = 0x00;
  GIMSK = 0x00;
  TIMSK|=(1<<TOIE0);
  SEI(); //re-enable interrupts
 
 mic_startup();//等待声控启动函数

  gzsl=0x55;t0_times=0;sound_1 = 0x88;
	  TCCR1A = 0x00;
	  TCCR1B =(1<<CS11);//8分频
n01:
  temp=temp1 = 0;
  sound_add = 0;	  
	  while(temp1!=0)
  	    {
			temp=temp1; 
			temp&=0x0f;
						  //PA口为音乐灯
			  if(temp==1 || temp==2 || temp==3 || temp==4){PORTA = 0xfe;}
              if(temp==5 || temp==6){PORTA = 0xfc;}
              if(temp==7 || temp==8 ){PORTA = 0xf8;}
              if(temp==9){PORTA = 0xf0;}
              if(temp==10){PORTA = 0xe0;}
              if(temp==11){PORTA = 0xc0;}
			  if(temp==12){PORTA = 0x80;}
              if(temp==13||temp==14 || temp==15){PORTA = 0x00;}

			if(temp!=0)
			  {
			   TIMSK|=(1<<TOIE1);//根据SOUNDTABLE中数据的低四位选择音调频率
			   temp--;			   
        	   toneconst=TONETABLE[temp];
			   TCNT1=toneconst;
 			  }
			temp=temp1;
			temp>>=4;
			temp&=0x0f;
			delay_ms(temp*129);//根据SOUNDTABLE中数据的高四位*5后,为音调持续时间
			TIMSK&=~(1<<TOIE1);
			sound_add++;

	     if(rst_time > 40)
	       {//当按住< C > 键的持续时间超过10S,遥控机器人复位,等待声控重起运行
		    rst_time=0;
			sound_1 = 0x55;
			PORTA = 0x0f;
			delay_ms(1000);
		    goto music_rst;
	       }
	   }
	sound_1 = 0x88;   
	goto n01;
}

void main()
{
 music_car();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -