Understanding write implementation of SPI for MCP2S317

I’m working on a project to change the way i output 2 4x4 Membrane keypads
and output 32 bits into a Leo Bodnar BUO836X USB Module.
I have implemented a project using a Mega 2650 scanning the
married keyboard matrix and it works well and switches 32 I/O thru
opto isolators to a hacked Logitech Joystick interface to use in Xplane 11 works well.

Now i want to discard the existing output hardware using the Leo Bodnar expansion board and more inputs to use a single serial link into xplane sim doable :slight_smile:
I have decided to use 2x MCP2S317’s and hardwire them directly to the expansion board.
I have decided to use SPI from the Mega and have read and plagiarized and integrated
the code for the Arduino Mega it’s looking good but ran into a 71 yr old brain stop. lol

I can grasp some of the SPI implemenation and the MCP2S317 library
Simple problem of understanding as some of the code i seemed to have a liking to … much like mine … is un commented.

MCPwrite(addr,val)
eg, MCP.pinmode16(0); all 16 bits of the 2S317 are inputs
MCPwrite(0x0000)

  MCP.write16(0);        sets LOW on all 16 bits in addr 0?

does MCPwrite1(0) set LOW on only 1 bit in addr 0
my comprehension is somewhat confused and the answer is so simple…

Would you please be able to confirm,
Cheers
Ray

It is not clear whether you are using the AdaFruit MCP23017 library or the Arduino MCP2S317 library, but as far as I can tell neither library has a MCP.write() function.

Can you indicate what sort of object MCP is and the source from which it derives the write() function you are using.

Hi Albert,

Welcome to the forum. That sounds like one beefy project you have going there! If you’d like to share photos of your current setup we’d love to have a look!

I have found a third library being used for the MCP23S17. That being said knowing which library you are using will go a long way to solving this problem.

Cheers Jack being new I assumed a lot sorry details are important.
MCP23S17
Library AUTHOR: Rob Tillaart
URL: GitHub - RobTillaart/MCP23S17: Arduino library for SPI based MCP23S17 16 channel port expander
If I paste the code in here I hope it will be clearer.
I wont paste the keypad global setup it works.
Primarily sending a position on the keypad to a corresponding bit to the mux
for USB transmission.

#include "MCP23S17.h"
#include <Keypad.h>

MCP23S17 MCP_A(53, &SPI);  //  HW SPI, address 0 (default)
MCP23S17 MCP_B(49, 1);     //  HW SPI, address 1

void setup() {
  Serial.begin(115200);
  SPI.begin();

  MCP_A.begin();
  MCP_B.begin();
  MCP_A.enableHardwareAddress();
  MCP_B.enableHardwareAddress();
  MCP_B.pinMode16(0);    
  MCP_B.write16(0x0000);  
}
void loop() {
 if (kpd.getKeys()) {
    for (int i = 0; i < LIST_MAX; i++)  //Scan whole keylist
    {
      if (kpd.key[i].stateChanged)  // Only find keys that have changed state
      {
        switch (kpd.key[i].kstate)  // Report active key state IDLE, PRESSED, HOLD, OR RELEASED
        {

          case PRESSED:
             if (kpd.key[i].kchar <= 16) {
              MCP_A.write1(kpd.key[i].kchar, LOW);
            }
            if (kpd.key[i].kchar >16 || kpd.key[i].kchar <=32) 

            { MCP_B.write1(kpd.key[i].kchar, LOW); }  
            break;

           case RELEASED:
            MCP_A.write1(kpd.key[i].kchar, LOW);  //check this out suspect error
            MCP_B.write1(kpd.key[i].kchar, LOW);  //Ditto
            break;
        }
      }
    }
  }
}

 

I’m missing something fundamental keen to follow advice
thank you.
Ray

1 Like

Just a follow on from my last post I think I have found some extra errors in the MCP write commands…duh! in the switch statements…:frowning:
Ray

1 Like

