Maker Pro
Maker Pro

8051 Programming

I need to convert a 32 bit binary number to gray code. i know how to do
it bit by bit, but i need to do a function to do it, so that i don't
need to repeat code 32 times. Can anybody help me?

Thanks

LDS
 
A

Arlet

Jan 1, 1970
0
I need to convert a 32 bit binary number to gray code. i know how to do
it bit by bit, but i need to do a function to do it, so that i don't
need to repeat code 32 times. Can anybody help me?
From binary to gray code is easy.

in C: gray = binary ^ (binary >> 1)

The reverse operation takes more effort.
 
P

petrus bitbyter

Jan 1, 1970
0
I need to convert a 32 bit binary number to gray code. i know how to do
it bit by bit, but i need to do a function to do it, so that i don't
need to repeat code 32 times. Can anybody help me?

Thanks

LDS

Mmm... It's quite some time I wrote 8051 assembler, but if memory serves
it's done like this:

num1 equ 64
num2 equ 65
num3 equ 66
num4 equ 67

clr c
mov a,num1
rlc a
xrl a,num1
mov a,num2
rlc a
xrl a,num2
mov a,num3
rlc a
xrl a,num3
mov a,num4
rlc a
xrl a,num4
end

petrus bitbyter
 
P

petrus bitbyter

Jan 1, 1970
0
petrus bitbyter said:
Mmm... It's quite some time I wrote 8051 assembler, but if memory serves
it's done like this:

num1 equ 64
num2 equ 65
num3 equ 66
num4 equ 67

clr c
mov a,num1
rlc a
xrl a,num1
mov a,num2
rlc a
xrl a,num2
mov a,num3
rlc a
xrl a,num3
mov a,num4
rlc a
xrl a,num4
end

petrus bitbyter

Oops,

Memory fails. The next code should be correct:
num1 equ 64
num2 equ 65
num3 equ 66
num4 equ 67

clr c
mov a,num1
rlc a
xrl num1,a
mov a,num2
rlc a
xrl num2,a
mov a,num3
rlc a
xrl num3,a
mov a,num4
rlc a
xrl num4,a
end

petrus bitbyter
 
K

Ken Smith

Jan 1, 1970
0
I need to convert a 32 bit binary number to gray code. i know how to do
it bit by bit, but i need to do a function to do it, so that i don't
need to repeat code 32 times. Can anybody help me?


If you are doing this in assembly, the these ideas will help you.


32 bits fits into 4 bytes. You want 2 nested loops to do the process.

R0 or R1 can be used to work your way down the bytes as you process.

There is no XRL C,XXX instruction. If you want a little speed, you want
two copies of the inner workings. One for when you are complimenting bits
and one for when you aren't. If your input values happen to be near zero,
you can also include special code for skipping the unused part of the
field.
 
Top