0
\$\begingroup\$

I am writing a program to make an output with a 30% duty cycle and a frequency of 3 kHz using an ATmega32 microcontroller. My controller's frequency is 8 MHz and I am going to decrease it to 1 MHz by using a prescale of 8.

I need to utilize the CTC mode of the microcontroller but I can not use an interrupt in my program.

I don't understand why the program I have written doesn't work properly; on the oscilloscope I can not see the 3 kHz frequency with 30% duty cycle which I want to achieve.

I would appreciate it if you could look at my code and help me find the issue.

#include <avr/io.h>
#include "global.h"

int main(void)
{
    DDRD = (1<< 7);
    OCR1A = 110;
    TCCR1B = (1 << WGM12) | (1 << CS11);
    TCNT1 = 0;

    while(1)
    {
        while(!CHECKBIT(TIFR, OCF1A));
        {
            sbi(TIFR, OCF1A);
            toggle (PORTD, 7);
            OCR1A = 223;
        }
        while(!CHECKBIT(TIFR, OCF1A))
        {
            sbi(TIFR, OCF1A);
            toggle (PORTD, 7);
            OCR1A = 110;
        }
    }
}

enter image description here

\$\endgroup\$
0

1 Answer 1

1
\$\begingroup\$

A semi-colon is a ;.

So because your second while loop is missing a semi-colon, the code that should run and set new compare value only after the interrupt flag is set, the code actually runs and sets new compare value until the interrupt flag is set.

As the while loop is so short, it can toggle the pin every 1.5 microseconds, which is 12 clock cycles at 8 MHz.

\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.