An Input Capture unit is a peripheral included in most modern microcontrollers. It allows events to be time stamped as they occur. This timing can often be accurate to a number of nanoseconds. The unit is an integral part of one or more of the microcontroller's timers. One of the processor's I/O lines is the trigger to the unit. A special function register is used to configure the behavior of the input capture- Often this register contains a bit or two which are set to describe the types of events which trigger an input capture. For example, the input capture could occur on a high-to-low transition of the IO line or a low-to-high transition instead. Some systems allow input captures on both types of transitions. Often, there's another bit in this register to tell the processor whether or not to generate an interrupt when an event is captured.

The average microcontroller timer is based on an 8 or 16 bit counter that can be constantly incremented (or decremented) automatically at a fixed rate. The rate at which the timer is incremented is set by one or more special function registers (SFR) associated with that timer. Usually, if the programmer wants, an interrupt can be generated with each tick of the timer. When the counter hits its maximum or minimum value, (0, 255, or 65535, for example) the counter rolls back around to the other extreme. These rollover events can also be used to generate interrupts.

When an input capture event occurs, the value of the timer's counter at that time is copied to the input capture unit's timestamp register. An interrupt, if enabled, occurs after that. (An interrupt pauses what the program is doing and jumps to an interupt handler routine, also known as an ISR. After the handler is done doing whatever to handle the event, program execution is resumed where it left off.)

It might be illustrative to show a practical application of an input capture unit. How about an electromagnetic Gauss gun? A concise definition of such a device is that it uses electromagnets to accelerate a ferrous projectile through a tube to a very high velocity. There is usually more than one electromagnet involved, and they need to be energized at just the right times to get the most acceleration. Lets design our gun to consist of a clear tube wrapped with three coils of wire. I'll try to illustrate this with a bit of ASCII art.

  Iron    Coil             Coil             Coil
,-Bullet   A                B                C    
|    _____==___________V___==____________V__==____
'-> / /\ ==               ==               ==    /\  
    | \/ ==               ==               ==   |  |  -- Business end
    \_____==_______________==_______________==___\/
                       ^                 ^	

The V's in the above diagram are infrared LEDs. The ^'s are infrared photodetectors. When a bullet interrupts the beam between an emitter and detector, the detector can be used to trigger an input capture event on the microcontroller. (In this project, all of the detectors can be ORed together using a logical OR circuit which allows one input capture line to respond to each of the photodetectors. That saves some precious pin real estate on the microcontroller.) We'll define an input capture interrupt handler to capture the time when a bullet breaks a beam, and a timer rollover interrupt handler to count the timer rollovers. These two numbers can be used to very accurately calculate the time at which an input capture occured. In this example, we will be using an 8 bit timer, so it will count from 0 to 255 before rolling back around to zero.

The basic procedure to fire the bullet would be as follows:

1. Clear any pending input capture event interrupts. 2. Energize coil A for a short time. (With as much oomph as possible) This gets the projectile moving. 3. When an input capture interrupt occurs, the following expression tells us what time it is: Ti=(TICP+256*Rollovers)/TicksPerSecond Where TICP is the input capture timestamp register, Rollovers is the number of timer rollovers counted by our rollover ISR, and TicksPerSecond is a constant which depends on how the timer is configured. 4. Energize coil B for a short time. 5. When another input capture occurs, store time Tf using the same expression from step 3. 6. Energize coil C for a short time. 7. Pray nobody got hurt.

Now that we know Ti, Tf, and the distance between the two detectors, we can calculate the average velocity of the projectile between coils B and C. With a little bit of physics, the muzzle velocity as the bullet leaves the gun can be calculated!

Log in or register to write something here or to contact authors.