Read 2 or more pins as binary bits

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
User avatar
devnull
Posts: 206
Joined: Sat Jan 07, 2017 1:52 am
Location: Singapore / Cornwall
Contact:

Read 2 or more pins as binary bits

Post by devnull » Mon Jun 04, 2018 5:29 am

Is there a simple was to combine 2 or more pins together as a virtual port and read as binary bits ?

I am using a PIC micro to do some things outside of micropython and one of the things it will do is to output a 2 bit value (0 to 3) which I would like to read in micropython.

OutoftheBOTS_
Posts: 314
Joined: Mon Nov 20, 2017 10:18 am

Re: Read 2 or more pins as binary bits

Post by OutoftheBOTS_ » Mon Jun 04, 2018 7:15 am

I would assume that you would do something like this

Code: Select all

two_bits = pin1.value() <<1 | pin2.value()

User avatar
pythoncoder
Posts: 2661
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Read 2 or more pins as binary bits

Post by pythoncoder » Mon Jun 04, 2018 10:55 am

That would be my approach.

If speed is vital it is possible to access multi-bit I/O but it isn't for the faint-hearted; it certainly needs a study of the chip datasheet. An example may be found here in the file TFT_io.py.
Peter Hinch

Post Reply