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.

value multiplied 256

Status
Not open for further replies.

savnik

Member
How to change the below code to show the results on LCD multiplied 256.
Code:
;---------------------------------------------------------------------------
; Bin To BCD conversion ( 32 bit word -> 8 digits )
; Bin in Result, Result+1, Result+2, Result+3
; BCD in BCD3, BCD2, BCD1, BCD0
Bin2BCD
           clrc
           Movlf      .32,N                 ; Init Loop counter
           clrf       BCD3                  ; Init result
           clrf       BCD2
           clrf       BCD1
           clrf       BCD0
LoopBB                                      ; For N = 1 To 32
           rlf        Result+3,f            ; Shift bin value (LSB first)
           rlf        Result+2,f            
           rlf        Result+1,f
           rlf        Result,f
           rlf        BCD3,f                ;  Shift BCD value (LSD first)
           rlf        BCD2,f
           rlf        BCD1,f
           rlf        BCD0,f
           decfsz     N,f
           goto       DecAdj                ;     Decimal adjust
           return

DecAdj
           Movlf      BCD3,FSR              ; Init pointer
           call       BCDAdj                ; BCD adjust
           Movlf      BCD2,FSR              ; Init pointer
           call       BCDAdj                ; BCD adjust
           Movlf      BCD1,FSR              ; Init pointer
           call       BCDAdj                ; BCD adjust
           Movlf      BCD0,FSR              ; Init pointer
           call       BCDAdj                ; BCD adjust
           goto       LoopBB
BCDAdj
           movlw      3
           addwf      INDF,w
           movwf      Temp
           btfsc      Temp,3                ; Test if result > 7
           movwf      INDF
           movlw      30H
           addwf      INDF,w
           movwf      Temp
           btfsc      Temp,7                ; Test if result > 7
           movwf      INDF                  ; Save as MSD
           return

;--------------------------------------------------------------------------
; Display Frequency stored in BCD3, BCD2, BCD1 and BCD0 (LSD)
;OK
DispFreq
           DDRam      3
           swapf      BCD0,W
           andlw      0FH
           call       DispHex
           movf       BCD0,W
           andlw      0FH
           call       DispHex
           Disp       '.'

           swapf      BCD1,W
           andlw      0FH
           call       DispHex
           movf       BCD1,W
           andlw      0FH
           call       DispHex
     ;DDRam      0x40
           swapf      BCD2,W
           andlw      0FH
           call       DispHex
           Disp       '.'

           movf       BCD2,W
           andlw      0FH
           call       DispHex
           swapf      BCD3,W
           andlw      0FH
           call       DispHex
           movf       BCD3,W
           andlw      0FH
           call       DispHex
                 
           return

;---------------------------------------------------------------------------
; Display hex digit
; input : w = hex
; OK
DispHex
           addlw      -0AH
           SkipNC
           addlw      7
           addlw      3AH
           call       SEND_CHAR

           return
 
To multiply by 256 is EXTREMELY trivial, you have a four byte number (for 32 bits), throw the MSB away, and move the other three up, replace the LSB by zero. As simple as that!.
 
This is like asking how to multiply by 100,000,000, it's really simple just count the zeros and add to the number.

The same is true for binary. You ask how to multiply by 256. Well, 256 in binary = 100,000,000 so, we add 8 zeros to the right of the number.

EG,35*256
35 = 10011
35*256 = 1001100000000 = 0x1300 = 8960

Because most processors store numbers 8 bits at a time then to multiply by 256 is accomplished by moving the bytes left one and adding a zero.

Hope that makes sense.

Mike
 
Mike,
With your vast knowledge, I'm sure you could offer more advise than that.

Mike.
 
I probably could if I had more time. I'm at a Truck Stop and only have a few minutes on-line (grin).

Mike
 
Nigel Goodwin said:
Doesn't the routine you posted do that? - if not it's of no use to you whatsoever.
Yes , this routine display the number on the LCD in decimal.
This routine is from a frequency meter.
I put a prescaler:)256) in front of frequency meter.
The LCD now show : 5.470.703 Hz and i want to show : 1400.5 Mhz
 
Just make sure the 4 byte 32-bit number is not higher than 24-bits before multiplying by 256. Any higher than 24-bits and the result will overflow pass 32-bits and not functionally fit in the 8 digit BCD conversion.

Code:
16,777,215 * 256    = 4,294,967,040
0XFF FFFF  * 0X100  = 0XFFFF FF00
24-bits    + 8-bits = 32-bits  	


