## Split from: frequency generator in C

37 posts / 0 new
Author
Message

how to make CTC have linear frequency between 0Hz - 20KHz? tq

Last Edited: Tue. Apr 21, 2020 - 08:22 AM

forgotten one wrote:
how to make CTC have linear frequency between 0Hz - 20KHz? tq
That's not possible. Say you have a 50us tick then the possible counting ranges/frequencies are:

```1   20,000 Hz
2   10,000 Hz
3    6,666 Hz
4    5,000 Hz
5    4,000 Hz
6    3,333 Hz
7    2,857 Hz
8    2,500 Hz
9    2,222 Hz
10   2,000 Hz
11   1,818 Hz
12   1,667 Hz
13   1,538 Hz
14   1,429 Hz
15   1,333 Hz
16   1,250 Hz
17   1,176 Hz
etc.```

As you can see this is an exponential/logarithmic not a linear relationship.

If you want LINEAR from 0..20kHz forget the timer and use DDS.

clawson wrote:

That's not possible. Say you have a 50us tick then the possible counting ranges/frequencies are:

With a much faster ticker you can get a lot closer to linear.  If the OP's AVR will do 20MHz, you could have a 50ns ticker, and much better resolution at the "low" (low counter clear, high-frequency output) end.

Here's a quick table of possibly useful values, given a 20MHz clock, to put into the timer TOP.  Note this requires a 16-bit timer...

The three columns are, in order:

Desired output frequency (kHz)

Value to put into Counter TOP

Actual output frequency (Hz)

```20	1000		20000
19	1053		18993
18	1111		18002
17	1176		17007
16	1250		16000
15	1333		15004
14	1429		13996
13	1538		13004
12	1667		11998
11	1818		11001
10	2000		10000
9	2222		9001
8	2500		8000
7	2857		7000
6	3333		6001
5	4000		5000
4	5000		4000
3	6667		3000
2	10000		2000
1	20000		1000
```

Worst-case is +/- 7Hz, and that's at the top end.  But at least it's pretty darn linear...  Calculating your own interim values (interpolating) is left up to you.

S.

Last Edited: Tue. Apr 21, 2020 - 11:30 AM

As I'm sure you are aware I was illustrating "worst case" where granularity is 1/(desired high frequency). In that case the non-linearity is its most prominent.

Welcome to the Forum.

What have you done so far?

What micro?

What language?

Home work question, or what do you intend to do with the signal?

JC

You can also change the clock divider, as needed, to maintain a high division number (lower clock divider==> higher counts) .  So perhaps you could even get away with an 8 bit timer, using the clock divider as your "gear shift".

Of course in any case, these don't give a smooth realt-time freq change, more of an abrupt jump.  DDS can be (is) smooth sweeping.

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

I guess you could stop the counter, shift the prescaler, change TOP, and carry on without too much of a jump.  At least, compared to the typical jumping around in an 8-bit resolution counter.  The big DDS chips like to have counters with a lot more bits in them.

Of course, if you really want smooth real-time frequency change, get a 555 timer and a pot...    I dunno if anyone makes analog synthesis chips like the ICL8038 anymore*.    S.

* https://www.digikey.com/products... (Digikey is totally unhelpful, but they do have a link to the Renesas datasheet (which clearly states 'Obsolete - do not use')).

Yeah, the DDS is  "smooth" since it uses a phase accumulator, so freq shifting is continuous.   Hard to beat 555 (analog) for some things...since the resolution is "infinite".  Of course, a 20 bit DAC is doggone fine.   Heck, remember when a good 16 bit DAC was a hundred \$\$.

Used to be lots of kits using the 8038 chip...make your own "signal generator".

This is interesting:

https://www.wikiwand.com/en/SWTPC     ...I think I still have several of their catalogs & MITS too...I have a full file cabinet of electronics catalogs I've toted around since the 70's...haven't really opened it since around 1985.

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

Welcome to AVR Freaks!

Critical question not asked: Linear with respect to WHAT?

Do you have some kind of control input? Or, is it so self-sweep? Or, ??? More information about the application and what it needs to do might result in more useful answers.

Jim

Until Black Lives Matter, we do not have "All Lives Matter"!

Last Edited: Tue. Apr 21, 2020 - 05:37 PM

I think we've frightened the OP off.  Now it's just us olde fartes splittin' hairs.

50uS is a pretty poor tick speed for an AVR, clawson!

(digital) phase accumulators are inherently quantized.  They just use very small quanta (and filters).

The ICL8038 offered a sweep input.

Linear with respect to the rate of responses from olde fartes splittin' hairs...

S.

Thanks for the welcome.

I use the frequency to control current output and i use ADC input to control the frequency. But the frequency output have different linearity with the ADC input. Is there a formula to make the freq have same linearity as the ADC input? Or it can't be help and i must use the other way.

I use atmega32 and 8MHz ext.clock. thanks

forgotten one wrote:
I use the frequency to control current output and i use ADC input to control the frequency

