The mcp23017 is really a nice piece of electronics especially when you need extra I/O's and you need a mix of outputs and inputs, now there is a lot of papers to discuss how to connect it to a bunch of microcontrollers ATMEL , PIC, Arduino or even raspberry pi.
but i will discuss here how to connect it to pokeys under linux environment and to be specific under Linuxcnc.
This tutorial won’t be here if this thread wasn't there
I would like to thank "fixer" for his work on this topic.
Step 1: Communicating With Mcp23017:
Now the mcp23017 is easy to work with but you must know a couple of things beforehand. a look in the datasheet wouldn't hurt at all, but who will read a dozen of pages to know just three lines:
- Set port direction.
- Set pull up resistors
- Read or write to that port.
Step 2: Some Basic Extra Information:
So as you may noticed connecting the mcp23017 to pokeys doesn't reserve any pins
with this configuration the address of mcp is 0x20 hexadecimal when all address pins a[0..2] are connected to ground, reset pin must be always connected to +5V unless you need reset it externally.
The MCP23017 has two 8 bits ports GPA and GPB these two ports as we said before needs to be initialized first to select IO direction, then if you select some of the IO's to be an input it's a good idea to use the internal programmable pull-up resistors, then you have to send a write command or send a read command to the mcp23017.
the table shows all registers that can be accessed in the mcp23017
whats really important is the
first two columns, the last column is just for knowing the default values as the mcp23017 is powered up or reseted.
Step 3: Setting Up the PoKeys Part:
Now pokeys is really designed to work on windows and the support for Linux is really limited but referring to this page i could have the cross platform library:
after downloading the library refer to installation section, in short some libraries must be installed first USBlib-1.0
sudo apt-get install libusb-1.0-0
sudo apt-get install libusb-1.0-0-dev
then install pokeys library :
sudo make -f Makefile.noqmake install
Step 4: Communicate With Mcp23017 Through PoKeys:
this simple example shows you how
to call the mcp23017 when it's connected to PoKeys.
address of the mcp23017 is 0x20 so pay attention if have other addresses .
the build code will be something like this:
gcc -Wall -o "i2c" "i2c.c" -lPoKeys -lusb-1.0 -std=gnu99
Step 5: Linuxcnc Part:
Compile this file use the following command:
sudo halcompile ./pokeys.comp
Then use this following big chunk of code to make the binary
gcc -Wall -Os -g -I. -I/usr/include/libusb-1.0 -I/usr/include -L/usr/lib/ -L/usr/lib/arm-linux-gnueabihf/ -lPoKeys -lusb-1.0 -I/usr/realtime-3.4-9-rtai-686-pae/include -I. -I/usr/realtime-3.4-9-rtai-686-pae/include -I/usr/include/i386-linux-gnu -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=0 -fno-math-errno -funsafe-math-optimizations -fno-rounding-math -fno-signaling-nans -fcx-limited-range -mhard-float -DRTAI=3 -fno-fast-math -mieee-fp -fno-unsafe-math-optimizations -DRTAPI -D_GNU_SOURCE -Drealtime -D__MODULE__ -I/usr/include/linuxcnc -Wframe-larger-than=2560 -URTAPI -U__MODULE__ -DULAPI -Os -o pokeys ./pokeys.c -Wl,-rpath,/lib -L/lib -llinuxcnchal
Then move the binary file to place that linuxcnc can reach:
sudo mv ./pokeys /usr/bin/pokeys
Step 6: Finding Pokeys Serial
you can get it through several ways:
-Windows : just open PoKeys program and when you connect to it will show the ID of your PoKeys
-Linux : use the following command to find it
scroll down till you find somthing related to PoKeys
Note the ID is mention in "U" line use the last five digits.
Step 7: Test the Comp File
To test our setup we will use halrun command, open a terminal window:
halcmd: loadusr pokeys
//to see all pins note there will be all pokeys pins and external ios from mcp23017
halcmd: show pin
To start your comp
setp pokeys.0.devSerial 12345
replace 12345 with device serial found from previous step.
the next step to make sure it's running and check signals
Step 8: Testing the Comp File
but how can be sure that it's really started, ok we need some kind of scope to see it, don't worry no need to buy fancy stuff it's all there you know it we can use halscope
then back to the halrun command:
halcmd: loadusr pokeys
halcms: setp pokeys.o.devserial 32832
halcmd: loadrt threads name1=fast period1=50000
halcmd: loadusr halscope
Step 9: Choose How Many Channels
Step 10: Select First Channel to Be Pokeys.0.alaive
Step 11: Then in Run Mode Select Roll and You Will See the Alive Pin Toggling
Step 12: Add Some Extra Channels
add some channels an IO like "pokeys.0.inext-0", and try to toggle it by wiring it to ground ( remember we used the the internal pull-up resistor ) .
Step 13: Final Word
please note the scan rate of this setup not more than10 Hz which is good for buttons lamps or none vital inputs or outputs ( don't use it for emergency or limit switches)