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.

Find Individual Digits from two Digit Number

Status
Not open for further replies.

Overclocked

Member
Whats the best way to display the individual digits of a two digit number? Im sure that Ive gotten the display routine down, but I have to figure out how to extract each digit from a dual digit number. I want One display to display the 10s, and the other to display the ones. Eventually this will be used in a PIC micro to display data Via 7 Segment Display or BCD.

This is what I came up with in Labview, but I think theres a simpler way to do this:

digits-jpg.22894


Number is the Incoming number, 10s is the 10s digit and 1s is the ones digit. It works, Except for Negative Numbers. If I need to display Negative numbers, I can only display digits -1 to -9 (two digit display, left most display will be the negative sign).
 

Attachments

  • digits.JPG
    digits.JPG
    29.5 KB · Views: 444
Last edited:
How is the number stored?

What microcontroller are you using?

What language are you writing in?

oppse, forgot that info.

PIC18F1320, Swordfish Basic, and For now, the number in question (DIGIT is what Im calling it) would be a byte. If I wanted negative numbers, then ShortInt (-127 to 127 iirc)
 
Last edited:
This is in assembly, not basic. I expect it can be included within basic.

This is what I use. w is split into msb and lsb base 10

The routine works like this:-
It divides by 10, takes the whole number value as the msb.
10 times the msb is subtracted from the original number to leave the lsb.

The dividing by 10 is done by multiplying by 51, and dividing by 256 and then 2. Dividing by 256 is done by taking the top byte of the result and dividing by 2 is done by a rotate. The 52 is added to prevent rounding errors.


Code:
         movwf     lsb           ;store number in temp
         mullw      d'51'             ;multiply by 51
         movlw     d'52'
         addwf     prodl, f          ;add 52      
         movlw     0x00             ;deal with carry
         addwfc    prodh, f         ;this will clear the carry bit
         rrcf         prodh, w        ;now w is the number/10
         movwf     msb              ;store the msb
         mullw      d'10'              ;multiply that by 10
         movf       prodl, w         
         subwf      lsb, f             ;subtract from the original number tp leave the lsb
 
why not shift over?

Bitwise Operators
>> and <<

Shift 4 bits >> for the first digit
Shift 4 bits << then shift it 4 bits back >> for second digit

As for negative number why not and a LED to indicate if its negative or the . (DP-DOT) of the 7 seg display
Code:
Digit01 = Value >> 4

Digit02 = Value << 4
Digit02 = Digit02 >> 4
 
Last edited:
Well, if the number is a true binary number and not binary coded decimal (BCD) then shifting or swapping and masking is not going to work.

You are correct that you get the tens digit by dividing by 10 and taking the integer result. The remainder is the units digit. It can be obtained as you have by multiplying the tens digit by 10 and substracting from the original number. It can also be obtained by calculating modulo 10 if there is a modulo function.
 
Well, if the number is a true binary number and not binary coded decimal (BCD) then shifting or swapping and masking is not going to work.

You are correct that you get the tens digit by dividing by 10 and taking the integer result. The remainder is the units digit. It can be obtained as you have by multiplying the tens digit by 10 and substracting from the original number. It can also be obtained by calculating modulo 10 if there is a modulo function.

There a operator called MOD, thats under mathematical functions, but I dont know what it does.

ADD: Or, I could work with Dec Numbers and use Convert.bas to convert them to BCD.
 
Last edited:
There a operator called MOD, thats under mathematical functions, but I dont know what it does.

Why not try it and find out. It probably takes two arguments, the first would be the number you are working with and the second in your case would be 10.
 
Why not try it and find out. It probably takes two arguments, the first would be the number you are working with and the second in your case would be 10.

Ive found a thread over on the swordfish forum, **broken link removed**

Said Thread gave me a couple Ideas, so Now Im looking through Math.bas (a bunch of math functions). Theres a function that truncates the decimal of a number, ie 2.7 --> 2. Thus for the 10s digit I can do this (and it works for negative numbers):

pseudo code
....
Digit = Digit/10
truncate Digit
Display Digit //10s digit
.....
1s digit will probably follow the way that I did it in labview.

ADD: Even better Idea, two Digits will allow me to go from 0 to 99, so what if I use a "rectangular" led to indicate a negative? It would save On Code, and I could use a simple if statement to determine if the number is positive or negative
 
Last edited by a moderator:
Duh, I didnt even think of looking through one of the modules that come with swordfish basic. In case any one wants to do the same, look under Utilities.bas. The function in question is called Digit (which, ironically, PBCpro shares also).

Code:
function   Digit(pValue as TType, pIndex as byte) as byte
 
pValue - Input value. 
pIndex - Digit index.
 
Return the value of a decimal digit. For example
Digit(123,3)
will return the number 1. TType can be byte, word or longword.
:duh:
 
I didnt want to waste a topic. anyone know how to do this in C18?

Example: Input would be 0x0A need to seperate to 2 variable

var1 = 1
var2 = 0

How is it done in C18.

EDIT
Felling kinda stupid the above post had the info:
Code:
        t_temp = minutes;
        min_h = t_temp / 10;
        min_l = t_temp - (min_h * 10);

        t_temp = hours;
        hour_h = t_temp / 10;
        hour_l = t_temp - (hour_h * 10);
 
Last edited:
I didnt want to waste a topic. anyone know how to do this in C18?

Example: Input would be 0x0A need to seperate to 2 variable

var1 = 1
var2 = 0

How is it done in C18.

Maybe like this.

var10s = input/10;
var1s = input%10;

for a 3 digit number

var100s = input/100;
input = input%100;
var10s = input/10;
var1s = input%10;

for a larger number use a loop.

But maybe you could find a routine to convert to BCD.
 
Last edited:
3v0:
Code:
        t_temp = minutes;
        min_h = t_temp / 10;
        min_l = t_temp - (min_h * 10);

        t_temp = hours;
        hour_h = t_temp / 10;
        hour_l = t_temp - (hour_h * 10);
and to BCD (DS1337)
Code:
    minutes = min_h;
    minutes = minutes << 4;
    minutes = minutes | min_l;
    rtc_write(0x01,minutes);

    hours = hour_h;
    hours = hours << 4;
    hours = hours | hour_l;
    hours = hours | 0b01000000;
    rtc_write(0x02,hours);
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top