I'm not getting much from that. How do you use frequency to control current?  And ADC to control frequency? You might need to give more detail.

Normally one might use PWM then a filter and voltage to current converter to have a variable current output. The ADC value would then vary the PWM width. Both these processes should be linear in response.

Also your statement of 'different linearity' is confusing - it is linear or not linear. 'different linearity' would suggest a different slope in which simply multiplying by a scaling factor would solve that.

Last Edited: Wed. Apr 22, 2020 - 04:10 AM

I use the frequency to control current output and i use ADC input to control the frequency. But the frequency output have different linearity with the ADC input. Is there a formula to make the freq have same linearity as the ADC input?

It might help if you say more about what you want/need (in detail), than what you are presently doing.   Maybe what you are doing needs a major revamp or some new thoughts.

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

Oh sorry.

I need to convert analog input to frequency between 1KHz - 5KHz.

That's very different to your first post.

#1 Hardware Problem? https://www.avrfreaks.net/forum/...

#3 All grounds are not created equal

#4 Have you proved your chip is running at xxMHz?

#5 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand."

And where does the current output come into the picture?

In my first post was my problem part from my project. I can't adjust frequency linearly when use CTC. Sorry for not explain it first.

current is obtained from the frequency output. but that's not a problem. I have the circuit to convert it.

You need to write one complete paragraph of several sentences detailing what you want to do...why do you throw out one sentence here, a partial sentence over there, and another sentence somewhere else?

Give a complete and thoughtful description in ONE PLACE, then it is a much clearer picture for everyone.

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

Last Edited: Wed. Apr 22, 2020 - 06:46 AM

Linearity seems to be a big factor in your requirements. But, you have not yet stated what it is to be linear with respect to. An analog voltage applied to an ADC? Some number, obtained somewhere? Some other factor? Even your #17 just says "linear when using CTC"; again, please, what is the input that the frequency is to be linear against?

avrcandies makes an important request. And, I concur. Please, in a single paragraph, explain what this is to do.

Jim

Until Black Lives Matter, we do not have "All Lives Matter"!

Last Edited: Wed. Apr 22, 2020 - 07:30 AM

Things to think about, for starters...

1) What is your minimum frequency?

2) What is your maximum frequency?

3) How many steps do you need between 1) and 2)?

4) What is the interval between those steps? Linear/Log/Something else?

4) What tolerance can you accept on all those numbers?

4) What is the requirement in regard of pulse width? ie Will your circuit accept just edges or do you need a specific/range of pulse widths?

#1 Hardware Problem? https://www.avrfreaks.net/forum/...

#3 All grounds are not created equal

#4 Have you proved your chip is running at xxMHz?

#5 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand."

OP seems to struggle stating his problem in English. Reading between the lines, I think OP is doing something like:

OCR1A =  A · (1023 - ADC) + B

Where A & B are constants to achieve 1KHz to 5kHz <edit> I've just calculated A=6.25, B=1599 </edit>

OP is expecting output frequency to be linear with ADC (and therefore OCR1A) ; but of course it isn't.

1

Instead it's actually linear against -----

OCR1A

Last Edited: Wed. Apr 22, 2020 - 10:20 AM

forgotten one wrote:
Is there a formula to make the freq have same linearity as the ADC input?

I'm sure there is such a formula, but without more details on your input and your output we don't know it.  The mathematics may be somewhat complex.

I suggest a lookup table.  How many entries do you need?  "IF Input X, THEN Output Y".  See my table in post #3.

Or develop an algorithm.  S.

Is that sine waves or just digital output ?

You need to do some dividing, one of the rare programming occasions dividing is needed .....   Calculate the OCR as:

OCR= [8184000/(1023+4*ADC)]-1     this will give you the desired freq range  (1 KHz to  5 KHz, with ADC count of 0-1023)

using this formula, these results are obtained (for both rounded up and truncated OCR calcs).  Rounding is just slightly harder to calc (need the remainder):

With OCR calc roundup, the max error occurs at ADC=939 (-1.34 Hz err) and ADC=1021 (1.54 Hz err)

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

Last Edited: Thu. Apr 23, 2020 - 01:12 AM

That's linear.  S.

avrcandies wrote:

Yeah, the DDS is  "smooth" since it uses a phase accumulator, so freq shifting is continuous.   Hard to beat 555 (analog) for some things...since the resolution is "infinite".  Of course, a 20 bit DAC is doggone fine.   Heck, remember when a good 16 bit DAC was a hundred \$\$.

Used to be lots of kits using the 8038 chip...make your own "signal generator".

This is interesting:

https://www.wikiwand.com/en/SWTPC     ...I think I still have several of their catalogs & MITS too...I have a full file cabinet of electronics catalogs I've toted around since the 70's...haven't really opened it since around 1985.

I use an R-2R ladder and an op amp as a DAC. Works as fast as I can throw data at it.

Gentlemen may prefer Blondes, but Real Men prefer Redheads!

I use an R-2R ladder and an op amp as a DAC.

