Mega2560 Fastest PWM frequencyTimer3 is?

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

Having a little trouble with the PWM on a Mega2560 Timer 3 running at 8Mhz 3.3v Vcc

 

Ideally I would like a PWM of 10Khz or above with a 0 - 100% duty factor, but from what I am seeing in KAVRCalc and from the CV wizard this is not possible.  I can get 10Khz  which is OK, but with the numbers I have to set the timer in order to obtain 10khz there is no room for duty cycle control and it appears that the PWM Fast is only 122hz.

 

Am I missing something, or is my deduction of KAVRCalc correct?

 

Jim

 

If you want a career with a known path - become an undertaker. Dead people don't sue! - Kartman

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
//TIMER3 initialize - prescale:1
// WGM: 14) PW&M fast, TOP=ICRn
// desired value: 10000Hz
// actual value: 10000.000Hz (0.0%)
void timer3_init(void)
{
 TCCR3B = 0x00; //stop
 TCNT3H = 0xFC; //setup
 TCNT3L = 0xE1;
 OCR3AH = 0x03;
 OCR3AL = 0x1F;
 OCR3BH = 0x03;
 OCR3BL = 0x1F;
 OCR3CH = 0x03;
 OCR3CL = 0x1F;
 ICR3H  = 0x03;
 ICR3L  = 0x1F;
 TCCR3A = 0xC2;
 TCCR3C = 0x00;
 TCCR3B = 0x19; //start Timer
}

Jim,  here is what ICC app builder came up with, looks like you can pwm with 1/4% steps (untested)!

 

Jim

 

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

jgmdesign wrote:
but with the numbers I have to set the timer in order to obtain 10khz there is no room for duty cycle control
???

With a 10kHz PWM at 8MHz clock you have 800 steps for the duty cycle.

Stefan Ernst

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

sternst wrote:
With a 10kHz PWM at 8MHz clock you have 800 steps for the duty cycle.

Ok, this is where I am missing then.  Where does the 800 steps come from?

 

@ki0bk

Wouldnt I have to add an interrupt to reload the timer on overflow?

 

JIm

If you want a career with a known path - become an undertaker. Dead people don't sue! - Kartman

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB user

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

jgmdesign wrote:
Ok, this is where I am missing then. Where does the 800 steps come from?
E.g. you choose mode "Fast-PWM with ICR as TOP" and set TOP to 0x31f, so you have a 10kHz PWM. Now you can set OCRx between 0 and 0x31f (799) for the duty cycle.

Stefan Ernst

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

Thanks for the education.

 

Jim

If you want a career with a known path - become an undertaker. Dead people don't sue! - Kartman

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB user

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

I must admit to calculating these things manually.   However,  I do use the CV CodeWizard for Xmega.

// Port E initialization
// Function: Bit7=In Bit6=In Bit5=Out Bit4=Out Bit3=Out Bit2=In Bit1=In Bit0=In 
DDRE=(0<<DDE7) | (0<<DDE6) | (1<<DDE5) | (1<<DDE4) | (1<<DDE3) | (0<<DDE2) | (0<<DDE1) | (0<<DDE0);

// Timer/Counter 3 initialization
// Clock source: System Clock
// Clock value: 8000.000 kHz
// Mode: Fast PWM top=ICR3
// OC3A output: Non-Inverted PWM
// OC3B output: Non-Inverted PWM
// OC3C output: Non-Inverted
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer Period: 0.1 ms
// Output Pulse(s):
// OC3A Period: 0.1 ms Width: 0.050063 ms
// OC3B Period: 0.1 ms Width: 0.025031 ms
// OC3C Period: 0.1 ms Width: 0.012516 ms
// Timer3 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
// Compare C Match Interrupt: Off
TCCR3A=(1<<COM3A1) | (0<<COM3A0) | (1<<COM3B1) | (0<<COM3B0) | (1<<COM3C1) | (0<<COM3C0) | (1<<WGM31) | (0<<WGM30);
TCCR3B=(0<<ICNC3) | (0<<ICES3) | (1<<WGM33) | (1<<WGM32) | (0<<CS32) | (0<<CS31) | (1<<CS30);
TCNT3H=0x00;
TCNT3L=0x00;
ICR3H=0x03;
ICR3L=0x1F;
OCR3AH=0x01;
OCR3AL=0x90;
OCR3BH=0x00;
OCR3BL=0xC8;
OCR3CH=0x00;
OCR3CL=0x64;

