Unable to make timer2 work on atmega644p

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

Hi,
I switched from atmega32 to atmega64. After changing the register names i hoped that it will work like it worked with atmega32. but some how I am not able to make timer2 work on 644p. (I didn't try other timers but i know timer1 worked fine as i used that to run motor PWM and it worked)

/*
 * servo644.c
 *
 * Created: 24-12-2013 02:17:09
 *  Author: Karmu
 */ 


#ifndef F_CPU
#define F_CPU 11059200UL // 16 MHz clock speed
#endif

#include 
#include 

#define SERVO_DDR DDRD
#define SERVO_PORT PORTD
#define SERVO_CTRL	PD7

uint8_t servo_max,servo_min,servo_mid;

void timer2_enable(void)
{
	/*
	WGM22,WGM20 - Fast PWM Mode TOP=0xFF (Waveform Generation Mode 3), Update of OCR2 at BOTTOM and TOV2 Flag set on MAX
	COM2A1 - When Fast PWM mode Clear OC2A (Output Compare pin, PD7 on ATMEGA644p) on compare match, set OC2A at BOTTOM, (non-inverting mode)
	CS22,CS21,CS20 - Clock select to 1024 pre-scaler
	OCR2A Value decides the Duty Cycle
	*/
	TCNT2 = 0x00; // 8-bit counter
	TCCR2A|=_BV(WGM21) | _BV(WGM20) | _BV(COM2A1) | _BV(CS22) | _BV(CS21) | _BV(CS20); //Prescaler 1024, 30mS
}

void timer2_disable(void)
{
	TCCR2A = 0x00; // Stop
}

void servo_init(uint8_t s_max, uint8_t s_min)
{
	servo_max=s_max;
	servo_min=s_min;
	timer2_disable();
	timer2_enable();
}

void servo_sweep(void)
{
	OCR2A=servo_min;
	_delay_ms(4000);
	OCR2A=servo_max;
	_delay_ms(4000);
}

void servo_position(uint8_t degree)
{
	degree=round(degree/10)*10;
	OCR2A=servo_min+(degree/(180/(servo_max-servo_min)));
}

int main(void)
{
	SERVO_DDR |= _BV(SERVO_CTRL);

	servo_init(8,26);

	while(1)
	{
		servo_sweep();
		//SERVO_PORT |= _BV(SERVO_CTRL);
		//_delay_ms(400);
		//SERVO_PORT &= ~(_BV(SERVO_CTRL));
		//_delay_ms(400);
	}

}

I check the PD7 on CRO there was no PWM there it was always 0.

Thanks for your time
K

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

The CS2x bits are in TCCR2B

Otherwise, the mega644 is much like the mega32.

David.

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

Didja look at the migration app note?
www.atmel.com/Images/doc8001.pdf

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

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

david.prentice wrote:
The CS2x bits are in TCCR2B

Otherwise, the mega644 is much like the mega32.

David.

Thanks, thats silly of me, I missed that even though i was looking for such things.

theusch wrote:
Didja look at the migration app note?
www.atmel.com/Images/doc8001.pdf

Thanks, this is indeed helpful, I didn't know of such app note for migration.

Regards,
Karmendra