M. Amer Iqbal Qureshi



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

Displaying Data 
Data can be displayed in a number of ways. Obviously LCD is the simplest and easiest to implement, how-
ever not cost effective if making a commercial device. 7-segment displays are another method, however 
using them to display data requires little bit of more software coding. 
For simplicity we shall use an LCD to display our data. 
This project uses TMR0 of PIC18F452 in 16 bit mode to count the input edges appearing from an external 
circuit. The pulses are measured precisely for 1 second and then the number of overflows multiplied by 
65536 plus the counts in TMR0 will give us the total number of pulses in 1 second. Since we are measuring 
it exactly for 1 second there is no need for further conversion, and the count is the frequency in Hz. How-
ever for display we can divide it by 1000 or 100000 to display as KHz or MHz, whichever is appropriate. 


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 

tải về 3.06 Mb.

Chia sẻ với bạn bè của bạn:
1   ...   121   122   123   124   125   126   127   128   ...   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