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.

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.
 
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.
 
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."
 
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               *
;                                                                 *
        radix   dec
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
;
        radix   dec
Bin2Bcd
        clrf    tens            ;
        addlw   -200            ; W = W - 200
        rlf     tens,F          ; pick up Carry result
        btfss   tens,0          ; borrow? no, skip, else
        addlw   200             ; add 200 back
        addlw   -100            ; subtract 100
        rlf     tens,F          ; pick up Carry result
        btfss   tens,0          ; borrow? no, skip, else
        addlw   100             ; add 100 back
        addlw   -80             ;
        rlf     tens,F          ;
        btfss   tens,0          ;
        addlw   80              ;
        addlw   -40             ;
        rlf     tens,F          ;
        btfss   tens,0          ;
        addlw   40              ;
        addlw   -20             ;
        rlf     tens,F          ;
        btfss   tens,0          ;
        addlw   20              ;
        addlw   -10             ;
        rlf     tens,F          ;
        btfss   tens,0          ;
        addlw   10              ;
        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:
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.

New Articles From Microcontroller Tips

Back
Top