I used to too, but nowadays you can't be the prices and accuracy of a real dac...let the factory get it nice and step-wise accurate! Try getting 12 bits with a handful of resistors....don't even talk about a 16 bit or more dac.

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

Try getting 12 bits with a handful of resistors....don't even talk about a 16 bit or more dac.

24-bit Stereo Audio DAC for Raspberry Pi

http://jayakody2000lk.blogspot.com/2020/01/24-bit-stereo-audio-dac-for-raspberry-pi.html

Done with 15K and 7.5K 0603 resistors of an unspecified tolerance...

Done with 15K and 7.5K 0603 resistors of an unspecified tolerance...

Is someone selling you tickets on the next cruise out??

The required accuracy doubles with each additional bit: for 8 bits, the accuracy required will be better than 1/256 (0.4%). Within integrated circuits, high-accuracy R–2R networks may be printed directly onto a single substrate using thin-film technology, ensuring the resistors share similar electrical characteristics. Even so, they must often be laser-trimmed to achieve the required precision. Such on-chip resistor ladders for digital-to-analog converters achieving 16-bit accuracy have been demonstrated.[2] On a printed circuit board, using discrete components, resistors of 1% accuracy would suffice for a 5-bit circuit, however with bit counts beyond this the cost of ever increasing precision resistors becomes prohibitive. For a 10-bit converter, even using 0.1% precision resistors would not guarantee monotonicity of output.

So they are buying 0.1% /2  /2 /2  /2 /2  /2 /2  /2 /2 /2  /2  /2 /2  /2  /2  percent resistors?? That's pretty good tolerance or matching (0.00000596%, or 0.0000000596)

10   11 12 13 14 15 16 17 18 19 20 21 21 22 23 24

I suspect they are not getting real 24 bit performance.....and what meter would be used to tell?   But it looks nice on the scope!

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

Last Edited: Sun. Apr 26, 2020 - 08:12 PM

And they sound much better with solid gold cables, too.  S.

It's funny, in a way....when 16 bit audio came out, everyone was flabbergasted "how much better"  it sounded...and that is only 1 part in 65000

Nowadays, we are so used to talking of millions or billions, we lose appreciation for how big those numbers are.

I'm willing to collect a billion pennies from everyone to get a better understanding.  Heck, I'd even start with a million pennies.

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

Last Edited: Sun. Apr 26, 2020 - 08:19 PM

Sure.  I'll send you one penny once every billion seconds.  S.

avrcandies wrote:

I use an R-2R ladder and an op amp as a DAC.

I used to too, but nowadays you can't be the prices and accuracy of a real dac...let the factory get it nice and step-wise accurate! Try getting 12 bits with a handful of resistors....don't even talk about a 16 bit or more dac.

Discrete resistors make a terrible R-2R ladder. That's the way I tried it at first. Super glitchy (meaning not monotonic).

Now, I use the little yellow 10 pin Bourns SIP module R-2R ladders. A whopping 83 cents each (USD). No point in stringing resistors together.

I once connected two of them together (made a 16 bit DAC) and wrote 0x0000 ... 0xFFFF to them and looked at it on the silly-scope. There was no visible glitch when values crossed between the top 8 bits and the bottom. It's not "instrument quality", but way good enough for generating digitally controlled voltage or audio.

I also had some fun with that breadboard using the two DAC channels separately going to the X and Y axis on the scope. I used a "Hershey" vector font table and a line draw algorithm to draw vector characters on the scope (but a "line" had to be drawn pixel by pixel because the scope trace between line ends obviously would not display). Along with an RTC, I could display the time on the scope.

One idea led to another... I then drew a round "analog" clock with the two DAC channels. Great way to waste time and have fun!

Gentlemen may prefer Blondes, but Real Men prefer Redheads!

Last Edited: Sun. Apr 26, 2020 - 11:50 PM

sbennett wrote:

Try getting 12 bits with a handful of resistors....don't even talk about a 16 bit or more dac.

24-bit Stereo Audio DAC for Raspberry Pi

http://jayakody2000lk.blogspot.com/2020/01/24-bit-stereo-audio-dac-for-raspberry-pi.html

Done with 15K and 7.5K 0603 resistors of an unspecified tolerance...

OMG! Why?

Gentlemen may prefer Blondes, but Real Men prefer Redheads!

There was no visible glitch when values crossed between the top 8 bits and the bottom. It's not "instrument quality"

Yeah, most scopes are piss-poor for any exacting level measurements (plus they are typically 8-bits or slightly better).  An analog scope cleans it up, but in any case, you won't see microscopic mismatches.  So, a 7 or 8 bit triangle wave will probably look about as good as a 16 bit triangle wave on a lot of scopes (especially when set so the wave is 1 inch tall).

Along with an RTC, I could display the time on the scope.

Nice!

I've built a few of these over the years:

http://www.dutchtronix.com/ScopeClockH3-1-Enhanced.htm#PCB-3.1-DesignFiles

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

Last Edited: Mon. Apr 27, 2020 - 04:51 AM