Counting Signal Rising and Falling edge using Pic Microcontroller Timer-0




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

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

Timer-0 has a pin named T0CKI which is used to measure the square wave input edges, frequency time period etc. This pin on pic18f4550 microcontroller is in digital Port-A of microcontrller. It is multiplex with RA-4 pin. According to the datasheet of pic microcontoller the input signal to pic microcontroller must not be greater than the voltage on its vcc pin. Normally it is +5 volts. Any square wave grater than this signal can damage the microcontroller. So please make sure before supplying any external square wave signal to T0CKI pin that it is less than vcc.

Practical example

I am going to count the number of Low-to-High edges of a particular frequency and then display them on led’s. I am using timer-0 in 8-bit mode with no prescaler. Coming to the code portion. Code is written in mplabx ide and xc8 compiler is used to compile it. First i included the necessary header files for working with pic18f4550 microcontroller in mplabx ide and with xc8 compiler. The statement TRISAbits.TRISA4=1 is enabling the T0CKI pin as input. Then TRISB=0 statement is declaring the Port-B as output port. Led’s are connected with Port-B. The statement next T0CON=0x68 is setting the Timer-0 8-bit Mode with no prescaler and increment on Low-to-High transition. TMR0L is the 8-bit timer register that is incremented on each edge that is detected on T0CKI pin.

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. 

Pic18f4550 Leds at Port-B and T0CKI measuring edges of signal

/**************************************************
* 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




Leave a Comment

*
= 3 + 3

Read previous post:
THE EVOLUTION OF LIGHT NICHIA AND INFINEON TO BUILD HIGH-DEFINITION MICRO-LED MATRIX SOLUTION FOR ADAPTIVE DRIVING BEAMS
THE EVOLUTION OF LIGHT: NICHIA AND INFINEON TO BUILD HIGH-DEFINITION MICRO-LED MATRIX SOLUTION FOR ADAPTIVE DRIVING BEAMS

Automotive lighting as an important safety and popular design feature in vehicles has evolved rapidly in the past years. Now, Nichia Corporation...

Close
Scroll to top