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
The statement INTCONbits.TMR0IF is checking the timer overflow flag. The TMR0IF flag bit is present in the Interrupt control register INTCON. when the TMR0L value increases 255 the timer overflow interrupt will occur. Since we are using the timer in 8-bit mode the maximum count for timer will be 2^8 = 255. After 255 the timer will through overflow interrupt. So the above statement is checking the timer overflow flag if it happens the code switches off the timer and clear the timer over flow flag, then starts again from 0.
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.
/**************************************************
* Property of: www.microcontroller-project.com *
* Author: Usman Ali Butt *
* Created on 21 April, 2017, 2:30 PM *
**************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <pic18f4550.h> //Pic18f4550 Header File
int main(int argc, char** argv) {
TRISAbits.TRISA4=1; //Enable T0CKI
TRISB=0; //Port-B as Input
T0CON=0x68; //Timer-0 8-bit Mode, No Prescaler
//Increment on Low-to-High Transition
TMR0L=0; //Timer Low register
while(1)
{
do{
T0CONbits.TMR0ON=1; //Switch On Timer-0
PORTB=TMR0L; //Output count value on Port-B
}
while(INTCONbits.TMR0IF==0); //Wait for TF0 to roll over, INTCON Register flag
//TF0 = Timer-0 Register Over flow flag
T0CONbits.TMR0ON=0; //Turn Off Timer-0
INTCONbits.TMR0IF=0; //Clear TF0
}
return (EXIT_SUCCESS);
}
Download the project code. Folder contains the project Mplabx Project files. For any queries please write your comments below in the comments section.
Source: Counting Signal Rising and Falling edge using Pic Microcontroller Timer-0