# Display number in a register on an LCD

Status
Not open for further replies.

#### Nis

##### New Member
I am currently working with PIC microcontroller 16F877A. I am only allowed to work in Assembly (nothing high level like C). I have a number saved in a register. I need to display it on the LCD.

I was thinking of separating the digits, then sending the ASCII value of each digit. The only issue is that the value is of course stored in binary. I am stuck. Does anyone have any ideas.

Like for example, if what I have saved in the register is 168, then I would separate it to 1 and 6 and 8 then send the ASCII values of each of those.

#### OBW0549

##### Active Member
I was thinking of separating the digits, then sending the ASCII value of each digit. The only issue is that the value is of course stored in binary. I am stuck. Does anyone have any ideas.
You might try Googling on "binary to BCD conversion" to look at methods for converting a binary number to its BCD equivalent. This one looks pretty good.

Once you have the number represented as 4-bit BCD digits, you can then add 0x30 to each digit to form the corresponding ASCII characters.

#### Nigel Goodwin

##### Super Moderator
You might try looking at my tutorials, which contain the examples you need.

#### Nis

##### New Member
You might try Googling on "binary to BCD conversion" to look at methods for converting a binary number to its BCD equivalent. This one looks pretty good.

Once you have the number represented as 4-bit BCD digits, you can then add 0x30 to each digit to form the corresponding ASCII characters.
Thanks! Do you understand why would do this step though? "If any column (100's, 10's, 1's, etc.) is 5 or greater, add 3 to that column."

#### OBW0549

##### Active Member
Thanks! Do you understand why would do this step though? "If any column (100's, 10's, 1's, etc.) is 5 or greater, add 3 to that column."
Because it works.

#### Mike - K8LH

##### Well-Known Member
One resource you might check out is the PICLIST Source Code Library. You'll find many different radix conversion routines in this particular section.

Have fun. Cheerful regards, Mike

#### Mike - K8LH

##### Well-Known Member
Here's an example subroutine to convert an 8-bit binary input to three packed BCD digits;
Code:
;******************************************************************
;                                                                 *
;  8 bit to 3 digit half-packed BCD, Mike McLaren, K8LH (Jan-09)  *
;                                                                 *
;   input: WREG, 0x00..0xFF, 0..255                               *
;  output: tens, 0x00..0x25, packed bcd hundreds and tens         *
;          ones, 0x00..0x09                                       *
;                                                                 *
;  12 words, 2 variables, 12/14/16 bit core devices               *
;                                                                 *
Bin2Bcd
clrf    tens            ;
decf    tens,F          ; preset 'tens' to -1
div10   movwf   ones            ;
incf    tens,F          ; bump 'tens', 0x00..0x25
movlw   6               ; using "packed bcd" format
addwf   tens,W          ; bcd "digit carry"?
skpndc                  ; no, skip, else
movwf   tens            ; fix 'tens'
movlw   10              ; ones = ones - 10
subwf   ones,W          ; borrow?
bc      div10           ; no, branch, else
Here's one that's isochronous (it takes the same amount of time to process any input value) which can be handy in certain situations;
Code:
;******************************************************************
;
;  8-bit Binary to 3 digit half-packed BCD (isochronous)
;
;   input: WREG, 0x00..0xFF, 0..255
;  output: tens, 0x00..0x25, packed bcd hundreds and tens
;          ones, 0x00..0x09
;
;  26 words/cycles (isochronous), not including call and return
;
Bin2Bcd
clrf    tens            ;
addlw   -200            ; W = W - 200
rlf     tens,F          ; pick up Carry result
btfss   tens,0          ; borrow? no, skip, else
rlf     tens,F          ; pick up Carry result
btfss   tens,0          ; borrow? no, skip, else
rlf     tens,F          ;
btfss   tens,0          ;
rlf     tens,F          ;
btfss   tens,0          ;
rlf     tens,F          ;
btfss   tens,0          ;
rlf     tens,F          ;
btfss   tens,0          ;
movwf   ones            ;
return                  ;
You would still need code to convert the decimal BCD values (0..9) to ASCII ('0'..'9') values before sending those values to your LCD.

Last edited:

#### jakeselectronics

##### Member
I'm working on a project that involves doing this..
I can post the source code tonight.
What have you got working so far?
Have you got the LCD driver routines going or do you need help with that too? Are you using the Hitachi 44780 type LCDs?

Status
Not open for further replies.