Author Topic: Generating a 38KHz waveform with AVR 2313  (Read 4551 times)

0 Members and 1 Guest are viewing this topic.

Offline joe61Topic starter

  • Supreme Robot
  • *****
  • Posts: 417
  • Helpful? 16
Generating a 38KHz waveform with AVR 2313
« on: May 21, 2011, 12:26:00 PM »
I'm working on an obstacle detection module using a plain IR LED and receiver. The receiver part seems to work ok, at least it flashes a visible LED when I shoot it with my tv remote.

I have an ATtiny2313 generating a 38KHz carrier for the transmitting LED, I think. My multimeter says it's 37.7 KHz, but the value I had to put into OCR1A to get that is way off what I think it should be, so I'm looking for someone who'll check what I'm doing.

Here's the code. My multimeter says this generates a 37.7 KHz wave.


// --------------------------------------------------------------------------
// modulate a set of IR LEDs at 38KHz. PWM output drives a transistor
// controlling the LEDs. OC1A (PB3) controlls the transistor.
//
// ATTiny2313
// --------------------------------------------------------------------------

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

// CTC mode
int main()
{
    DDRB = (1 << PB3);

    TCCR1A = (1 << COM1A0);             // Toggle OC1A at TOP

    TCCR1B =
        (1 << WGM12) |                  // CTC mode 4
        (1 << CS10);                    // Prescaler = 1

    OCR1A = 105;

    for (;;)
    {
    }
}


The data sheet gives this formula to calculate the value for output frequency, which I re arranged to get the value for TOP (OCR1A).

waveform_freq = clock_freq / (2 x N x OCR1A)

38000 = 1000000 / (2 x 1 x OCR1A)

38000 x OCR1A = 1000000 / (2 x 1)

OCR1A = 1000000 / (2 x 1 x 38000)
OCR1A = 13

This value is so far off what my multimeter says is the right value that I think I'm either using the multimeter wrong, or I suck at math. I'd appreciate it if someone would show me the error of my ways.

Thanks

Joe
« Last Edit: May 21, 2011, 12:28:40 PM by joe61 »