Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

LSBs in a Fraction byte Coding Help

Status
Not open for further replies.

Suraj143

Active Member
Hi I have a 14 bit number I'm right shift this number 6 times (divide by 64).
But when i right shift i need to make a fraction byte which is goes to the carry (without discarding LSB).

Can somebody help me to make a fraction byte using this system.

I'm doing this to detect the final result is a half divide or fully divide one.

So looking at the fraction byte i can detect that.

Ex:26.5,31.5 etc.....

Code:
Divide	bcf	STATUS,C
	rrf	TotalH,F
	rrf	TotalL,F	;divide by 2 
	bcf	STATUS,C
	rrf	TotalH,F
	rrf	TotalL,F	;divide by 4 
	bcf	STATUS,C
	rrf	TotalH,F
	rrf	TotalL,F	;divide by 8
	bcf	STATUS,C
	rrf	TotalH,F
	rrf	TotalL,F	;divide by 16
	bcf	STATUS,C
	rrf	TotalH,F
	rrf	TotalL,F	;divide by 32 
	bcf	STATUS,C
	rrf	TotalH,F
	rrf	TotalL,F	;divide by 64
 
Why can't you just do this:
Code:
Divide
        clrf    Fractionbyte
	bcf	STATUS,C
	rrf	TotalH,F
	rrf	TotalL,F	;divide by 2
        rrf     Fractionbyte,F
	bcf	STATUS,C
	rrf	TotalH,F
	rrf	TotalL,F	;divide by 4 
        rrf     Fractionbyte,F
	bcf	STATUS,C
	rrf	TotalH,F
	rrf	TotalL,F	;divide by 8
        rrf     Fractionbyte,F
	bcf	STATUS,C
	rrf	TotalH,F
	rrf	TotalL,F	;divide by 16
        rrf     Fractionbyte,F
	bcf	STATUS,C
	rrf	TotalH,F
	rrf	TotalL,F	;divide by 32 
        rrf     Fractionbyte,F
	bcf	STATUS,C
	rrf	TotalH,F
	rrf	TotalL,F	;divide by 64
        rrf     Fractionbyte,F
 
Last edited:
Hi kchriste Thanks for your coding.So how can I detect whether it is a halfly divided one Ex 26.5 etc....Only one bit will set in the Fractionbyte isn't it.So what bit to check in the Fractionbyte?.

Thanks you.
 
When the fraction byte equals 128 (0x80) then the result is ending in 0.5 decimal. The fraction byte is really called the "remainder" in math and in your case will contain a number between 0-255 depending on what was in TotalH and TotalL when you started.
 
Last edited:
Hi kchriste thanks for your guidence.I'll use your method for sure.

just a small question.In your upper part if there were no carry occurs in the (rrf TotalL,F) does it make bit0 set in the Fractionbyte?

Code:
Divide
        clrf    Fractionbyte
	bcf	STATUS,C
	rrf	TotalH,F
	rrf	TotalL,F	;divide by 2
        rrf     Fractionbyte,F
 
Because you are rotating right with the rrf instruction, the carry bit (From the previous rrf TotalL,F instruction) goes into bit7 of Fractionbyte. Bit1 of Fractionbyte moves into bit0 of Fractionbyte during the rrf Fractionbyte,F instruction. If you think of TotalH:TotalL:Fractionbyte as a 24bit number being rotated right, by the three consecutive rrf instructions, you'll be on the right track.
 
Last edited:
Hi now I got the point. Thanks for that.

For example
If the total value let say 1769 then the binary = b’11011101001’.

When I right shift this number 6 times divide by 64.
Then the fraction byte = decimal 164 = b’10100100’

The bit 7 of fraction byte already is set then what about the rest of the bits do I need to check them as well or do I need to check only bit 7 of fraction byte to determine is it a halfly divide one?

Thanks for your support.
 
If the entire Fractionbyte is equal to 0x80 then the number is an exact multiple of 0.5. In other words, if bit7 of fraction byte is set and bits 0-6 are all clear, then the number in Fractionbyte equals 0.5. ie: the decimal equivalent of the fraction byte is Fractionbyte/256.
 
Hi kchriste now i totally understood.Your new formula is really helpful.So I can detect the decimal available in the Fractionbyte using your formula.

Thanks for your help.That is really helpful.

Bye.
 
Ok guys, I have a question. If we only want to look at the most significant bit of the 6-bit fractional portion of this 14-bit number wouldn't it be easier to simply multiply by 4 (shift the word 2-bits to the left) which would leave the 8-bit integer portion of the number in the hi byte and the most significant bit of the 6-bit fractional portion of the number in bit 7 of the lo byte?

Code:
;
;  multiply result by 4
;
;  --IIIIII IIFFFFFF -> IIIIIIII FFFFFF--
;
;  8-bit Integer result in TotalH
;  6-bit Fractional result in TotalL (fraction = TotalL << 2)
;
        clrc                    ;
        rlf     TotalL,F        ;
        rlf     TotalH,F        ;
        clrc                    ;
        rlf     TotalL,F        ;
        rlf     TotalH,F        ;
 
Hi Mike, K8LH I wrote your coding in a book & experimented & its giving good results.

In you method i don't need to divide upto 64 & no need to rotate the fractionbyte as well.Simply I can multiply by 4 the 14bit result.

So the integer part is in the TotalH & the fraction byte is in the TotalL.

Why you telling only we can check the msb in the fraction (TotalL) I think in your method the whole (TotalL) contains the fractional part.
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top