AVR GCC,使用Timer0生成音不起作用

时间:2014-08-09 09:24:30

标签: c avr avr-gcc

我试图将旧的ASM程序转换为C.

我确信我已经提取了所需的所有逻辑,但它根本不起作用。

我的目标是使用定时器比较匹配来切换输出引脚,为扬声器生成嗡嗡声。

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

// speaker pin
#define SPK _BV(PA0)

#define SYSPORT         PORTA
#define SYSPIN          PINA
#define SYSDDR          DDRA

ISR(TIMER1_COMPA_vect)
{
    SYSPIN |= SPK; // toggle speaker
}

void main()
{
    cli(); // disable all interrupts

    SYSDDR  = 0b00000001;  // SPK to output
    SYSPORT = 0b00000001;  // turn off SPK

    TCCR0A  = _BV(WGM01);  // CTC mode
    TCCR0B  = _BV(CS01) | _BV(CS00);  // Prescaler 64
    OCR0A   = 62;  // compare match = 62 (output freq 1kHz)
    TIMSK = _BV(OCIE0A);  // enable timer interrupt

    sei();  // enable all interrupts

    while(1) {}  // infinite loop.
}

此代码不会发出一声嘟嘟声,甚至没有点击,也没有任何内容。

然而,当我只使用一个循环和延迟时,它可以美化地工作 - 证明扬声器连接正确。

虽然我不能这样做,但是在制作声音时我必须做些别的事。

#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/delay.h>
#include <stdint.h>

// speaker pin
#define SPK _BV(PA0)

// PORTA
#define SYSPORT         PORTA
#define SYSPIN          PINA
#define SYSDDR          DDRA

void main()
{
    SYSDDR  = 0b00000001; // SPK to output
    SYSPORT = 0b00000001; // turn off SPK

    while(1) {
        _delay_us(100);
        SYSPIN |= SPK;
    }
}

保险丝设置为

LFUSE       = 0xE2
HFUSE       = 0xDF

频率4MHz。

1 个答案:

答案 0 :(得分:0)

哦哈哈,我是如此不专心

这应该是

ISR(TIMER0_COMPA_vect)

而不是

ISR(TIMER1_COMPA_vect)

正在处理一个不同的计时器:D