# FREQUENCY COUNTER USING PIC MICROCONTROLLER

ABSTRACT The multiplexed seven segment display PIC frequency counter uses the PIC microcontrollers for operation. This PIC frequency counter circuit uses a multiplexed seven segment display and uses timer 1 to count edges of the input signal. It uses the simpler method of direct frequency measurement which is easy to do but means that the number of digits displayed depends on the input frequency. This frequency counter circuit uses TMR1 in 16 bit counter mode to count the input signal edges. Counter overflows are accumulated to give the total count in multiples of 65536. Adding the current value of the counter at the end gives the total count. Since the measurement time is 1 second the final count is actually the frequency of the input signal. Using the 1 second measurement time also gives a frequency resolution of 1 Hz. The microcontroller used is PIC16F877A.

Frequency counter
1| Page

FREQUENCY COUNTER USING PIC MICROCONTROLLER

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). Crystal oscillator
2| Page

FREQUENCY COUNTER USING PIC MICROCONTROLLER

For 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 accuracy The accuracy of the frequency counter depends on the accuracy of the crystal driving the microcontroller. ppm calculation This 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 Maximum possible error 4MHz - (4MHz x 50 x 1e-6) = 39998e6 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:
3| Page

FREQUENCY COUNTER USING PIC MICROCONTROLLER

Max cycle time : 1/(1.00005e6) or 1/(1MHz + 50ppm) Min cycle time : 1/(0.99995e6) 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. Max delay time : 1e6/(9.9995e6) 1.00005s 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 crystals Commonly 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.
4| Page

FREQUENCY COUNTER USING PIC MICROCONTROLLER

PIC MICROCONTROLLERS A PIC microcontroller is a processor with built in memory and RAM and you can use it to control your projects (or build projects around it). So it saves you building a circuit that has separate external RAM, ROM and peripheral chips.
5| Page

FREQUENCY COUNTER USING PIC MICROCONTROLLER

What this really means for you is that you have a very powerful device that has many useful built in modules e.g.
• • •

EEPROM. Timers. Analogue comparators.

UART.

Even with just these four modules (note these are just example modules - there are more) you can make up many projects e.g.:

* Frequency counter - using the internal timers and reporting through UART (RS232) * * Capacitance Event or meter timer output analogue using to comparator internal LCD. oscillator. timers.

