Electronic Projects, forums and more.

Go Back   Electronic Circuits Projects Diagrams Free > Electronics Categories > Micro Controllers


Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc.

Reply
 
LinkBack Thread Tools Display Modes
Old 11th October 2008, 10:22 PM   (permalink)
Default Find Individual Digits from two Digit Number

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:



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).
Attached Images
File Type: jpg digits.JPG (29.5 KB, 84 views)
__________________
I believe that you're wrong.
Insinuating that they hold the bomb.
---

Last edited by Overclocked; 11th October 2008 at 10:23 PM.
Overclocked is offline  
Old 11th October 2008, 10:30 PM   (permalink)
Default

How is the number stored?

What microcontroller are you using?

What language are you writing in?
Diver300 is offline  
Old 11th October 2008, 10:48 PM   (permalink)
Default

Quote:
Originally Posted by Diver300 View Post
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)
__________________
I believe that you're wrong.
Insinuating that they hold the bomb.
---

Last edited by Overclocked; 11th October 2008 at 10:49 PM.
Overclocked is offline  
Old 11th October 2008, 11:28 PM   (permalink)
Default

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
Diver300 is offline  
Old 12th October 2008, 12:53 AM   (permalink)
Default

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 by AtomSoft; 12th October 2008 at 01:14 AM.
AtomSoft is offline  
Old 12th October 2008, 01:09 AM   (permalink)
Default

Swap and mask

Search for packed BCD
__________________
Bill
Smart Kits build Smart People

http://www.blueroomelectronics.com
blueroomelectronics is offline  
Old 12th October 2008, 02:09 AM   (permalink)
Default

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.
skyhawk is offline  
Old 12th October 2008, 02:17 AM   (permalink)
Default

Quote:
Originally Posted by skyhawk View Post
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.
__________________
I believe that you're wrong.
Insinuating that they hold the bomb.
---

Last edited by Overclocked; 12th October 2008 at 02:24 AM.
Overclocked is offline  
Old 12th October 2008, 02:42 AM   (permalink)
Default

Quote:
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.
skyhawk is offline  
Old 12th October 2008, 05:21 AM   (permalink)
Default

Quote:
Originally Posted by skyhawk View Post
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, http://www.sfcompiler.co.uk/forum/vi...&highlight=mod

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
__________________
I believe that you're wrong.
Insinuating that they hold the bomb.
---

Last edited by Overclocked; 12th October 2008 at 05:25 AM.
Overclocked is offline  
Old 15th October 2008, 02:17 AM   (permalink)
Default

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 believe that you're wrong.
Insinuating that they hold the bomb.
---
Overclocked is offline  
Old 20th October 2008, 08:27 PM   (permalink)
Default

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 by AtomSoft; 20th October 2008 at 08:54 PM.
AtomSoft is offline  
Old 20th October 2008, 08:57 PM   (permalink)
Default

Quote:
Originally Posted by AtomSoft View Post
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 by 3v0; 20th October 2008 at 08:58 PM.
3v0 is offline  
Old 20th October 2008, 11:02 PM   (permalink)
Default

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);
AtomSoft is offline  
Reply

Bookmarks

Thread Tools
Display Modes



Similar Threads
Title Starter Forum Replies Latest
Please help me to find IC Number smarttechie Electronic Projects Design/Ideas/Reviews 1 30th August 2008 04:15 PM
Individual Cell Discharger mrfunkyjay Electronic Projects Design/Ideas/Reviews 9 6th May 2008 05:14 PM
multiplication of 1 digit BCD with 1 digit BCD David84 Electronic Projects Design/Ideas/Reviews 3 18th October 2006 04:21 PM
Need to find AR RF amp transistor part number. TheOne Datasheet/Parts Requests 0 24th January 2005 06:46 PM
Monitoring individual 20 amp breakers mesican General Electronics Chat 10 21st February 2004 02:16 AM



All times are GMT. The time now is 04:17 AM.


Electronic Circuits  |  Learning Electronics
Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.

eXTReMe Tracker