Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

Frequency Measurements

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    www.mcselec.com Forum Index -> BASCOM-AVR Archive
View previous topic :: View next topic  
Author Message
MOR_AL

Bascom Member



Joined: 08 May 2014
Posts: 15
Location: Rio de Janeiro - Brasil

brazil.gif
PostPosted: Fri Dec 04, 2020 3:31 pm    Post subject: Frequency Measurements Reply with quote

"Sorry about my English"
I designed a frequency meter with PIC microcontroller in Assembly language. The accuracy of this frequency meter equaled that of the microcontroller crystal.
Now I am studying the Basic language of Bascom-AVR and I have a question.
Is there a way to obtain frequency measurements with the precision of the crystal used in the microcontroller with Bascom's Basic language?
MOR_AL

(BASCOM-AVR version : 2.0.8.2 , Latest : 2.0.8.3 )
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5917
Location: Holland

blank.gif
PostPosted: Fri Dec 04, 2020 9:08 pm    Post subject: Reply with quote

i think your English is ok. But it is the lack of info.
it is like asking if you can do pwm.
sure you can. but the important info is missing.
which micro do you plan to use (there are variants in timers). what is the range of the frequency you want to measure.
which xtal you use, and what is the tolerance.
then you can use the search function of the forum.
i have seen shared code too.
as you used the pic you should know that freq. measurement is done using the timers.
it has little to do with bascom language.
Quote:
. The accuracy of this frequency meter equaled that of the microcontroller crystal

sure it will never become more accurate. a temperature controlled xtal/oven will be best.

you can do the normal gate based counting or a reciprocal variant.

if your question was 'is there a built in command to do freq measurement' then it is no. but after 5 years you should be aware of the F1 button.

_________________
Mark
Back to top
View user's profile Visit poster's website
MOR_AL

Bascom Member



Joined: 08 May 2014
Posts: 15
Location: Rio de Janeiro - Brasil

brazil.gif
PostPosted: Fri Dec 04, 2020 11:42 pm    Post subject: Reply with quote

albertsm wrote:
... But it is the lack of info.
it is like asking if you can do pwm.
sure you can. but the important info is missing.
which micro do you plan to use (there are variants in timers).

ATmega328 or ATmega328P

what is the range of the frequency you want to measure.

20Hz <= freq. <= 10MHz

which xtal you use, and what is the tolerance.

Cheap crystal, usually found in commerce.
Something like 3 to 4 digits of precision @ 25ºC


then you can use the search function of the forum.
i have seen shared code too.

Sorry, I didn't understand what you meant.
Did you mean that there is a topic on this subject here in the forum?
I searched for "accurate frequency meter", but I couldn't find a more specific topic.


as you used the pic you should know that freq. measurement is done using the timers.
it has little to do with bascom language.
Quote:
. The accuracy of this frequency meter equaled that of the microcontroller crystal

sure it will never become more accurate. a temperature controlled xtal/oven will be best.

Sorry, but I didn't mention the opposite.

you can do the normal gate based counting or a reciprocal variant.

Can you add more details?

if your question was 'is there a built in command to do freq measurement' then it is no. but after 5 years you should be aware of the F1 button.


I'm actually more interested in being able to measure an exact number of pulses from a generator.
This requires a counting period of 1.000,000s and a timer whose "on and off" period can be known to maintain accuracy