* Event data logger -capturing analogue data using an internal ADC and using the internal EEPROM for storing data (using an external I2C for high data storage capacity. * Servo controller (Control through UART) - using the internal PWM module or using a software created PWM.

The PIC Micro is one of the most popular microcontrollers and in case you were
6| Page

FREQUENCY COUNTER USING PIC MICROCONTROLLER

wondering the difference between a microprocessor and a microcontroller is that a microcontroller has an internal bus within built memory and peripherals. In fact the 8 pin (DIL) version of the 12F675 has an amazing number of internal peripherals. These are:
• • • • •

Two timers. One 10bit ADC with 4 selectable inputs. An internal oscillator (or you can use an external crystal). An analogue comparator. 1024 words of program memory 64 Bytes of RAM. 128 Bytes of EEPROM memory. External interrupt (as well as interrupts from internal peripherals). External crystal can go up to 20MHz. ICSP : PIC standard programming interface.

• • • • •

And all of these work from within an 8 pin DIL package! In the mid-range devices the memory space ranges from 1k to 8k (18F parts have more) - this does not sound like a lot but the processor has an efficient instruction set and you can make useful projects even with 1k e.g. LM35 temperature sensing project that reports data to the serial port easily fits within 1k. Features In fact a PIC microcontroller is an amazingly powerful fully featured processor with internal RAM, EEROM FLASH memory and peripherals. One of the
7| Page

FREQUENCY COUNTER USING PIC MICROCONTROLLER

smallest ones occupies the space of a 555 timer but has a 10bit ADC, 1k of memory, 2 timers, high current I/O ports a comparator a watch dog timer... I could go on as there is more! Programming One of the most useful features of a PIC Microcontroller is that you can reprogram them as they use flash memory (if you choose a part with an F in the part number e.g. 12F675 not 12C509). You can also use the ICSP serial interface built into each PIC Microcontroller for programming and even do programming while it's still plugged into the circuit!

You can either program a PIC microcontroller using assembler or a high level language and I recommend using a high level language such as C as it is much easier to use (after an initial learning curve). Once you have learned the high level language you are not forced to use the same processor e.g. you could go to an AVR or Dallas microcontroller and still use the same high level language.

Input / Output - I/O A PIC Microcontroller can control outputs and react to inputs e.g. you could drive a relay or read input buttons.

With the larger devices it's possible to drive LCDs or seven segment displays with very few control lines as all the work is done inside the PIC Micro. Comparing a frequency counter to discrete web designs you'll find two or three
8| Page

FREQUENCY COUNTER USING PIC MICROCONTROLLER

chips for the microcontroller design and ten or more for a discrete design. So using them saves prototype design effort as you can use built in peripherals to take care of lots of the circuit operation. Many now have a built in ADC so you can read analogue signal levels so you don't need to add an external devices e.g. you can read an LM35 temperature sensor directly with no interface logic.

Peripherals The PIC microcontroller has many built in peripherals and this can make using them quite daunting at first which is why I have made this introductory page with a site. The best way to start is to learn about the main features of a chip and then begin to use each peripheral in a project. I think learning by doing is the best way. PIC microcontroller PIC microcontroller Feature Flash memory RAM EEPROM I/O ports Timers/Counters USART
9| Page

summary

of

each

major

peripheral

block.

At the end is a short summary of the main devices used in projects shown on this

feature description Re-programmable program storage. Memory storage for variables. Long term stable memory : Electrically Erasable Programmable Read Only Memory. High current Input/Output ports (with pin direction change). Typically 3. Built in RS232 protocol (only needs level translator chip).

FREQUENCY COUNTER USING PIC MICROCONTROLLER

CCP SSP Comparator ADC PSP LCD Special features ICSP

Capture/Compare/PWM module. I2C and SPI Interfaces. An analogue comparator and internal voltage reference. Analogue to digital converter. Parallel Slave Port (for 8 bit microprocessor systems). LCD interface. ICSP,WDT,BOR,POR,PWRT,OST,SLEEP Simple programming using In Circuit Serial Programming. Note:these are some of the main features (some chips have all of these and some don't).

Flash memory This is the program storage area and gives you the most important benefit for using a PIC microcontroller - You program the device many times. Since when does anyone get a program right first time?

Devices used in projects on this site can be re-programmed up to 100,000 times (probably more) as they use Flash memory - these have the letter F in the part name. You can get cheaper (OTP) devices but these are One-TimeProgrammable; once programmed you can't program it again! ICSP In Circuit Serial Programming (ICSP) is the next most important benefit. Instead of transferring your chip from the programmer to the development board you just leave it in the board. By arranging the programming connections to your circuit correctly
10 | P a g e

you

won't

need

to

remove

the

chip!

FREQUENCY COUNTER USING PIC MICROCONTROLLER

You can re-program the device while it's still in the circuit so once your programmer is setup you can leave it on the bench and test your programs without moving the chip around and it makes the whole process much easier. I/O Ports Input / Output ports let you communicate with the outside world so you can control leds, LCDs or just about anything with the right interface. You can also set them as inputs to gather information.

Pin direction Most PIC microcontroller pins can be set as an input or and output and this can be done on the fly e.g. for a dallas 1 wire system a pin can be written to generate data and read at a later stage. The TRIS register controls the I/O direction and setting a bit in this register to zero sets the pin as output while setting it as one sets the pin as input. This allows you to use a pin for multiple operations e.g. the Real Time clock project uses RA0, the first pin of PORTA, to output data to a seven segment display and at a later point in the program read the analogue value as an input. Current

11 | P a g e

FREQUENCY COUNTER USING PIC MICROCONTROLLER

The PIC I/O ports are high current ports capable of directly driving LEDs (up to 25ma output current) - the total current allowed usually ~200mA this is often for the whole chip (or specified for several ports combined together). Timer / Counters Each PIC microcontroller has up to three timers that you can either use as a timer or a counter (Timer 1 & 2) or a baud clock (Timer 2). Timer 0 The original timer: Timer 0 was the first timer developed and you can find it in all the earliest devices e.g. 16F84 up to the most current e,g, 16F877A. It is an 8 bit timer with an 8 bit prescaler that can be driven from an internal (Fosc/4) or external clock. It generates an interrupt on overflow when the count goes from 255 to zero. Timer 0 always synchronizes the input clock (when using external clock). Note: You can read and write timer 0 but you can not read the prescaler. Note: The prescaler changes its effect depending on whether it is a timer prescaler or a watch dog prescaler - so the same prescaler setting may prescale by 2 or by 1 depending on its use! Timer 1

12 | P a g e

FREQUENCY COUNTER USING PIC MICROCONTROLLER

This is a 16 bit timer that generates an overflow interrupt when it goes from 65535 to zero. It has an 8 bit programmable prescaler and you can drive it from the internal clock (Fosc/4) or an external pin. To eliminate false triggering it also has an optional input synchronizer for external pin input. This timer can be used in sleep mode and will generate a wakeup interrupt on overflow. Timer 1 is also read by the CCP module to capture an event time. Note: Using this timer in sleep mode will use more current. In addition it can be used to drive a low power watch crystal. This is something that sounds good but I don't recommend you do it as watch crystals are extremely difficult to drive correctly. You should only use it if you are going to make a pcb and follow all the guidelines in making it noise free. I used a DS1307 in the Real Time clock project which drives the crystal directly but even this is difficult to get operating accurately. Timer 2 This is an 8 bit timer with an 8 bit prescaler and an 8 bit postscaler. It takes its input only from the internal oscillator (Fosc/4). This timer is used for the timebase of a PWM when PWM is active and it can be software selected by the SSP module as a baud clock.
13 | P a g e

FREQUENCY COUNTER USING PIC MICROCONTROLLER

It also has a period register that allows easy control of the period. When timer 2 reaches the PR2 register value then it resets. This saves having to check the timer value in software and then reset the timer and since it is done in hardware the operation is much faster - so you can generate fast clocks with periods that are multiples of the main clock. USART The USART is a useful module and saves having to code up a software version so it saves valuable program memory. You can find more information on RS232 here and how to make it work. Look here for pin outs. All you need to interface it to a PC serial port is a MAX232 chip (or equivalent). Note: An equivalent MAX232 chip is the SP202ECP that has the same pinout as the MAX232 but lets you use 100nF capacitors - so you don't need the large 1uF caps. Baud Rates You have to be careful using the baud rates as they depend on the main clock in use and normal oscillator values in general do not fit very well with 'real' baud rates. There is a table of baud rates in microchip data sheet DS33023A which indicates the expected percentage error for a specific clock rate and in general the higher the main clock the lower the error.

14 | P a g e

FREQUENCY COUNTER USING PIC MICROCONTROLLER

You sometimes have to play around with the register settings to get a better fit with your clock rate and the baud rate you want. An example is for an 8MHz clock - if you use BRGH=1 and an 8MHz clock (see the 16F88 datasheet) you get accurate baud rates up to 38.4kbaud. You have to force this to work e.g. in mikroC the built in USART routines use BRGH=0 so at 8MHz the baud rate is only accurate to 9.6kbaud. If you want a super-accurate baud rate the best way is to use a clock crystal that ends up giving you that baud rate i.e. work back through the baud rate equations to find the crystal you need. CCP The Capture/Compare/PWM module has three modes of operation:
• • •

Capture - Capture the time of an event. Compare - Generate an output when Timer 1 reaches a value. PWM - Pulse Width Modulation.

Capture Capture mode is used to capture the value of Timer 1 when a signal at the CCP pin goes high (or low depending on how the CCP is set up). The CCP can accurately capture the arrival time of a signal at the CCP pin so it can be used for pulse time measurement. Compare

15 | P a g e

FREQUENCY COUNTER USING PIC MICROCONTROLLER

Compare mode is used to generate an output when Timer 1 reaches a value you put into CCPR1. One special event trigger mode lets you start the ADC when the compare mode triggers. PWM PWM gives you one Pulse Width Modulation output with 10 bit resolution and with no software overhead - once started it operates all by itself unless you want to change the duty cycle. It uses Timer 2 to define its operation using Timer 2 period register to define the frequency of the PWM. Note: The duty cycle is not a percentage it is the number of periods of the PWM clock that the output is high! SSP The Synchronous Serial Port lets you communicate with devices that use either the SPI (Serial Peripheral Interface) or I2C (Inter IC communication) protocols. Note that for full Master mode I2C operation you need to choose a PIC device that has the MSSP device (Master Synchronous Serial Port). SPI and I2C are shared so you can only use one at a time (or you could use the I2C bit banged routines in the Real Time Clock project to have both at the same time). You can find a project that uses I2C here and you can find more information on I2C here. Comparator and comparator voltage reference
16 | P a g e

FREQUENCY COUNTER USING PIC MICROCONTROLLER

The comparator is module that has two analogue comparators which can be set up in one of 8 different ways. Either digital or analogue inputs can be compared to reference voltages. In one mode an internally generated voltage reference is used as an input to both comparators and in the same mode multiplexing lets you monitor up to four different input pins. You can even send the output of the comparator to a pin so that it is used independently from the microcontroller e.g. in a circuit where you need a comparator you don't need an extra chip! The analogue level must be between Vdd and Vss as protection diodes won't allow anything else. The module will generate an interrupt if the comparator output changes. You can use it in sleep mode and the interrupt will wake it up. The source impedance of the analogue signal must be smaller than 10k. ADC The single 10 bit Analogue to Digital Converter can have up to 8 inputs for a device multiplexed from input pins. The ADC can be used during sleep but you have to use the RC clock mode. One
17 | P a g e

FREQUENCY COUNTER USING PIC MICROCONTROLLER

benefit of this is that there will be no digital switching noise so you will get better conversion accuracy. For the 16F877A you can not just choose to use an analogue input if you feel the need as there are only a specific and limited number of ways that the analogue input pins can be enabled. It is best to start with AN0 and add more as necessary see the datasheet for which analogue inputs can be enabled e.g. if you started a design using only AN5 you would find that you may have to enable a few more analogue inputs as well! The 16F675 can measure 4 analogue input pins! PSP The Parallel Slave Port lets you to connect the PIC microcontroller directly into a microprocessor system. It provides an 8 bit read/write data bus and RD (read) WR (write) and CS (chip select) inputs - all active low. This will let you add a PIC microcontroller to a system so that the PIC microcontroller can be treated as a memory mapped peripheral. It will let the microcontroller behave just as though it was another microprocessor building block e.g. some memory or ram but in this case you have full control over exactly what the building block is i.e. you can re-program the PIC microcontroller to do just about anything. This provides an easy route to adding a PIC microcontroller to an 8 bit system that already exists. LCD
18 | P a g e

FREQUENCY COUNTER USING PIC MICROCONTROLLER

The LCD interface lets you directly interface to an LCD saving you having to use an LCD module such as the HD44780. I have not used this feature as it is another commercial requirement where removing a chip (HD44780) saves money in a production run. I think it is capable of driving a graphic LCD.

Special Features In Circuit Serial Programming Watch dog timer click here (jumps to ICSP section). This is a software error protector. This detects if the power BOR Brown Out reset supply dips slightly and resets the device if so. POR Power on reset This starts microcontroller initialization. A time delay to let Vdd rise. Wait for 1024 cycles after PWRT. Enter low power mode.

ICSP WDT

PWRT PoWeR up Time OST SLEEP Oscillator start up timer PIC microcontroller sleep mode

WDT
19 | P a g e

FREQUENCY COUNTER USING PIC MICROCONTROLLER

If your software goes haywire then this timer resets the processor. To stop the reset the well behaved software must periodically issue the CLRWDT instruction to stop a resert. The WDT runs using its own oscillator. It runs during sleep and shares Timer 0 prescaler.

POR Power On Reset starts PIC microcontroller initialization when it detects a rising edge on MCLR. PWRT If you enable this then 72ms after a POR the PIC microcontroller is started. OST Oscillator Startup Timer delays for 1024 oscillator cycles after PWRT (if PWRT is enabled) ensuring that the oscillator has started and is stable. It is automatic and only used for crystal oscillator modes and is active after POR or wake from sleep. SLEEP Sleep mode (or low power consumption mode) is entered by executing the 'SLEEP' command. The device can wake from sleep caused by an external reset, Watch Dog Timer timeout, INT pin RB port change or peripheral interrupt. Project device overview This site mainly uses three PIC devices out of the hundreds of different chips that microchip produces. This does not sound like a lot but you can use the devices in
20 | P a g e

FREQUENCY COUNTER USING PIC MICROCONTROLLER

almost any project and they have so many built in peripherals that you can make hundreds of projects with them.

The other microchip devices are all useful in different situations - perhaps they have more memory or different peripherals - this is useful if you want to tailor your designs to the system you build - but probably more useful in a commercial environment where every cent counts in a production run. All three devices are extremely powerful and the main difference is that they have different numbers of pins and memory size. Note: There are differences in using the devices i.e. there are some registers that are different but in the generally you can interchange them - this is made easier using a high level language. The devices used in this site are: PIC Device 12F675 16F88 16F877A PIC No. Pins 8 18 40 PIC Flash memory WORDS 1k 4k 8k

microcontroller microcontroller microcontroller

Note : When looking at the microchip site the memory size is kwords - ignore kbytes - you need the kword size as this is what each instruction occupies - the
21 | P a g e

FREQUENCY COUNTER USING PIC MICROCONTROLLER

kbyte size is for comparison to other types of micros (probably). But the microcontroller data bus is 8 bits wide so it is an 8 bit microcontroller (different program memory and data memory due to using Harvard architecture). (Note: that all of them have the letter F in - this means it is a Flash reprogrammable part - don't go and buy a part with O in as its OTP - programmable only once! - only do that if you are really really sure it's the final design). PIC Microcontroller Flash Memory size You may think that 1k or even 8k is so tiny that it won't be useful but each PIC microcontroller uses RISC (Reduced Instruction Set Computing) which simply means that it has a cleverly arranged instruction set that only has a few instructions. The mid range parts have 35 instructions. If you use the high level language as recommended in this site then you won't need to be too aware of the instruction set it just means you can do a lot with a small amount of memory. Most of the projects on this site although they are fully working projects fit within 2k words! Note: If you need more memory you can always move to the 18F series of PIC microcontrollers. Another option is to add an I2C serial eprom. PIC microcontroller RAM and EEPROM size The PIC microcontroller RAM size is also important as it stores all your variables and intermediate data.

22 | P a g e

FREQUENCY COUNTER USING PIC MICROCONTROLLER

Note: You can usually alter the program to use less RAM by choosing the right variable sizes or changing how your program works

For example don't use floating point alter it to use a different variable type e.g. you can use long integers with fixed point operation to avoid floating point. PIC microcontroller EEROM : Electrically Erasable ROM is used to store data that must be saved between power up and power down.

This area is readable and writable and has a much longer life than the main program store i.e. it has been designed for more frequent use.

23 | P a g e

FREQUENCY COUNTER USING PIC MICROCONTROLLER

PIC16F877A The 40 pins make it easier to use the peripherals as the functions are spread out over the pins. This makes it easier to decide what external devices to attach without worrying too much if there enough pins to do the job. One of the main advantages is that each pin is only shared between two or three functions so its easier to decide what the pin function (other devices have up to 5 functions for a pin). Note: A disadvantage of the device is that it has no internal oscillator so you will need an external crystal of other clock source.

24 | P a g e

FREQUENCY COUNTER USING PIC MICROCONTROLLER

PIC16F877A pinout

25 | P a g e

FREQUENCY COUNTER USING PIC MICROCONTROLLER

PIC16F877A

As we can see the PIC16F877A is rich in peripherals so you can use it for many different projects.

26 | P a g e

FREQUENCY COUNTER USING PIC MICROCONTROLLER

40 MHZ FREQUENCY COUNTER USING PIC MICROCONTROLLER This PIC frequency counter project uses an LCD to display the frequency and PIC timer 1 (TMR1) to measure the input signal.

It uses TMR1 in 16 bit counter mode to count the input signal edges and overflows of the counter are accumulated to give the total count in multiples of 65536. Adding the current value of the counter at the end gives the total count. Since the measurement time is 1 second the final count is actually the frequency of the input signal. Using the 1 second measurement time also gives a frequency resolution of 1 Hz. Specification Min frequency Max frequency Input signal level 1Hz ~50MHz (limited by input pin characteristics). TTL

27 | P a g e

FREQUENCY COUNTER USING PIC MICROCONTROLLER

PIC frequency counter schematic using LCD and TMR1.

28 | P a g e

FREQUENCY COUNTER USING PIC MICROCONTROLLER

Pic frequency counter Hardware The hardware is simple and the main blocks are shown in the diagram below.
29 | P a g e

FREQUENCY COUNTER USING PIC MICROCONTROLLER

The LCD is used in 4 bit mode interface so you only need 4 data lines and three control lines and it then fits into a single 8 bit port. The crystal oscillator is simply a crystal and two capacitors connected to the PIC oscillator port at OSC1 and OSC2. The capacitors can both be fixed at the same value unless you want to tune it using a frequency reference. If you don't have an accurate reference then use fixed capacitors. The PIC micro can be any type that has a Timer 1 hardware and and has enough memory to hold the program. The LED is toggled at the end of every gate time to indicate that the processor is alive - so if there is no input signal you can tell that the software is working. You can program the PIC in circuit through the ICSP connector.

Project files for the PIC frequency counter

30 | P a g e

FREQUENCY COUNTER USING PIC MICROCONTROLLER

Compiler project files Frequency_counter_4MHz_LCD_TMR1.ppc C Source files. Frequency_counter_4MHz_7seg_tmr1.c bcd.c delay.c seven_segment.c gate.c Header files. def.h bit.h bcd.h delay.h seven_segment.h gate.h Output files Frequency_counter_4MHz_LCD_TMR1.hex

Brief description

frequency_counter...c : contains the code start point (in routine 'main') and the 1 second delay (measurement time) - gate_loop.

31 | P a g e

FREQUENCY COUNTER USING PIC MICROCONTROLLER
• • •

bcd.c : contains machine code to convert a long to a bcd. delay.c : contains code to create fixed delay times. bit.h : contains macros for bit manipulation.

All other header files contain prototypes.

PIC frequency counter code operation. The code uses the built in LCD driver routines which are automatically included by the compiler. Note automatic include is unusual but it seems to work well in mikroC. Interrupts are not used only the flags that can be polled (timer overflow) are activated. frequency_counter_4MHz_LCD_TMR1.c This file contains the port initialization, gate loop and main routine. After initialization the code enters an endless loop where it continuously performs a measurement and display operation. After an accurate 1 second delay the counter result is processed and displayed on the LCD. The gate loop is tuned to just below a millisecond so that the caller (in main) can adjust the exact delay taking account of any delays caused by calling the gate loop routine itself. Note that the timer overflow is polled within the gate loop - the extra statements in the else part of the if statement allow constant execution time whether the
32 | P a g e

FREQUENCY COUNTER USING PIC MICROCONTROLLER

condition was true or false. This allows the loop time to be accurately calculated since it always has the same execution time. delay.c Delay routines were created using machine code so that they have a fixed execution time i.e. they do not change as the compiler re-optimizes the code. They are also fixed in memory location to avoid bank change problems. bcd.c This routine was created in machine code to save space on the smaller chips and it also results in faster code than using the built in routines for long multiply and divide. It uses the Add 3 method to convert the unsigned long into an ASCII value that can be displayed on the LCD. gate.c This file contains the gate loop time measurement routines - the loop time is tuned to 999us so that the caller can calibrate the 1 second delay time (accounting for compiler optimisation and return from call instructions). The gate_loop routine calls the constant time seven segment display update and also checks (in constant time) the timer 1 overflow counter. sevensegment.c This file has the 8 digit seven segment display driver. The first output from the 4017 is not connected so this acts as the reset state. At every call the next digit is output on port D and the 4017 is advanced one bit by strobing the clock. In this way after each call the next digit is displayed.
33 | P a g e

FREQUENCY COUNTER USING PIC MICROCONTROLLER

All the routines here are made into constant time routines so that the gate loop (the calling routine) can make an accurate 1 second time measurement.

bit.h This contains macros for bit manipulation which should be compiler independent. def.h
Contains a control to let the simulator update variables where needed.

PROGRAM

Delay.c
#include "def.h" ////////////////////////////////////////////////////////////////////// // global as easier to manage ?!? i.e. its name unsigned short CounterA absolute 0x53; ////////////////////////////////////////////////////////////////////// //
34 | P a g e

FREQUENCY COUNTER USING PIC MICROCONTROLLER void delay_10_us(void) org 10 { asm { CLRW CLRW CLRW CLRW CLRW CLRW } } ////////////////////////////////////////////////////////////////////// // void delay_100_us(void) org 20 { CounterA = 29; asm { // must init in C so comiler knows these vars are used.

//;PIC Time Delay = 0.0000920 s with Osc = 4 MHz // movlw movwf D'29' CounterA

loop100us: // NB make this label different from all other asm labels! decfsz goto // } // tune routine to get correct delay. asm { CLRW
35 | P a g e

CounterA,1 loop100us

retlw

FREQUENCY COUNTER USING PIC MICROCONTROLLER CLRW CLRW CLRW CLRW } } ////////////////////////////////////////////////////////////////////// // void delay_500_us(void) org 40 { CounterA = 164; asm { // must init in C so comiler knows these vars are used.

