PWM precision on Atmega 328P

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

Hi,

Il have built a perpetual clock with an Atmega 328P , crystal is a 4,194304 MKz with a small adjustable cap on one side to slightly modify the frequency, all code is assembly.

Timer1 is used in fast PWM mode(15) with prescaler set to 64 and OCRIAH/L set to 65533 to compensate for gross crystal error.

The problem is :

If i adjust the cap then output period change only by step of half clock period instead of a continuous change as expected, and so for thermal shift, more the shift changes always  by step of  magnitude equal to a multiple of half clock period (119ns).

It is clear that the periodmeter (80MHz reference clock) as no knowledge of the clock period of the AVR so what is measured is actual.

Test is made with only basic initialisation of timers( i used also the Timer0 in CTC mode for debug with 8hz output which is also unstable but more randomly).

Atmega is supplied by a 4,8V battery and bypassed by 100nf plastic cap  and electrolityc cap.

Does somebody already got this problem ?

Thanks

JR

electronic is not voodoo.

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

That might happen if you have a prototype setup with long wires (such as a spring board). If the PWM output can couple, even lightly, into the crystal signal, what you describe might happen.

 

That said, I am a bit suspicious. More than a bit suspicious. While I can't think of a configuration setup that would produce what you describe, it seems pretty improbable. Tell us more. Maybe post the entire program that is doing what you describe?

 

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

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

ka7ehk wrote:
post the entire program that is doing what you describe?

+1

 

See Tip #1 for how to post source code.

 

That might happen if you have a prototype setup with long wires (such as a spring board)

So some good, clear, in-focus photos could also help.

 

Also schematics and, if used, PCB design.

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

The PWM is not analog, so won't it >>always<< change by a step?

 

Besides the complete test program, please give some numbers about test values, what you expect, and what you observe.

 

The obvious question/hypothesis:  What speed is your AVR really running at?  How have you proven that?  The symptoms suggest 8MHz.

 

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

jiherve wrote:

The problem is :

If i adjust the cap then output period change only by step of half clock period instead of a continuous change as expected, and so for thermal shift, more the shift changes always  by step of  magnitude equal to a multiple of half clock period (119ns).


4.194304MHz is special frequency for precision application
Adjustable a few nano seconds seems normal as fine adjust for calibration, it is not a problem
.
What do you expect? How you explain "normal" as your requirements.
.
Edit:4.194304MHz

Majid

Last Edited: Mon. Oct 21, 2019 - 05:03 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi,

thanks for answers!

@ka7ehk : i already though of that so i made a guard around crystal lines that are wides and shorts

@theusch: PWM frequency or period is linked to oscillator frequency so if this frequency changes in a continuous manner then the PWM will follow, µC is running at 4.194MHz

But at last the problem is solved: it was a bug in my periodmeter, to long to explain here as it uses a Propeller and there are some tricks with counters and phase accumulators.

regards.

JR

 

electronic is not voodoo.

Last Edited: Mon. Oct 21, 2019 - 07:41 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

jiherve wrote:
the problem is solved

See Tip #5.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

m.majid wrote:

4.194304MHz is special frequency for precision application

Huh?  That is 4MHz.

 

Besides telling us how solved, tell why you nee to try to tune the clock rather than just using a precision oscillator.

 

The rest of my queries remain.

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

hi,

Iin fact the problem was due to limited precision of IEEE 754 32bit floating point computations in my measuring instrument (home made) when making substractions as i computed : measured period - 1.0.

With  values close to 1.0  LSB is 1,19*10^-7 (2^-23)  so difference value smaller than that could not be calculated and granularity will be linked to that value.

As Murphy was watching this value correspond to half period of my crystal (freq=2^22) that is now obvious, i discovered it by changing to a 3.768 MHz crystal and steps were always some multiple of 119ns.

With a sligth code modification of the instrument then steps are multiple of 12,5ns as expected.

Why tunning the clock : it is because precise oscillators are costly and i have a lot of crystals in my garbage box; And it is just for fun.

thanks again

JR

 

electronic is not voodoo.

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

What is a perpetual clock?  What is not a perpetual clock--are some clocks supposed to come to a halt? 

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

Last Edited: Wed. Oct 23, 2019 - 03:03 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Term "perpetual clock" seems to refer to a clock with a built-in perpetual calendar. For the latter, note The Free Dictionary by Farlex:

 

Perpetual Calendar: A chart or mechanical device that indicates the day of the week corresponding to any given date over a period of many years.

Jim 

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

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

Hi,

Jim is right and this clock gives also Moon's phase and approximative tides level at Lacanau Ocean, well known surf spot on SW coast of France.

JR

electronic is not voodoo.

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

avrcandies wrote:
What is not a perpetual clock--are some clocks supposed to come to a halt?

Well my kitchen timer does. cheeky

 

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

The dictionary seem to suggest that we are talking about a cock that ALSO has a calendar function. Many clocks just give you the time of day.

 

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

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

we are talking about a cock that ALSO has a calendar function.

Some cocks I have seen indicate the wind direction .....

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

js wrote:
Some cocks I have seen indicate the wind direction .....

I wonder if any indicate the elevation of the Sun ?

 

 

Oh dear - I had a serious comment to make about floats having more discrete values  between 0 and 1 than the rest of the range; but it seems out-of-place now.

 

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

I don't have time to learn about perpetual clocks!!

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