Share this

A Brief Analysis of the Working Principle and Application Circuit of Photoelectric Encoders

2026-04-06 05:59:13 · · #1

1. Working principle of photoelectric encoder

An optical encoder , commonly known as a "single-button rotary encoder," resembles a potentiometer in appearance. It features an external knob that can be rotated left and right while also being pressed down. Many devices (such as monitors and oscilloscopes) use it as a human-machine interface. The following explanation uses an optical encoder manufactured by Greyhill Corporation of the United States as an example to illustrate its working principle and usage. The internal circuit of the optical encoder is shown in Figure 1. It contains one light-emitting diode (LED) and two phototransistors. When the knob is rotated left or right, the central light-shielding plate rotates with it, sequentially blocking the phototransistors. This results in the A and B phases outputting the waveform shown in Figure 2. When the knob is pressed down, pins 2 and 3 are connected, functioning like a regular button.

When rotating clockwise, phase A of the photoelectric encoder will lead phase B by half a cycle; conversely, phase A will lag phase B by half a cycle. By detecting the phases of phases A and B, it can be determined whether the knob is rotating clockwise or counterclockwise. By recording the number of changes in phases A or B, the number of times the knob has rotated can be determined. By checking whether pins 2 and 3 are connected, it can be determined whether the knob has been pressed. The specific phase detection rules are as follows:

When A is the rising edge and B=0, the knob is turned clockwise.

B is the rising edge; when A=1, the knob rotates clockwise.

When A is the falling edge and B=1, the knob rotates clockwise.

B is the falling edge; when A=0, the knob is turned clockwise.

B is the rising edge; when A=0, the knob rotates counterclockwise.

When A is the rising edge and B=1, the knob rotates counterclockwise.

B is the falling edge; when A=1, the knob rotates counterclockwise.

When A is the falling edge and B=0, the knob rotates counterclockwise.

The rotation direction of the knob can be easily determined using the method described above. Adding an appropriate delay during the determination process can eliminate jitter interference.

2. Driver model provided by WinCE

The WinCE operating system supports two types of drivers. One is the native driver, which implements the device driver as an independent task, directly performing hardware operations at the top-level task level, thus having a clear and specific purpose. Native device drivers are suitable for devices integrated into the Windows CE platform, such as keyboards, touchscreens, and audio devices. The other is the stream interface driver, which has a custom interface. It is a general type of device driver. Stream interface drivers take the form of user-level dynamic link libraries (DLLs) that implement a fixed set of functions called "stream interface functions." These stream interface functions allow applications to access these drivers through the file system. The photoelectric encoder discussed in this article belongs to the stream interface device category.

2.1 Streaming device driver loading process

When the WinCE.NET system runs, it launches the DEVICE.exe process, which is responsible for loading the stream driver. The DEVICE.exe process loads the driver by loading the registry enumerator (RegEnum.dll). In WinCE.NET, all device resource information is recorded in the system registry by OAL. RegEnum.dll scans the subkeys under the registry key HEKY_LOCAL_MACHINE\Driver\BuilTIn one by one, and initializes the hardware device according to the contents of each entry when a new device is found.

2.2 Interrupts and Interrupt Handling

If a driver needs to handle an interrupt, it first needs to create an event using the `CreateEvent` function and then bind the event to an interrupt flag by calling the `InterruptInterruptIdentify` function. The Interrupt Thread (IST) in the driver can then use the `WaitForSingleObject` function to wait for the interrupt to occur. After a hardware interrupt occurs, the operating system enters the exception handler, which calls the `OEMInterruptHandler` function of the Open Execution Language (OAL). This function detects the hardware and returns the interrupt flag to the system. The system, upon receiving the interrupt flag, finds the event corresponding to that flag and wakes up the thread (IST) waiting for the event. The IST then performs interrupt handling. After handling is complete, the IST calls the `InterruptDone` function to inform the operating system that interrupt handling is finished. The operating system then calls the `OEMInterruptDone` function in the OAL again, finally completing the interrupt handling. Figure 3 shows the flowchart of interrupt handling in WinCE.NET.

3. Design of the photoelectric encoder driver

3.1 Hardware Interface between Photoelectric Encoder and S3C2410

