The best magazine
How to Measure Frequency Using a Microcontroller
- 1). Insert a microcontroller into an available area of a breadboard, and wire it according to the data sheet's minimal connectivity diagram. Check to ensure common design practices such as 0.1uF bypass capacitors across power supply pins and low-impedance pathways to ground.
- 2). Connect eight I/O lines of the microcontroller to eight LEDs through 220-ohm current-limiting resistors. You must keep at least one I/O available for the act of frequency measurement.
- 3). Connect one end of the programming adapter to the workstation computer via the appropriate interface, such as USB, serial or DB-25. Attach the other end of the adapter to the programming lines of the microcontroller. You may have to make these connections one wire at at time, since microcontrollers between different models and manufacturers rarely use the same pins. Connect the power, and set it to 3.3 volts.
- 4). Check to verify connectivity between the computer and the microcontroller circuit. The programming adapter's software can perform the test. If the software cannot verify the presence of the microcontroller, it will not program it.
- 5). Create a simple test C-program in the microchip's C compiler software, such as flashing the eight LEDs, to ensure the complete system is ready.
- 6). Set up the internal hardware. Enable a free I/O pin as input. This pin will measure the external frequency source. Set up a free running timer module within the microcontroller using an interrupt rate of 1,000 times per second to keep track of elapsed time in milliseconds.
- 7). Write the main code. In a while, loop in the C-program and poll the chip for the state of the frequency counter pin's state. Each time it toggles from off to on and then on to off, increment a global counting variable in the program. In the interrupt function, write an if-and-then statement that checks for a certain elapsed time, such as 1,000 cycles. The action of this conditional test will reset the timer cycles counter and output the value of the frequency counting variable to the LED output port.
- 8). Compile the program and load the resulting hex file into the microcontroller's memory. The program will run inside the microcontroller and begin to read the I/O pin for frequency. This whole process of reading and measuring the frequency will repeat once per second displaying, in binary on the LEDs.
Source: ...