PWM phase and freg ..AtMega128

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

Hi,
I'm trying to make a PWM phase and freg correct, I've read 2 tutorials, on was dean's. I finally made a code for my AtMega128, but I doesn't work(AvrStudio and Stk501).

Everthing is initialised as it should be,but it doesn't work as it should be,meaning ICR1 should be my TOP but instead it goes all the way to 0xffff .
Code:

void PWM_init()
{
	DDRB = 0b00100000;	//PB5, OC1A

	TCCR1A = 0b11000000;// 
	TCCR1B = 0b00010011;// N=64,ICR1=top

	unsigned int t=2500;
	ICR1H = (unsigned char)(t>>8);
	ICR1L = (unsigned char)t;
	unsigned int t2=1000;
	OCR1AH = (unsigned char)(t2>>8);
	OCR1AL = (unsigned char)t2;
}

Thanx for any response

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

First this is the wrong forum - hopefully a moderator will move this to AVR Forum. Anyway..

Quote:

ICR1 should be my TOP but instead it goes all the way to 0xffff

It's not clear to me whether you are talking about the AVR Stduio simulator or a real AVR. You mentioned an STK501 which suggests you are observing the real chip but to do that you presumably have a JTAG interface. If it IS the simulator you are talking about then, as noted in the manual, it doesn't get on too well with timers and especially the more advanced modes and especially the 16 bit timers. In fact I've given up ever trying to simulate timer behaviour in simulator V1 as it's exactly things like CTC not clearing but counting on to 0xFFFF that it gets wrong. Where possible I switch to using simulator V2 which is much more accurate. Lucky for you one of the first chips Atmel implemented in sim V2 was the mega128 - so if it is the sim you are using and it is V1 you are currently trying then switch to V2 and see if that's any better.

Cliff

PS By the way, which C compiler is this? Most will offer an ICR1 and OCR1A which are combined 16 bit registers so you can just write:

ICR1 = 2500;
OCR1A = 1000;

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

I'm not using a JTAG.
I debug in AVR studio, and then load the program in STK. If it was correct I would get a servo moving but I' don't.
Yea, the code you posted also works.
I think it has to do sth with the initialization code.
Thanx for the quick response

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

Sorry for asking again..but it makes no sense.
Has everyone used PWM phase and freg correct??
I'm trying to drive 2 digital servos, with PWM of 50Hz.
So i Figured out I could use PWM phase and freg correct.
Can I do this with this mode????

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

Quote:
So i Figured out I could use PWM phase and freg correct.
Can I do this with this mode????

You can, but it halves your resolution compared to Fast PWM.

   unsigned int t=2500; 
   ICR1H = (unsigned char)(t>>8); 
   ICR1L = (unsigned char)t; 
   unsigned int t2=1000; 
   OCR1AH = (unsigned char)(t2>>8); 
   OCR1AL = (unsigned char)t2;

First, there is no need for the temporary variables. Just use:

   ICR1 = 2500;
   OCR1A = 1000;

Second, the numbers are almost certainly not correct. Typically the pulse width is 1/10 to 1/20 of the period. You only have 1/2.5.

Regards,
Steve A.

The Board helps those that help themselves.

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

ok I agree with

Quote:
Second, the numbers are almost certainly not correct. Typically the pulse width is 1/10 to 1/20 of the period. You only have 1/2.5.
but that only explains why the servo didn't work..the think is that the assembler doesn't behave as if ICR =TOP..