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

ocrdu
9,34123 gold badges33 silver badges43 bronze badges
asked Jan 12, 2022 at 20:39
\$\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.

answered Jan 12, 2022 at 21:44
\$\endgroup\$
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.