Hi Albert
I too am using a Leo Bodnar BU0836X with X-Plane 10. It accepts 32 buttons and 8x 12bit analog inputs but I don’t see how you could communicate with it using SPI but that is not to say it can’t be done.
I found this device very easy to use and it interfaces with X-Plane seamlessly.
I have 10 rotary encoders. 6 are for ILS and VOR bug setting and tuning, 3 for NDB tuning and 1 for elevator trim. This 10th one also inputs to a NanoEvery to drive a LED display of elevator trim position. The button on this one is connected to an unused button input to quickly return to “take off” trim position
2 of the analog inputs are used for yolk pitch and roll inputs to bypass the Saitek yolk built in dead zone.
The whole thing simply plugs into a USB port. Actually via a powered USB hub. The throttle quadrant and rudder pedals are connected to the Yolk built in USB hub so I have actually got the output of 1 hub as an input of another hub (where I introduce the Leo Bodnar board output) so there is actually only one USB connection to the computer.
The whole thing works well.
I posted details and circuits etc on this forum which might be 2 or 3 years ago now (time flies these days) if you are interested I can see if I can find it or you could search yourself.
Cheers Bob

1 Like

There is quite a bit going on here. One thing I can see is the MCP’s are addressed 0-15.
These statements

kpd.key[i].kchar <= 16

could be

kpd.key[i].kchar <= 15
and
kpd.key[i].kchar <= 31

As for your original question write1() still looks for a pin and a value so write1(0,0) would set the bit in address 0 to low.

write16() uses all of them so there is no need to specify a start point. write8() I am still having a look at.

Hi Albert
Sorry that NanoEvery above should have been ProMini.
Link to that project if you are interested

Project by Robert; Potentiometer Position Monitor

Cheers Bob

1 Like

Excellent setup … I’d seen a lot of sims and my point was to learn and tinker with arduino and just fool about with the element of control.
I was happy not mirror all the controls on the 737-800x Zibo mod just the essential buttons on the MCU and dials…The adjustment of dials and and important stuff is all done seperately using a Teensy 4.0 and a 128x64 GDU I just saw this Leo Bodnar thing and thought I could get rid of hacked Logitech Joystick and CH products Control yoke that cam free gratis from a neighbor.
I have a minimalist control panel lol suits me fine I was more interested in Navigation and the flight computer programming.
I was keen to get Xplane-12 but seems to me as I found out you can’'t update a dvd version and the digital download would wipe out my download. Maybe later
I’m assuming you can configure the BU0836X as a LInux user I don’t see a lot of info on that I have now nuther win 11 puter so we’ll see.
Thanks for the reply now I know someone who has one…
Cheers Robert

1 Like

Thanks for the corrections Jack
Lol I found a few more after I’d posted
such is the way when you cut and past out of a trial page onto another heh.

I had some trouble stating the write question explicitly thru the web.
thanks again…i’ll blunder on
Albert

1 Like

I didn’t realize you could attach a pic so I thought I’d send this
the other half my Xplane sim i can input varible using a rotary encoder or and a keypad
and also get flight controls and info status directly from the plane us Teensy controls for
Xplane plugins.


ALbert

Hi Albert

I don’t think there is any configuration involved for differs op systems. The board just emulates a joystick, albeit one with a lot of buttons. I think for any game use requiring a joystick or flight yolk you just plug it in.
I use it on a Mac with X-Plane 10 and didn’t have to do anything. But to configure the Bodnar board as for setting up for buttons or rotary encoder you DO need windows. When you have the configuration software downloaded and installed you only have to plug the Bodnar board into a USB hole and it starts up. I spoke to them on the phone about this and they said Windows is the only OS supported for this software. May have changed now, that was 3 years ago.

One thing I must say though. X-plane 10 (don’t know about 11 or 12) makes it very easy to set up. You can assign any button to any function and all these buttons (or rotary encoder switches) appear in the X-plane configuration as any other buttons so can be assigned at will. Very easy. Same with the analog inputs, they appear as axis (or axii, don’t know what the plural is). Another point is if a button or analog input is not connected (don’t know how it knows this) it is ignored so nothing is cluttered up with unused inputs.
Cheers Bob

Sending layout of said software


Cheers

1 Like

Hi Albert
Don’t know what is going on th the left of those yellow bits but if you take a button input to ground on the Bodnar board it will show up on your X-Plane or any other application as a button press and in the case of X-Plane you can assign this to anything. You should just have to plug the Bodnar board into a USB hole for this to happen.
Cheers Bob

1 Like