using timer 2 on atmega328

Thread Starter

MacIntoshCZ

Joined Sep 4, 2019
22
Hello there,
i would like use timer 2 on avr, but i probably made somewhere a mistake or avr is faulty becouse i am getting square wave about 95%duty cycle.
I already succesfully used timer 0.
I should get 100Khz square wave, 50% duty cycle, on pin 11. Thanks for help
Here is code:


void setup() {
TCCR2A = 0;
TCCR2B = 0;
TCNT2 = 0;
TCCR2A |= (1 << WGM20) | (1 << WGM21);
TCCR2B |= (1 << WGM22);
TIMSK2 |= 1;
TCCR2A |= (1 << COM2A1);
TCCR2A |= (1 << COM2B1);
OCR2A |= 160;
OCR2B |= 80;
pinMode(3, OUTPUT);
pinMode(11, OUTPUT);
TCCR2B |= (1 << CS20);
interrupts();
}


void loop() {

}
 

Thread Starter

MacIntoshCZ

Joined Sep 4, 2019
22
That doesn't look like a complete program.
Where is your main( ) code?
There is no code needed in main function. Actually its from ArduinoIDE, so function main() is function loop(). But in my case there should be phase shifting. Becouse i want to delay other timers by 1/3 of period between each
 

MrChips

Joined Oct 2, 2009
29,203
There is no code needed in main function. Actually its from ArduinoIDE, so function main() is function loop(). But in my case there should be phase shifting. Becouse i want to delay other timers by 1/3 of period between each
Sorry. You are right. Goes to show that I don't do Arduino.
 

BobaMosfet

Joined Jul 1, 2009
2,081
I would recommend reading the datasheet and doing so until you understand it. Atmel datasheets are written really well, and usually provide a step-by-step walking through of how to use a feature (even to the point of providing C & assembly language examples in some cases).
 

Thread Starter

MacIntoshCZ

Joined Sep 4, 2019
22
I would recommend reading the datasheet and doing so until you understand it. Atmel datasheets are written really well, and usually provide a step-by-step walking through of how to use a feature (even to the point of providing C & assembly language examples in some cases).
I already solve the problem. There was no mistake in my code. Atmega was just faulty.
 
Top