---------- > From: justin crooks > To: piclist@mivvma.mit.edu > Subject: Re: Conversion of byte to percentage (math corrected) > Date: Thursday, May 21, 1998 11:11 AM > > > > ---------- > > From: justin crooks > > To: pic microcontroller discussion list > > Subject: Re: Conversion of byte to percentage > > Date: Thursday, May 21, 1998 10:58 AM > > > > Or, if you just need a super-fast approximation for a PIC 16C7x or > similar > > (in assembly) > > > > Byte/2 - Byte/8 +Byte/64=100(Byte)/256 > > > > bcf STATUS,C > > rrf BYTE,w ; Byte / 2 > > movwf PERCENT > > > > movwf TEMP > > > > bcf STATUS,C > > rrf TEMP,F rrf TEMP,W > > andlw 0x1F ;Byte / 8 > > subwf PERCENT,W btfsc BYTE,7 addlw 2 btfsc BYTE,6 addlw 1 ;Add Byte / 64 movwf PERCENT > > > > Example: Byte = 0x80 (128) > > divide by 2: 0x40 (64) > > swap into TEMP, rotate left, and erase high 3 bits. TEMP=0x10 (16) > > Subtract this value from percent: 0x30 (48%) Add 2. This gives 50%. 255: 127 - 31 + 3 = 99% > > > > ---------- > > > From: Keith Howell > > > To: PICLIST@MITVMA.MIT.EDU > > > Subject: Re: Conversion of byte to percentage > > > Date: Thursday, May 21, 1998 8:53 AM > > > > > > adrian aslett wrote: > > > > > > > > I have a need to convert a byte value into a percentage. > > > > > > By coincidence, I have to do the exact same thing > > > to show a quantity (0-255) on a display of 0-99 bars. > > > > > > unsigned char cByte; > > > unsigned int nPercent; > > > > > > nPercent = (((int)cByte)*100)/256; > > > > > > and the division by 256 is identical to >>8, > > > or just take the high byte. > > > 0--> 0, > > > 255 -> 99.