Help with PWM frequency

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

Hi!
i´m working on a proyect that needs diferent frequencies with the pwm, im using a Atmega168p and 16Mhz cystal.

The output of the PWM is at pin14/PB2, i cannot change it
Here my PWM config.

	TIMSK1 |= (1<<ICIE1); //input capture interrupt enable
	TCCR1B = 0x00; //stop
	OCR1AH = 0; //cleaning values
	OCR1AL = 241; //
	TCCR1A = 0b00100011; 
	TCCR1B = 0b00011001; //start Timer  | No  prescaling
	TCNT1  = 0;


	if(OCR1AH==1)i=OCR1AL/2+128;	//50% duty cycle (aprox)
	if(OCR1AH==0)i=OCR1AL/2;
	OCR1BL=i;

Well, changing the value of OCR1AH and OCR1AL, i´m having these Frequencies.

OCR1A /OCR1AL /Freq khz

305	49	48,1
303	47	48,5
301	45	49
299	43	49,3
297	41	49,7
295	39	50,1
293	37	50,5
291	35	50,8
289	33	51
287	31	51,2
285	29	51,6
283	27	52,1
281	25	52,5
279	23	52,8
277	21	53,1
275	19	53,7
273	17	54,1
271	15	54,4
269	13	54,9
267	11	55,3
265	9	55,5
263	7	56
261	5	56,4
259	3	56,8
257	1	57,3
256	0	57,5
255	255	57,7
253	253	58,1
251	251	58,7
249	249	59,1
247	247	59,7
245	245	60,2
243	243	60,5
241	241	61
239	239	61,5
237	237	62,1
235	235	62,5
233	233	63,1
231	231	63,8
229	229	64,1
227	227	64,8
225	225	65,3
223	223	66,1
221	221	66,8
219	219	67,1
217	217	67,8
215	215	68,1
213	213	69,1
211	211	69,1
209	209	70,8
207	207	71,3
205	205	71,9
203	203	72,3
201	201	73,3
199	199	73,9
197	197	74,8
195	195	75,2
193	193	73,1
191	191	77,1
189	189	77,9
187	187	78,6
185	185	79,2
183	183	80,3
181	181	82,3
179	179	82,1
177	177	83,1
175	175	83,9
173	173	85,1
171	171	85,8
169	169	87,1
167	167	88,1
165	165	89,7
163	163	90,1
161	161	91,9
159	159	92,6

My question is, what i need to change to have Frequencies in the middle of 2 consecutive points? (i know that the pairs are missing, i just have the odd/uneven ones in the list)

For example the last 2

161 -> 91,9
160 -> 92,2
159 -> 92,6

But, What can i do if i want for example 92 or 92.4

i dont need all the frequencies, i need from 48k to 93k.

Is that possible?

Thanks in advantage!

Try it, again and again.

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

Quote:

i dont need all the frequencies, i need from 48k to 93k.

Is that possible?


With that setup, you are about as close as you are going to get. The results are approximately 0.1% from a desired arbitrary frequency. (that kind of makes sense, as the numbers for the OCR values are in the 100 to 200 range, so one bit is about 0.5% or so)

What kind of application needs this type of accuracy and resolution?

You can generate a bit better with a DDS approach:
http://www.myplace.nu/avr/minidds/

Quote:
can generate Sine-, Sawtooth-, Trangle- and Sqare-waves ranging from 0.07 Hz to about 200-300 kHz in 0.07 Hz steps (depending on your crystal).

Or, perhaps if you picked an AVR model with high-speed PWM and PLL you can do better in your range of interest.

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
  if(OCR1AH==1)i=OCR1AL/2+128;   //50% duty cycle (aprox) 
   if(OCR1AH==0)i=OCR1AL/2; 
   OCR1BL=i; 

This can be simplified to:

OCR1B = OCR1A / 2;

Regards,
Steve A.

The Board helps those that help themselves.

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

Depending on if your application allows it, you could modulate between different OCR1A values. For example, if you want 92.4, then in a given period, half the time OCR1A is 160 and half the time is 159. This modulation can be fixed or can vary like using delta-sigma modulator.

However, note that this method is crude in the sense it intentionally adds jitter to get the desired frequency.

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

DanielCardiel wrote:

For example the last 2

161 -> 91,9
160 -> 92,2
159 -> 92,6

But, What can i do if i want for example 92 or 92.4
i dont need all the frequencies, i need from 48k to 93k.
Is that possible?

What precision do you actually need ?
Is that Ok as an average only ?
How rapidly do you need to change frequency ?

As mentioned, you can dither the LSB to give almost any average, but you are actually generating two frequencies alternating. Google Binary Rate multiplier for examples on how to modulate the LSB.
Change to a uC with a PLL is another method, or if you can tolerate small shifts in SysClock, you can modulate that to get your higher precision.
A Clock device like Si5351A-B-GT can generate high precision frequencies, at moderate cost (100: $1.24)
That would give you ppm-level granularity in frequency

- or, you could change to a LC Osc and VCO that.
This would need a calibrate step.

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

theusch wrote:

With that setup, you are about as close as you are going to get. The results are approximately 0.1% from a desired arbitrary frequency. (that kind of makes sense, as the numbers for the OCR values are in the 100 to 200 range, so one bit is about 0.5% or so)

What kind of application needs this type of accuracy and resolution?

That´s not good, i was looking forward to not have this answer hahaaah. About the frequency, is for an inductor.

Thanks, i´ll keep that in mind.

Koshchi wrote:

  if(OCR1AH==1)i=OCR1AL/2+128;   //50% duty cycle (aprox) 
   if(OCR1AH==0)i=OCR1AL/2; 
   OCR1BL=i; 

This can be simplified to:

OCR1B = OCR1A / 2;

Hi!
Thanks fo helping, actually i tried at the beggining, but was giving me zeros in some points, i dunno why :S. So i changed it. Actually saving the value direct to OCR1A was giving me problems too, so i decided to save first H then L.

Who-me wrote:

What precision do you actually need ?
Is that Ok as an average only ?
How rapidly do you need to change frequency ?

As mentioned, you can dither the LSB to give almost any average, but you are actually generating two frequencies alternating. Google Binary Rate multiplier for examples on how to modulate the LSB.
Change to a uC with a PLL is another method, or if you can tolerate small shifts in SysClock, you can modulate that to get your higher precision.
A Clock device like Si5351A-B-GT can generate high precision frequencies, at moderate cost (100: $1.24)
That would give you ppm-level granularity in frequency

- or, you could change to a LC Osc and VCO that.
This would need a calibrate step.

Precision: the highest in these Frequencies.

Average: in some poitns work well, like (70%), the bad thing is that the other 30% is the inportant part, so i need more accuracy.

Changes: 200ms between every one.

Depend how big are the shifts.

Thanks for helping!, i´m reading about wha you told me guys, i´ll keep looking i guess.

Try it, again and again.

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

DanielCardiel wrote:
About the frequency, is for an inductor.

Do you mean a resonant circuit ?
An inductor is not frequency-paranoid.

To be worried about fractions of a percent, you must have quite high Q in that resonant circuit

DanielCardiel wrote:

Precision: the highest in these Frequencies.

Average: in some poitns work well, like (70%), the bad thing is that the other 30% is the inportant part, so i need more accuracy.

Still no numbers - saying 'highest' does not help much.

What exactly are you trying to do, with your frequency granularity ?

If it is a sweep measurement system, then dithering will likely add effects you would rather simply avoid.

The Si5351A has an 800MHz VCO, so it will give 50x better precision by simple division, and even more again using the full PLL setups.
You should be able to define to parts-per-million.

eg I asked for 0.092005242MHz and it reports
Output Frequency (MHz) = 0.092005242

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

