[TUT] [C] Newbie's Guide to AVR PWM (Incomplete)

Go To Last Post
119 posts / 0 new

Pages

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

It's more to do with when you are playing back something like audio where the duty cycle varies in each PWM cycle and you don't want artefacts at the switching point. I'm not sure I can see a point of PC for motor control though when you are doing three phase brushless I guess it might come into play - but not for a simple DC motor.

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

IIRC there is a AVR App Note where phase correct is used to drive a DC motor.

Happy 75th anniversary to one of the best movies ever made! Rick Blane [Bogart]: "Of all the gin joints, in all the towns, in all the world, she walks into mine."

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

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

Thanks for the tutorial. I am using the atMega324P and without particular reason I like to use the 8-bit timer2 to explorer the in and outs of Phase correct PWM. Both OCRxn (OCR2A & OCR2B) are clear to me and I want to use them both to drive pins OC2A and OC2B. But I did not find a way to set the TOP value.
After reading the datasheet over and over it became clear the TOP value is predefined as 255 for the Phase correct PWM when you want drive both pins using OCR2A and OCR2B, to manipulate the duty cycle. In that case Timer2 has no additional register like ICR1 in timer1 to vary the TOP value in Phase correct PWM.

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

#include

// Declare your global variables here

void main(void)
{
// Declare your local variables here

// Input/Output Ports initialization
// Port A initialization
// Func0=In Func1=In Func2=In Func3=In Func4=In Func5=In Func6=In Func7=In
// State0=T State1=T State2=T State3=T State4=T State5=T State6=T State7=T
PORTA=0x00;
DDRA=0x00;

// Port B initialization
// Func0=In Func1=In Func2=In Func3=Out Func4=In Func5=In Func6=In Func7=In
// State0=T State1=T State2=T State3=0 State4=T State5=T State6=T State7=T
PORTB=0x00;
DDRB=0x08;

// Port C initialization
// Func0=In Func1=In Func2=In Func3=In Func4=In Func5=In Func6=In Func7=In
// State0=T State1=T State2=T State3=T State4=T State5=T State6=T State7=T
PORTC=0x00;
DDRC=0x00;

// Port D initialization
// Func0=In Func1=In Func2=In Func3=In Func4=In Func5=In Func6=In Func7=In
// State0=T State1=T State2=T State3=T State4=T State5=T State6=T State7=T
PORTD=0x00;
DDRD=0x00;
DDRD.5=1;
DDRD.7=1;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: 1000.000 kHz
// Mode: CTC top=OCR0
// OC0 output: Toggle on compare match
TCCR0=0x1a;
TCNT0=0x00;
OCR0=0x0;

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
TCCR1A=0x43;
TCCR1B=0x91;
TCNT1H=0x00;
TCNT1L=0x00;
OCR1AH=0x00;
OCR1AL=0xFF;
OCR1BH=0x00;
OCR1BL=0x00;

// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x19;
TCNT2=0x00;
OCR2=0x0F;

// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Off
GICR=0x00;
MCUCR=0x00;
MCUCSR=0x00;

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;

// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
// Analog Comparator Output: Off
ACSR=0x80;
SFIOR=0x00;

while (1)
{
// Place your code here

};
}

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

@narayanatammineni - And the point of your post?

Regards,
Steve A.

The Board helps those that help themselves.

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

I am running a mega328p for PWM a fuel injector for a car. I need a PWM frequency of about 15hz, but the FPU is 8Mhz and the max prescaler (1024) won't bring it down enough. Are there any alternatives to getting the PWM frequency down to 15hz without lowering the FCPU?

Christan

Chief Tinkerer

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

15 Hz PWM can easily be done by bit-banging it. Make a timer tick with a frequency that is the PWM frequency (15 Hz) times the resolution (in PWM steps). So for a resolution in 100 steps, yout make the timer tick with 1.5 KHz. In the ISR you simply maintain a counter variable that goes from from 0 to 100 and then wrap. Every time you wrap, you raise a pin. Every time you pass the desired duty cycle you lower the pin.

If your PWM frequency is actually 15 KHz then it's getting a wee bit tighter, and the resolution might have to be lowered. (But then again, you'd just go back to using a h/w timer...)

What resolution do you need?

