![]() |
||||||||||||||||||
![]()
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() I use
and
recommend Firefox for the best internet browsing experience. Click below for your free copy today: CLICK
THE IMAGE:
Note:
I find it faster
and easier to use and it has great plugins. It even imports all your current browser settings! There's
no risk as your existing browser is not affected in any way - you can just choose which
one to use.
|
|
Useful For You. Readers
comments
|
||||||||||||||||
A PIC microcontroller frequency counter using TMR1.
More Resources...
A simple frequency counter measures frequency by counting the number of edges of an input signal over a defined period of time (T). A more complex method is reciprocal counting. Frequency is defined as (Number of events) / (time in seconds) and measured in Hz. To make calculations trivial using a 1 second gate time (T) gives a direct reading of frequency from the edge counter. Making a frequency counter for frequencies up to 65.536kHz is easy as the counters in a PIC chip can count up to 65535 without overflowing. Up to 65.535kHz all you do is wait for 1 second while the count accumulates, read the value and display it. It will be the frequency in Hertz. Above 65.536kHz you have to monitor the overflow value while at the same time making an accurate delay time (T). Note: Using a 1 second measurement period results in the frequency counter count value being a direct measurement of frequency requiring no further processing. It also means that the measurement is resolved to 1Hz. (Increasing T to 10s resolves to 0.1Hz while using T=0.1s gives a resolution of 10Hz). Get a frequency counter nowHere's a selection of frequency counters that you can buy on Ebay (if you don't want to make up your own then you can get a bargain here - and it will have all the front end circuitry made for you). Just click on the Counter you want and start bidding. Click here to see more frequency counters on ebay. Crystal oscillatorFor the following projects the crystal oscillator (of the microcontroller) is used as the timebase. In these projects measurement of T (set at one second) is made by executing a delay that takes a set number of machine cycles. Using a 4MHz oscillator gives a machine cycle of 1MHz (a period of 1us) which makes calculating and setting time delays fairly easy since most PIC instructions execute in one machine cycle. So executing 1,000,000 of these cycles gives a delay of 1 second. Frequency counter accuracyThe accuracy of the frequency counter depends on the accuracy of the crystal driving the microcontroller. ppm calculationThis is specified in ppm or parts per million. Its actually quite simple: taking an example of ±50ppm for a 4Mhz crystal. The error that the crystal could have (assuming that the crystal is loaded with the correct capacitance) will be in the range : Maximum possible error 4MHz +
(4MHz x 50 x 1e-6) = 4.0002e6 So the crystal could oscillate at any frequency between 4000200Hz and 3999800Hz. Note that this frequency can be changed by changing the loading capacitance on the crystal. The delay time is the important measurement so for the above crystal at fosc/4) for the cycle time of the PIC chip (nominally 1MHz) we have: Max cycle time : 1/(1.00005e6)
or 1/(1MHz + 50ppm) Multiply by 1e6 to give a 1 second period gives the delay time Min delay time :
1e6/(1.00005e6) 0.99995s or (1s - 1s x 50ppm) seconds. So you don't have to calculate all the intermediate steps just use the ppm value directly. Note: If you have a reference oscillator that is more accurate than the crystal used in the frequency counter project then you can calibrate the project crystal. You can do this by adjusting a variable capacitor on one side of the crystal oscillator circuit while reading the output frequency displayed. If you don't have a reference then just use a fixed capacitor to give the correct parallel load capacitance for the crystal you use.
Common crystalsCommonly available crystals have a ppm specification of ±30ppm to ±50ppm (part per million error) but you can buy crystals with a ppm of ±20ppm. The smaller the ppm value (the smaller the error) the more accurately you can measure frequency.
More Resources...
Watch crystalNote: The temperature coefficient is about ±50ppm unless you use a watch crystal which has a temperature coefficient of 0.034 ±0.006ppm/ºC but if the temperature does not change much then this will not matter as much e.g. where the temperature is fairly stable.
Timer 1 algorithmTMR1 (timer one) is ideal for frequency measurement (counting edges) as it functions as a 16 bit counter taking its input directly from a port pin. This the easiest way of measuring frequency using a PIC micro - you can use Timer 0 but more software is required to make it work as a 16 bit counter. The maximum count for Timer one is 65535 with one more count setting the overflow flag (the timer then reads zero again). Counting the number of overflows gives the count in multiples of 65536 and reading the value of TMR1 at the end will give the total edge count over the period of measurement - T. The trick to making the frequency counter algorithm work is that the overflow flag must be polled within the delay routine but it must be polled ensuring that the polling routine takes a constant time (So that the delay period can be calculated exactly). In fact the delay time period mentioned earlier does take 1,000,000 cycles (xtal 4MHz) but it must also include the constant time polling routine. This makes the code slightly more complex. For the frequency counter interrupts are not used at all in either measurement of the input signal edges or measurement of the time period T. This is because using an interrupt as part of the measurement process would interrupt the time measurement part of the code. The number of interrupts would be dependent on the input signal frequency and so the time measurement would be inaccurate. If the time period (T) measurement was made using a different method e.g. Using a 1s externally generated time period T then interrupts could safely be used. LCD frequency counter displayThe 1st project uses an LCD to display the measurement result. This is a fairly easy way to display the result as high level languages usually include drivers for the LCD. Using the LCD does not require any special techniques and it can be updated quickly. You can find the lcd frequency counter project here. Seven segment frequency counter displayThe 2nd project uses several Seven segment displays providing an 8 digit output which is far more complex to drive but it is so much more satisfying than the LCD display. You can find the seven segment led frequency counter project here. To display numbers on multiple 7 segment displays you rely on persistence of vision (a way of describing how your eye perceives light). With a single 7 segment display you can wire all its connections directly to the microcontroller and you need a total of 8 connections, with 2 segments you need 16 connections and with 8 segments you need 64 connections. Obviously most microcontrollers don't have 64 pins so you need a different method. The answer is to use multiplexing which is a way of recycling pins. All the seven segment displays are connected to the same 8 bit port. Then the displays are multiplexed i.e. each display is turned on for a short period of time and then off. You usually switch the seven segment display on by turning on a transistor connected to the common cathode (ground) of the display. When the transistor is on all the LEDs are enabled - the corresponding LED lights up when a bit in the 8 bit port is high. The following example shows four multiplexed seven segment displays using only 12 pins (without multiplexing you would need 32 pins).
To make the illusion that the LEDs are on all the time the LEDs must be refreshed faster than your eye notices. You need to refresh the display at about 50Hz relying on persistence of vision i.e. the image you see does not need to be constant as long as it is repeated quickly enough. The trick with driving the multiplexed display for the frequency counter application is that the multiplex routine must work in a constant time. The delay period T can then incorporate refreshing the seven segment display. The constant time is needed in the same way that it is needed for polling the overflow flag of timer 1 (see earlier).
Jump to Best Microcontroller Projects Home Page. |
||||||||||||||||||
|
||||||||||||||||||
|
||||||||||||||||||