this is probably easy to do but i cant think how at the moment.
how could i take an 8 bit number in a file and break it down into the 100s
10s and 1s and put the results into three other files so for example if i had 11001011 which is 203, how can i put 2 in the hundreds file, 0 in the tens and 3 in the units file.
Hmm, can you elaborate? What would this be used for (I smell homework :-), and how exactly are your files encoded - is this "11001011" a byte, or 8 bytes (e.g. 31 31 30 30 31 30 31 31)? How should the resulting files be encoded?
I don't think so, unless you have 768 bytes to spare and need to compute this really fast - since there apparently are files involved, the file system operations will use most of the time anyway.
I'd do it like this:
- Divide the value (assuming you already have it "verbatim" and not as "text") by ten. Keep the integer division result and the remainder.
- write the remainder into the current file (the first time you do this, it'll be the units file, then the tens, then the hundreds)
- for the next file, repeat the above with the division result as the new number.
(Another way to do it, if your micro does not have division, would be to subtract 100 until you end up with a value below 100, and count how often you can to do that. Repeat with 10 and 1 keeping the final result of the previous steps. The files are written to in the reverse order in this case.)
this is a home project for a telescope, im making a device that counts the number of times the focus knob is turned and then displayed on 3x 7 segment displays that was why i needed the binary counting number broken down into the 100s 10s and 1s so they can be sent to the correct display segment. the focus knob is geared down and motorised you have the turn it many times for just a slight adjustment so to keep track of different focal points i want to put a counter on it. i can put the number in the EEdata so it wont be lost during powerdown.
Interesting project and apologies for the "assignment assumption" :-)
the
focal
The "files" had me confused, where do they come in? I'd store the current focus setting in the EEdata as a single byte (in case that is the persistent "file" storage), converting to three digits for 7 segment display only as needed. (on the fly)
What programming language are you using? If C, something along the following lines would compute the values to output to the 7 segment decoders given an 8 bit unsigned number:
for(currentSegment = 0; currentSegment < digits; currentSegment++) { SetSegmentValue(currentSegment, number % 10); number /= 10; } }
You would pass 3 for "digits" in this case. (the maximum that makes sense with 8 bit integers. You could also make the currently hardcoded "10" a into a "base" parameter, and alternatively pass 16 to show values in hex - assuming your digit decoding mechanism/hardware supports it)
I am using assembler with a pic 16f84, my idea was to have a register address with the focus count position in it. which is incremented or decremented accordingly. the bit i am stuck on is when i want to send the 3 digit number to the outside world to some 7 segment displays i need to convert the binary number into the 100s
10s and 1s.
I am ok with the input to the pic, counting, driving the display, its just the breaking down the number i am stuck with
I'm not familiar with that chip unfortunately, and its been some time I did assembly (6502, 6809, 8080, Z80, 68000, you can probably tell how long its been).
Had a quick glance at the datasheet - no division instruction, so method number 2 (subtract 100 as long as value is larger than 100, keep track of how many times that worked, this is digit 2, repeat same principle for 10 and 1 yielding digits 1 and 0 respectively) could work.
In pseudo-assembler:
reserve three registers or memory locations for the 100s, 10s and 1s values. An additional tempory location (maybe that weird "W" register that seems to play the role of a "wrong side of the equation accumulator" :-) is needed for computation.
"Code" for the first (leftmost) digit clear register for 100s digit loop100: subtract 100 from current value if result is negative jump to done100
increment register for the 100s digit goto loop100
done100: add 100 to current value (to recover the value before it got negative, may need to clear carry here before proceeding)
repeat code above for loop10/done10 and loop1/done1 accordingly.
You should end up with the 3 7 seg values in the three registers.
Hope this helps / makes sense / actually works, good luck! Regards, Gilles.
You should have just said this to begin with. ;-) To use the following code, just put your binary value into binX, binU, binH, and binL. Then "call binary_to_bcd". The output will be stored as two BCD digits per byte. To turn the values into ASCII, you can simply "addlw 0x30" for each 4-bit BCD value. This code treats the 32-bit input as unsigned.
ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here.
All logos and trade names are the property of their respective owners.