M. Amer Iqbal Qureshi



tải về 3.06 Mb.
Chế độ xem pdf
trang124/132
Chuyển đổi dữ liệu09.12.2022
Kích3.06 Mb.
#53945
1   ...   120   121   122   123   124   125   126   127   ...   132
Amer Iqbal - Teach Yourself PIC Microcontrollers for Absolute Beginners-Microtronics Pakistan (2001)

Crystal oscillator 
For the following projects the crystal oscillator is used as the time-base. In these projects measurement of T 
(set at one second) is made by executing a delay that takes a set number of machine cycles. 
Project 1 
Frequency Counter


Teach Yourself PIC Microcontrollers | www.electronicspk.com | 134 
Using a 20MHz oscillator gives a machine cycle of 5MHz (a period of 0.2us) which makes calculating and 
setting time delays fairly easy since most PIC instructions execute in one machine cycle. The accuracy of 
the frequency counter depends on the accuracy of the crystal driving the microcontroller. All crystals have 
some factor of error, which is expressed as PPM, parts per million. Thus a 4MHz crystal may actually be 
oscillating between 3.998 to 4.002 MHz, which can slightly change our time-base and therefore the counts. 
For our project we accept this small change extremely small and ignore it. However the frequency of oscil-
lator can be changed by changing the capacitance, or by calibrating an internal correction variable to com-
pensate for the change. Calibration is usually done by giving a signal of known frequency. 
Timer 0 Algorithm 
TMR0 (timer Zero) is ideal for frequency measurement (counting edges) as it can function as a 16 bit 
counter taking its input directly from a port pin (RA4,T0CKI). PIC-Lab-II has this pin separately taken out 
as T0CKI header, which also contains 5V power supply and ground for the probe, or for a test circuit. This 
the easiest way of measuring frequency using a PIC micro - you can use Timer 1 as well which can get its 
input from RC0. 
By default TMR0 in 18F452 is an 8 bit counter, however it can be configured to work as 16 bit counter by 
setting T0CON bit 6 to 0. (see data sheet). In PIC 16F877 and like devices, it is 8 bit timer. So when de-
signing your actual project either select TMR1, or appropriately adjust software if using TMR0. We will 
use TMR0 in 16bit mode in this project. 
So if we begin with TMR0=0 every clock pulse on the appropriate pin will increase the count. This count 
cn reach a maximum value of 65535 (all 1s in 16 bit register). Now when the counter has reached its maxi-
mum, next pulse will reset it to 0, however it will generate an interrupt and set an overflow flag high. If we 
count the number of overflows, this gives us the number of 65536 cycles. Multiplying overflows with 
65536 will give the number of pulses during that period of time, plus we have to add any more accumulated 
in TMR0 after last overflow. This will give us a total number of pulses arriving at the input pin in a given 
time period –T 
The trick to making the frequency counter algorithm work is that the overflow flag must be polled within 
the delay routine but it must be polled ensuring that the polling routine takes a constant time (So that the 
delay period can be calculated exactly). 
For the frequency counter interrupts are not used at all in either measurement of the input signal edges or 
measurement of the time period T. This is because using an interrupt as part of the measurement process 
would interrupt the time measurement part of the code. The number of interrupts would be dependent on 
the input signal frequency and so the time measurement would be inaccurate. If the time period (T) meas-
urement was made using a different method e.g. Using a 1s externally generated time period T then inter-
rupts could safely be used. 

tải về 3.06 Mb.

Chia sẻ với bạn bè của bạn:
1   ...   120   121   122   123   124   125   126   127   ...   132




Cơ sở dữ liệu được bảo vệ bởi bản quyền ©hocday.com 2024
được sử dụng cho việc quản lý

    Quê hương