1
votes

I'm trying to convert an old ASM programme to C.

I'm quite sure I extracted all the logic needed, but it simply doesn't work.

My goal is to use a timer compare match to toggle output pin, generating a buzz for speaker.

#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.
}

This code does not make a single beep, not even clicking, nothing at all.

However, when I just use a loop and delay, it works beautifylly - proving the speaker is connected properly.

I cannot do this though, I have to do something else while it is making the sound.

#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;
    }
}

Fuses are set to

LFUSE       = 0xE2
HFUSE       = 0xDF

Frequency 4MHz.

1

1 Answers

0
votes

Oh lol, I'm so inattentive

This should have been

ISR(TIMER0_COMPA_vect)

instead of

ISR(TIMER1_COMPA_vect)

It was handling a different timer :D