JOMO KENYATTA UNIVERSITY OF AGRICULTURE & TECHNOLOGY
DEPPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING
BSC. ELECTRICAL & ELECTRONICS ENGINEERING
MICROPROCESSOR I
REPORT ON THE GRAY CODE SHAFT ENCODER ASSEMBLER PROGRAM (LAB 2)
EEE 4.1 HC
GROUP MEMBERS
NAME REG. NUMBER SIGNATURE
EDWIN NGATIA ENE211-0195/2019
TREVOR MUNYEKI ENE211-0001/2019
RYAN KIBET ENE211-0283/2021
FAITH CHEBOI ENE211-0196/2019
GEORGE WAMATHU ENE211-0005/2021
Contents
GRAY CODE SHAFT ENCODER ASSEMBLER PROGRAM................................................ 3
OBJECTIVES ............................................................................................................................ 3
LITERATURE REVIEW ........................................................................................................... 3
APPARATUS/EQUIPMENT REQUIRED ................................................................................ 6
PROCEDURE............................................................................................................................ 6
RESULTS .................................................................................................................................. 7
DISCUSSION ...........................................................................................................................15
CONCLUSIONS.......................................................................................................................17
RECOMMENDATIONS ..........................................................................................................17
REFERENCES .........................................................................................................................18
2
GRAY CODE SHAFT ENCODER ASSEMBLER PROGRAM
OBJECTIVES
i. To design/generate a gray code shaft encoder assembler program such that a user is given a choice
of ‘Binary to Gray’ or ‘Gray to Binary’. The user is then made to key in input bits to the program.
The program should then give the equivalent binary of gray bits.
ii. To run the designed program under emulator/EMU 8086 to verify it’s working.
iii. To download the program from emulator and run it under proteus environment to verify it’s
working.
iv. To give an explanation of the designed code.
LITERATURE REVIEW
A specific type of encoders used in industry is the gray code encoder. It is a special subdivision of absolute
encoders that provides maximal accuracy with the help of gray code. A Gray code shaft encoder, also
known as a gray code rotary encoder or simply a gray encoder, is a device used to convert rotary motion
into a digital signal. It is commonly used in various applications, including robotics, industrial automation,
and digital input devices.
A shaft encoder consists of a rotating shaft and a stationary body. As the shaft rotates, the encoder generates
an output signal based on the position of the shaft. The primary purpose of a gray code encoder is to
provide a digital representation of the rotational position, ensuring accurate and reliable position sensing.
The term "Gray code" refers to a specific binary code in which adjacent codes differ by only one bit. In a
standard binary code, the transition from one count to the next may involve multiple bits changing
simultaneously, which can lead to ambiguity when the code transitions. Gray code eliminates this
ambiguity by ensuring that only one-bit changes between successive counts.
With an absolute encoder, the entire rotation circle is divided into a certain number of sectors, most often
of the same size. These sectors are numbered. The encoder, during operation, gives the number of the sector
in which it is currently located. That is why it is called absolute. With this type of encoder, it is always
possible to determine at what angle relative to the zero sector the encoder is rotated at a particular moment,
that is, when turning, it outputs the values of the sector numbers, up to the maximum value.
3
Then it goes back to zero. If the encoder shaft is turned in the opposite direction, then it will start giving
opposite values. In particular, absolute encoders have their drawbacks that are depicted in absolutely
arbitrary value. Such errors are irregular and difficult to calculate. That problem is solved by
introducing the gray code. A feature of the gray code is that when the encoder switches to one, the value
of the gray code also changes to one. Only one species change. As a result, an encoder does not provide
errors when changing bits in a binary system that helps to get the most precise result of measurements.
Types of Gray Code Shaft Encoders:
There are two main types of Gray code shaft encoders:
a). Incremental Gray Code Encoder: This type of encoder produces a Gray code output that changes as
the shaft rotates, providing incremental position information. It generates a series of pulses that represent
the rotational movement. By monitoring the pulse count and direction, you can determine the position and
rotation direction of the shaft. Incremental encoders are widely used in applications where relative motion
tracking is required.
b). Absolute Gray Code Encoder: An absolute Gray code encoder generates a unique Gray code output
for each position of the shaft. This allows the encoder to provide the exact shaft position at any given time
without requiring a reference point. Even if there is a power cycle, the absolute encoder retains position
information, making it ideal for applications that demand continuous positional accuracy.
Advantages of Gray Code Shaft Encoders
a). Reduced Ambiguity: Gray code eliminates the problem of multiple bit transitions between successive
counts, making it easier to interpret the rotational position accurately.
b). High Resolution: Gray code encoders can achieve high resolution, providing precise and fine- grained
position measurements.
c). Low Noise and Error Tolerance: Gray code encoders are less susceptible to noise and errors, ensuring
reliable operation even in noisy environments.
4
d). Compact and Lightweight: Gray code encoders are often compact and lightweight, making them
suitable for space-constrained applications.
e). Cost-Effective: Compared to some other position sensing technologies, Gray code encoders are cost-
effective, especially in incremental encoder variants.
Disadvantages of Gray Code Shaft Encoders
a). Complexity: Absolute Gray code encoders can be more complex than incremental ones due to the need
for multiple tracks or discs to represent all possible positions.
b). Cost (for Absolute Encoders): Absolute encoders may be more expensive than incremental encoders
due to the increased complexity and additional components required.
c). Sensitivity to Misalignment: Gray code encoders can be sensitive to mechanical
misalignments, which may result in inaccuracies in position readings.
Practical Applications of Gray Code Shaft Encoders
a). Robotics: Gray code encoders are widely used in robotic systems to provide accurate feedback on the
position and orientation of robot joints and end-effectors.
b). Industrial Automation: In manufacturing and industrial automation, Gray code encoders are used in
motor control, conveyor systems, and CNC machines to ensure precise positioning and movement.
c). Digital Input Devices: Gray code encoders are used in input devices such as computer mice and rotary
knobs to track user interactions accurately.
d). Motion Control Systems: In applications like camera gimbals, telescopes, and antenna positioning
systems, Gray code encoders enable precise tracking and control of motion.
e). Navigation and Aerospace: In navigation systems and aerospace applications, Gray code encoders
are used for position feedback in control surfaces, aircraft rudders, and autopilot systems.
5
APPARATUS/EQUIPMENT REQUIRED
1. Emulator 8086 software
2. Proteus simulation software to provide the following equipment:
Microprocessor 8086
Latch
Programmable Peripheral Interphase (PPI 8255)
Liquid Crystal Display (LCD)
Connecting Buses
Ground (0 volts potential)
PROCEDURE
To achieve the desired objectives, the following procedure was adopted:
1. We wrote the program using the emulator software.
2. We then ran/emulated the program using the emulator.
3. Compiling the program was then done to convert the designed program to an exe. File.
4. The program was then downloaded as an exe file and ran/simulated under the
proteus environment using the proteus simulation software with the circuit
connections shown below.
The circuit diagram on proteus environment was as shown below.
6
RESULTS
After designing and emulating the program the following results were obtained.
a). The following code was written.
.model small
.stack 100h
.data
menu db 10, 13, "Select an option:", 10, 13, "1. Binary to Gray", 10, 13, "2. Gray to Binary",
10, 13, "$"
prompt_binary db 10, 13, "Enter 4-bit binary value (e.g., 1101): ", "$"
prompt_gray db 10, 13, "Enter 4-bit gray value (e.g., 1101): ", "$"
result_msg db 10, 13, "Converted value: ", "$"
invalid_msg db 10, 13, "Invalid input. Only 1s and 0s allowed. Try again.", "$"
binary db 4 dup(?) ; 4-bit binary input
gray db 4 dup(?) ; 4-bit gray input
output db 5 dup(?) ; Space for displaying the converted value (4 bits + null terminator)
.code
start:
7
mov ax, @data
mov ds, ax
main_menu:
; Display menu
lea dx, menu
call print_string
; Get user choice
mov ah, 1
int 21h
sub al, '0'
cmp al, 1
je binary_to_gray
cmp al, 2
je gray_to_binary
; Invalid choice
lea dx, invalid_msg
call print_string
jmp main_menu
binary_to_gray:
lea dx, prompt_binary
call print_string
call get_4bit_input
mov si, offset binary
mov di, offset gray
call convert_b_to_g
lea dx, result_msg
call print_string
lea dx, output
call print_string
jmp main_menu
gray_to_binary:
8
lea dx, prompt_gray
call print_string
call get_4bit_input
mov si, offset gray
mov di, offset binary
call convert_g_to_b
lea dx, result_msg
call print_string
lea dx, output
call print_string
jmp main_menu
; Subroutine: Get 4-bit user input
get_4bit_input proc
mov di, offset binary
mov cx, 4 ; Expect 4 bits
input_loop:
mov ah, 1
int 21h
sub al, '0' ; Convert ASCII to numeric
cmp al, 0
jb invalid_input
cmp al, 1
ja invalid_input
mov [di], al ; Store the valid bit
inc di
loop input_loop
ret
invalid_input:
lea dx, invalid_msg
call print_string
jmp input_loop
9
get_4bit_input endp
; Subroutine: Convert Binary to Gray
convert_b_to_g proc
mov cx, 4 ; Process 4 bits
mov al, [si] ; First Gray bit = First Binary bit
mov [di], al
inc si
inc di
dec cx
b2g_loop:
mov al, [si-1] ; Previous Binary bit
xor al, [si] ; XOR with current Binary bit
mov [di], al ; Store the result as Gray bit
inc si
inc di
loop b2g_loop
; Convert the result into a string to display
lea si, gray
lea di, output
call bin_to_string
ret
convert_b_to_g endp
; Subroutine: Convert Gray to Binary
convert_g_to_b proc
mov cx, 4 ; Process 4 bits
mov al, [si] ; B0 = G0
mov [di], al ; Store B0
inc si ; Move to the next gray bit
inc di ; Move to the next binary position
dec cx ; Decrease loop counter
g2b_loop:
10
mov al, [si] ; Load the current Gray bit
xor al, [di-1] ; XOR with the last Binary bit (di-1)
mov [di], al ; Store the result as Binary bit
inc si ; Move to the next Gray bit
inc di ; Move to the next Binary bit position
loop g2b_loop
; Convert the result into a string to display
lea si, binary
lea di, output
call bin_to_string
ret
convert_g_to_b endp
; Subroutine: Convert Binary to String
bin_to_string proc
mov cx, 4 ; Process 4 bits
bin_loop:
mov al, [si] ; Get the bit (0 or 1)
add al, '0' ; Convert to ASCII
mov [di], al ; Store in the output string
inc si
inc di
loop bin_loop
; Null terminate the string
mov byte ptr [di], '$'
ret
bin_to_string endp
; Subroutine: Print string
print_string proc
mov ah, 09h
int 21h
ret
11
print_string endp
; Exit program
exit:
mov ah, 4Ch
int 21h
end start
b). The code was proved to be correct and had followed all the necessary libraries and procedures. It was
compliable using the Emulator software.
The EMU8086 outputs were as shown below
c). The code was working as per the needs of the user, (Initially when the code is run, it prompts the user
to select between binary to gray conversion or gray to binary conversion. After the user selects his or her
choice, he/she is then asked to key in 4-bits of gray or binary bits to be converted to the equivalent binary
or gray bits respectively. After execution is complete, the program then returns the control to the
operating system). The initial display was as shown below:
12
binary to gray code conversion
13
gray code to binary conversion
d). Program compilation and download was successful and an exe file of the code was obtained
which was used for the proteus environment. The LCD gave a similar output to the emulator.
14
DISCUSSION
The different sections/lines of the adopted code are discussed below:
.model small: This line sets the memory model to "small," indicating that the program
will use 16-bit memory addressing.
.data: This section declares and initializes the data variables used in the program.
prompt1, prompt2, prompt3, prompt4, prompt5, errmsg, errmsg2: These are ASCII
strings that represent different prompts and error messages used in the program.
b: An array of four bytes to store binary bits input by the user.
g: An array of four bytes to store Gray code bits input by the user.
.code: This section contains the main code logic of the program.
start:: The program entry point. Execution starts here.
mov ax, @data and mov ds, ax: These instructions set the data segment register (DS)
to point to the beginning of the data segment.
err1:: A label used to handle errors when the user enters an invalid choice at the beginning.
lea dx, prompt1 and call output: This displays the main menu prompt, asking the user
to choose between binary to Gray code conversion or Gray code to binary conversion.
mov ah, 1h and int 21h: This reads a single character from the user (the user's choice)
using DOS function 01h.
sub al, 30h: Convert the ASCII character entered by the user into its numeric value (0 or 1).
cmp al, 1h and je graytobinary: If the user enters '1', jump to the Gray code to binary
conversion routine (graytobinary).
cmp al, 0h and je binarytogrey: If the user enters '0', jump to the binary to Gray code
conversion routine (binarytogrey).
If the user enters any other value, the program will display an error message (errmsg)
and prompt the user to try again.
15
binarytogrey:: This is a label representing the binary to Gray code conversion routine.
lea dx, prompt2 and call output: Display the prompt asking the user to input a 4-bit
binary value to convert to Gray code.
err2:: A label used to handle errors when the user enters an invalid binary value.
mov di, offset b and mov cx, 4: Initialize di to point to the b array and set the counter
cx to 4 for the 4-bit input.
get_char:: A loop to read four binary characters (0 or 1) from the user and store them
in the b array.
mov ah, 1h and int 21h: Read a single character from the user.
sub al, 30h: Convert the ASCII character to its numeric value (0 or 1).
mov [di], al: Store the numeric value (0 or 1) in the b array.
inc di: Move the destination pointer to the next element in the b array.
loop get_char: Repeat the loop until four binary characters are obtained.
The following lines of code (e.g., mov cl, b[0], cmp cl, 0, jb incre1, etc.) check the
validity of the binary input. If any character is not 0 or 1, an error message is displayed
(errmsg2) and the program jumps to the error label (incre1).
If the input is valid, the program will call the calculate_g_bits procedure to convert the
binary bits in b to Gray code bits and store them in g.
graytobinary: This is a label representing the gray code to binary conversion routine
lea dx, prompt3 and call output: Display the prompt asking the user to input a 4-bit gray
code value to convert to binary.
err3:: A label used to handle errors when the user enters an invalid Gray code value.
mov di, offset g and mov cx, 4: Initialize di to point to the g array and set the counter
cx to 4 for the 4-bit input.
get_g_char: A loop to read four gray code characters (0 or 1) from the user and store
them in the g array.
16
mov ah, 1h and int 21h: Read a single character from the user.
sub al, 30h: Convert the ASCII character to its numeric value (0 or 1).
mov [di], al: Store the numeric value (0 or 1) in the g array.
inc di: Move the destination pointer to the next element in the g array.
loop get_g_char: Repeat the loop until four Gray code characters are obtained.
The following lines of code (e.g., mov cl, g [0], cmp cl, 0, jb incre2, etc.) check the
validity of the gray code input. If any character is not 0 or 1, an error message is
displayed (errmsg2) and the program jumps to the error label (incre2).
If the input is valid, the program will call the calculate_b_bits procedure to convert the gray
code bits in g to binary bits and store them in b.
The calculate_g_bits and calculate_b_bits procedures are responsible for converting
between binary and gray code representations.
The program then displays the converted results, either the binary value or the gray code
value. The program ends with a clean exit (quit:) and returns to the operating system.
CONCLUSIONS
Based on the carried-out exercise/task, the following can be concluded:
The required gray code shaft encoder assembler program was generated successfully and
was proved to be working as per the requirements. This is evident from the results section
of this report.
RECOMMENDATIONS
After successfully carrying out the assigned task the following we made the following
recommendations
Documentation and Comments: It is essential to document the assembly language
program thoroughly and add comments to explain the purpose and logic of each section of
the code. This practice will make it easier for others to understand and maintain the
program in the future.
17
Optimization and Efficiency: Review the program's efficiency and optimize it where
possible. Efficient algorithms and code structures can reduce execution time and memory
usage, enhancing the overall performance of the program.
Testing and Validation: Perform rigorous testing of the program with different sets of Gray
code and binary inputs to ensure its correctness and accuracy. Validate the program against
known inputs and expected outputs to confirm its functionality.
Modularity and Reusability: The code maybe broken into reusable modules or functions.
Modular code enhances code readability, maintainability, and can be utilized in other
programs when needed.
User Interface: If applicable, we should provide a user-friendly interface for input and
output. This could include displaying prompts and messages to guide users through the
process of entering gray code or binary values and presenting the results clearly.
REFERENCES
1. Maxon Precision Motors, Inc. (2019). Gray Code vs. Binary Code Encoders. Retrieved
18
from https://www.maxongroup.us/maxon/view/content/gray-code-vs-
binary-code- encoders
2. Alciatore, D. G., & Histand, M. B. (2012). Introduction to Mechatronics and
Measurement Systems (4th ed.). McGraw-Hill Education.
3. Chen, L. J., & Chen, C. Y. (2013). Development of a Noncontact Rotary Encoder Based
on the Scanning of a Circular Gray Code. IEEE Transactions on Industrial Electronics,
60(8), 3302-3310. doi:10.1109/TIE.2012.2213156
4. Triebel, W. A., & Singh, A. V. (2006). The 8088 and 8086 Microprocessors:
Programming, Interfacing, Software, Hardware, and Applications (4th ed.). Prentice
Hall.
5. Mazidi, M. A., & Mazidi, J. G. (2016). The 8086 Microprocessor: Programming and
Interfacing the PC (2nd ed.). Pearson.
6. Liu, Y. (2015). Introduction to 80x86 Assembly Language and Computer Architecture.
Jones & Bartlett Learning.
7. Ray, A. K. (2006). The Intel Microprocessors 8086/8088, 80186/80188, 80286, 80386,
80486, Pentium, and Pentium Pro Processor Architecture, Programming, and
Interfacing (8th ed.). Pearson.
8. Brey, B. B. (2009). The Intel Microprocessors: 8086/8088, 80186/80188, 80286,
80386, 80486, Pentium, Pentium Pro Processor, Pentium II, Pentium III, Pentium 4,
and Core2 with 64-bit Extensions, 8th Edition. Pearson.
19