// //

;PIC Time Delay = 0.0004970 s with Osc = 4 MHz movlw movwf D'164' CounterA

loop500us: // NB make this label different from all other asm labels! decfsz goto // } } retlw CounterA,1 loop500us

Bcd.c
#include "def.h" // BCD digit store static char DIG absolute 0x30; // was array = faster if use assembler static char DIG1 absolute 0x31; // was array = faster if use assembler static char DIG2 absolute 0x32; // was array = faster if use assembler
36 | P a g e

FREQUENCY COUNTER USING PIC MICROCONTROLLER static char DIG3 absolute 0x33; // was array = faster if use assembler static char DIG4 absolute 0x34; // was array = faster if use assembler static char DIG5 absolute 0x35; // was array = faster if use assembler static char DIG6 absolute 0x36; // was array = faster if use assembler static char DIG7 absolute 0x37; // was array = faster if use assembler static char DIG8 absolute 0x38; // was array = faster if use assembler static char DIG9 absolute 0x39; // was array = faster if use assembler // Loop counters static unsigned short BITCNT absolute 0x3A; // bit counter. static unsigned short DIGCNT absolute 0x3B; // digit counter. // Binary number store static unsigned short BIN3 static unsigned short BIN2 static unsigned short BIN1 static unsigned short BIN0 absolute 0x2C; absolute 0x2E; absolute 0x2D; absolute 0x2F;

////////////////////////////////////////////////////////////////////// // // Convert unsigned long 32bit (4byte) to unpacked BCD. // // Using serial hardware algorithm from application note xapp029. // // Identical to ulong2bcd except for BITCNT=16 changed from BITCNT=32 // void ulong2bcd_p1(unsigned long num) { // mod to 32 bits unsigned short *p; p = (unsigned short) &num; // assignments made so assembler can see the C variables
37 | P a g e

FREQUENCY COUNTER USING PIC MICROCONTROLLER bin0 = *(p++); bin1 = *(p++); bin2 = *(p++); bin3 = *(p++); dig = 0; dig1 = 0; dig2 = 0; dig3 = 0; dig4 = 0; dig5 = 0; dig6 = 0; dig7 = 0; dig8 = 0; dig9 = 0; DIGCNT = 0; // keep these just before asm to intialise asm BITCNT = 4; // my asm asm { BITLOOP: movlw 10 // ; ten digits

movwf bcd_DIGCNT movlw bcd_DIG movwf FSR // Get the next bit rlf bcd_BIN0,F rlf bcd_BIN1,F rlf bcd_BIN2,F rlf bcd_BIN3,F // ; MSBit shifted into the carry flag here // ; location of output Digit // ; set indirection reg to point at op digit.

PROCESSDIGITSLOOP: rlf INDF,F movlw -10 addwf INDF,W btfsc STATUS,C addlw 6 addlw 10 movwf INDF
38 | P a g e

// Rotate carry into current digit // Get ready to compare to 10 decimal // W = -10 + (dig[n] << 1) // should be >=5 but rotated >=10 (c=1 if dig >=5).

// greater than equal to 10 (5) so add 6 (3) // get rid of comparison test // store so can test bit4.

FREQUENCY COUNTER USING PIC MICROCONTROLLER BCF STATUS,C btfsc INDF,4 BSF STATUS,C andlw 0x0f movwf INDF INCF FSR,F // bit 4 is the overflow (effectively DC) // make C equal to this bit C=dig(bit4)

// keep upper nibble zero // store the processed digit. // next digit

decfsz bcd_DIGCNT,F goto PROCESSDIGITSLOOP decfsz bcd_BITCNT,F goto BITLOOP } // end asm } // end long to bcd ////////////////////////////////////////////////////////////////////// // // ulong2bcd_p2 // // Call this after calling ulong2bcd_p1 to complete the ulong to bcd // conversion. // // Using these two routines lets you do work between bcd processing // as the state of the bcd conversion is preserved after ulong2bcd_p1. // void ulong2bcd_process(void) { BITCNT = 4; asm { call BITLOOP }
39 | P a g e