The interface circuit between the photoelectric encoder and the S3C2410 is shown in Figure 4. Phases A and B of the photoelectric encoder are open-collector outputs. Since the I/O port level of the S3C2410 is 3.3V, it is pulled up to 3.3V through resistors and then connected to the CPU's EINT0 and EINT1 pins respectively. P1 is directly connected to 3.3V, and P2 is pulled down to GND through a resistor. When the knob is pressed, the P2 port outputs a high level; otherwise, it outputs a low level.

In operation, EINT0 and EINT1 are configured as external interrupts triggered by both rising and falling edges, and EINT2 is configured as an interrupt triggered by the rising edge. When the knob is pressed, the EINT2 pin generates a rising edge triggered interrupt.

3.2 External Interrupt Initialization and Interrupt Service Routine Writing

First, the CPU's I/O ports and interrupts must be initialized, and then the interrupt handlers must be written. This involves four steps:

Initialize the I/O ports. In the Port_Init() function, initialize EINT0 and EINT1 as interrupts triggered on both rising and falling edges. Initialize EINT2 as an interrupt triggered on the rising edge.

Add an interrupt number. Add a macro definition for the photoelectric encoder interrupt vector in oalint.h. The code is: #define SYSINTR_OED(SYSINTR_FIRMWARE+20)

Add functions for interrupt initialization, disabling, and resetting, and add relevant code to functions such as OEMInterruptEnable(), OEMInterruptDisable(), and OEMInterruptDone().

Returns the interrupt flag, which is returned by the OEMInterruptHandler() function (SYSINTR_OED).

3.3 Writing the stream interface driver

Windows CE.net divides interrupt handling into two parts: Interrupt Service Routine (ISR) and Interrupt Service Thread (IST). The ISR is typically required to be as short and fast as possible; its sole task is to return the interrupt flag. Because the ISR is small and can only perform minimal processing, the interrupt handler calls the IST to perform most of the interrupt handling. The Interrupt Service Thread (IST) remains idle until it receives a signal from the waitForSingleObject() function indicating that an interrupt has occurred. Upon receiving an interrupt signal, it calls subroutines in the PDD layer of the native device driver. These subroutines, in turn, access the hardware to obtain its status. The IST registers itself using the InterruptInitialize() function and then waits for an interrupt signal using the waitForSingleObject() function. If an interrupt signal arrives, the status of the photoelectric encoder should be recorded and stored in the variable OED_Status. OED_Status=1 indicates the knob is pressed, OED_Status=2 indicates the knob is rotated counterclockwise, and OED_Status=3 indicates the knob is rotated clockwise.

There is also a simpler phase detection rule. The specific steps are as follows: when the thread is created, the level state of EINT1 is read and stored in the variable PreEINT1. Each time an interrupt occurs, it first checks whether EINT2 is high. If it is high, it means the button is pressed; if EINT2 is low, it checks whether the level of EINT1 is the same as that of PreEINT1. If they are the same, it means the knob is rotated counterclockwise; otherwise, the knob is rotated clockwise. The judgment process is shown in Figure 5.

The Windows CE streaming interface driver model requires driver developers to write 10 interface functions. For optical encoder drivers, this mainly involves writing two functions: device initialization and data reading. Windows CE device filenames are prefixed with three uppercase letters, which the operating system uses to identify the device corresponding to the streaming interface driver. Here, the device filename prefix is ​​defined as "OED" (OpticalEncoder). The device initialization function OED_Init() is used to create interrupt events and interrupt service threads when Windows CE loads the driver. The OED_Read() function returns the optical encoder's status (OED_Status).

3.4 Package the driver and add it to WinCE

Compiling a dynamic link library (DLL) using the above method is not enough, because its interface functions are not yet exported. It's also necessary to tell the linker what functions to export; therefore, a file with the .def extension must be created. In this design, it's named OpticalEnccder.def. Below is the content of this file:

A specific stream interface driver and the registry are inseparable. There are two methods to add registry entries to the WinCE kernel: one is to directly modify the .reg file under PlatformBuilder; the other is to write a custom registry file and add the dynamic link library file to the kernel by adding components. Here, we use the second method to add OpticalEncoder.dll to the kernel. The content of the registry file is as follows:

Finally, write a CEC file to modify the custom kernel registry and add OpticalEncoder.dll to the system kernel. Then, you can directly add the pre-written driver in PlatformBuilder.

Applications of photoelectric encoders

1. Angle Measurement

In car driving simulators, photoelectric encoders are used as sensors to measure the steering wheel rotation angle. Gravity measuring instruments also use photoelectric encoders, connecting their rotating shaft to the compensation knob shaft within the instrument. Torsion angle meters utilize encoders to measure changes in torsion angle, as seen in torsion testing machines and fishing rod torsion performance tests.

The pendulum impact testing machine uses an encoder to calculate the change in the pendulum angle during impact.

2. Length measurement

A meter counter uses the circumference of a roller to measure the length and distance of an object.

A wire displacement sensor measures the length and distance of an object using the circumference of a winding reel.

The coupling direct measurement is connected to the main shaft of the power unit that drives linear displacement, and the measurement is achieved by the number of output pulses.

Medium detection uses sprockets, sprockets of rotating chains, and synchronous belt pulleys to transmit linear displacement information.

3. Speed ​​Measurement

Linear speed, measured by connecting to an instrument, is the linear speed of the production line.

Angular velocity, measured by an encoder, is the speed of a motor, shaft, etc.

4. Position measurement

In terms of machine tools, it can memorize the coordinate positions of various coordinate points on the machine tool, such as drilling machines.

In terms of automation control, the system controls the execution of specified actions at designated locations, such as elevators and hoists.

5. Synchronous control

Tension control is achieved by synchronously controlling the transmission components using angular velocity or linear velocity.

Conclusion

This paper mainly introduces the principle and application method of photoelectric rotary encoders, and details the structure of the WinCE driver. A driver for the photoelectric encoder under the embedded operating system WinCE was successfully developed. Experiments prove that the method is correct and feasible, and the program runs stably and reliably.

Application circuit of photoelectric encoder

1. Application of EPC-755A photoelectric encoder

The EPC-755A photoelectric encoder boasts excellent performance, exhibiting strong anti-interference capabilities in angle and displacement measurements, and providing a stable and reliable output pulse signal. This pulse signal, after counting, yields the measured digital signal. Therefore, in developing a car driving simulator, we selected the EPC-755A photoelectric encoder as the sensor for measuring the steering wheel rotation angle. Its output circuit is an open-collector type, with an output resolution of 360 pulses/revolution. Considering that the car steering wheel rotates bidirectionally (clockwise and counterclockwise), phase detection of the encoder's output signal is necessary for counting. Figure 2 shows the phase detection and bidirectional counting circuit actually used in the photoelectric encoder. The phase detection circuit consists of one D flip-flop and two NAND gates, while the counting circuit uses three 74LS193 chips.

When the photoelectric encoder rotates clockwise, the output waveform of channel A leads the output waveform of channel B by 90°. The D flip-flop outputs Q (waveform W1) are high and Q (waveform W2) are low. The upper NAND gate is open, and the counting pulse passes through (waveform W3) and is sent to the add pulse input terminal CU of the bidirectional counter 74LS193 for addition counting. At this time, the lower NAND gate is closed, and its output is high (waveform W4). When the photoelectric encoder rotates counterclockwise, the output waveform of channel A is delayed by 90° compared to the output waveform of channel B. The D flip-flop outputs Q (waveform W1) are low and Q (waveform W2) are high. The upper NAND gate is closed, and its output is high (waveform W3). At this time, the lower NAND gate is open, and the counting pulse passes through (waveform W4) and is sent to the decrement pulse input terminal CD of the bidirectional counter 74LS193 for subtraction counting.

When the car steering wheel rotates clockwise and counterclockwise, its maximum rotation angle is two and a half turns. An encoder with a resolution of 360 pulses/revolution is selected, and its maximum output pulse count is 900. The actual counting circuit used consists of three 74LS193 chips. During system power-on initialization, the chip is first reset (CLR signal), and then its initial value is set to 800H, which is 2048 (LD signal). Thus, when the steering wheel rotates clockwise, the output range of the counting circuit is 2048 to 2948, and when the steering wheel rotates counterclockwise, the output range of the counting circuit is 2048 to 1148. The data outputs D0 to D11 of the counting circuit are sent to the data processing circuit.

In actual use, the steering wheel is frequently turned clockwise and counterclockwise. Due to quantization errors, after working for a long time, the output of the counting circuit when the steering wheel returns to center may not be 2048, but rather deviate by a few digits. To solve this problem, we added a steering wheel centering detection circuit. After the system is working, when the simulator is in a non-operational state, the data processing circuit detects the centering detection circuit. If the steering wheel is in the centering state and the data output of the counting circuit is not 2048, the counting circuit can be reset and the initial value can be reset.

2.2 Application of photoelectric encoders in gravity measuring instruments

A rotary photoelectric encoder is used, with its shaft connected to the compensation knob shaft in the gravity measuring instrument. The angular displacement of the compensation knob in the gravity measuring instrument is converted into a certain electrical signal. There are two types of rotary photoelectric encoders: absolute encoders and incremental encoders.

Incremental encoders are sensors that output pulses. Their code disks are much simpler and have higher resolution than those of absolute encoders. Generally, only three tracks are needed. These tracks no longer have the same function as those in an absolute encoder; instead, they generate counting pulses. The outer and middle tracks of the code disk have an equal number of evenly distributed transparent and opaque sector areas (gratings), but the two sectors are offset by half a zone. When the code disk rotates, its output signals are A-phase and B-phase pulse signals with a 90° phase difference, plus a pulse signal generated by the third track, which has only one transparent slit (this serves as the reference position of the code disk, providing an initial zero-position signal to the counting system). The direction of rotation can be determined from the phase relationship (leading or lagging) of the A and B output signals. As shown in Figure 3(a), when the code disk rotates clockwise, the pulse waveform of track A leads track B by π/2, while in reverse rotation, the pulse of track A lags track B by π/2. Figure 3(b) shows a practical circuit where the lower edge of the A-channel shaping wave triggers a positive pulse generated by a monostable multivibrator, which is then ANDed with the B-channel shaping wave. When the code disk rotates forward, only the positive pulse is output; conversely, only the negative pulse is output. Therefore, the incremental encoder determines the rotation direction and relative angular displacement of the code disk based on the output pulse source and pulse count. Typically, if the encoder has N (code channels) output signals with a phase difference of π/N, the countable pulses are 2N times the number of gratings; in this case, N=2. A drawback of the circuit in Figure 3 is that it sometimes generates false pulses, causing errors. This occurs when one signal is at a 'high' or 'low' level, while another signal is fluctuating between 'high' and 'low'. In this case, although the code disk does not move, it generates a unidirectional output pulse. For example, this can happen when the code disk jitters or when manually aligned (as seen below in gravimeter measurements).

Figure 4 shows a quadruple frequency subdivision circuit that can both prevent false pulses and improve resolution. Here, a D-type flip-flop with memory function and a clock generation circuit are used. As shown in Figure 4, each channel has two D flip-flops connected in series. Thus, during the clock pulse interval, the two Q terminals (e.g., pins 2 and 7 of the 74LS175 corresponding to channel B) maintain the input state of the previous two clock cycles. If they are the same, it indicates no change during the clock interval; otherwise, the direction of change can be determined based on their relationship, thus generating a 'positive' or 'reverse' output pulse. When a channel fluctuates between 'high' and 'low' due to vibration, it will alternately generate 'positive' and 'reverse' pulses. This can be eliminated when the algebraic sum of the two counters is applied (this will also be relevant to the instrument readings below). Therefore, the frequency of the clock generator should be greater than the maximum possible value of the vibration frequency. Figure 4 also shows that four counting pulses are obtained within the original pulse signal period. For example, an encoder with 1000 pulses per revolution can generate 4000 pulses at 4 times the frequency, with a resolution of 0.09°. In fact, current sensor products of this type encapsulate the amplification and shaping circuits of the photosensitive element's output signal with the sensing element, so by simply adding subdivision and counting circuits, an angular displacement measurement system can be formed (74159 is a 4-to-16 decoder).

Read next

CATDOLL 108CM Dodo (TPE Body with Hard Silicone Head)

Height: 108cm Weight: 14.5kg Shoulder Width: 26cm Bust/Waist/Hip: 51/47/59cm Oral Depth: 3-5cm Vaginal Depth: 3-13cm An...

Articles 2026-02-22