Project files for the PIC frequency counter circuit
Compiler project filesFrequency_counter_4MHz_7seg_tmr1.ppc
C Source files.Frequency_counter_4MHz_7seg_tmr1.cbcd.cdelay.cseven_segment.cgate.c
Header files.def.hbit.hbcd.hdelay.hseven_segment.hgate.h
Output filesFrequency_counter_4MHz_7seg_tmr1.hex
Brief description
Frequency_counter...c : contains the code start point (in routine 'main').
bcd.c : machine code to convert a long to a bcd.
delay.c : code to create fixed delay times.
sevensegment.c : Constant time seven segment refresh update routines.
gate.c : Accurate 1 second time delay while calling 7seg refresh.
bit.h : macros for bit manipulation.
def.h : control for simulation help.
All other header files contain prototypes.
PIC frequency counter circuit 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_7seg_tmr1.c
This file contains the port initialization and main routine.
After initialization the code enters an endless loop where it continuously performs a measurement and display operation. Throughout the loop the display refresh routine is called (approx every millisecond). Also in main the gate_loop routine is called.
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 and the seven segment display is refreshed . Extra statements in the else part of if statements allow constant execution time whether the conditions are true or false. This allows the loop time to be accurately calculated since it always has the same execution time.
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.
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.
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.
For this project the routine is split into separate processing functions
ulong2bcd_p1(df);ulong2bcd_process(void);
The second routine is called 8 times so that the seven segment display can be updated every millisecond (approx). This is true at 4MHz - obviously using a faster crystal would allow less calls e.g. for a 20MHz crystal you could get away with two calls. Using a faster crystal makes timing slightly more difficult since for a 20MHz clock the instruction cycle is 200ns not 1us.
The bcd conversion is not the fastest around but it is faster than letting the compiler using long divide or multiply and this does show how to use machine code in the C source code. It also shows how to split processing time up for the machine code program.
def.h
Contains a control to let the simulator update variables where needed.
bit.h
This contains macros for bit manipulation which should be compiler independent.
End of code description for the frequency counter circuit.

0 মন্তব্য(গুলি):
একটি মন্তব্য পোস্ট করুন