Pages: [1]   Go Down
Author Topic: Problems with Tone and PWM  (Read 487 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,

New here, so forgive me if this is in the wrong spot.

I am using a Teensy2.0 Atmega32U4, very similar to the other devices used on Arduino boards.  I've installed the necessary libraries for the Teensy and have had no real problems getting my device working.

I am working on a project to read in a value from a temperature controller using Modbus, this portion is working without a problem.  I am taking a value read from the temperature controller (0-10,000) and will do one of two things, I will output a frequency which will be fed into a frequency to voltage converter, or I will output a voltage and bypass my converter.

I am unsure of which method to choose as I have run into problems trying both.  The first using a frequency output utilizing the Tone function.  This appears to work great at first but upon further investigation I run into several problems. 

First problem with Tone is the duration value does not seem to work unless the Tone function is followed by a delay of equal value, for my application I need a continuous output, so I leave the duration value blank, but the function will not work without being followed by a delay of some value.

My second problem is that I can not determine the resolution of the frequency output available through the Tone.  The Tone function takes in frequency as a uint_16 so I was assuming 65K.  Below around 1Khz, I am easily able to resolve and output in 1Hz increments, however, above 1Khz I start to resolve only 2 or 3Hz, above 5K its closer to 10 to 20Hz.  For example, my program is trying to output a value of 7061Hz, however I only achieve 7042Hz, this is the output from around 7000 to 7050Hz.  Is there something I am missing in being able to resolve 1Hz from 0 to 10Khz?

My second option of using a PWM to output a voltage was even less successful as the standard analogwrite function will only take 0-255, I would again need at least a resolution to handle 0-10000.  The Atmega32u4 has PWM outputs that can be setup for 16-bit, its just the case of figuring out how to do it.

If anyone has any information on how to get by either of these problems I would greatly appreciate it, or at the very least if anyone could point me in the direction where I may be able to find out how to modify the functions if need be that would be great.

Thanks
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 509
Posts: 31483
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
My second option of using a PWM to output a voltage was even less successful as the standard analogwrite function will only take 0-255, I would again need at least a resolution to handle 0-10000.
I assume this is to smooth it and get a voltage out.
PWM is as you say only 8 bits in resolution. Your required range would require 14 bits resolution. With this much resolution required you are probably going to need a D/A converter.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
My second option of using a PWM to output a voltage was even less successful as the standard analogwrite function will only take 0-255, I would again need at least a resolution to handle 0-10000.
I assume this is to smooth it and get a voltage out.
PWM is as you say only 8 bits in resolution. Your required range would require 14 bits resolution. With this much resolution required you are probably going to need a D/A converter.

That is a possible solution, I am trying to make this as compact as possible.  I still have to explore the option of a 16-bit PWM from this particular chip.  I would still like to know what the problem(?) is with the Tone function though.
Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 176
Posts: 12283
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Is there something I am missing in being able to resolve 1Hz from 0 to 10Khz?

Generating a frequency boils down to dividing the processor clock by an integer value.  Some examples...

16000000 / 1600 = 10000 Hz  (Excellent!  A perfect match.)
16000000 / 1601 = 9993.753904 Hz  (Uh oh.)
16000000 / 1602 = 9987.515605 Hz
16000000 / 1603 = 9981.285090 Hz

There is no possible way to get 9999 Hz out of a ATmega32u4 running at 16000000 Hz.

Quote
My second option of using a PWM to output a voltage was even less successful as the standard analogwrite function will only take 0-255, I would again need at least a resolution to handle 0-10000.  The Atmega32u4 has PWM outputs that can be setup for 16-bit, its just the case of figuring out how to do it.

Make an attempt to get it working, post your Sketch, and I suspect someone will provide help.
Logged

Pages: [1]   Go Up
Jump to: