hongkong 发表于 2013-8-4 10:52:24

马老师的音乐播放例程中,外部中断和比较中断执行顺序...

本帖最后由 hongkong 于 2013-8-4 11:10 编辑

马老师的音乐播放例程中,外部中断和比较中断执行顺序...
外部中断优先级高于定时器
画了流程图,外部中断响应后开启了定时器,但是计数器要加到溢出才会执行比较中断吧?
而这个时候按键应该是松开了对吗?然后执行--int_n,那不就成了-1?条件不成立就退出了,怎么会可以播放音乐呢?

#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>

prog_uint16_t t[]= {0,956,865,759,638,568,506,470};
prog_uchar d[] = {0,105,116,132,140,157,176,198,209};
#define Max_note 32
prog_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};
unsigned char note_n;
unsigned int int_n;
unsigned charplay_on;

int main(void)
{
        PORTD = 0x08;
        DDRD = 0x02;
        TCCR1A = 0x40;
        TCCR1B = 0x08;
        TIMSK = 0x10;
        GICR |= 0x80;
        MCUCR = 0x08;
        MCUCSR = 0x00;
        GIFR = 0x80;
        sei();

        while(1)
        {
       
        }
}


ISR(INT1_vect)
{
        if(!play_on)
        {
                TCCR1B = 0x09;
        }
}


ISR(TIMER1_COMPA_vect)
{
        if(!play_on)
        {
                note_n = 0;
                int_n = 1;
                play_on= 1;
        }
        else
        {
                if(--int_n == 0)
                {
                        TCCR1B = 0x08;
                        if(note_n < Max_note)
                        {
                                OCR1A = t];
                                int_n = d];
                                note_n++;
                                int_n = int_n * music;
                                note_n++;
                                TCCR1B = 0x09;
                        }
                        else
                                play_on = 0;
                }
        }
}

hongkong 发表于 2013-8-4 16:27:38

没人回答吗,我为了这买了个仿真器 希望弄明白

hongkong 发表于 2013-8-7 11:30:04

还是自己弄明白了,上面流程图不对,play_on是标志,不是按键检测引脚,按键接在了外部中断1上

llqy 发表于 2014-4-18 22:24:14

楼主说一下正确的流程图呗
页: [1]
查看完整版本: 马老师的音乐播放例程中,外部中断和比较中断执行顺序...