STM32F0 Frequency counter!

So we’ve finally got our second semester project briefs and it has been declared that we need to build a half duplex one direction communication system! Obviously, as its a project given by the university, it can’t be as easy as sending USART down a cable and receiving it, requiring pretty much no effort… at all! So instead, it involves modulating various sine waves to complete the tasks of transmitting bits. As a side project, I decided to have a go at making a frequency counter that could be used for demodulating a FSK packet. FSK is a form of modulation where various frequency sine waves are used to encode data. By decoding the frequency of the sine waves, the data can then be recovered.

In this application, I’m using an interrupt to sample an analog input (connected to a transistor amplifier in hardware). The sampled data is then high pass filtered and a simple comparative zero crossing algorithm is used to find the zero crossing points. These points are recorded and once two negative to positive crossings have been found, the period of the sampled waveform is calculated and is displayed to my good ol’ 128×64 UC1701X LCD.P1010614
The waveform present at the analog input on my STM32F0 – 520.8Hz!

P1010615
Reading the frequency off my LCD! A slight bit of error in the frequency compared to my oscilloscope (~2Hz) but not bad for a pretty simple piece of code!

P1010616
The whole setup! The small bit of circuitry is a transistor amplifier with the output DC coupled to the STM32F0 input, this provides DC bias required for the ADC to work correctly.

The code isn’t particularly hard but I’ll include it on my github anyway! This could be further improved by adding hysteresis for the zero cross detection but that’s more effort than I’m willing to commit at the moment, either way, at least it worked!

Transistor amplifier

To ensure the STM32F0 ADCs are using their full headroom, a transistor amplifier is used to boost the signal, along with provide a midpoint bias of ~Vcc/2 (Vcc being 3v in this case). Obviously, I through this circuit together and in an ideal application, for the frequency range I’d be using, it would probably be better to just use an opamp based boosting circuit as opposed to the transistor method. The circuit for the amplifier is as shown:

Transistor amplifier
Simulating the transistor amplifier in LTSpice, I originally worked out the resistor values by hand, honest!

Nothing brings me back to first year like designing a Transistor amplifier… apart from the fact that everything goes squiffy once you quantize all resistor values to E12 values! As you can see, the Q point isn’t exactly 1.5V but considering the data is high pass filtered in software, its not a massive deal and it just means there is slightly less headroom on the inputs by ~((1.85-1.5)/2)V = 0.175V, not forgetting the fact that it does its job!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s