I have successfully connected this expanded to an Arduino and a Pi with no problem but the BBC proved to be very difficult as I could not find much information on programming the devices advanced features. I finally got it working but I am sure there must be an easier way of doing it as my knowledge of Python is limited. The first program reads the ports and the second program flashes an LED connected between port 0 and +V. Comments please.
from microbit import *
while True:
d=i2c.read(0x24, 1, repeat=True)
c=str(d)
tens=ord(c[4])
if tens >= 97:
tens=tens-87
else:
tens=tens-48
units=ord(c[5])
if units >= 97:
units=units-87
else:
units=units-48
val=tens*16+units
display.scroll(str(val),delay=400)
sleep(2000)
__________________________________________
from microbit import *
while True:
buf = bytearray(1)
buf[0]=0
i2c.write(0x24, buf, repeat=True)
sleep(1000)
buf[0]=1
i2c.write(0x24, buf, repeat=True)
sleep(1000)
BBC Micro:bit I2C Port Expander PCF8574
-
- Posts: 463
- Joined: Wed Apr 08, 2015 5:19 am
Re: BBC Micro:bit I2C Port Expander PCF8574
Sorry, I got no help to offer on your actual question, but if you enclose the code in your post with [code]...[/code], it will display properly with indentation and will be much easier to read for someone inclined to look into it.
Re: BBC Micro:bit I2C Port Expander PCF8574
New Users can't use tags since you could also use them to add images. Unfortunately, we received quite a few not very office friendly posts for approval. As soon as a new user reaches the limit which enables him/her to post without approval, tags are activated.
We are working on a solution to activate code tags only, but this is something not standard within the software we are using.
We are working on a solution to activate code tags only, but this is something not standard within the software we are using.
-
- Posts: 463
- Joined: Wed Apr 08, 2015 5:19 am
Re: BBC Micro:bit I2C Port Expander PCF8574
Ok, sorry, I didn't know that. Bit of a shame, really.
BBCode must die!
BBCode must die!
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: BBC Micro:bit I2C Port Expander PCF8574
@gowelding1 I haven't got your hardware so can't test this, but it looks like you're converting an ASCII hexadecimal text string to an integer. This can be done as follows:
In your second example there isn't an obvious need to create the bytearray unless you're planning code which changes the contents dynamically. An alternative is to issue
Here a bytes instance is created each time.
Code: Select all
val = int(c[4:6], 16)
Code: Select all
i2c.write(0x24, b'\0', repeat=True)
i2c.write(0x24, b'\1', repeat=True)
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.