NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » How can I play an LFO on a square wave.

May 13, 2011
by rboggs10
rboggs10's Avatar

I want to be able to use a sine wave Low Frequency Oscillator to alter the frequency of the square wave generated by my NerdKit. So what I want is to give my note some vibrato. The way that I am playing the note is based off of the "Making Music with a Microcontroller" video so if I wanted to play a D5 note on the PC4 pin I would code this:

while(1) {
  PORTC |= (1<<PC4);
  delay_us(851);
  PORTC &= ~(1<<PC4);
  delay_us(851);
}
May 13, 2011
by bretm
bretm's Avatar

Does it have to be a sine wave, or will triangle wave do?

#define MYPIN (1<<PC4)

int f;

while (1)
{
    for (f = 826; f < 875; f++)
    {
        PINC |= MYPIN; // toggles the pin
        delay_us(f);
        PINC |= MYPIN;
        delay_us(f);
    }

    for (f = 875; f > 826; f--)
    {
        PINC |= MYPIN;
        delay_us(f);
        PINC |= MYPIN;
        delay_us(f);
    }
}

Total duration of each trip through the while loop is about 166700us, which gives a 6Hz vibrato which should be about right.

May 14, 2011
by rboggs10
rboggs10's Avatar

Thanks for the reply. That is a good idea but I have changed my plan and I am going to try using PWM to play my notes.

Post a Reply

Please log in to post a reply.

Did you know that negative numbers are represented in two's complement notation in binary? Learn more...