Home
Diploma Practice C Online People
Certificate Contact Us

Understanding and using Timer/Counter peripheral chip

Counter or Timer is the third IO or peripheral chip we are interfacing to the CPU. The other two IO chips we already interfaced are general purpose IO chip and serial communication chip. These three IO chips are very basic and fundamental chips. Every embedded hardware certainly will have these chips. But now a days, embedded hardware contains many more IO interface chips. Some of these chips are listed in the previous article, while discussing various interface or communication protocols.

Counter/Timer as Counter

The Counter/Timer chip is essentially a counter. It counts the clock pulses that received on the input clock pin. Every counter/timer chip will have a counter register in it. Whenever a pulse is received on the input pin, the value in the counter will be incremented. Basically thats all a counter will do. Program running on the CPU can read this counter register and can find, how many pulses are received so far. We can connect this input pin to a push button. Whenever user pushes and releases the button a pulse is generated, and counter increments by one. This may not be very interesting use of the counter, let us see the more interesting applications of counter.

If you observe, electricity or energy meter in your home, it keeps producing pulses. You will observe an led is blinking. Led blinks when certain amount of energy is consumed. By counting these pulses, the embedded hardware can maintain the amount of electricity consumed. In fact the embedded hardware present in the energy meter will do the same thing.

Similarly there are water or liquid flow meters. Whenever certain amount of liquid is passed through this meter it will produce a pulse. By counting these pulses we can measure the quantity of liquid.

It is possible to connect a pulse generating sensor to a rotating wheel of a car or auto. For each revolution of a wheel, the sensor will produce fixed number of pulses. By counting these pulses, we can find the distance travelled. By counting the pulses in every second, the speed of auto can be found.

We can setup an infrared sensor and infrared light source in the path of moving items. For example, items are moving on a conveyor built in a production industry. Whenever item obstructs the light for certain interval, a pulse is generated by the infrared sensor. By counting these pulses we can find the number of items passed across the light source.

Now i hope, you are convinced that there is a need for counting the pulses. And the counter chip is just made for this purpose. Also note that, without using any counter chip, we can also measure the pulses by using a digital input line. But in this case CPU has to continuously monitor the line, and CPU wastes its precious time for this simple counting. So it is better to use counter chip instead of wasting CPU time.

Counter/Timer as Timer

We can use counter as timer also. In this case, we will connect a fixed, known frequency clock pulses to the input pin of counter. For example if we connect 1 MHz clock source to the input of counter, the counter will increment by one in every micro second. So by reading the counter at two events, we can find the time difference between these two events. If we wish to glow LED for 100 milli seconds, we can switch on the led and start the counter. When counter value reaches 100,000, switch off the led.

Interfacing to CPU

The following figure shows how timer/counter chip is interfaced to the CPU. Note that for the lack of space, we are showing only timer chip in the figure. One should assume that all the three peripheral chips (GPIO, Serial, and Timer) are attached to the CPU bus. In the figure only timer chip is shown. Timer chip will have chip select pin, read pin, write pin, data pins and address pins. These pins are connected to the CPU's bus. CPU can read and write to the registers of this chip.

Interfacing to devices

For interfacing to the external world, the timer chip has two pins. One is clock input pin, through which counter receives the pulses. In counter application this pin is connected to the external sevice like energy meter or water meter. To use this as timer, this pin is connected to the on-board clock oscillator.

The second pin is timer output pin. This pin can be used to generate output pulses, based on the timer events. For example a pulse will be generated on this pin, whenever timer receives 1000 input clock pulses. So assuming timer clock is 1 MHz, a output pulse is generated once in every millisecond. Instead of generating periodic pulses, we can generate a single pulse of known width. In this case, the pin will go to high state when timer is started. When timer reaches specified count, the pin will go to low state, producing a single output pulse of given width. This pulse may be used to open the water tap for a particular duration. Or to open a lift door for a certain duration.

Registers

This chip has got the following three registers. Each of these register is having unique address, to access by the CPU.

  • Counter Register
  • Match Register
  • Status and control register

By writing to control register, we can start and stop the counter from counting. With control register, we can also control the behaviour of timer and timer output pin. Using the status bits we can find whether counter is reached specified value or not. We can write the maximum count value to count, in the match register. When counter reaches this maximum value, one of the folliwing things can happen.

  • On reaching match register value, stop the counter. And also set the status bit in the status register, indicating counter has finished counting.
  • On reaching match register value, start the counter from 0 onwards. Also set the status bit in the status register, indicating that counter has reached the maximum value.
  • In addtion to one of the first two, change the state of output pin to low
  • In addtion to one of the first two, change the state of output pin to high
  • In addtion to one of the first two, toggle the state of output pin

Which of the above will happen is controlled through the control register.





#401, Sai Sushma Homes, 23/A, S.R. Nagar Main Road, Hyderabad-500038, India.
Ph: +91-40-48508764, 7702028989, depik.help@gmail.com