![]() | ![]() | ![]() |
| | |||||||
| Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc. |
| | LinkBack | Thread Tools | Display Modes |
| | (permalink) |
| 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).
__________________ I believe that you're wrong. Insinuating that they hold the bomb. --- Last edited by Overclocked; 11th October 2008 at 10:23 PM. | |
| |
| | (permalink) |
| How is the number stored? What microcontroller are you using? What language are you writing in? | |
| |
| | (permalink) | |
| Quote:
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. | ||
| |
| | (permalink) |
| 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 | |
| |
| | (permalink) |
| 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
__________________ AtomSoftTech.com My YouTube Videos! My Favorite Stores: dipmicro Electronics SparkFun Electronics Futurlec BG Micro Last edited by AtomSoft; 12th October 2008 at 01:14 AM. | |
| |
| | (permalink) |
| Swap and mask Search for packed BCD | |
| |
| | (permalink) |
| 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. | |
| |
| | (permalink) | |
| Quote:
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. | ||
| |
| | (permalink) | |
| Quote:
| ||
| |
| | (permalink) | |
| Quote:
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. | ||
| |
| | (permalink) |
| 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.
__________________ I believe that you're wrong. Insinuating that they hold the bomb. --- | |
| |
| | (permalink) |
| 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);
__________________ AtomSoftTech.com My YouTube Videos! My Favorite Stores: dipmicro Electronics SparkFun Electronics Futurlec BG Micro Last edited by AtomSoft; 20th October 2008 at 08:54 PM. | |
| |
| | (permalink) | |
| Quote:
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.
__________________ search engine for electronic partsJunebug USB PIC programmer kit., USB Bit Wacker, 3v0's Homepage The 15 Minute Printed Circuit Board! (+drill time) Last edited by 3v0; 20th October 2008 at 08:58 PM. | ||
| |
| | (permalink) |
| 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); 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);
__________________ AtomSoftTech.com My YouTube Videos! My Favorite Stores: dipmicro Electronics SparkFun Electronics Futurlec BG Micro | |
| |
| Bookmarks |
| Thread Tools | |
| Display Modes | |
| |
| | ||||
| 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 |