Who-me wrote:
DanielCardiel wrote:
About the frequency, is for an inductor.

Do you mean a resonant circuit ?
An inductor is not frequency-paranoid.

To be worried about fractions of a percent, you must have quite high Q in that resonant circuit

DanielCardiel wrote:

Precision: the highest in these Frequencies.

Average: in some poitns work well, like (70%), the bad thing is that the other 30% is the inportant part, so i need more accuracy.

Still no numbers - saying 'highest' does not help much.

What exactly are you trying to do, with your frequency granularity ?

If it is a sweep measurement system, then dithering will likely add effects you would rather simply avoid.

The Si5351A has an 800MHz VCO, so it will give 50x better precision by simple division, and even more again using the full PLL setups.
You should be able to define to parts-per-million.

eg I asked for 0.092005242MHz and it reports
Output Frequency (MHz) = 0.092005242

i think that a diference of .1khz every step would be perfect, but i´m not sure if that´s possible. And Yes, is for a resonant tank. I´m tying to avoid to change the hardware, main reason is because the time. I don´t have much.

Try it, again and again.

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

DanielCardiel wrote:

i think that a diference of .1khz every step would be perfect, but i´m not sure if that´s possible. And Yes, is for a resonant tank. I´m tying to avoid to change the hardware, main reason is because the time. I don´t have much.

Do you need to adjust the duty cycle, or just the frequency ?
0.1kHz step size at 92KHz is an apparent clock of around 85MHz

If you can get hold of a ATXMEGAE5-XPLD, that gives you 128MHz PLL, & hires PWM which would make the SW you have workable.

or, I think the ATmega16/32U4 can do 64MHz, which is getting close.

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

Who-me wrote:

Do you need to adjust the duty cycle, or just the frequency ?
0.1kHz step size at 92KHz is an apparent clock of around 85MHz

If you can get hold of a ATXMEGAE5-XPLD, that gives you 128MHz PLL, & hires PWM which would make the SW you have workable.

or, I think the ATmega16/32U4 can do 64MHz, which is getting close.

The ideal is just he frequency, right now i´m trying to understand how the duty cycle afects the resonant tank, and maybe i can use it, but is kinda complicated cause i have 3 variables right now :(
OCR1A/freq, ADC channel and Duty cycle. (i use a formula to predict how the resonant tank will react, for calibration porpouses), now i have 3, ahahahah so my problems are x(duty cycle possibilities) more "weird", not hard, just complicated xD and abstract.

All the ideas your´re giving me are soo good :), but the problem is because the time i cannnot change the hardware, i need to work with this.

I´ll keep looking and see whats going on :D, i´ll post if i have found something :P
Thanks!

Try it, again and again.

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

DanielCardiel wrote:

All the ideas your´re giving me are soo good :), but the problem is because the time i cannnot change the hardware, i need to work with this.

If you are stuck with the HW, then you need to change the
Oscillator.

Look at the DS1077, and DS1085.

DS1077 is simpler and has a 10 bit divider, so could step in ~0.138% granularity at 92KHz (0.127KHz) directly driving your resonant circuit.

The DS1085 has a divider and also a DAC to the VCO, so it has choices of "three resolution options of 5kHz, 12.5kHz, and 25kHz."

These have a simple i2c interface, and the DS1085 would be able to replace the Crystal.
I think the simpler DS1077 would need to drive directly to keep the granularity, whilst a DS1085 with 5KHz steps at 16Mhz are ~ 29Hz steps at 92KHz, which is inside your target.

A frequency counter would confirm the actual frequency.

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

Who-me wrote:
DanielCardiel wrote:

All the ideas your´re giving me are soo good :), but the problem is because the time i cannnot change the hardware, i need to work with this.

If you are stuck with the HW, then you need to change the
Oscillator.

Look at the DS1077, and DS1085.

DS1077 is simpler and has a 10 bit divider, so could step in ~0.138% granularity at 92KHz (0.127KHz) directly driving your resonant circuit.

The DS1085 has a divider and also a DAC to the VCO, so it has choices of "three resolution options of 5kHz, 12.5kHz, and 25kHz."

These have a simple i2c interface, and the DS1085 would be able to replace the Crystal.
I think the simpler DS1077 would need to drive directly to keep the granularity, whilst a DS1085 with 5KHz steps at 16Mhz are ~ 29Hz steps at 92KHz, which is inside your target.

A frequency counter would confirm the actual frequency.

i ordered a couple of samples! ( :D
Thanks for your help, i´ll tell you when they arrive ok? :D

Try it, again and again.

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

I was reading in some Forums, that a 20Mhz crystal can be used with the Atmega168p, but at the datasheet says 16Mhz. Somenone knows something about this?, meanwhile i´m trying to look for one.

Try it, again and again.

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

Quote:

meanwhile i´m trying to look for one.

Trying to look for one >>what<<?

Quote:

but at the datasheet says 16Mhz.

You'd better give a document number and revision for that one. Then I'll come back with one that says 24MHz.

Quote:
– ATmega48/88/168: 0 - 12 MHz @ 2.7 - 5.5V, 0 - 24 MHz @ 4.5 - 5.5V
...
REV. 2545B–AVR–01/04

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

Quote:
ATmega48/88/168
He said mega168p not mega168. From the mega168p datasheet:
Quote:
Speed Grade:
– 0-4MHz @1.8-5.5V,0-10MHz@2.7-5.5.V,0-20MHz@4.5-5.5V

Regards,
Steve A.

The Board helps those that help themselves.

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

Quote:

From the mega168p datasheet:

Which document/rev is that? I have a number of '88-family datasheets downloaded over the years. My memory is hazy about when there was a P that wasn't a PA... Aaah, found one --
– ATmega48P/88P/168P: 0 - 10 MHz @ 2.7 - 5.5V, 0 - 20 MHz @ 4.5 - 5.5V
Rev. 8025D–AVR–03/08

[But I still want to hear which document says 16MHz...]
[[...and I quoted from the venerable document with the 24MHz in it :twisted: ]]

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

theusch wrote:
Quote:

From the mega168p datasheet:

Which document/rev is that? I have a number of '88-family datasheets downloaded over the years. My memory is hazy about when there was a P that wasn't a PA... Aaah, found one --
– ATmega48P/88P/168P: 0 - 10 MHz @ 2.7 - 5.5V, 0 - 20 MHz @ 4.5 - 5.5V
Rev. 8025D–AVR–03/08

[But I still want to hear which document says 16MHz...]
[[...and I quoted from the venerable document with the 24MHz in it :twisted: ]]

i´m trying to look for a 20Mhz crystal.

Shame! xD is old, very! xD i dont even want to show it. I downloaded the "Rev. 8025M–AVR–6/11"
And yes it says, 20Mhz.

– ATmega48P/88P/168P: 0 - 10MHz @ 2.7 - 5.5V, 0 - 20MHz @ 4.5 - 5.5V

I´m newbie :(

Try it, again and again.

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

Quote:

Shame! xD is old, very! xD i dont even want to show it.

I'm still curious--I never noticed a datasheet for this family over the years that said 16MHz. Is there in fact such a beast? I don't care if it is old or not. Why are you so reluctant to share more information about this datasheet?

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

Third time today I've not understood what the OP was really trying to do. Maybe I should just go back to bed.

Perhaps you can state what exactly your project is?

If you are feeding a tuned circuit with a variable input signal then presumably you are making a measurement somewhere else in the systm. Measuring a transfer function, circuit impedance, circuit resonance peak, etc.

How are you making these measurements? Automated with the micro's ADC, bench top lab instruments? How much error is in the entire process? This impacts how much a slight delta in the "exactly desired" frequency will effect the "result" for the output.

Next: Plotting your resonance curce, (or whatever), as a function of the frequency will give you a curve, and you can certainly interpolate for the exact frequency if you are off a tiny amount in the measurement you want. It also means that you have a full curve, and a "working" system, and are then only working on improving the system.

Lastly: Yes, changing the duty cycle will significantly change the circuit's output signal! If you are measuring the circuit's transfer function, etc., then it is MUCH easier to feed a tuned circuit with a sin wave. A square has MANY component frequencies you have to consider. The variable duty cycle pulse train has yet another frequency spectrum.

You can certainly calculate what the theoretical output of a given tuned circuit is given a known frequency and duty cycle pulse train input, but its messy, and MUCH more complicated than calculating the output for a single frequency, sin wave, input.

Hence the recommendation given earlier in this Thread about using a DDS (with a sin waveform), makes sense both from a tuning perspective, and from a signal processing perspective.

But one is really guessing at what you are trying to accomplish, as you haven't given much information in this regard.

JC

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

DocJC wrote:
Third time today I've not understood what the OP was really trying to do. Maybe I should just go back to bed.

Perhaps you can state what exactly your project is?

If you are feeding a tuned circuit with a variable input signal then presumably you are making a measurement somewhere else in the systm. Measuring a transfer function, circuit impedance, circuit resonance peak, etc.

How are you making these measurements? Automated with the micro's ADC, bench top lab instruments? How much error is in the entire process? This impacts how much a slight delta in the "exactly desired" frequency will effect the "result" for the output.

Next: Plotting your resonance curce, (or whatever), as a function of the frequency will give you a curve, and you can certainly interpolate for the exact frequency if you are off a tiny amount in the measurement you want. It also means that you have a full curve, and a "working" system, and are then only working on improving the system.

Lastly: Yes, changing the duty cycle will significantly change the circuit's output signal! If you are measuring the circuit's transfer function, etc., then it is MUCH easier to feed a tuned circuit with a sin wave. A square has MANY component frequencies you have to consider. The variable duty cycle pulse train has yet another frequency spectrum.

You can certainly calculate what the theoretical output of a given tuned circuit is given a known frequency and duty cycle pulse train input, but its messy, and MUCH more complicated than calculating the output for a single frequency, sin wave, input.

Hence the recommendation given earlier in this Thread about using a DDS (with a sin waveform), makes sense both from a tuning perspective, and from a signal processing perspective.

But one is really guessing at what you are trying to accomplish, as you haven't given much information in this regard.

JC

the PWM goes to a cicuit that uses this signal (PWM) and makes it stronger, more current and voltage, and converts it to a Sinusoidal wave. Enough to give energy to a coil (sender). This coil induces energy to another coil(reciever).

Diferent frequency induces diferent power.

The goal is make that, in the second coil you always will have 5 volts, no matter the current you are asking for.

for example a 70Mhz Pwm, will induce 5 volts and .6 amperes at the reciever.
and 70,1Mhz will induce 5 and ,61.

That´s the goal of the device.

Well, the resonant tank is afected by the voltaje at the reciever, the reciever has a couple of TL431 with capacitors, that afect the resonant tank, affecting the complete electromagnetic field, including the sender(where the PWM and the IC is connected). This gaves me diferent lecture a the ADC and different Phase (i´m measuring it too) when the voltaje is 4.9, 5 o 5.1 volts.

I´m measuring those parameter to make changes to the frequency and always send 5 volts, doesn´t matter the current.

The point is that i need more resolution, cause there is point where i can´t send an specific quantity of energy that makes 5 volts and x current.

About the Formula, from 0-.777 amperes it´s working and i took like 40 samples of the complete DATA (like 8000 numbers). And i programmed a formula, and it works, but the problem right now is the resolution. I need to send 0-.999 Amps. i´m having problems from .777 to .999.

I´m trying to look for a 20Mhz crystal, but the type of crystal i need is too small (package, like 1x2mm), i´m waiting for it. Meanwhile i´m trying to make work the DS1077u.

Thanks for helping!

Try it, again and again.