Happy 75th anniversary to one of the best movies ever made! Rick Blane [Bogart]: "Of all the gin joints, in all the towns, in all the world, she walks into mine."

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

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

Is the 16 bit timer available? With that you can go down much farther than 15Hz.

Regards,
Steve A.

The Board helps those that help themselves.

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

I need a pulse width from 4.0 to about 60.0ms. 15hz is about 66ms max so that should work. It would be very beneficial to be able to vary the PW by 0.1 so thats a resolution of about 560 steps I guess. So this means I would make the timer tick at about 8.4khz? Seems like it should work...never tried pwm this slow before.

Koschi: the 16 bit timer is available. I am running an 8mhz cpu. The only thing is in PWM mode, top is not user settable

JohanEkdahl wrote:
15 Hz PWM can easily be done by bit-banging it. Make a timer tick with a frequency that is the PWM frequency (15 Hz) times the resolution (in PWM steps). So for a resolution in 100 steps, yout make the timer tick with 1.5 KHz. In the ISR you simply maintain a counter variable that goes from from 0 to 100 and then wrap. Every time you wrap, you raise a pin. Every time you pass the desired duty cycle you lower the pin.

If your PWM frequency is actually 15 KHz then it's getting a wee bit tighter, and the resolution might have to be lowered. (But then again, you'd just go back to using a h/w timer...)

What resolution do you need?

Chief Tinkerer

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

Quote:
The only thing is in PWM mode, top is not user settable
Yes it is. Modes 8, 10 and 14 use ICR1 as TOP, Modes 9, 11 and 15 use OCR1A as TOP. And mode 6 has a fixed 511 as top which at 8MHz and a pre-scalar of 1024 gives you 15.2587890625Hz.

Regards,
Steve A.

The Board helps those that help themselves.

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

please, i want to control my brushless motor in airship(blimp)application but i don't know the most suitable type of timer to use either (timer with 8bit )or (timer with 16 bit)?.and if i decide to use timer 1 what is the most suitable mode for using in this application (mode 1 or 2 or 3 or 11 or 12)in case of the phase correct pwm

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

Please don't ask general questions on the end of tutorials - take your question to the AVR Forum.

Moderator.

(BTW for BLDC motor control you are going to need three phased PWM signals - may be easiest to bit bang it in fact)

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

sir can u please provide me some information on phase & frequency control PWM of atmega 8535.my id is deepakgupta2186@gmail.com

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

Exactly what information do you need? Is not the description of that mode in the datasheet clear?

Regards,
Steve A.

The Board helps those that help themselves.

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

hi dean...i must say you are the GOD for all of us beginner freaks..you really made the life easier for us...thanks a lot...please write more tutorials once you are done with the exam...wish you good luck...trying PWM on my own..let's see how it goes....thanks again....

thanks and regards
aks :D :D :D

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
#define F_CPU 8192000UL 

#include  
#include  

void timer1_init(void) 
{ 
   //   Set PB3 (OC1A) as output pin; 
   DDRB   |=   (1<<PB3); 
   /*   Clear OC1A on Compare Match when upcounting. 
      Set OC1A on Compare Match when downcounting.Enable PWM   */ 
   TCCR1A   |=   (1<<COM1A1); 
   //   Select PWM, Phase and Frequency Correct, 10-bit mode, TOP value=ICR1, WGM(13-10)=1000, 
   TCCR1B   |=   (1

i'm trying to make a 400hz clock(50% dutycycle pwm)for switching H bridge?
i try the code in avr studio4,it compile without errors but while debugging it hangs.
attiny2313
is i need to change the code.
is it gives 400hz?

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

1) Do not ask questions about your code in the tutorial forum.

2) Do not cross post.

Why haven't you posted your code in your own PWM thread?
Then you would have got already the answer that it is total nonsense to have a compare value that is bigger than TOP.

Stefan Ernst

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

Quote:

1) Do not ask questions about your code in the tutorial forum.

Exactly. Threads in tutorial are only for providing feedback for the article in the first post - not for general questions/problems. As such I'll lock this thread. If anyone has things to add about the PWM tutorial then PM one of clawson, plons, js, abcminiuser, EW and ask us to unlock the thread.

Pages

Topic locked