I studied an application note (AN # 172 - Inductance Meter) but I noticed that turning TIMERS 1 and 0 on and off and handling TIMER1 interruption can add built-in delays, with push and pop instructions.

Thank you for your prestigious response.
I made a video about the design of a frequency meter. Unfortunately it is in Portuguese written language. But much of the procedure can be understood by the images.
In case you are interested, I can post the link of the YouTube video.
MOR_AL
Back to top
View user's profile
Duval JP

Bascom Member



Joined: 22 Jun 2004
Posts: 1161
Location: France

france.gif
PostPosted: Sat Dec 05, 2020 11:08 am    Post subject: Reply with quote

have a look at
https://www.mcselec.com/index2.php?option=com_forum&Itemid=59&page=viewtopic&t=14403&highlight=frequency+counter

EDC does very good sample
JP Wink

_________________
pleasure to learn, to teach, to create
Back to top
View user's profile Visit poster's website
JC

Bascom Member



Joined: 15 Dec 2007
Posts: 586
Location: Cleveland, OH

usa.gif
PostPosted: Sat Dec 05, 2020 7:39 pm    Post subject: Reply with quote

As stated, need more information.

How wide are the pulses you wish to measure, (capture)?

How close can they be, (what is the maximum frequency of the signal)?

How fast does the signal change frequency?

How fast do you need to update the measurement?

What is the output of the project, an LCD / GLCD for a human to read, or is it passing the information on to another micro/PC for real time data processing?

Many of the AVRs can measure a pulse width, or count pulses on a Timer/Counter, module directly within the hardware, without the micro "processing" the data.

The XmegaE5 and, IIRC, some of the newer generation XTiny micros also have a programmable logic unit within the micro.
With a wide range of input frequencies, a frequency counter likely will use a different measuring technique for low frequencies and for high frequencies.
The programmable logic unit can be used for a Divide By 2 (or perhaps more) without using an external chip.

Lastly, one of the great features of Bascom is that you can easily mix assembly language with the Basic language, if you found a reason to do so.
(It is rarely needed).
Likewise, although you can generally configure the various modules within the micro through Basic, you can also simply set up the registers yourself, directly, from within your Basic program.
So as mentioned above, the limitations of a project using Bascom and an AVR are really the limitations of the micro, not the language.

The "limitations" of an 8-bit micro running at 20 MHz are pretty similar regardless of the micro, (not trying to start a micro war here!).
Know, however, that the newer AVRs can run at 24 MHz, and the Xmegas can run at 32 MHz, so one has a lot more clock cycles with which to process the data between data result outputs!
Also, some of the micros have both the programmable logic unit, and the Event System, both of which can sometimes be very helpful in pushing the limits of the hardware.

Finally, for a frequency meter, which likely doesn't require the use of the analog modules, (ADC, DCA, AC, etc.), know that for a non-commercial product one might overclock the Xmega well beyound 32 MHz ! That becomes trivial with the internal clock PLL, you simply give it a new multiplier.

Good luck with your project!

JC


Edit: Typo
Back to top
View user's profile Visit poster's website
MOR_AL

Bascom Member



Joined: 08 May 2014
Posts: 15
Location: Rio de Janeiro - Brasil

brazil.gif
PostPosted: Mon Dec 07, 2020 12:39 pm    Post subject: Reply with quote

Duval JP wrote:
have a look at
https://www.mcselec.com/index2.php?option=com_forum&Itemid=59&page=viewtopic&t=14403&highlight=frequency+counter

EDC does very good sample
JP Wink


Thank you Duval JP!
I will study this solution.
MOR_AL
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5917
Location: Holland

blank.gif
PostPosted: Tue Dec 08, 2020 11:17 am    Post subject: Reply with quote

there are 2 methods to measure frequency. the simple one is to use a known time base and count for say 1 sec the number of pulses. the gate period you can vary depending on the input frequency.
the other method is where you count the internal periods of the clock. and now the input signal turns on/off the measurements.
since you need to calculate : f= 1.0 / period it is called reciprocal method.
but just as the simple method there are things to consider. with the simple gate method it is obvious that a measurement takes 1 sec. you can make the gate 100 ms to shorten that but that also changes the resolution.
for example when you make it 1 uS in order to be quick you can only grab input signals with a high frequency.
with the reciprocal method the internal clock is in most cases very high. say 10 MHz. when you count this high freq. signal in a 16 bit time it will overflow quick. of course depending on the input signal.
you could cascade timers.

_________________
Mark
Back to top
View user's profile Visit poster's website
MOR_AL

Bascom Member



Joined: 08 May 2014
Posts: 15
Location: Rio de Janeiro - Brasil

brazil.gif
PostPosted: Tue Dec 08, 2020 2:44 pm    Post subject: Reply with quote

Duval JP wrote:
have a look at
https://www.mcselec.com/index2.php?option=com_forum&Itemid=59&page=viewtopic&t=14403&highlight=frequency+counter

EDC does very good sample
JP Wink

I started to study the instructions. Right at the beginning I noticed some inconsistencies.
Freq_duty_meter_v1.bas

Line 19.
Config TIMER1 = Counter, Prescale = 64, Capture_edge = Falling, Noise_cancel = 1 '262ms max
From Bascom Help - CONFIG TIMER1
PRESCALE
The TIMER is connected to the system clock in this case. You can select the division of the system clock with this parameter.
Valid values are 1, 8, 64, 256 or 1024
PRESCALE can't be used in COUNTER mode.
From Atmel-7766-8-bit-AVR-ATmega16U4-32U4_Datasheet.pdf, pg 112
“The Timer / Counter can be clocked internally, via the prescaler, or by an external clock source on the Tn pin.”
Line 23.
Enable COMPARE2: On COMPARE2 Przerwanie_timer2 Nosave
From Bascom Help - Did not find Timer2. Atmega 32 do not have Timer 2. Only Timer0 and Timer3.]
From Atmel-7766-8-bit-AVR-ATmega16U4-32U4_Datasheet.pdf,
Did not find Timer2, only Timers 0, 1 and 3.

I browsed the author's website, but did not identify anything that could justify the inconsistencies found.
For this reason I gave up on continuing

MOR_AL



NOTE FROM ADMIN : you should note that M32 is something else than M32U4.
Back to top
View user's profile
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    www.mcselec.com Forum Index -> BASCOM-AVR Archive All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum