Shop OBEX P1 Docs P2 Docs Learn Events
Decimal to 7 Bit Value — Parallax Forums

Decimal to 7 Bit Value

Chris SpaconeChris Spacone Posts: 7
edited 2009-07-08 00:31 in BASIC Stamp
Apologies if this question has been asked and answered, I haven't been able to find anything that quite addresses my need. I want to convert a decimal number with a maximum value of 127 into a 7 bit binary value. The application is pretty straightforward; I have an MP3 player that will play back any one of 127 individual files based on the binary value presented at the interface. The file bits are loaded to the interface and allowed to settle for several tens of milliseconds then the 8th bit is used to strobe the playback.

I could use a LUT but that is inelegant, I'm sure that there is an algorithmic way of addressing the problem. I'm no programmer but I'm not lazy either. I'd like some direction to existing documentation or methods so that I can stop stumbling around.

Thanks,
Chris

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
TWSRO schmismo! Low level scanning is the key to total field perception!

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2009-07-07 19:56
    What do you mean by "decimal number"? I ask because, within the Stamp, all values are just numbers. If you copy a number to a group of 7 I/O pins, it's already a 7 bit binary value at that point. Look in the Basic Stamp Syntax and Reference Manual on page 83 for the names for various combinations of groups of I/O pins. Since there's no group of 7 I/O pins, you'd typically use a group of 8 I/O pins with the 7 lower numbered pins containing the 7 bit value and the 8th pin would be the strobe. When you set the 8 pin group to a number from 0 to 127, the 8th bit would be zero (low) and the 7 bits would contain the number's value. You'd then use a HIGH, then LOW statement to generate the strobe. If you need some delays in there, you'd use a PAUSE statement.

    Say your MP3 player is connected to I/O pins 8 to 15 with the file number on pins 8 to 14 and the strobe on pin 15. You'd have something like:
    Initialization ...
    DIRH = $FF ' Make sure the I/O pins are all outputs

    Start track 37
    OUTH = 37
    PAUSE 10 ' Allow to settle for 10ms
    HIGH 15 ' Take strobe high
    PAUSE 1 ' Leave high for 1ms
    LOW 15 ' Take strobe low

    Note: This is considered introductory Stamp programming. There's nothing fancy about it. In addition to the Manual, you should download and read the "What's a Microcontroller?" tutorial.

    Go to Parallax's main webpage and click on the "Resources" tab. In the list of links that shows up, click on "Downloads", then "Stamps in Class Downloads". To get the Manual, go back to "Downloads", then click on "Stamp Documentation".

    Post Edited (Mike Green) : 7/7/2009 8:09:24 PM GMT
  • Chris SpaconeChris Spacone Posts: 7
    edited 2009-07-07 22:01
    Mike,

    I'm using 8 parts from ( http://store.gravitech.us/i2c16gpex.html ) to form a 128 port GPI expander. The application is a bit esoteric but basically I am attempting to have any given input of the 128 mapped to a corresponding *.MP3 file. Closing a GPI on the expander chip sets a value that corresponds to the input. From what I gather the chip is capable of outputting a decimal value that corresponds to the appropriate pin. So for example if the GPI pin 3 had an active state one could read the chip using I2C and obtain a decimal value. In addition this expander has an interrupt of sorts. Basically if a pin changes state (for example, if set to an input and loaded with a zero which subsequently toggles to a one) the interrupt pin goes high. This pin stays high until the chip is read. So what I was planning is to have an outer loop that scans all eight interrupt pins. When a state change is detected in inner set of loops store the signalling chips 'base value' (for example the first chip base value is '0', the second is 16 and so on) and an I2C routine is called to poll the expander chip. The value read from the chip is then added to the base value and converted to BCD where it is output to 7 pins on the BS2. The 8th pin is the strobe which then directs the player to play back the corresponding file.

    I chose the 'interrupt' strategy because I felt that the system would probably respond faster and would do far less I2C IO doing it this way. So, that is what I meant by a "decimal number'. So reading up on OUTH reveals to me that I was grossly overthinking the problem. Page 177 of the "What's a Microcontroller" reveals that the binary number %00001100 is decimal 12. So by the same reasoning the value 127 = %01111111. I was initially confused on the DIRH=$FF until I realized that you were using Hex FF which is all 1's.

    Does this sound like I understand what you are trying to tell me?

    -Chris

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    TWSRO schmismo! Low level scanning is the key to total field perception!
  • Mike GreenMike Green Posts: 23,101
    edited 2009-07-07 22:50
    It sounds like you understand. I think you've basically got a good idea. Try it out on a breadboard and use DEBUG statements to make sure it works the way you think it should.
  • Chris SpaconeChris Spacone Posts: 7
    edited 2009-07-08 00:31
    Mike,

    This same strategy should hold for the BS2p version of chips as well, correct? My understanding of the 40 pin version is that there is a dedicated auxiliary IO port that behaves in an identical fashion to the BS2. Is my understanding correct on this point as well? I needed the 40 pin version because of the I2C, the 8 'interrupts' and the 8 outputs to the MP3 player.

    Chris

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    TWSRO schmismo! Low level scanning is the key to total field perception!
Sign In or Register to comment.