大家帮忙看看 到底哪错了

2019-07-16 04:15发布

#include <string.h>
#include <stdio.h>
#define         F_CPU        7372800                                                // 单片机主频为7.3728MHz,用于延时子程序和计算定时器初始值
#include <util/delay.h>
#include <avr/io.h>
#include <avr/iom128.h>
#include <avr/interrupt.h>                                     //中断信号头文件

//***********************************************************************
//                        定义变量区
//***********************************************************************


#define uchar           unsigned char
#define uint            unsigned int
#define ulong           unsigned long

#define Data_IO                PORTB                //音频输出口
#define Data_DDR        DDRB                 

uint t[9]={0,956,865,759,716,638,568,506,470};
uint d[9]={0,105,116,132,140,157,176,198,209};

uchar music[]={5,2,8,2,5,2,4,2,3,2,2,2,1,4,1,2,1,2,2,2,3,2,3,2,1,2,3,2,4,2,5,8};
uchar note_n;
uint int_n;
uint play_on;

//***********************************************************************
//                        程序初始化
//***********************************************************************

void system_init()
{
    Data_IO=0x20;                                                             //数据口为输出
    Data_DDR=0xFF;
}


//*************************************************************************
//                        定时器1初始化子程序
//*************************************************************************

void timer1_init()                                 //定时器初始化
{
  TCCR1A=0x40;                                     //定时器工作方式
  TCCR1B=0x11;                                    
  TIFR=0x10;                                       //清中断标志位
  TIMSK=0x10;                                      //使能定时器0溢出中断
}

//*************************************************************************
//                定时器1中断服务子程序
//*************************************************************************
ISR(SIG_OUTPUT_COMPARE1A)                                   //中断服务程序
{
  if(!play_on)           //是否播放完
  {
    note_n=0;
        int_n=1;
        play_on=1;
  }
  else
  {
    if(--int_n==0)
        {
           TCCR1B=0X10;
           if(note_n<32)
           {
             OCR1A=t[music[note_n]];
                 int_n=d[music[note_n]];
                 note_n++;
                 int_n=int_n*music[note_n];
                 note_n++;
                 TCCR1B=0x11;
                }
                else
                play_on=0;
        }          
}


//*************************************************************************
//                主程序
//*************************************************************************

int main(void)
{
  system_init();                             //系统初始化
  timer1_init();                             //定时器0初始化,完成定时器相关配置
  SREG|=0x80;                                //开启全局中断
  while(1)
  {
  }
}


捕获1.PNG
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。