FREQUENCY COUNTER USING PIC MICROCONTROLLER } ////////////////////////////////////////////////////////////////////// // // Convert unsigned long 32bit (4byte) to unpacked BCD. // void ulong2bcd(unsigned long num) { unsigned short i; ulong2bcd_p1(num); for (i=0;i<7;i++) { ulong2bcd_process(void); // conversion 2nd part. } } ///////////////////////////////////////////////////////////////// void packedBCDToStr(unsigned short packedBCD,unsigned short len, char *op) { unsigned short *p=packedBCD, val, i; for (i=0;i<len;i++) { val = (*p >> 4) + '0'; *(op++) = val; val = (*p & 0x0f) + '0'; *(op++) = val; p++; } } ///////////////////////////////////////////////////////////////// void unpackedBCDtoStr(unsigned short *bcd, unsigned short c, char *op) { unsigned short i;

40 | P a g e

FREQUENCY COUNTER USING PIC MICROCONTROLLER for(i=0;i<c;i++) { *(op++) = (*(bcd++)) + '0'; } *op=0; // terminate the string } ///////////////////////////////////////////////////////////////// // Reverse a string // Argunents: // requires a null terminated string. // void strrev(char *s) { unsigned short i,len; char *e, *p, st; len = strlen(s); e = s+len-1; for ( i=0; i<(len>>1); i++) { // >>1 = len/2 st = *e; // temporary store. *e=*s; *s = st; // swapped. e--; s++; } } ///////////////////////////////////////////////////////////////// // mikroC long 4 bytes : 0 .. 4294967295 // // This needs to take constant time - so does not break from loop
41 | P a g e

FREQUENCY COUNTER USING PIC MICROCONTROLLER // always completes each digit. // void process_ulong2str(char *str) { char *s=str; unsigned short stop=0; // use s as mikroC does not initialise str unpackedBCDtoStr(&DIG, 10, str); // this null terminates. // Leading digit blanking (to space if value is ascii zero) strrev(str); s = str; while (*s) { if (*s!='0') { stop=1; } if (!stop) { // replace leading zeros with spaces. if (*s=='0') {*s=' '; } } s++; } } ///////////////////////////////////////////////////////////////// // mikroC long 4 bytes : 0 .. 4294967295 void ulong2str(long ip, char *str) { char *s=str; ulong2bcd(ip); process_ulong2str(str); } //#define TEST_BCD 1 #ifdef TEST_BCD
42 | P a g e

FREQUENCY COUNTER USING PIC MICROCONTROLLER ///////////////////////////////////////////////////////////////// void main(void) { char pmain; unsigned short c = 89; long lng = 6781736L; char opm[12]; lng = 0xffffffff; // 4294967295 ulong2str(lng,opm); ulong2bcd(123456UL); ulong2bcd( (unsigned long) c); ulong2bcd( c ); ulong2bcd( lng ); lng = 65535 << 8; // 24 bit ulong2bcd( lng); lng = 65536 << 8; // 32 bit = 16777216 ulong2bcd( lng ); lng = 65536 << 10; // 32 bit = 67108864 ulong2bcd( lng ); unpackedBCDtoStr(dec,10,opm); strrev(opm); strrev(opm); ulong2bcd( 123); unpackedBCDtoStr(dec,10,opm); strrev(opm); strrev(opm); ulong2bcd( 98); unpackedBCDtoStr(dec,10,opm);
43 | P a g e

FREQUENCY COUNTER USING PIC MICROCONTROLLER strrev(opm); strrev(opm); lng = 0xffffffff; // 4294967295 ulong2str(lng,opm); pmain = (char *)dec; pmain++; } #endif

Frequency_counter_4MHz_LCD_TMR1.c
#include "bit.h" #include "delay.h" #include "gate.h" #include "bcd.h" #include "sevensegment.h" //#include "usart_support.h" ////////////////////////////////////////////////////////////////////// // Globals ////////////////////////////////////////////////////////////////////// // globals for time critical code - to force compiler to keep. volatile unsigned short sdmy=0; // for code critical section sdmy must not be optimised out volatile unsigned int udmy=0; // for code critical section sdmy must not be optimised out volatile unsigned int st_TMR1O; // updated in gate.c // controls on this code comment out the following to stop using LCD //#define USE_MY_LCD //#define USE_LCD //#define USE_USART
44 | P a g e

FREQUENCY COUNTER USING PIC MICROCONTROLLER // This macro saves typing a lot of code. #define REFRESH_7SEG display_str_8seg7(&op[2],dpIdx) //////////////////////////////////////////////////////////////////////// void init_ports(void) { ADCON1 = 0x06; // all digital outputs PORTA = 0; TRISA = 0xFF; // inputs PORTB = 0; TRISB = 0; // out

PORTC = 0x01; // b0 = 1 for 7seg TRISC = 0x81; // B7 = i/p for USART, RC0 T1CKI-Timer1 i/p. PORTD = 0; TRISD = 0; PORTE = 0; TRISE = 0; } //////////////////////////////////////////////////////////////////////// // Start here // void main() org 440 { char op[12] ; // Display string for seven segment display ; // Decimal point index into seven segment. // out // out

unsigned short dpIdx=20 unsigned int i

; // loop counter

unsigned short st_TMR1L ; // Store Timer 1 low byte (after calced). unsigned short st_TMR1H unsigned short refresh unsigned long df = 0
45 | P a g e

; // store Timer 1 high byte time.

; // refresh display interval count (during calcs). ; // dispFlay frequency value to use.

FREQUENCY COUNTER USING PIC MICROCONTROLLER unsigned short blinkc=0 #ifdef USE_LCD char lcdop[4]; #endif #ifdef USE_MY_LCD char lcdop[4]; #endif init_ports(); init_str_8seg7(); #ifdef USE_USART USART_Init(2400); #endif // USE_USART // Timer 1 // prescale = 0, oscillator off, timer on, external clock, unsynchronized. T1CON = (1<<TMR1ON) | (1<<TMR1CS) | (1<<T1SYNC); // Enable Timer 1 interrupt flag so that the flag is updated // interrupt not used just its output. PIE1 = (1<<TMR1IE) ; // TMR1 overflow flag is enabled and can be polled. // Note: actual interrupts not enabled. #ifdef USE_MY_LCD _LCD_init(); _LCD_Cmd(LCD_CLEAR); _LCD_Cmd(LCD_CURSOR_OFF); #endif #ifdef USE_LCD LCD_Config(&PORTB,2,1,0,7,6,5,4); // RS,E,W,D7..4 LCD_Cmd(LCD_CLEAR);
46 | P a g e

; // blink counter

FREQUENCY COUNTER USING PIC MICROCONTROLLER LCD_Cmd(LCD_CURSOR_OFF); #endif for(;;) { // blink the led on port C. if (blinkc>0) { // processor alive indicator setBit(PORTC,2); } else { resBit(PORTC,2); } blinkc=~blinkc; //////////////////////////// // Calculate frequency from timer 1 // if (!(st_TMR1L==0 && st_TMR1H==0 && st_TMR1O==0) ) { // Fails in 5.0.0.3 // // // df =( ((unsigned long) st_TMR1L) ( ((unsigned long) st_TMR1H)<<8 ( ((unsigned long) st_TMR1O)<16); )+\ )+\

df = (unsigned long) st_TMR1L + (unsigned long) st_TMR1H * 256 + 65536 * (unsigned long) st_TMR1O; } else { df = 0; } REFRESH_7SEG; // convert long to the display string. ulong2bcd_p1(df); REFRESH_7SEG;
47 | P a g e

// conversion 1st part.

FREQUENCY COUNTER USING PIC MICROCONTROLLER for (i=0;i<7;i++) { ulong2bcd_process(void); // conversion 2nd part. REFRESH_7SEG; } process_ulong2str(op); REFRESH_7SEG; // Initialize ready for next gate time st_TMR1L = 0; st_TMR1H = 0; st_TMR1O = 0; ////////////////////////// // GATE and REFRESH // // TUNE THIS GATE TIME to 1 second. // // 1. Tune the gate_loop first in gate.c // 2. Then tune here (the overall gate time). // // 1.Break on // gate_loop(1000,op); // 2. reset the stop watch // 3. step through until landing on statement: // TMR1L_st = TMR1L; // // Check simluation stopwatch result is 1 second. // TMR1H = 0; // clear timer1 high count - the timer1 hardware.
48 | P a g e

FREQUENCY COUNTER USING PIC MICROCONTROLLER TMR1L = 0; // clear timer1 low count - the timer1 hardware. PIR1 &= ~(1<<TMR1IF); ; // clear Timer1 overflow bit. ////////////////////////// // Start of gate time // gate_loop(1000,op); delay_500_us(); delay_100_us(); delay_100_us(); delay_10_us(); delay_10_us(); delay_10_us(); asm { CLRW CLRW CLRW CLRW CLRW } st_TMR1L = TMR1L; // get counter values before it updates st_TMR1H = TMR1H; /// End of gate time //////////////////// // test for overflow after tune delay if ( PIR1 & (1<<TMR1IF) ) { PIR1 &= ~(1<<TMR1IF); // clear the bit. // increase high count. // op is the seven segment display string

st_TMR1O += 1;
49 | P a g e

FREQUENCY COUNTER USING PIC MICROCONTROLLER } #ifdef USE_MY_LCD // lots of refresh needed to keep approx 1ms 7seg refresh rate _LCD_Print(2,1,"FREQ:"); _LCD_Print(2,6,op); _LCD_Print(1,1,"H"); ByteToStr(st_TMR1H,lcdop); _LCD_Print(1,2, lcdop); _LCD_Print(1,9,"L"); ByteToStr(st_TMR1L,lcdop); _LCD_Print(1, 10, lcdop); #endif #ifdef USE_LCD // lots of refresh needed to keep approx 1ms 7seg refresh rate LCD_Out(2,1,"FREQ:"); LCD_Out(2,6,op); LCD_Out(1,1,"H"); REFRESH_7SEG; REFRESH_7SEG; REFRESH_7SEG; REFRESH_7SEG; REFRESH_7SEG; REFRESH_7SEG; REFRESH_7SEG; REFRESH_7SEG; REFRESH_7SEG; REFRESH_7SEG; REFRESH_7SEG; REFRESH_7SEG;

ByteToStr(st_TMR1H,lcdop); LCD_Out(1,2, lcdop); LCD_Out(1,9,"L");

REFRESH_7SEG; REFRESH_7SEG; REFRESH_7SEG;

ByteToStr(st_TMR1L,lcdop); LCD_Out(1, 10, lcdop); #endif #ifdef USE_USART // test shift left in 5.0.0.3 // // st_TMR1O=5; st_TMR1H=20;

REFRESH_7SEG;

50 | P a g e

FREQUENCY COUNTER USING PIC MICROCONTROLLER // // // // // df = 70 + 21 * 256 + 65536 * st_TMR1O; // OK df = 70 + 21 * 256 + st_TMR1O<<16; USART_Print("LONG:"); LongToStr(df,bcd_ascii); // check that df is accurate USART_Println(bcd_ascii); USART_Print("FREQ:"); USART_Println(op); USART_Print("H:"); ByteToStr(st_TMR1H,op); USART_Print(op); USART_Print(" L:"); ByteToStr(st_TMR1L,op); USART_Print(op); USART_Print(" O:"); WordToStr(st_TMR1O,op); USART_Println(op); #endif // USE_USART } } // fails

Frequency_counter_4MHz_LCD_TMR1.ppc
[DeviceName] Value=P16F877A [DeviceClock] Value=4 [MainUnit] Value=Frequency-counter-4MHz-7seg-TMR1.c [DeviceFlags] Count=3 Value0=_LVP_OFF = \$3F7F Value1=_WDT_OFF = \$3FFB Value2=_HS_OSC = \$3FFE [ProjectFiles]
51 | P a g e

FREQUENCY COUNTER USING PIC MICROCONTROLLER Count=6 Value0=sevensegment.c Value1=bcd.c Value2=delay.c Value3=Frequency-counter-4MHz-7seg-TMR1.c Value4=gate.c Value5=usart_support.c Value6=lcd_keypad.c [SearchPath] Count=7 Value0=D:\Program Files\Mikroelektronika\mikroC\Defs\ Value1=D:\Program Files\Mikroelektronika\mikroC\Uses\p16\ Value2=D:\Program Files\Mikroelektronika\mikroC\Examples\new\P16F877A\Frequency_counter_4MHz_7seg_TM R1\ Value3=D:\Program Files\Mikroelektronika\mikroC\Examples\new\P16F877A\Frequencycounter-4MHz-7seg-TMR1\ Value4=C:\Program Files\Mikroelektronika\mikroC\2.100\Examples\P16F877A\LCD\ Value5=C:\Program Files\Mikroelektronika\mikroC\2.100\Defs\ Value6=C:\Program Files\Mikroelektronika\mikroC\2.100\Uses\p16\ [EEPROMinfo] isused=0 [ObjLibFiles] Count=0 [IncludePath] Count=0

CONCLUSION
52 | P a g e

FREQUENCY COUNTER USING PIC MICROCONTROLLER

53 | P a g e