// Timer/Counter 3 Interrupt(s) initialization
TIMSK3=(0<<ICIE3) | (0<<OCIE3C) | (0<<OCIE3B) | (0<<OCIE3A) | (0<<TOIE3);

David.

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

jgmdesign wrote:
Thanks for the education.

Indeed, you are sending us on a confusing hunt.

 

Mega2560 Fastest PWM frequencyTimer3 is?

Fastest trivial frequency and still be called a PWM?  clk/4.

 

I know kavrcalc will do it, but with so much stuff on that panel sometimes I have to fuss with it as well.

 

When you ask that question, I'm sure besides the nominal 10kHz target frequency, you also  had some resolution number in mind.  You were given a rough number by taking the target period and figuring out how many steps that might be at your given clkIO.

 

 

 

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

I sat down for a few minutes and loaded Davids example - I too also use CV's wizard, but I was not getting the same results David did, will have to look at that later.

 

Davids code indeed works correctly and I see where the 799 steps come in to play.  Thank you David!

 

Now comes the dumb question  I have usually ignored the PWM Normal, and PWM fast and the Phase correct windows at the bottom of the middle panel, but I am trying to figure out what those windows are representing.  Theres no manual for KAVRcalc so hence the question

 

JIm

If you want a career with a known path - become an undertaker. Dead people don't sue! - Kartman

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB user

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

jgmdesign wrote:
Now comes the dumb question  I have usually ignored the PWM Normal, and PWM fast and the Phase correct windows at the bottom of the middle panel, but I am trying to figure out what those windows are representing.  Theres no manual for KAVRcalc so hence the question

I have always wondered same thing, would be great if someone knows.

I just skipped adding those to my own tool, but that is only because I have no clue what those represents. Imo showing requested frequency(+ possible error) and steps(resolution?) would be more useful?

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

If you take the overflow time and invert it, you get the 122 hz value, so if you were updating the pwm value on each overflow, that is how fast it would update???

Anyway, it appears to be related to the overflow interrupt rate.

 

Jim

 

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

it appears that the PWM Fast is only 122hz.

Your 122hz PWM is what you get when you operate the timer as a full 16-bit timer, so that each PWM cycle is 65536 clocks, and you have 65536 possible width settings.  (Sure enough, 65k 8MHz clock cycles gives you 122Hz.)

To get faster frequencies, you decrease the possible PWM settings by changing the "top" value (and perhaps reconfiguring the timer as an 8-bit timer.)   To get 10kHz at 8MHz, you could set "top" to 800, and you'd have 800 possible settings for the pulse width.   If you only needed 100 settings, you could use an 8x prescaler and set "top" to 100...  It looks like setting an arbitrary "top" value requires that you use one of the OCR registers, so you'd no longer have as many PWM outputs.  There are some other "fixed" "top" values (0xFF, 0x1ff, 0x3ff) that would give you all the PWM outputs, but with less flexibility as to exact frequency.)

 

(I'm no timer expert, so there may be some errors in the details above.  But the basic concepts should be right...   I don't know anything about KAVRCALC or why it's not giving you appropriate values.)

 

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

The original question was about 10kHz PWM i.e. 100us period.  

ICR3 = 0x031F (799) @ 8MHz div1 800 steps

ICR3 = 0x0063 (99) @ 8MHz div8 100 steps

 

If you had F_CPU=16MHz,  it would be 1599 for div1, 199 for div8, 24 for div64 (25 steps).

 

Quite honestly,  there seems little point in worrying about the resolution.  100 steps should be good enough for most apps.

But a high resolution 800 steps costs you nothing from the AVR.   And nothing from the external electronics because switching losses depend on PWM period and not resolution.

 

David.

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

Thanks!

Jim

If you want a career with a known path - become an undertaker. Dead people don't sue! - Kartman

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB user

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

But a high resolution 800 steps costs you nothing from the AVR.

You need 16bit variables/calculations/data; there is value to keeping the steps less than 255...

 

I hadn't noticed at all that you could use ICR for TOP (the 8bit timers don't do that!); that's rather neat...

 

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

Yes, of course there is a bit more code in writing a 16-bit ICRn or OCRn than an 8-bit TOP.
It is fairly ignisficant. Once set, the hardware takes over,
.
David.