16,777,216 * 256    = 4,294,967,296
0X100 FFFF * 0X100  = 0X1 0000 0000
25-bits    + 8-bits = 33-bits

You can try use 40-bit to 10 digit BCD, multiply the 8 digit BCD by 256 and take care of the overflow yourself, or make sure the 32-bit multiplicand is not greater than 16,777,215.
 
donniedj said:
Just make sure the 4 byte 32-bit number is not higher than 24-bits before multiplying by 256. Any higher than 24-bits and the result will overflow pass 32-bits and not functionally fit in the 8 digit BCD conversion.

Code:
16,777,215 * 256    = 4,294,967,040
0XFF FFFF  * 0X100  = 0XFFFF FF00
24-bits    + 8-bits = 32-bits  	


16,777,216 * 256    = 4,294,967,296
0X100 FFFF * 0X100  = 0X1 0000 0000
25-bits    + 8-bits = 33-bits

You can try use 40-bit to 10 digit BCD, multiply the 8 digit BCD by 256 and take care of the overflow yourself, or make sure the 32-bit multiplicand is not greater than 16,777,215.
The max number is 6.500.000 . smaller than 24-bits.
 
If that is the max, then you should be good to go. Take what Nigel gave you and VOILA!!
 
Just move your bytes. If your bytes are B4 (most significant) to B1 then move B3 to B4, B2 to B3, B1 to B2 and clear B1.

Mike.
 
Pommie said:
Just move your bytes. If your bytes are B4 (most significant) to B1 then move B3 to B4, B2 to B3, B1 to B2 and clear B1.

Mike.

EDIT: Posted later update on this finding.

hi Mike,
Looking at his original code subr, it looks as though its converting a 32 binary value into 4, packed BCD bytes, which are unpacked, then displayed as 8, hex characters on the LCD ??
Unless I'm going gaga this dosnt tie in with his later posts,,,

In his later posts he talks of decimal 100.000 MHz, after division by the prescaler as 390.625, a decimal/ascii value?
To keep the accuracy, he will have to read the contents of the prescaler.

What you think.:confused:

EDIT: looking at the hex2lcd subr its formatted as: dd.ddd.ddd
Regards
 
Last edited:
savnik said:
I don't know how to do.
When measure a frequency 100.000MHZ on LCD show : 00.390.625 (because i have the prescaler :256 - 0.390625x256 = 100MHZ)
I use an external prescaler U813 (divide : 256)
When measure any frequency without the prescaler show the right frequency on LCD. (show 5.437.376 Hz)
When i put an external divider show on LCD the frequency : 256 (show 00.390.625 instead 100.000.000 Hz)
I want to show on LCD 100.000.000 Hz, but how;
 
Last edited:
hi savnik,
I understand what you are saying regarding dividing the 100 MHz by 256,so that you get 390.625 displayed.

Did you write the program code that you posted in your first post?

Did you try moving the binary value left by 8 bits [ that is 256]?

As suggested you could drop off the MSD 8 bit byte, which is probably all 0's as its a 32bit value.
Decimal 100.000,000 is 05 F5 E1 00 before you have divided by 256,,
after division by 256, its 00 05 F5 E1 so the MSD is 00.

Move all the bytes left by one position, so you have 05 F5 E1 and make the LSD byte 00.

When you have done that call the bin2bcd subr.

You should display 100.000,000
 
hi savnik,

I have just run your code thru my PIC sim.

Loaded it with 100.000/256 then shifted the BCD bytes around, as the previous post described, and called the Bin2BCD subr and it output was 00099.84.

The write to LCD writes the LSD first, so it comes out as 4899000.

The value of 99.84 is what I would expect, because you are not reading/using the contents of the prescaler, [ so you lose the decimal part of the megaHz]

What results do you get???
 
i change the number from 32 to 40

Movlf .32,N ; Init Loop counter
Movlf .40,N ; Init Loop counter

; Bin To BCD conversion ( 32 bit word -> 8 digits )
; Bin in Result, Result+1, Result+2, Result+3
; BCD in BCD3, BCD2, BCD1, BCD0
Bin2BCD
clrc
Movlf .40,N ; Init Loop counter
clrf BCD3 ; Init result
clrf BCD2
clrf BCD1
clrf BCD0

when i measure 99mhz show on lcd 99.000.000
but when i measure 102mhz show on lcd 02.000.000
why ;
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top