In this tutorial i am going to count the number of rising and falling edges of a square wave signal that is input to the pic microcontroller. The rising and falling edges are useful in generating time delays from external clock sources. They can be used to determine the frequency of the square wave input signal. They can be utilized to pop up an event. We can also use edge detection information to determine the PWM(Pulse Width Modulation) signal frequency, duration and duty cycle.
I am going to use Pic18f4550 microcontroller for this tutorial and i am utilizing its timer-0 for detecting the square wave signal rising and falling edges. Timer-0 offer many features and we can use them for different applications. Timer-0 offer following features
- Software select-able operation as a timer or counter in both 8-bit or 16-bit modes
- Readable and writable registers
- Dedicated 8-bit, software programmable prescaler
- Select-able clock source (internal or external)
- Edge select for external clock
- Interrupt on overflow
Timer-0 of pic18f4550 can be used in 8-bit and 16-bit mode. Timer-0 is controlled with T0CON register. T0CON contains the individuals bits that are used to set timer in different modes and other working configurations. T0Con is an 8-bit register. Individual bits with corresponding characteristics are shown below.
Timer-0 T0CON Timer Control register bits
TMRON: To activate timer-0 we write 1 to this bit and to deactivate timer we write 0 to it. This bit can individually be accessed by the statement T0CONbits.TMR0ON in Mplabx ide.
T08BIT: This bit is used to select between 8-bit or 16-bit timer. Write 1 to it for 8-bit timer counter.Write 0 for 16-bit timer counter.
T0CS: Select timer-0 clock source. Internal or external clock.
T0SE: Timer 0 edge trier transition selection. Write 1 to this bit for High-to-Low transition and 0 for Low-to-High transition.
PSA: Timer-0 Prescaler assignment bit.
T0PS2:T0PS0: Timer-0 prescaler value selection bits. T0CON is readable and writable register. We can read this register and can also write to it. Not only whole port we can read and write its individual bits. |
T0CKI Timer-0 Clock Input Pin
Practical example
Note: According to the pic microcontroller datasheet at least one rising/falling edge must be registered prior to the first increment edge. Means that the counter will start counting the edges once the first edge is passed away OR the counter will not start counting the edges if a first edge is not detected. The output edge count will always 1 less due to the above behavior of the counter. One can increment the final count by it self. In my code i did not added 1 in final count.
Project Circuit Diagram:
Circuit diagram of the project is given on the left side. Led’s are connected to Port-B of microcontroller. T0CKI pin is connected square eave external signal input from frequency generator.
Scope of the project:
The upper tutorial is valuable only for calculating the low frequencies. At higher frequency levels the 8-bit timer register will overflow very immediately. You can also use timer in 16-bit mode for higher frequencies which can count up to 65535, but still 65535 is not a huge number in counting higher square wave signal frequencies.