Teach Yourself PIC Microcontrollers | www.electronicspk.com | 135
By keeping the time-base at 1 second there is another advantage. The minimum number of counts detected
can be 1. So we can measure from as low as 1Hz to as high as 50MHz. (Upper limit is the limit of PIC input
pin).
In order to learn the things we will begin with a simple procedure and then improve the project gradually.
Frequency Source:
In order to measure an input frequency you must have an external source of frequency. The external source
must give its output as TTL level, like 0 and 5V but not more than that. If you want
to measure analog fre-
quency source then an additional circuitry to convert it into appropriate TTL level using gates and Schmitt
triggers will be necessary. Foe demonstration purpose we will use 555 timer in astable mode to produce
clock pulses. Its output will be given to the T0CKI pin of PIC.
The above figure shows how you can make a simple oscillator using 555 timer IC. You can replace R1 with
a variable resistor to change the frequency. You can omit C2 if you want. So all you need is an IC, two re-
sistors and a capacitor. In present configuration this cir-
cuit will generate almost 1000 cycles per second or 1KHz.
Instead of using 12V as VCC, you can use 5V from your
PIC-Lab-II. So T0CKI header can
be directly connected to
this board, powering it as well as measuring the fre-
quency.
Figure on right shows the construction of 555 timer based
oscillator on bread-board. The output of timer circuit is
connected to PIC-Lab-II T0CKI header (RA4).
In our first and preliminary program, we have used
R1,R2=2.2K and C1=1uf this is
circuit should give a cal-
culated frequency of 217 Hz, however when actually
tested on an oscilloscope, because of small variation in
resistors and capacitance, the measured frequency way
206.6 Hz .
Since the frequency is below 255, we can simply use the
TMR0 in either 8 bit mode, or if used in 16 bits mode
Teach Yourself PIC Microcontrollers | www.electronicspk.com | 136
measure the counts, accumulated in TMR0L register. In PIC18F452 TMR0 output is placed in TMR0H and
TMR0L
registers, which are two 8 bit registers. In our simplest frequency counter, we configure TMR0 as
16 bit timer, and do not use any pre-scalar, thus every pulse will increment the counter. Before measuring
the sample we set TMR0 output registers to 0, and wait for 1 second, after 1 second we store the value of
TMR0 registers into a 16 bit variable and display it. Since this variable can hold a value up to 65535, and
the value corresponds
exactly with the pulses, the count exactly gives us the frequency in Hz.
Since we are going to use TMR0 as our counter, it has an associated
T0CON register which configures the properties of this timer. Instead of
remembering its bits and their function, it is better to declare its as useful
bit names and declare them as symbols in your program.
We have declared the entire T0CON register,
however in this very pro-
gram, we need only to manipulate few bits.
•
Configure TMR0 to get clock pulses from RA4 pin
•
Put TMR0 into 16 bits mode
•
Disable Pre-Scalar
•
Enable / Disable Timer when required
After appropriate configuration, the TMR0 output registers,
TMR0H:TRM0L are initialized to 0. The Timer is then enabled the value
of TMR0 registers is recorded in a variable
and microcontroller put to
wait for 1 second. After this the TMR0 value is again recorded in another
variable. The difference in two variables is the accumulated value. We
' frequency counter
Chia sẻ với bạn bè của bạn: