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

Reply to
luisdanielsilva
Loading thread data ...

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

The reverse operation takes more effort.

Reply to
Arlet

Thanks but i'm doing this in assembler, and i want to do it bit by bit, not all the bits at once.

Arlet escreveu:

Reply to
luisdanielsilva

Have a look here:

formatting link

It's for a PIC but I'm sure it will be easy to convert.

--
| Brendan Gillatt						|
|  brendan {at} brendan \\removethis// gillatt {dot} co {dot} uk |
|      http://www.brendangillatt.co.uk				|
| PGP Key: pgp.mit.edu:11371/pks/lookup?op=get&search=0x6E265E61|
Reply to
Brendan Gillatt

schreef in bericht news: snipped-for-privacy@h54g2000cwb.googlegroups.com...

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

Reply to
petrus bitbyter

"petrus bitbyter" schreef in bericht news:4557bdb1$0$26967$ snipped-for-privacy@dreader32.news.xs4all.nl...

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

Reply to
petrus bitbyter

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.

--
--
kensmith@rahul.net   forging knowledge
Reply to
Ken Smith

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.