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

📄 main.c

📁 单片机做音乐播放。欢迎下载并参考
💻 C
字号:
/*********************************************

Chip type           : AT90S8535 or AT90S8535
Clock frequency     : 8,000000 MHz
Memory model        : Small
Internal SRAM size  : 512
External SRAM size  : 0
Data Stack size     : 128
*********************************************/

#include <90s8535.h>

#include <delay.h>
#include <stdio.h>

// Declare your global variables here

#define Taste1 PINA.7
#define Taste2 PINA.6
#define Taste3 PINA.5
#define Taste4 PINA.4
#define Taste5 PINA.3

#include "Ton.c"
unsigned char T1Reload;
bit Play;

// Timer 0 overflow interrupt service routine
interrupt [TIM0_OVF] void timer0_ovf_isr(void)
{
// 装入初值
 TCNT0 = T1Reload;
 if(T1Reload != Silence && Play)    
  {
   if(Taste2) PORTB.4 = !PORTB.4; // Relais
   else       PORTB.3 = !PORTB.3; // Speaker
  }
 else
  {
   PORTB.3 = 0;
   PORTB.4 = 0;
  } 
}
// Timer 2 overflow interrupt service routine
interrupt [TIM2_OVF] void timer2_ovf_isr(void)
{
// Reinitialize Timer's 2 value
// TCNT2 = T2Reload;
// Place your code here
}

// Declare your global variables here

void main(void)
{
// Declare your local variables here
char txt[16];
int Speed = 6; 
unsigned char Song = 0;


// Input/Output Ports initialization
// Port A
PORTA=0x00;
DDRA=0x00;

// Port B
PORTB=0x00;
DDRB=0x1f;

// Port C
PORTC=0x00;
DDRC=0x00;

// Port D
PORTD=0x00;
DDRD=0b11000000;

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: 125,000 kHz
// Mode: Output Compare
// OC0 output: Disconnected
TCCR0=0x03;

TCNT0=0x30;

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Output Compare
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;

// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: 125,000 kHz
// Mode: Pulse Width Modulation
// OC2 output: Inverted PWM
TCCR2=0x00;
ASSR=0x00;
TCNT2=0x80;
OCR2=0x40;

// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
GIMSK=0x00;
MCUCR=0x00;

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x41;

// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;


// Global enable interrupts
#asm("sei")
Play = 1;
while (1)
      {
       unsigned char index;
       unsigned int lang; 
//用PINA的电平实现调整音高,速度,放音功能
//
         if(Taste1)  { Song++; index = 0; Play = 1; delay_ms(200);};
         if(Taste2)  TCCR0 = 0x02; // 高八度
                else TCCR0 = 0x03; // 标准音高
         if(Taste3)  Speed++; 
         if(Taste4)  if(Speed > 1) Speed--;  
         if(Taste5)  { Play = 1;};
 
       if(!Play) index = 0;
  //歌曲数组数据是个2维数组,第一列是发声频率,第二列是持续时间     
       switch(Song)
        {
         case 0:     //播放第一首
               // 是否播放完成?
               if(index >= WARS_LAENGE) { Play = 0;};
               // 设定定时器溢出频率
               T1Reload = WarsSong[index][0]; 
               // 设定每个音调的时长
               lang =     WarsSong[index][1] * Speed;
               break; 
         case 1://曲名:Axel F
               if(index >= AXEL_LAENGE) { Play = 0;};
               T1Reload = AxelSong[index][0];
               lang =     AxelSong[index][1] * Speed;
               break; 
         case 2://曲名:Haydn
               if(index >= HAYDN_LAENGE) { Play = 0;};
               T1Reload = HaydnSong[index][0];
               lang =     HaydnSong[index][1] * Speed;
               break; 
         case 3://曲名:Voyager
               if(index >= VOY_LAENGE) { Play = 0;};
               T1Reload = VoySong[index][0];
               lang =     VoySong[index][1] * Speed;
               break; 
         case 4://曲名:James Bond
               if(index >= BOND_LAENGE) { Play = 0;};
               T1Reload = BondSong[index][0];
               lang =     BondSong[index][1] * Speed;
               break; 
         case 5://曲名:Baby Girl
               if(index >= BABY_LAENGE) { Play = 0;};
               T1Reload = BabySong[index][0];
               lang =     BabySong[index][1] * Speed;
               break; 
         default: Song = 0;
               break; 
        }       
       
       // 等待一个音节演奏完成
       for(lang;lang;lang--) 
        {
         delay_ms(25);
        } 

       // 准备下一个音节的偏移
       index++;
       
       // 在两个音节间少许停顿
       T1Reload = Silence; 
       delay_ms(20);
      };
}

⌨️ 快捷键说明

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