Convert an integer to Binary
T Chap
Posts: 4,223
There is an i2c project that requires two bytes followed by the start/address/dir byte. The device is the PCF8575 i/o expander, and in this case, all 16 pins are outputs, and generally only one pin will be high, but that could change.
A serial string is sent to several different Propellers, typically with a value of 0 to 16. A 1 sent to the i2c device should look like this:
'the start byte
%10000000
%00000000
This produces a 1 on pin one of the device. Here is an example of what happens:
The above is only 1 output of 16 to manage out of 3 banks totalling 48 pins, and I think there must be a better way to code this than to create 48 methods for each pin state, 16 for each individualy pin control per bank, and 3 Off methods for each address. I imagine that sending one method a parameter or value, such as Ampin(16) to produce the i2c value below would be best, but don't know a way to produce this value from integer 16. The trick will be turning on more than one pin should that state occur.
%11111111
%11111111
Using the integer that reflects the pin(high state) would really make things nice. It would be just an nice to be able to send the value in word form if I can find a way to break the word in half within the method prior to it sending it to the i2c part.
Can anyone suggest a conversion method to use the integer as value(0-16)? I am open to suggestions on other methods to simplify the process.
Thanks
Post Edited (originator) : 2/1/2007 1:09:44 PM GMT
A serial string is sent to several different Propellers, typically with a value of 0 to 16. A 1 sent to the i2c device should look like this:
'the start byte
%10000000
%00000000
This produces a 1 on pin one of the device. Here is an example of what happens:
PUB AMPIN_on lcd.cls lcd.str(string("Ampin_on")) i2c.i2cStart(25) 'start the transaction i2c.i2cWrite(25,%0100_000_0 + 0) ' this is a write i2c.i2cWrite(25, %10000000) ' send the 1st byte = relay 1 on i2c.i2cWrite(25, %00000000) ' send the 2nd byte i2c.i2cStop(25) PUB AMPIN_off lcd.cls lcd.str(string("Ampin_off")) i2c.i2cStart(25) 'start the transaction i2c.i2cWrite(25,%0100_000_0 + 0) ' this is a write i2c.i2cWrite(25, %00000000) ' send the 1st byte = relay 1 off i2c.i2cWrite(25, %00000000) ' send the 2nd byte i2c.i2cStop(25)
The above is only 1 output of 16 to manage out of 3 banks totalling 48 pins, and I think there must be a better way to code this than to create 48 methods for each pin state, 16 for each individualy pin control per bank, and 3 Off methods for each address. I imagine that sending one method a parameter or value, such as Ampin(16) to produce the i2c value below would be best, but don't know a way to produce this value from integer 16. The trick will be turning on more than one pin should that state occur.
%11111111
%11111111
Using the integer that reflects the pin(high state) would really make things nice. It would be just an nice to be able to send the value in word form if I can find a way to break the word in half within the method prior to it sending it to the i2c part.
Can anyone suggest a conversion method to use the integer as value(0-16)? I am open to suggestions on other methods to simplify the process.
Thanks
Post Edited (originator) : 2/1/2007 1:09:44 PM GMT
Comments
Note that this accumulates overall relay settings so every time you
set a pin on, you'll have to set it off and you can have more than
one relay on at a time. If you turn off pin zero, all pins are turned
off.
I tried to get yours to take all to 0 on pin == 0 but it wouldn't reset, so I just used my old version that outputs all 0's and that is easy enough.
Many thanks.