Frequency Counter

From Ethersex_Wiki
Jump to: navigation, search
Frequency Counter
Status
Stable
menuconfig Applications->Frequency Counter
Pinning yes
Ecmd yes
Depends on ECMD (optional)
Requires -
Code https://github.com/ethersex/ethersex/tree/master/services/freqcount

Measures the frequency and duty cycle of a signal

Pinning

  • the signal must always be fed to the ICP1-pin. Look up in the datasheet which physical pin this is on the used controller.
  • you need to define the pinning in pinning/hardware/<your board>.m4:
 pin(FREQCOUNT_PIN, PB0, INPUT)
  • if you want to measure the frequency of multiple signals, you can multiplex them with one or two 74HC251, up to 16 channels are supported
  • define the pinning for the control lines you need like this:
 pin(FREQCOUNT_CHANNEL_MULTIPLEX_BIT1, PB1, OUTPUT)
 pin(FREQCOUNT_CHANNEL_MULTIPLEX_BIT2, PB2, OUTPUT)
 pin(FREQCOUNT_CHANNEL_MULTIPLEX_BIT2, PB3, OUTPUT)
 pin(FREQCOUNT_CHANNEL_MULTIPLEX_CS_A, PB4, OUTPUT)
 pin(FREQCOUNT_CHANNEL_MULTIPLEX_CS_B, PB5, OUTPUT)

Frequency

  • CPU ticks from rising edge to rising edge are measured
  • minimum frequency is the smaller value of 1 Hz and CPU frequency divided by 16777216, in practice 2 Hz
  • if the frequency is below the minimum, the output is 0
  • maximum frequency is about 50 cpu ticks, about 400 KHz at 20 MHz CPU
  • if the frequency is above the maximum you get bogus results
  • when the irq load of the cpu increases (e.g. by network traffic or UART), the maximum frequency decreases

Duty Cycle

  • you can also measure the duty cycle of a PWM signal
  • duty cycle is (time from rising edge to falling edge) / time of whole cycle from rising to rising
  • frequency and duty cycle are measured one after another
  • when the frequency changes significantly the measured duty cycle will be wrong
  • duty cycle is stored as 8 bit value

Basic Averaging

  • you will sometimes get a wrong result due to interrupt latencies etc.
  • the module always averages the result of n measurements. You can define n in the menuconfig.
  • additionally the highest and lowest value of a averaging-block will be cut off, so n+2 samples are taken
  • use a power of 2 for n to reduce code size and improve execution speed

Moving Average

  • additionally to the basic averaging you can enable the moving average
  • the moving average uses the output from basic averaging as input
  • you can define the number of samples in the menuconfig
  • use a power of 2 for the number of samples to reduce code size and improve execution speed

ECMD Commands

Channel is the number of the multiplexing channel (0-15), 0 if you do not use channel multiplexing.
Command Function
fc freq Channel Returns the frequency in Hz (Attention: 32 Bit).
fc ticks Channel Returns the frequency in CPU ticks (Attention: 32 Bit).
fc duty Channel Returns the duty cycle (0-255 decimal).
fc %duty Channel Returns the duty cycle in percent.
fc on Channel Switches on frequency counting on the given channel.
fc off Channel Switches off frequency counting on the given channel.