An elegant way to reverse 8 bits on the smaller chips?

SgtB

Member
Hello everyone,

I'm trying to port Hippy's DS1302 code to an 18M2. Basically the way you pull data out of the DS1302 is to do a REV 8, and then BCD2BIN on each byte. I've figured out BCD to BIN, and BIN to BCD substitutes for the X2 commands.

I can replace REV with my included code, but I'm wondering if anyone knows a better way. My bit math is a little basic. Thanks!

Code:
Code I'm replacing: 
secs  = secs  Rev 8 : secs  = BcdToBin secs


My REV replacement: 
bit8 = bit7
bit9 = bit6
bit10 = bit5
bit11 = bit4
bit12 = bit3
bit13 = bit2
bit14 = bit1
bit15 = bit0
 

Buzby

Senior Member
Seriously now, I don't know hippy's code, but are the bits pulled in by bit-banging ?.

If so, just bit-bang the other way, then you don't need to reverse.
 

hippy

Ex-Staff (retired)
b0 = b0 REV 8 : b0 = BCD2BIN b0

b1 = bit0 * 2 + bit1 * 2 + bit2 * 2 + bit3 * 10
b0 = bit4 * 2 + bit5 * 2 + bit6 * 2 + bit7 + b1
 

SgtB

Member
b0 = b0 REV 8 : b0 = BCD2BIN b0

b1 = bit0 * 2 + bit1 * 2 + bit2 * 2 + bit3 * 10
b0 = bit4 * 2 + bit5 * 2 + bit6 * 2 + bit7 + b1
Is this just a reverse, or is that the entire operation? Lookup is nice, but I can't index "value". I'll have to time the code and see if a couple of loops are slower or faster than 8 lets.

Thank you for looking at this. I really appreciate the original code you wrote too. It's really the only HSPI code available. I got it working on a 28X1 easy, but am trying to prune all the X code out of it for the M chips without resorting to bit banging.
 

vttom

Senior Member
I think this is equivalent to bit8=bit7: bit9=bit6 etc..

b1 = bit0*128 + bit1*64 + bit2*32 + bit3*16 + bit4*8 + bit5*4 + bit6*2 + bit7
 

vttom

Senior Member
Oh, wait.... I forgot about PICAXE's order of operations... Hippy's example from before was a complete BCD2BIN function with backwards bits...

Well done, sir. Elegant, indeed!
 
Top