ATMEGA8 PWM OC2 Not working

Go To Last Post
5 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi guys!

I have trouble with OC2 output pin.
The code below, working on ATMEGA32 but not with ATMEGA8! and I can't see the problem!
Can anybody help me to found the mistake?
Thanks in advance!
Fabian.

#include 
#include 

#define CHECKBIT(ADDRESS,BIT) (ADDRESS & (1<<BIT))

volatile unsigned char DutyCicle = 0x00;

//----------------------------------------------------------
void PWMInit(unsigned char count)
{
    // Enable timer0 compare interrupt
	TIMSK = (1<<OCIE2);

	// Sets the compare value
	OCR2 = count;
	
	//FAST PWM mode
	//CLEAR OC2 on MATCH
	//Prescaller Fclk/8
	
	//For ATMEGA32
	//TCCR2 = (0<<FOC2)|(1<<WGM21)|(1<<WGM20)|(1<<COM21)|(0<<COM20)|(0<<CS20)|(1<<CS21)|(0<<CS22);
	
	//For ATMEGA8
	TCCR2 = (0<<FOC2)|(1<<WGM21)|(1<<WGM20)|(1<<COM21)|(0<<COM20)|(0<<CS20)|(1<<CS21)|(0<<CS22);

}

//----------------------------------------------------------
ISR(TIMER2_COMP_vect)
{
	//OCR2 = DutyCicle;
	return;
}

//----------------------------------------------------------
int main(void)
{
	//DDRD |= (1<<PD7);	//For ATMEGA32
	DDRD |= (1<<PB3);	//For ATMEGA8
	DDRD &= ~(1<<PD6);	//KEY +
	DDRD &= ~(1<<PD0);	//KEY -

	PWMInit(0x7F);	//50%
	
	while(1)
	{
		if (!CHECKBIT(PIND, PD6)) 
		{
			while (!CHECKBIT(PIND, PD6));
				DutyCicle++;
		}

		if (!CHECKBIT(PIND, PD0)) 
		{
			while (!CHECKBIT(PIND, PD0));
				DutyCicle--;
		}
		OCR2 = DutyCicle;
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Timers in AVRs can generate a PWM-signal without the need for an interrupt.

In your code I don't see where the used AVR is defined.

A GIF is worth a thousend words   They are called Rosa, Sylvia and Tessa, You can find them https://www.linuxmint.com/

Dragon broken ? http://aplomb.nl/TechStuff/Dragon/Dragon.html for how-to-fix tips

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi!
thanks for you ans.
Is defined in project configuration options.

Quote:
Timers in AVRs can generate a PWM-signal without the need for an interrupt.

Yes! i know! is another test in my confution!
Thanks.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Resolved!!!
here the mistake!

	//DDRD |= (1<<PD7);	//For ATMEGA32
	DDRB |= (1<<PB3);	//For ATMEGA8

Is PORTB not PORTD. :(

THANKS!!!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Good ! Congratulations :)

Your While-loop executes in a few microseconds, so incrementing or decrementing takes place very fast .... much faster than you can release the button. You can add a delay() in the loop for test-purposes. Have fun !

A GIF is worth a thousend words   They are called Rosa, Sylvia and Tessa, You can find them https://www.linuxmint.com/

Dragon broken ? http://aplomb.nl/TechStuff/Dragon/Dragon.html for how-to-fix tips